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