Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.build; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 6 | import aQute.bnd.differ.*; |
| 7 | import aQute.bnd.differ.Baseline.Info; |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 8 | import aQute.bnd.osgi.*; |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 9 | import aQute.bnd.service.*; |
| 10 | import aQute.bnd.version.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | |
| 12 | public class ProjectBuilder extends Builder { |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 13 | private final DiffPluginImpl differ = new DiffPluginImpl(); |
| 14 | Project project; |
| 15 | boolean initialized; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | public ProjectBuilder(Project project) { |
| 18 | super(project); |
| 19 | this.project = project; |
| 20 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 22 | public ProjectBuilder(ProjectBuilder builder) { |
| 23 | super(builder); |
| 24 | this.project = builder.project; |
| 25 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 27 | @Override |
| 28 | public long lastModified() { |
| 29 | return Math.max(project.lastModified(), super.lastModified()); |
| 30 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | /** |
| 33 | * We put our project and our workspace on the macro path. |
| 34 | */ |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 35 | @Override |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | protected Object[] getMacroDomains() { |
| 37 | return new Object[] { |
| 38 | project, project.getWorkspace() |
| 39 | }; |
| 40 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 42 | @Override |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 43 | public Builder getSubBuilder() throws Exception { |
| 44 | return project.getBuilder(this); |
| 45 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 47 | public Project getProject() { |
| 48 | return project; |
| 49 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 51 | @Override |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | public void init() { |
| 53 | try { |
| 54 | if (!initialized) { |
| 55 | initialized = true; |
| 56 | for (Container file : project.getClasspath()) { |
| 57 | addClasspath(file.getFile()); |
| 58 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 59 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 60 | for (Container file : project.getBuildpath()) { |
| 61 | addClasspath(file.getFile()); |
| 62 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 64 | for (Container file : project.getBootclasspath()) { |
| 65 | addClasspath(file.getFile()); |
| 66 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 67 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | for (File file : project.getAllsourcepath()) { |
| 69 | addSourcepath(file); |
| 70 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | catch (Exception e) { |
| 75 | msgs.Unexpected_Error_("ProjectBuilder init", e); |
| 76 | } |
| 77 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 78 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 79 | @Override |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | public List<Jar> getClasspath() { |
| 81 | init(); |
| 82 | return super.getClasspath(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected void changedFile(File f) { |
| 87 | project.getWorkspace().changedFile(f); |
| 88 | } |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Compare this builder's JAR with a baseline |
| 92 | * |
| 93 | * @throws Exception |
| 94 | */ |
| 95 | @Override |
| 96 | protected void doBaseline(Jar dot) throws Exception { |
| 97 | |
| 98 | Jar jar = getBaselineJar(false); |
| 99 | if (jar == null) { |
| 100 | return; |
| 101 | } |
| 102 | try { |
| 103 | Baseline baseline = new Baseline(this, differ); |
| 104 | |
| 105 | Set<Info> infos = baseline.baseline(dot, jar, null); |
| 106 | for (Info info : infos) { |
| 107 | if (info.mismatch) { |
| 108 | error("%s %-50s %-10s %-10s %-10s %-10s %-10s\n", info.mismatch ? '*' : ' ', info.packageName, |
| 109 | info.packageDiff.getDelta(), info.newerVersion, info.olderVersion, info.suggestedVersion, |
| 110 | info.suggestedIfProviders == null ? "-" : info.suggestedIfProviders); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | finally { |
| 115 | jar.close(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | protected Jar getBaselineJar(boolean fallback) throws Exception { |
| 120 | |
| 121 | String baseline = getProperty(Constants.BASELINE); |
| 122 | if ((baseline == null || baseline.trim().length() == 0) && !fallback) |
| 123 | return null; |
| 124 | |
| 125 | trace("baseline %s", baseline); |
| 126 | |
| 127 | File baselineFile = null; |
| 128 | if ((baseline == null || baseline.trim().length() == 0) && fallback) { |
| 129 | |
| 130 | String repoName = getProperty(Constants.BASELINEREPO); |
| 131 | if (repoName == null) { |
| 132 | repoName = getProperty(Constants.RELEASEREPO); |
| 133 | if (repoName == null) { |
| 134 | return null; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | List<RepositoryPlugin> repos = getPlugins(RepositoryPlugin.class); |
| 139 | for (RepositoryPlugin repo : repos) { |
| 140 | if (repoName.equals(repo.getName())) { |
| 141 | SortedSet<Version> versions = repo.versions(getBsn()); |
| 142 | if (!versions.isEmpty()) { |
| 143 | baselineFile = repo.get(getBsn(), versions.last(), null); |
| 144 | } |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } else { |
| 149 | |
| 150 | Collection<Container> bundles = project.getBundles(Strategy.LOWEST, baseline); |
| 151 | for (Container c : bundles) { |
| 152 | |
| 153 | if (c.getError() != null || c.getFile() == null) { |
| 154 | error("Erroneous baseline bundle %s", c); |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | baselineFile = c.getFile(); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | if (fallback && baselineFile == null) { |
| 163 | return new Jar("."); |
| 164 | } |
| 165 | return new Jar(baselineFile); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Gets the baseline Jar. |
| 170 | * |
| 171 | * @return the baseline jar |
| 172 | * @throws Exception |
| 173 | */ |
| 174 | public Jar getBaselineJar() throws Exception { |
| 175 | return getBaselineJar(true); |
| 176 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 177 | } |