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.jvm.java.CompileToJarStepFactory; |
| 19 | import com.facebook.buck.jvm.java.DefaultJavaLibrary; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 20 | import com.facebook.buck.jvm.java.HasMavenCoordinates; |
| 21 | import com.facebook.buck.jvm.java.JavaLibrary; |
| 22 | import com.facebook.buck.jvm.java.MavenPublishable; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 23 | import com.facebook.buck.model.BuildTarget; |
| 24 | import com.facebook.buck.rules.AddToRuleKey; |
| 25 | import com.facebook.buck.rules.BuildRule; |
| 26 | import com.facebook.buck.rules.BuildRuleParams; |
| 27 | import com.facebook.buck.rules.SourcePath; |
| 28 | import com.facebook.buck.rules.SourcePathResolver; |
| 29 | import com.google.common.base.Optional; |
| 30 | import com.google.common.collect.ImmutableList; |
| 31 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableSortedMap; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 33 | import com.google.common.collect.ImmutableSortedSet; |
| 34 | |
Brian O'Connor | 921593c | 2017-04-28 00:42:37 -0700 | [diff] [blame] | 35 | import java.io.IOException; |
| 36 | import java.math.BigInteger; |
| 37 | import java.net.URL; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 38 | import java.nio.file.Path; |
Brian O'Connor | 921593c | 2017-04-28 00:42:37 -0700 | [diff] [blame] | 39 | import java.security.DigestInputStream; |
| 40 | import java.security.MessageDigest; |
| 41 | import java.security.NoSuchAlgorithmException; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | import java.util.regex.Pattern; |
| 44 | |
| 45 | /** |
| 46 | * Implementation of a build rule that generates a onosjar.json file for a set |
| 47 | * of Java sources. |
| 48 | */ |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 49 | public class OnosJar extends DefaultJavaLibrary |
| 50 | implements MavenPublishable{ |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 51 | |
Brian O'Connor | 921593c | 2017-04-28 00:42:37 -0700 | [diff] [blame] | 52 | // Inject the SHA of this rule's jar into the rule key |
| 53 | private static String RULE_VERSION; |
| 54 | static { |
| 55 | URL pluginJarLocation = OnosJar.class.getProtectionDomain().getCodeSource().getLocation(); |
| 56 | try { |
| 57 | MessageDigest md = MessageDigest.getInstance("SHA"); |
| 58 | DigestInputStream dis = new DigestInputStream(pluginJarLocation.openStream(), md); |
| 59 | // Consume the InputStream... |
| 60 | while (dis.read() != -1); |
| 61 | RULE_VERSION = String.format("%032x", new BigInteger(1, md.digest())); |
| 62 | } catch (NoSuchAlgorithmException | IOException e) { |
| 63 | System.err.println("Failed to compute hash for OnosJar rule"); |
| 64 | RULE_VERSION = "nil"; |
| 65 | //TODO consider bailing here instead |
| 66 | } |
| 67 | } |
| 68 | @AddToRuleKey |
| 69 | private final String ruleVersion = RULE_VERSION; |
| 70 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 71 | @AddToRuleKey |
| 72 | final Optional<String> webContext; |
| 73 | |
| 74 | @AddToRuleKey |
| 75 | final Optional<String> apiTitle; |
| 76 | |
| 77 | @AddToRuleKey |
| 78 | final Optional<String> apiVersion; |
| 79 | |
| 80 | @AddToRuleKey |
| 81 | final Optional<String> apiPackage; |
| 82 | |
| 83 | @AddToRuleKey |
| 84 | final Optional<String> apiDescription; |
| 85 | |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 86 | @AddToRuleKey |
| 87 | final Optional<ImmutableSortedMap<String, SourcePath>> includedResources; |
| 88 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 89 | private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps; |
| 90 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 91 | public OnosJar(BuildRuleParams params, |
| 92 | SourcePathResolver resolver, |
| 93 | Set<? extends SourcePath> srcs, |
| 94 | Set<? extends SourcePath> resources, |
| 95 | Optional<Path> generatedSourceFolder, |
| 96 | Optional<SourcePath> proguardConfig, |
| 97 | ImmutableList<String> postprocessClassesCommands, |
| 98 | ImmutableSortedSet<BuildRule> exportedDeps, |
| 99 | ImmutableSortedSet<BuildRule> providedDeps, |
| 100 | SourcePath abiJar, boolean trackClassUsage, |
| 101 | ImmutableSet<Path> additionalClasspathEntries, |
| 102 | CompileToJarStepFactory compileStepFactory, |
| 103 | Optional<Path> resourcesRoot, |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 104 | Optional<SourcePath> manifestFile, |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 105 | Optional<String> mavenCoords, |
| 106 | ImmutableSortedSet<BuildTarget> tests, |
| 107 | ImmutableSet<Pattern> classesToRemoveFromJar, |
| 108 | Optional<String> webContext, |
| 109 | Optional<String> apiTitle, |
| 110 | Optional<String> apiVersion, |
| 111 | Optional<String> apiPackage, |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 112 | Optional<String> apiDescription, |
| 113 | Optional<ImmutableSortedMap<String, SourcePath>> includedResources) { |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 114 | super(params, resolver, srcs, resources, generatedSourceFolder, |
| 115 | proguardConfig, postprocessClassesCommands, exportedDeps, |
| 116 | providedDeps, abiJar, trackClassUsage, additionalClasspathEntries, |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 117 | compileStepFactory, resourcesRoot, manifestFile, mavenCoords, |
| 118 | tests, classesToRemoveFromJar); |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 119 | this.webContext = webContext; |
| 120 | this.apiTitle = apiTitle; |
| 121 | this.apiVersion = apiVersion; |
| 122 | this.apiPackage = apiPackage; |
| 123 | this.apiDescription = apiDescription; |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 124 | this.includedResources = includedResources; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 125 | this.mavenDeps = computeMavenDeps(); |
| 126 | } |
| 127 | |
| 128 | private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() { |
| 129 | ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder(); |
| 130 | for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) { |
| 131 | if (this.equals(javaLibrary)) { |
| 132 | // no need to include ourself |
| 133 | continue; |
| 134 | } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) { |
| 135 | mavenDeps.add(javaLibrary); |
| 136 | //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably. |
| 137 | mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps()); |
| 138 | } |
| 139 | } |
| 140 | return mavenDeps.build(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Iterable<HasMavenCoordinates> getMavenDeps() { |
| 145 | return mavenDeps; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Iterable<BuildRule> getPackagedDependencies() { |
| 150 | //FIXME this is not supported at the moment |
| 151 | return ImmutableList.of(); |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 152 | } |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 153 | |
| 154 | @Override |
| 155 | public Optional<Path> getPomTemplate() { |
| 156 | //FIXME we should consider supporting this |
| 157 | return Optional.absent(); |
| 158 | } |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 159 | } |