blob: e551c9c9acdfca933c17381db476f9500b6b2d7a [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.build;
2
3import java.io.*;
4import java.util.*;
5
Stuart McCulloch2a0afd62012-09-06 18:28:06 +00006import aQute.bnd.differ.*;
7import aQute.bnd.differ.Baseline.Info;
Stuart McCulloch42151ee2012-07-16 13:43:38 +00008import aQute.bnd.osgi.*;
Stuart McCulloch2a0afd62012-09-06 18:28:06 +00009import aQute.bnd.service.*;
10import aQute.bnd.version.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000011
12public class ProjectBuilder extends Builder {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000013 private final DiffPluginImpl differ = new DiffPluginImpl();
14 Project project;
15 boolean initialized;
Stuart McCullochf3173222012-06-07 21:57:32 +000016
Stuart McCulloch4482c702012-06-15 13:27:53 +000017 public ProjectBuilder(Project project) {
18 super(project);
19 this.project = project;
20 }
Stuart McCullochf3173222012-06-07 21:57:32 +000021
Stuart McCulloch4482c702012-06-15 13:27:53 +000022 public ProjectBuilder(ProjectBuilder builder) {
23 super(builder);
24 this.project = builder.project;
25 }
Stuart McCullochf3173222012-06-07 21:57:32 +000026
Stuart McCulloch4482c702012-06-15 13:27:53 +000027 @Override
28 public long lastModified() {
29 return Math.max(project.lastModified(), super.lastModified());
30 }
Stuart McCullochf3173222012-06-07 21:57:32 +000031
Stuart McCulloch4482c702012-06-15 13:27:53 +000032 /**
33 * We put our project and our workspace on the macro path.
34 */
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000035 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000036 protected Object[] getMacroDomains() {
37 return new Object[] {
38 project, project.getWorkspace()
39 };
40 }
Stuart McCullochf3173222012-06-07 21:57:32 +000041
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000042 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000043 public Builder getSubBuilder() throws Exception {
44 return project.getBuilder(this);
45 }
Stuart McCullochf3173222012-06-07 21:57:32 +000046
Stuart McCulloch4482c702012-06-15 13:27:53 +000047 public Project getProject() {
48 return project;
49 }
Stuart McCullochf3173222012-06-07 21:57:32 +000050
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000051 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000052 public void init() {
53 try {
54 if (!initialized) {
55 initialized = true;
56 for (Container file : project.getClasspath()) {
57 addClasspath(file.getFile());
58 }
Stuart McCullochf3173222012-06-07 21:57:32 +000059
Stuart McCulloch4482c702012-06-15 13:27:53 +000060 for (Container file : project.getBuildpath()) {
61 addClasspath(file.getFile());
62 }
Stuart McCullochf3173222012-06-07 21:57:32 +000063
Stuart McCulloch4482c702012-06-15 13:27:53 +000064 for (Container file : project.getBootclasspath()) {
65 addClasspath(file.getFile());
66 }
Stuart McCullochf3173222012-06-07 21:57:32 +000067
Stuart McCulloch4482c702012-06-15 13:27:53 +000068 for (File file : project.getAllsourcepath()) {
69 addSourcepath(file);
70 }
Stuart McCullochf3173222012-06-07 21:57:32 +000071
Stuart McCulloch4482c702012-06-15 13:27:53 +000072 }
73 }
74 catch (Exception e) {
75 msgs.Unexpected_Error_("ProjectBuilder init", e);
76 }
77 }
Stuart McCullochf3173222012-06-07 21:57:32 +000078
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000079 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000080 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 McCulloch2a0afd62012-09-06 18:28:06 +000089
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
Stuart McCullochc990fb02012-11-22 00:42:55 +0000119 public Jar getBaselineJar(boolean fallback) throws Exception {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000120
121 String baseline = getProperty(Constants.BASELINE);
Stuart McCullochc990fb02012-11-22 00:42:55 +0000122 String baselineRepo = getProperty(Constants.BASELINEREPO);
123 if ((baseline == null || baseline.trim().length() == 0)
124 && (baselineRepo == null || baselineRepo.trim().length() == 0) && !fallback)
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000125 return null;
126
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000127 File baselineFile = null;
Stuart McCullochc990fb02012-11-22 00:42:55 +0000128 if ((baseline == null || baseline.trim().length() == 0)) {
129 baselineFile = getBaselineFromRepo(fallback);
130 if (baselineFile != null)
131 trace("baseline %s", baselineFile.getName());
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000132 } else {
133
Stuart McCullochc990fb02012-11-22 00:42:55 +0000134 trace("baseline %s", baseline);
135
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000136 Collection<Container> bundles = project.getBundles(Strategy.LOWEST, baseline);
137 for (Container c : bundles) {
138
139 if (c.getError() != null || c.getFile() == null) {
140 error("Erroneous baseline bundle %s", c);
141 continue;
142 }
143
144 baselineFile = c.getFile();
145 break;
146 }
147 }
148 if (fallback && baselineFile == null) {
149 return new Jar(".");
150 }
151 return new Jar(baselineFile);
152 }
153
Stuart McCullochc990fb02012-11-22 00:42:55 +0000154 private File getBaselineFromRepo(boolean fallback) throws Exception {
155 String repoName = getProperty(Constants.BASELINEREPO);
156 if (repoName == null && !fallback)
157 return null;
158
159 if (repoName == null) {
160 repoName = getProperty(Constants.RELEASEREPO);
161 if (repoName == null) {
162 return null;
163 }
164 }
165
166 List<RepositoryPlugin> repos = getPlugins(RepositoryPlugin.class);
167 for (RepositoryPlugin repo : repos) {
168 if (repoName.equals(repo.getName())) {
169 SortedSet<Version> versions = repo.versions(getBsn());
170 if (!versions.isEmpty()) {
171 return repo.get(getBsn(), versions.last(), null);
172 }
173 break;
174 }
175 }
176 return null;
177
178 }
179
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000180 /**
181 * Gets the baseline Jar.
182 *
183 * @return the baseline jar
184 * @throws Exception
185 */
186 public Jar getBaselineJar() throws Exception {
187 return getBaselineJar(true);
188 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000189}