Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 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; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 31 | import com.facebook.buck.step.fs.CopyStep; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 32 | import com.google.common.base.Optional; |
| 33 | import com.google.common.collect.ImmutableList; |
| 34 | import com.google.common.collect.ImmutableSet; |
| 35 | import com.google.common.collect.ImmutableSortedSet; |
| 36 | |
| 37 | import java.nio.file.Path; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 38 | import java.nio.file.Paths; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 39 | import java.util.regex.Pattern; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | /** |
| 43 | * Creates the list of build steps for the onos_jar rules. |
| 44 | */ |
| 45 | public class OnosJarStepFactory extends JavacToJarStepFactory { |
| 46 | |
| 47 | private static final String DEFINITIONS = "/definitions/"; |
| 48 | private final String webContext; |
| 49 | private final String apiTitle; |
| 50 | private final String apiVersion; |
| 51 | private final String apiPackage; |
| 52 | private final String apiDescription; |
| 53 | private final Optional<ImmutableSortedSet<SourcePath>> resources; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 54 | private final String groupId; |
| 55 | private final String bundleName; |
| 56 | private final String bundleVersion; |
| 57 | private final String bundleLicense; |
| 58 | private final String bundleDescription; |
| 59 | private final String importPackages; |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 60 | private final String privatePackages; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 61 | private final String exportPackages; |
| 62 | private final String includeResources; |
| 63 | private final String dynamicimportPackages; |
Viswanath KSP | 3568df7 | 2017-05-11 13:52:25 +0530 | [diff] [blame] | 64 | private final String embeddedDependencies; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 65 | |
| 66 | public OnosJarStepFactory(JavacOptions javacOptions, |
| 67 | JavacOptionsAmender amender, |
| 68 | Optional<String> webContext, |
| 69 | Optional<String> apiTitle, |
| 70 | Optional<String> apiVersion, |
| 71 | Optional<String> apiPackage, |
| 72 | Optional<String> apiDescription, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 73 | Optional<ImmutableSortedSet<SourcePath>> resources, |
| 74 | Optional<String> groupId, |
| 75 | Optional<String> bundleName, |
| 76 | Optional<String> bundleVersion, |
| 77 | Optional<String> bundleLicense, |
| 78 | Optional<String> bundleDescription, |
| 79 | Optional<String> importPackages, |
| 80 | Optional<String> exportPackages, |
| 81 | Optional<String> includeResources, |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 82 | Optional<String> dynamicimportPackages, |
Viswanath KSP | 3568df7 | 2017-05-11 13:52:25 +0530 | [diff] [blame] | 83 | Optional<String> privatePackages, |
| 84 | Optional<String> embeddedDependencies) { |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 85 | super(javacOptions, amender); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 86 | this.bundleDescription = processParameter(bundleDescription); |
| 87 | this.importPackages = processParameter(importPackages); |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 88 | this.privatePackages = processParameter(privatePackages); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 89 | this.exportPackages = processParameter(exportPackages); |
| 90 | this.includeResources = processParameter(includeResources); |
| 91 | this.dynamicimportPackages = processParameter(dynamicimportPackages); |
| 92 | this.groupId = processParameter(groupId); |
| 93 | this.bundleName = processParameter(bundleName); |
| 94 | this.bundleVersion = processParameter(bundleVersion); |
| 95 | this.bundleLicense = processParameter(bundleLicense); |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 96 | this.webContext = processParameter(webContext); |
| 97 | this.apiTitle = processParameter(apiTitle); |
| 98 | this.apiVersion = processParameter(apiVersion); |
| 99 | this.apiPackage = processParameter(apiPackage); |
| 100 | this.apiDescription = processParameter(apiDescription); |
| 101 | this.resources = resources; |
Viswanath KSP | 3568df7 | 2017-05-11 13:52:25 +0530 | [diff] [blame] | 102 | this.embeddedDependencies = processParameter(embeddedDependencies); |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private String processParameter(Optional<String> p) { |
| 106 | return !p.isPresent() || p.get().equals("NONE") ? null : p.get(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void createCompileToJarStep(BuildContext context, |
| 111 | ImmutableSortedSet<Path> sourceFilePaths, |
| 112 | BuildTarget invokingRule, |
| 113 | SourcePathResolver resolver, |
| 114 | ProjectFilesystem filesystem, |
| 115 | ImmutableSortedSet<Path> declaredClasspathEntries, |
| 116 | Path outputDirectory, |
| 117 | Optional<Path> workingDirectory, |
| 118 | Path pathToSrcsList, |
| 119 | Optional<SuggestBuildRules> suggestBuildRules, |
| 120 | ImmutableList<String> postprocessClassesCommands, |
| 121 | ImmutableSortedSet<Path> entriesToJar, |
| 122 | Optional<String> mainClass, |
| 123 | Optional<Path> manifestFile, |
| 124 | Path outputJar, |
| 125 | ClassUsageFileWriter usedClassesFileWriter, |
| 126 | ImmutableList.Builder<Step> steps, |
| 127 | BuildableContext buildableContext, |
| 128 | ImmutableSet<Pattern> classesToRemoveFromJar) { |
| 129 | |
| 130 | ImmutableSet.Builder<Path> sourceFilePathBuilder = ImmutableSet.builder(); |
| 131 | sourceFilePathBuilder.addAll(sourceFilePaths); |
| 132 | |
| 133 | ImmutableSet.Builder<Pattern> blacklistBuilder = ImmutableSet.builder(); |
| 134 | blacklistBuilder.addAll(classesToRemoveFromJar); |
| 135 | |
| 136 | // precompilation steps |
| 137 | // - generate sources |
| 138 | // add all generated sources ot pathToSrcsList |
| 139 | if (webContext != null && apiTitle != null && resources.isPresent()) { |
| 140 | ImmutableSortedSet<Path> resourceFilePaths = findSwaggerModelDefs(resolver, resources.get()); |
| 141 | blacklistBuilder.addAll(resourceFilePaths.stream() |
| 142 | .map(rp -> Pattern.compile(rp.getFileName().toString(), Pattern.LITERAL)) |
| 143 | .collect(Collectors.toSet())); |
| 144 | Path genSourcesOutput = workingDirectory.get(); |
| 145 | |
| 146 | SwaggerStep swaggerStep = new SwaggerStep(filesystem, sourceFilePaths, resourceFilePaths, |
| 147 | genSourcesOutput, outputDirectory, |
| 148 | webContext, apiTitle, apiVersion, |
| 149 | apiPackage, apiDescription); |
| 150 | sourceFilePathBuilder.add(swaggerStep.apiRegistratorPath()); |
| 151 | steps.add(swaggerStep); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 152 | |
| 153 | // steps.addAll(sourceFilePaths.stream() |
| 154 | // .filter(sp -> sp.startsWith("src/main/webapp/")) |
| 155 | // .map(sp -> CopyStep.forFile(filesystem, sp, outputDirectory)) |
| 156 | // .iterator()); |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | createCompileStep(context, |
| 160 | ImmutableSortedSet.copyOf(sourceFilePathBuilder.build()), |
| 161 | invokingRule, |
| 162 | resolver, |
| 163 | filesystem, |
| 164 | declaredClasspathEntries, |
| 165 | outputDirectory, |
| 166 | workingDirectory, |
| 167 | pathToSrcsList, |
| 168 | suggestBuildRules, |
| 169 | usedClassesFileWriter, |
| 170 | steps, |
| 171 | buildableContext); |
| 172 | |
| 173 | // post compilation steps |
| 174 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 175 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 176 | // FIXME BOC: add mechanism to inject new Steps |
| 177 | //context.additionalStepFactory(JavaStep.class); |
| 178 | |
| 179 | // build the jar |
| 180 | steps.add(new JarDirectoryStep(filesystem, |
| 181 | outputJar, |
| 182 | ImmutableSortedSet.of(outputDirectory), |
| 183 | mainClass.orNull(), |
| 184 | manifestFile.orNull(), |
| 185 | true, |
| 186 | blacklistBuilder.build())); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 187 | |
| 188 | OSGiWrapper osgiStep = new OSGiWrapper( |
| 189 | outputJar, //input jar |
| 190 | outputJar, //Paths.get(outputJar.toString() + ".jar"), //output jar |
| 191 | invokingRule.getBasePath(), // sources dir |
| 192 | outputDirectory, // classes dir |
| 193 | declaredClasspathEntries, // classpath |
| 194 | bundleName, // bundle name |
| 195 | groupId, // groupId |
| 196 | bundleVersion, // bundle version |
| 197 | bundleLicense, // bundle license |
| 198 | importPackages, // import packages |
| 199 | exportPackages, // export packages |
| 200 | includeResources, // include resources |
| 201 | webContext, // web context |
| 202 | dynamicimportPackages, // dynamic import packages |
Viswanath KSP | 3568df7 | 2017-05-11 13:52:25 +0530 | [diff] [blame] | 203 | embeddedDependencies, // embedded dependencies |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 204 | bundleDescription, // bundle description |
| 205 | privatePackages // private packages |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 206 | ); |
| 207 | steps.add(osgiStep); |
| 208 | |
| 209 | //steps.add(CopyStep.forFile(filesystem, Paths.get(outputJar.toString() + ".jar"), outputJar)); |
| 210 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | private ImmutableSortedSet<Path> findSwaggerModelDefs(SourcePathResolver resolver, |
| 214 | ImmutableSortedSet<SourcePath> resourcePaths) { |
| 215 | if (resourcePaths == null) { |
| 216 | return ImmutableSortedSet.of(); |
| 217 | } |
| 218 | return ImmutableSortedSet.copyOf(resourcePaths.stream() |
| 219 | .filter(sp -> sp.toString().contains(DEFINITIONS)) |
| 220 | .map(resolver::getRelativePath) |
| 221 | .collect(Collectors.toList())); |
| 222 | } |
| 223 | } |