Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.onosjar; |
| 17 | |
| 18 | import com.facebook.buck.io.ProjectFilesystem; |
| 19 | import com.facebook.buck.jvm.core.SuggestBuildRules; |
| 20 | import com.facebook.buck.jvm.java.ClassUsageFileWriter; |
| 21 | import com.facebook.buck.jvm.java.JarDirectoryStep; |
| 22 | import com.facebook.buck.jvm.java.JavacOptions; |
| 23 | import com.facebook.buck.jvm.java.JavacOptionsAmender; |
| 24 | import com.facebook.buck.jvm.java.JavacToJarStepFactory; |
| 25 | import com.facebook.buck.model.BuildTarget; |
| 26 | import com.facebook.buck.rules.BuildContext; |
| 27 | import com.facebook.buck.rules.BuildableContext; |
| 28 | import com.facebook.buck.rules.SourcePath; |
| 29 | import com.facebook.buck.rules.SourcePathResolver; |
| 30 | import com.facebook.buck.step.Step; |
| 31 | import com.google.common.base.Optional; |
| 32 | import com.google.common.collect.ImmutableList; |
| 33 | import com.google.common.collect.ImmutableSet; |
| 34 | import com.google.common.collect.ImmutableSortedSet; |
| 35 | |
| 36 | import java.nio.file.Path; |
| 37 | import java.util.regex.Pattern; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | /** |
| 41 | * Creates the list of build steps for the onos_jar rules. |
| 42 | */ |
| 43 | public class OnosJarStepFactory extends JavacToJarStepFactory { |
| 44 | |
| 45 | private static final String DEFINITIONS = "/definitions/"; |
| 46 | private final String webContext; |
| 47 | private final String apiTitle; |
| 48 | private final String apiVersion; |
| 49 | private final String apiPackage; |
| 50 | private final String apiDescription; |
| 51 | private final Optional<ImmutableSortedSet<SourcePath>> resources; |
| 52 | |
| 53 | public OnosJarStepFactory(JavacOptions javacOptions, |
| 54 | JavacOptionsAmender amender, |
| 55 | Optional<String> webContext, |
| 56 | Optional<String> apiTitle, |
| 57 | Optional<String> apiVersion, |
| 58 | Optional<String> apiPackage, |
| 59 | Optional<String> apiDescription, |
| 60 | Optional<ImmutableSortedSet<SourcePath>> resources) { |
| 61 | super(javacOptions, amender); |
| 62 | this.webContext = processParameter(webContext); |
| 63 | this.apiTitle = processParameter(apiTitle); |
| 64 | this.apiVersion = processParameter(apiVersion); |
| 65 | this.apiPackage = processParameter(apiPackage); |
| 66 | this.apiDescription = processParameter(apiDescription); |
| 67 | this.resources = resources; |
| 68 | } |
| 69 | |
| 70 | private String processParameter(Optional<String> p) { |
| 71 | return !p.isPresent() || p.get().equals("NONE") ? null : p.get(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void createCompileToJarStep(BuildContext context, |
| 76 | ImmutableSortedSet<Path> sourceFilePaths, |
| 77 | BuildTarget invokingRule, |
| 78 | SourcePathResolver resolver, |
| 79 | ProjectFilesystem filesystem, |
| 80 | ImmutableSortedSet<Path> declaredClasspathEntries, |
| 81 | Path outputDirectory, |
| 82 | Optional<Path> workingDirectory, |
| 83 | Path pathToSrcsList, |
| 84 | Optional<SuggestBuildRules> suggestBuildRules, |
| 85 | ImmutableList<String> postprocessClassesCommands, |
| 86 | ImmutableSortedSet<Path> entriesToJar, |
| 87 | Optional<String> mainClass, |
| 88 | Optional<Path> manifestFile, |
| 89 | Path outputJar, |
| 90 | ClassUsageFileWriter usedClassesFileWriter, |
| 91 | ImmutableList.Builder<Step> steps, |
| 92 | BuildableContext buildableContext, |
| 93 | ImmutableSet<Pattern> classesToRemoveFromJar) { |
| 94 | |
| 95 | ImmutableSet.Builder<Path> sourceFilePathBuilder = ImmutableSet.builder(); |
| 96 | sourceFilePathBuilder.addAll(sourceFilePaths); |
| 97 | |
| 98 | ImmutableSet.Builder<Pattern> blacklistBuilder = ImmutableSet.builder(); |
| 99 | blacklistBuilder.addAll(classesToRemoveFromJar); |
| 100 | |
| 101 | // precompilation steps |
| 102 | // - generate sources |
| 103 | // add all generated sources ot pathToSrcsList |
| 104 | if (webContext != null && apiTitle != null && resources.isPresent()) { |
| 105 | ImmutableSortedSet<Path> resourceFilePaths = findSwaggerModelDefs(resolver, resources.get()); |
| 106 | blacklistBuilder.addAll(resourceFilePaths.stream() |
| 107 | .map(rp -> Pattern.compile(rp.getFileName().toString(), Pattern.LITERAL)) |
| 108 | .collect(Collectors.toSet())); |
| 109 | Path genSourcesOutput = workingDirectory.get(); |
| 110 | |
| 111 | SwaggerStep swaggerStep = new SwaggerStep(filesystem, sourceFilePaths, resourceFilePaths, |
| 112 | genSourcesOutput, outputDirectory, |
| 113 | webContext, apiTitle, apiVersion, |
| 114 | apiPackage, apiDescription); |
| 115 | sourceFilePathBuilder.add(swaggerStep.apiRegistratorPath()); |
| 116 | steps.add(swaggerStep); |
| 117 | } |
| 118 | |
| 119 | createCompileStep(context, |
| 120 | ImmutableSortedSet.copyOf(sourceFilePathBuilder.build()), |
| 121 | invokingRule, |
| 122 | resolver, |
| 123 | filesystem, |
| 124 | declaredClasspathEntries, |
| 125 | outputDirectory, |
| 126 | workingDirectory, |
| 127 | pathToSrcsList, |
| 128 | suggestBuildRules, |
| 129 | usedClassesFileWriter, |
| 130 | steps, |
| 131 | buildableContext); |
| 132 | |
| 133 | // post compilation steps |
| 134 | |
| 135 | // FIXME BOC: add mechanism to inject new Steps |
| 136 | //context.additionalStepFactory(JavaStep.class); |
| 137 | |
| 138 | // build the jar |
| 139 | steps.add(new JarDirectoryStep(filesystem, |
| 140 | outputJar, |
| 141 | ImmutableSortedSet.of(outputDirectory), |
| 142 | mainClass.orNull(), |
| 143 | manifestFile.orNull(), |
| 144 | true, |
| 145 | blacklistBuilder.build())); |
| 146 | } |
| 147 | |
| 148 | private ImmutableSortedSet<Path> findSwaggerModelDefs(SourcePathResolver resolver, |
| 149 | ImmutableSortedSet<SourcePath> resourcePaths) { |
| 150 | if (resourcePaths == null) { |
| 151 | return ImmutableSortedSet.of(); |
| 152 | } |
| 153 | return ImmutableSortedSet.copyOf(resourcePaths.stream() |
| 154 | .filter(sp -> sp.toString().contains(DEFINITIONS)) |
| 155 | .map(resolver::getRelativePath) |
| 156 | .collect(Collectors.toList())); |
| 157 | } |
| 158 | } |