private ImmutableList<Artifact> generatedOutputArtifacts(FileType newFileType) { ImmutableList.Builder<Artifact> builder = new ImmutableList.Builder<>(); for (Artifact protoFile : getFilteredProtoSources()) { String protoFileName = FileSystemUtils.removeExtension(protoFile.getFilename()); String generatedOutputName; if (attributes.outputsCpp()) { generatedOutputName = protoFileName; } else if (usesProtobufLibrary()) { // The protobuf library generates filenames with some slight modifications. generatedOutputName = generateProtobufFilename(protoFileName); } else { String lowerUnderscoreBaseName = protoFileName.replace('-', '_').toLowerCase(); generatedOutputName = LOWER_UNDERSCORE.to(UPPER_CAMEL, lowerUnderscoreBaseName); } PathFragment generatedFilePath = new PathFragment( protoFile.getRootRelativePath().getParentDirectory(), new PathFragment(generatedOutputName)); PathFragment outputFile = FileSystemUtils.appendExtension(generatedFilePath, newFileType.getExtensions().get(0)); if (outputFile != null) { builder.add( ruleContext.getUniqueDirectoryArtifact( UNIQUE_DIRECTORY_NAME, outputFile, ruleContext.getBinOrGenfilesDirectory())); } } return builder.build(); }
/** * 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 generateProtobufFilename(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(); }