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