blob: bb3998376cc536e583d32a25f5003430e195474c [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
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 McCullochf3173222012-06-07 21:57:32 +0000177}