diff options
author | 2016-09-19 21:44:11 +0000 | |
---|---|---|
committer | 2016-09-20 06:47:01 +0000 | |
commit | b85c770b564bf688c4a6e4b2d955ef9db4bff7af (patch) | |
tree | ff4b8b6442bfdce893ef8cf6a9ef9fbc82536f0d /src/main/java/com/google/devtools/build/lib/rules/objc | |
parent | 01a9bd1579b0a6155ee9364b55f2a3b868f8d8bf (diff) |
Uses the same filename generation algorithm as the Protobuf version.
--
MOS_MIGRATED_REVID=133634326
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/objc')
3 files changed, 87 insertions, 80 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtoAttributes.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtoAttributes.java index f9635a946d..9257c26990 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtoAttributes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtoAttributes.java @@ -14,12 +14,16 @@ package com.google.devtools.build.lib.rules.objc; +import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE; +import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.CharMatcher; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.AbstractConfiguredTarget; import com.google.devtools.build.lib.analysis.PrerequisiteArtifacts; @@ -33,9 +37,18 @@ import com.google.devtools.build.lib.rules.proto.ProtoSourceFileBlacklist; import com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.util.FileType; +import java.util.ArrayList; /** Common rule attributes used by an objc_proto_library. */ final class ProtoAttributes { + + /** + * List of file name segments that should be upper cased when being generated. More information + * available in the generateProtobufFilename() method. + */ + private static final ImmutableSet<String> UPPERCASE_SEGMENTS = + ImmutableSet.of("url", "http", "https"); + @VisibleForTesting static final String FILES_DEPRECATED_WARNING = "Using files and filegroups in objc_proto_library is deprecated"; @@ -240,6 +253,78 @@ final class ProtoAttributes { return wellKnownProtoBlacklist.isBlacklisted(protoFile); } + /** + * Processes the case of the proto file name in the same fashion as the objective_c generator's + * UnderscoresToCamelCase function. This converts snake case to camel case by splitting words + * by non alphabetic characters. This also treats the URL, HTTP and HTTPS as special words that + * need to be completely uppercase. + * + * Examples: + * - j2objc_descriptor -> J2ObjcDescriptor (notice that O is uppercase after the 2) + * - my_http_url_array -> MyHTTPURLArray + * - proto-descriptor -> ProtoDescriptor + * + * Original code reference: + * <p>https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc + */ + String getGeneratedProtoFilename(String protoFilename, boolean upcaseReservedWords) { + boolean lastCharWasDigit = false; + boolean lastCharWasUpper = false; + boolean lastCharWasLower = false; + + StringBuilder currentSegment = new StringBuilder(); + + ArrayList<String> segments = new ArrayList<>(); + + for (int i = 0; i < protoFilename.length(); i++) { + char currentChar = protoFilename.charAt(i); + if (CharMatcher.javaDigit().matches(currentChar)) { + if (!lastCharWasDigit) { + segments.add(currentSegment.toString()); + currentSegment = new StringBuilder(); + } + currentSegment.append(currentChar); + lastCharWasDigit = true; + lastCharWasUpper = false; + lastCharWasLower = false; + } else if (CharMatcher.javaLowerCase().matches(currentChar)) { + if (!lastCharWasLower && !lastCharWasUpper) { + segments.add(currentSegment.toString()); + currentSegment = new StringBuilder(); + } + currentSegment.append(currentChar); + lastCharWasDigit = false; + lastCharWasUpper = false; + lastCharWasLower = true; + } else if (CharMatcher.javaUpperCase().matches(currentChar)) { + if (!lastCharWasUpper) { + segments.add(currentSegment.toString()); + currentSegment = new StringBuilder(); + } + currentSegment.append(Character.toLowerCase(currentChar)); + lastCharWasDigit = false; + lastCharWasUpper = true; + lastCharWasLower = false; + } else { + lastCharWasDigit = false; + lastCharWasUpper = false; + lastCharWasLower = false; + } + } + + segments.add(currentSegment.toString()); + + StringBuilder casedSegments = new StringBuilder(); + for (String segment : segments) { + if (upcaseReservedWords && UPPERCASE_SEGMENTS.contains(segment)) { + casedSegments.append(segment.toUpperCase()); + } else { + casedSegments.append(LOWER_UNDERSCORE.to(UPPER_CAMEL, segment)); + } + } + return casedSegments.toString(); + } + /** Returns the sets of proto files that were added using proto_library dependencies. */ private NestedSet<Artifact> getProtoDepsSources() { NestedSetBuilder<Artifact> artifacts = NestedSetBuilder.stableOrder(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java index f17e6f6c74..47cd02e308 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtobufSupport.java @@ -14,11 +14,8 @@ package com.google.devtools.build.lib.rules.objc; -import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE; -import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.devtools.build.lib.rules.objc.XcodeProductType.LIBRARY_STATIC; -import com.google.common.base.CharMatcher; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -40,7 +37,6 @@ import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory. import com.google.devtools.build.lib.rules.proto.ProtoSourcesProvider; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.PathFragment; -import java.util.ArrayList; import java.util.HashMap; import java.util.Set; @@ -63,13 +59,6 @@ final class ProtobufSupport { private static final String BUNDLED_PROTOS_IDENTIFIER = "BundledProtos"; - /** - * List of file name segments that should be upper cased when being generated. More information - * available in the generateProtobufFilename() method. - */ - private static final ImmutableSet<String> UPPERCASE_SEGMENTS = - ImmutableSet.of("url", "http", "https"); - private static final String UNIQUE_DIRECTORY_NAME = "_generated_protos"; private final RuleContext ruleContext; @@ -546,7 +535,7 @@ final class ProtobufSupport { ImmutableList.Builder<Artifact> builder = new ImmutableList.Builder<>(); for (Artifact protoFile : outputProtos) { String protoFileName = FileSystemUtils.removeExtension(protoFile.getFilename()); - String generatedOutputName = getProtobufFilename(protoFileName); + String generatedOutputName = attributes.getGeneratedProtoFilename(protoFileName, true); PathFragment generatedFilePath = new PathFragment( @@ -582,70 +571,6 @@ final class ProtobufSupport { return ruleContext.getPrerequisites("deps", Mode.TARGET, ProtoSourcesProvider.class); } - /** - * Processes the case of the proto file name in the same fashion as the objective_c generator's - * UnderscoresToCamelCase function. - * - * <p>https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc - */ - private String getProtobufFilename(String protoFilename) { - boolean lastCharWasDigit = false; - boolean lastCharWasUpper = false; - boolean lastCharWasLower = false; - - StringBuilder currentSegment = new StringBuilder(); - - ArrayList<String> segments = new ArrayList<>(); - - for (int i = 0; i < protoFilename.length(); i++) { - char currentChar = protoFilename.charAt(i); - if (CharMatcher.javaDigit().matches(currentChar)) { - if (!lastCharWasDigit) { - segments.add(currentSegment.toString()); - currentSegment = new StringBuilder(); - } - currentSegment.append(currentChar); - lastCharWasDigit = true; - lastCharWasUpper = false; - lastCharWasLower = false; - } else if (CharMatcher.javaLowerCase().matches(currentChar)) { - if (!lastCharWasLower && !lastCharWasUpper) { - segments.add(currentSegment.toString()); - currentSegment = new StringBuilder(); - } - currentSegment.append(currentChar); - lastCharWasDigit = false; - lastCharWasUpper = false; - lastCharWasLower = true; - } else if (CharMatcher.javaUpperCase().matches(currentChar)) { - if (!lastCharWasUpper) { - segments.add(currentSegment.toString()); - currentSegment = new StringBuilder(); - } - currentSegment.append(Character.toLowerCase(currentChar)); - lastCharWasDigit = false; - lastCharWasUpper = true; - lastCharWasLower = false; - } else { - lastCharWasDigit = false; - lastCharWasUpper = false; - lastCharWasLower = false; - } - } - - segments.add(currentSegment.toString()); - - StringBuilder casedSegments = new StringBuilder(); - for (String segment : segments) { - if (UPPERCASE_SEGMENTS.contains(segment)) { - casedSegments.append(segment.toUpperCase()); - } else { - casedSegments.append(LOWER_UNDERSCORE.to(UPPER_CAMEL, segment)); - } - } - return casedSegments.toString(); - } - private boolean isLinkingTarget() { return !ruleContext .attributes() diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtocolBuffers2Support.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtocolBuffers2Support.java index 87f22d946d..fd2694f5cb 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ProtocolBuffers2Support.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ProtocolBuffers2Support.java @@ -14,8 +14,6 @@ package com.google.devtools.build.lib.rules.objc; -import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE; -import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.devtools.build.lib.rules.objc.XcodeProductType.LIBRARY_STATIC; import com.google.common.base.Function; @@ -238,8 +236,7 @@ final class ProtocolBuffers2Support { if (attributes.outputsCpp()) { generatedOutputName = protoFileName; } else { - String lowerUnderscoreBaseName = protoFileName.replace('-', '_').toLowerCase(); - generatedOutputName = LOWER_UNDERSCORE.to(UPPER_CAMEL, lowerUnderscoreBaseName); + generatedOutputName = attributes.getGeneratedProtoFilename(protoFileName, false); } PathFragment generatedFilePath = |