blob: ad1c902e649082973033c8e2ae5ad46fcc527b83 [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;
Brian O'Connore8468b52016-07-25 13:42:36 -070065
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'Connoree674952016-09-13 16:31:45 -070073 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 saraswala899a212017-02-28 13:19:57 +053082 Optional<String> dynamicimportPackages,
Viswanath KSP3568df72017-05-11 13:52:25 +053083 Optional<String> privatePackages,
84 Optional<String> embeddedDependencies) {
Brian O'Connore8468b52016-07-25 13:42:36 -070085 super(javacOptions, amender);
Brian O'Connoree674952016-09-13 16:31:45 -070086 this.bundleDescription = processParameter(bundleDescription);
87 this.importPackages = processParameter(importPackages);
Bharat saraswala899a212017-02-28 13:19:57 +053088 this.privatePackages = processParameter(privatePackages);
Brian O'Connoree674952016-09-13 16:31:45 -070089 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'Connore8468b52016-07-25 13:42:36 -070096 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 KSP3568df72017-05-11 13:52:25 +0530102 this.embeddedDependencies = processParameter(embeddedDependencies);
Brian O'Connore8468b52016-07-25 13:42:36 -0700103 }
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'Connoree674952016-09-13 16:31:45 -0700152
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'Connore8468b52016-07-25 13:42:36 -0700157 }
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'Connoree674952016-09-13 16:31:45 -0700175
Brian O'Connore8468b52016-07-25 13:42:36 -0700176 // 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'Connoree674952016-09-13 16:31:45 -0700187
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 KSP3568df72017-05-11 13:52:25 +0530203 embeddedDependencies, // embedded dependencies
Bharat saraswala899a212017-02-28 13:19:57 +0530204 bundleDescription, // bundle description
205 privatePackages // private packages
Brian O'Connoree674952016-09-13 16:31:45 -0700206 );
207 steps.add(osgiStep);
208
209 //steps.add(CopyStep.forFile(filesystem, Paths.get(outputJar.toString() + ".jar"), outputJar));
210
Brian O'Connore8468b52016-07-25 13:42:36 -0700211 }
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}