blob: 6550d89377aa4d04802b28316ff735326a23edda [file] [log] [blame]
Brian O'Connore8468b52016-07-25 13:42:36 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connore8468b52016-07-25 13:42:36 -07003 *
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 */
16package org.onosproject.onosjar;
17
18import com.facebook.buck.io.ProjectFilesystem;
19import com.facebook.buck.jvm.core.SuggestBuildRules;
20import com.facebook.buck.jvm.java.ClassUsageFileWriter;
21import com.facebook.buck.jvm.java.JarDirectoryStep;
22import com.facebook.buck.jvm.java.JavacOptions;
23import com.facebook.buck.jvm.java.JavacOptionsAmender;
24import com.facebook.buck.jvm.java.JavacToJarStepFactory;
25import com.facebook.buck.model.BuildTarget;
26import com.facebook.buck.rules.BuildContext;
27import com.facebook.buck.rules.BuildableContext;
28import com.facebook.buck.rules.SourcePath;
29import com.facebook.buck.rules.SourcePathResolver;
30import com.facebook.buck.step.Step;
Brian O'Connoree674952016-09-13 16:31:45 -070031import com.facebook.buck.step.fs.CopyStep;
Brian O'Connore8468b52016-07-25 13:42:36 -070032import com.google.common.base.Optional;
33import com.google.common.collect.ImmutableList;
34import com.google.common.collect.ImmutableSet;
35import com.google.common.collect.ImmutableSortedSet;
36
37import java.nio.file.Path;
Brian O'Connoree674952016-09-13 16:31:45 -070038import java.nio.file.Paths;
Brian O'Connore8468b52016-07-25 13:42:36 -070039import java.util.regex.Pattern;
40import java.util.stream.Collectors;
41
42/**
43 * Creates the list of build steps for the onos_jar rules.
44 */
45public 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'Connoree674952016-09-13 16:31:45 -070054 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 saraswala899a212017-02-28 13:19:57 +053060 private final String privatePackages;
Brian O'Connoree674952016-09-13 16:31:45 -070061 private final String exportPackages;
62 private final String includeResources;
63 private final String dynamicimportPackages;
Viswanath KSP3568df72017-05-11 13:52:25 +053064 private final String embeddedDependencies;
Jian Li91b47392018-02-19 02:15:41 +090065 private final String bundleClasspath;
Brian O'Connore8468b52016-07-25 13:42:36 -070066
67 public OnosJarStepFactory(JavacOptions javacOptions,
68 JavacOptionsAmender amender,
69 Optional<String> webContext,
70 Optional<String> apiTitle,
71 Optional<String> apiVersion,
72 Optional<String> apiPackage,
73 Optional<String> apiDescription,
Brian O'Connoree674952016-09-13 16:31:45 -070074 Optional<ImmutableSortedSet<SourcePath>> resources,
75 Optional<String> groupId,
76 Optional<String> bundleName,
77 Optional<String> bundleVersion,
78 Optional<String> bundleLicense,
79 Optional<String> bundleDescription,
80 Optional<String> importPackages,
81 Optional<String> exportPackages,
82 Optional<String> includeResources,
Bharat saraswala899a212017-02-28 13:19:57 +053083 Optional<String> dynamicimportPackages,
Viswanath KSP3568df72017-05-11 13:52:25 +053084 Optional<String> privatePackages,
Jian Li91b47392018-02-19 02:15:41 +090085 Optional<String> embeddedDependencies,
86 Optional<String> bundleClasspath) {
Brian O'Connore8468b52016-07-25 13:42:36 -070087 super(javacOptions, amender);
Brian O'Connoree674952016-09-13 16:31:45 -070088 this.bundleDescription = processParameter(bundleDescription);
89 this.importPackages = processParameter(importPackages);
Bharat saraswala899a212017-02-28 13:19:57 +053090 this.privatePackages = processParameter(privatePackages);
Brian O'Connoree674952016-09-13 16:31:45 -070091 this.exportPackages = processParameter(exportPackages);
92 this.includeResources = processParameter(includeResources);
93 this.dynamicimportPackages = processParameter(dynamicimportPackages);
94 this.groupId = processParameter(groupId);
95 this.bundleName = processParameter(bundleName);
96 this.bundleVersion = processParameter(bundleVersion);
97 this.bundleLicense = processParameter(bundleLicense);
Brian O'Connore8468b52016-07-25 13:42:36 -070098 this.webContext = processParameter(webContext);
99 this.apiTitle = processParameter(apiTitle);
100 this.apiVersion = processParameter(apiVersion);
101 this.apiPackage = processParameter(apiPackage);
102 this.apiDescription = processParameter(apiDescription);
103 this.resources = resources;
Viswanath KSP3568df72017-05-11 13:52:25 +0530104 this.embeddedDependencies = processParameter(embeddedDependencies);
Jian Li91b47392018-02-19 02:15:41 +0900105 this.bundleClasspath = processParameter(bundleClasspath);
Brian O'Connore8468b52016-07-25 13:42:36 -0700106 }
107
108 private String processParameter(Optional<String> p) {
109 return !p.isPresent() || p.get().equals("NONE") ? null : p.get();
110 }
111
112 @Override
113 public void createCompileToJarStep(BuildContext context,
114 ImmutableSortedSet<Path> sourceFilePaths,
115 BuildTarget invokingRule,
116 SourcePathResolver resolver,
117 ProjectFilesystem filesystem,
118 ImmutableSortedSet<Path> declaredClasspathEntries,
119 Path outputDirectory,
120 Optional<Path> workingDirectory,
121 Path pathToSrcsList,
122 Optional<SuggestBuildRules> suggestBuildRules,
123 ImmutableList<String> postprocessClassesCommands,
124 ImmutableSortedSet<Path> entriesToJar,
125 Optional<String> mainClass,
126 Optional<Path> manifestFile,
127 Path outputJar,
128 ClassUsageFileWriter usedClassesFileWriter,
129 ImmutableList.Builder<Step> steps,
130 BuildableContext buildableContext,
131 ImmutableSet<Pattern> classesToRemoveFromJar) {
132
133 ImmutableSet.Builder<Path> sourceFilePathBuilder = ImmutableSet.builder();
134 sourceFilePathBuilder.addAll(sourceFilePaths);
135
136 ImmutableSet.Builder<Pattern> blacklistBuilder = ImmutableSet.builder();
137 blacklistBuilder.addAll(classesToRemoveFromJar);
138
139 // precompilation steps
140 // - generate sources
141 // add all generated sources ot pathToSrcsList
142 if (webContext != null && apiTitle != null && resources.isPresent()) {
143 ImmutableSortedSet<Path> resourceFilePaths = findSwaggerModelDefs(resolver, resources.get());
144 blacklistBuilder.addAll(resourceFilePaths.stream()
145 .map(rp -> Pattern.compile(rp.getFileName().toString(), Pattern.LITERAL))
146 .collect(Collectors.toSet()));
147 Path genSourcesOutput = workingDirectory.get();
148
149 SwaggerStep swaggerStep = new SwaggerStep(filesystem, sourceFilePaths, resourceFilePaths,
150 genSourcesOutput, outputDirectory,
151 webContext, apiTitle, apiVersion,
152 apiPackage, apiDescription);
153 sourceFilePathBuilder.add(swaggerStep.apiRegistratorPath());
154 steps.add(swaggerStep);
Brian O'Connoree674952016-09-13 16:31:45 -0700155
156// steps.addAll(sourceFilePaths.stream()
157// .filter(sp -> sp.startsWith("src/main/webapp/"))
158// .map(sp -> CopyStep.forFile(filesystem, sp, outputDirectory))
159// .iterator());
Brian O'Connore8468b52016-07-25 13:42:36 -0700160 }
161
162 createCompileStep(context,
163 ImmutableSortedSet.copyOf(sourceFilePathBuilder.build()),
164 invokingRule,
165 resolver,
166 filesystem,
167 declaredClasspathEntries,
168 outputDirectory,
169 workingDirectory,
170 pathToSrcsList,
171 suggestBuildRules,
172 usedClassesFileWriter,
173 steps,
174 buildableContext);
175
176 // post compilation steps
177
Brian O'Connoree674952016-09-13 16:31:45 -0700178
Brian O'Connore8468b52016-07-25 13:42:36 -0700179 // FIXME BOC: add mechanism to inject new Steps
180 //context.additionalStepFactory(JavaStep.class);
181
182 // build the jar
183 steps.add(new JarDirectoryStep(filesystem,
184 outputJar,
185 ImmutableSortedSet.of(outputDirectory),
186 mainClass.orNull(),
187 manifestFile.orNull(),
188 true,
189 blacklistBuilder.build()));
Brian O'Connoree674952016-09-13 16:31:45 -0700190
191 OSGiWrapper osgiStep = new OSGiWrapper(
192 outputJar, //input jar
193 outputJar, //Paths.get(outputJar.toString() + ".jar"), //output jar
194 invokingRule.getBasePath(), // sources dir
195 outputDirectory, // classes dir
196 declaredClasspathEntries, // classpath
197 bundleName, // bundle name
198 groupId, // groupId
199 bundleVersion, // bundle version
200 bundleLicense, // bundle license
201 importPackages, // import packages
202 exportPackages, // export packages
203 includeResources, // include resources
204 webContext, // web context
205 dynamicimportPackages, // dynamic import packages
Viswanath KSP3568df72017-05-11 13:52:25 +0530206 embeddedDependencies, // embedded dependencies
Bharat saraswala899a212017-02-28 13:19:57 +0530207 bundleDescription, // bundle description
Jian Li91b47392018-02-19 02:15:41 +0900208 privatePackages, // private packages
209 bundleClasspath // bundle classpath
Brian O'Connoree674952016-09-13 16:31:45 -0700210 );
211 steps.add(osgiStep);
212
213 //steps.add(CopyStep.forFile(filesystem, Paths.get(outputJar.toString() + ".jar"), outputJar));
214
Brian O'Connore8468b52016-07-25 13:42:36 -0700215 }
216
217 private ImmutableSortedSet<Path> findSwaggerModelDefs(SourcePathResolver resolver,
218 ImmutableSortedSet<SourcePath> resourcePaths) {
219 if (resourcePaths == null) {
220 return ImmutableSortedSet.of();
221 }
222 return ImmutableSortedSet.copyOf(resourcePaths.stream()
223 .filter(sp -> sp.toString().contains(DEFINITIONS))
224 .map(resolver::getRelativePath)
225 .collect(Collectors.toList()));
226 }
227}