blob: adb78e9adfc2e387a5656d73caa9ff1514a198ae [file] [log] [blame]
Brian O'Connore8468b52016-07-25 13:42:36 -07001/*
Brian O'Connorce2a03d2017-08-03 19:21:03 -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;
Brian O'Connore8468b52016-07-25 13:42:36 -070064
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'Connoree674952016-09-13 16:31:45 -070072 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 saraswala899a212017-02-28 13:19:57 +053081 Optional<String> dynamicimportPackages,
82 Optional<String> privatePackages) {
Brian O'Connore8468b52016-07-25 13:42:36 -070083 super(javacOptions, amender);
Brian O'Connoree674952016-09-13 16:31:45 -070084 this.bundleDescription = processParameter(bundleDescription);
85 this.importPackages = processParameter(importPackages);
Bharat saraswala899a212017-02-28 13:19:57 +053086 this.privatePackages = processParameter(privatePackages);
Brian O'Connoree674952016-09-13 16:31:45 -070087 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'Connore8468b52016-07-25 13:42:36 -070094 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'Connoree674952016-09-13 16:31:45 -0700149
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'Connore8468b52016-07-25 13:42:36 -0700154 }
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'Connoree674952016-09-13 16:31:45 -0700172
Brian O'Connore8468b52016-07-25 13:42:36 -0700173 // 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'Connoree674952016-09-13 16:31:45 -0700184
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 saraswala899a212017-02-28 13:19:57 +0530200 bundleDescription, // bundle description
201 privatePackages // private packages
Brian O'Connoree674952016-09-13 16:31:45 -0700202 );
203 steps.add(osgiStep);
204
205 //steps.add(CopyStep.forFile(filesystem, Paths.get(outputJar.toString() + ".jar"), outputJar));
206
Brian O'Connore8468b52016-07-25 13:42:36 -0700207 }
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}