forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisopack-cache.js
323 lines (285 loc) · 11.5 KB
/
isopack-cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
var _ = require('underscore');
var buildmessage = require('./buildmessage.js');
var compiler = require('./compiler.js');
var files = require('./files.js');
var isopackModule = require('./isopack.js');
var utils = require('./utils.js');
var watch = require('./watch.js');
var colonConverter = require("./colon-converter.js");
exports.IsopackCache = function (options) {
var self = this;
options = options || {};
// cacheDir may be null; in this case, we just don't ever save things to disk.
self.cacheDir = options.cacheDir;
// This is a bit of a hack, but basically: we really don't want to spend time
// building web.cordova unibuilds in a project that doesn't have any Cordova
// platforms. (Note that we need to be careful with 'meteor publish' to still
// publish a web.cordova unibuild!)
self._includeCordovaUnibuild = !! options.includeCordovaUnibuild;
// Defines the versions of packages that we build. Must be set.
self._packageMap = options.packageMap;
// tropohouse may be null; in this case, we can't load versioned packages.
// eg, for building isopackets.
self._tropohouse = options.tropohouse;
// If provided, this is another IsopackCache for the same cache dir; when
// loading Isopacks, if they are definitely unchanged we can load the
// in-memory objects from this cache instead of recompiling.
self._previousIsopackCache = options.previousIsopackCache;
if (self._previousIsopackCache &&
self._previousIsopackCache.cacheDir !== self.cacheDir) {
throw Error("previousIsopackCache has different cacheDir!");
}
// Map from package name to Isopack.
self._isopacks = {};
self._noLineNumbers = !! options.noLineNumbers;
self.allLoadedLocalPackagesWatchSet = new watch.WatchSet;
};
_.extend(exports.IsopackCache.prototype, {
buildLocalPackages: function (rootPackageNames) {
var self = this;
buildmessage.assertInCapture();
if (self.cacheDir)
files.mkdir_p(self.cacheDir);
var onStack = {};
if (rootPackageNames) {
_.each(rootPackageNames, function (name) {
self._ensurePackageLoaded(name, onStack);
});
} else {
self._packageMap.eachPackage(function (name, packageInfo) {
self._ensurePackageLoaded(name, onStack);
});
}
},
wipeCachedPackages: function (packages) {
var self = this;
// If we're not saving things to disk, there's nothing to wipe!
if (! self.cacheDir)
return;
if (packages) {
// Wipe specific packages.
_.each(packages, function (packageName) {
files.rm_recursive(self._isopackDir(packageName));
});
} else {
// Wipe all packages.
files.rm_recursive(self.cacheDir);
}
},
// Returns the isopack (already loaded in memory) for a given name. It is an
// error to call this if it's not already loaded! So it should only be called
// after buildLocalPackages has returned, or in the process of building a
// package whose dependencies have all already been built.
getIsopack: function (name) {
var self = this;
if (! _.has(self._isopacks, name))
throw Error("isopack " + name + " not yet loaded?");
return self._isopacks[name];
},
eachBuiltIsopack: function (iterator) {
var self = this;
_.each(self._isopacks, function (isopack, packageName) {
iterator(packageName, isopack);
});
},
_ensurePackageLoaded: function (name, onStack) {
var self = this;
buildmessage.assertInCapture();
if (_.has(self._isopacks, name))
return;
var ensureLoaded = function (depName) {
if (_.has(onStack, depName)) {
buildmessage.error("circular dependency between packages " +
name + " and " + depName);
// recover by not enforcing one of the dependencies
return;
}
onStack[depName] = true;
self._ensurePackageLoaded(depName, onStack);
delete onStack[depName];
};
var packageInfo = self._packageMap.getInfo(name);
if (! packageInfo)
throw Error("Depend on unknown package " + name + "?");
var previousIsopack = null;
if (self._previousIsopackCache &&
_.has(self._previousIsopackCache._isopacks, name)) {
var previousInfo = self._previousIsopackCache._packageMap.getInfo(name);
if ((packageInfo.kind === 'versioned' &&
previousInfo.kind === 'versioned' &&
packageInfo.version === previousInfo.version) ||
(packageInfo.kind === 'local' &&
previousInfo.kind === 'local' &&
(packageInfo.packageSource.sourceRoot ===
previousInfo.packageSource.sourceRoot))) {
previousIsopack = self._previousIsopackCache._isopacks[name];
}
}
if (packageInfo.kind === 'local') {
var packageNames =
packageInfo.packageSource.getPackagesToLoadFirst(self._packageMap);
buildmessage.enterJob("preparing to build package " + name, function () {
_.each(packageNames, function (depName) {
ensureLoaded(depName);
});
// If we failed to load something that this package depends on, don't
// load it.
if (buildmessage.jobHasMessages())
return;
self._loadLocalPackage(name, packageInfo, previousIsopack);
});
} else if (packageInfo.kind === 'versioned') {
// We don't have to build this package, and we don't have to build its
// dependencies either! Just load it from disk.
if (!self._tropohouse) {
throw Error("Can't load versioned packages without a tropohouse!");
}
var isopack = null, packagesToLoad = [];
if (previousIsopack) {
isopack = previousIsopack;
packagesToLoad = isopack.getStrongOrderedUsedAndImpliedPackages();
}
if (! isopack) {
// Load the isopack from disk.
buildmessage.enterJob(
"loading package " + name + "@" + packageInfo.version,
function () {
var isopackPath = self._tropohouse.packagePath(
name, packageInfo.version);
var Isopack = isopackModule.Isopack;
isopack = new Isopack();
isopack.initFromPath(name, isopackPath);
// If loading the isopack fails, then we don't need to look for more
// packages to load, but we should still recover by putting it in
// self._isopacks.
if (buildmessage.jobHasMessages())
return;
packagesToLoad = isopack.getStrongOrderedUsedAndImpliedPackages();
});
}
self._isopacks[name] = isopack;
// Also load its dependencies. This is so that if this package is being
// built as part of a plugin, all the transitive dependencies of the
// plugin are loaded.
_.each(packagesToLoad, function (packageToLoad) {
ensureLoaded(packageToLoad);
});
} else {
throw Error("unknown packageInfo kind?");
}
},
_loadLocalPackage: function (name, packageInfo, previousIsopack) {
var self = this;
buildmessage.assertInCapture();
buildmessage.enterJob("building package " + name, function () {
var isopack;
if (previousIsopack && self._checkUpToDatePreloaded(previousIsopack)) {
isopack = previousIsopack;
} else {
// Do we have an up-to-date package on disk?
var isopackBuildInfoJson = self.cacheDir && files.readJSONOrNull(
self._isopackBuildInfoPath(name));
var upToDate = self._checkUpToDate(isopackBuildInfoJson);
if (upToDate) {
var Isopack = isopackModule.Isopack;
isopack = new Isopack();
isopack.initFromPath(name, self._isopackDir(name), {
isopackBuildInfoJson: isopackBuildInfoJson
});
// _checkUpToDate already verified that
// isopackBuildInfoJson.pluginProviderPackageMap is a subset of
// self._packageMap, so this operation is correct. (It can't be done
// by isopack.initFromPath, because Isopack doesn't have access to the
// PackageMap, and specifically to the local catalog it knows about.)
isopack.setPluginProviderPackageMap(
self._packageMap.makeSubsetMap(
_.keys(isopackBuildInfoJson.pluginProviderPackageMap)));
} else {
// Nope! Compile it again.
isopack = compiler.compile(packageInfo.packageSource, {
packageMap: self._packageMap,
isopackCache: self,
noLineNumbers: self._noLineNumbers,
includeCordovaUnibuild: self._includeCordovaUnibuild,
includePluginProviderPackageMap: true
});
// Accept the compiler's result, even if there were errors (since it
// at least will have a useful WatchSet and will allow us to keep
// going and compile other packages that depend on this one).
if (self.cacheDir && ! buildmessage.jobHasMessages()) {
// Save to disk, for next time!
isopack.saveToPath(self._isopackDir(name), {
includeIsopackBuildInfo: true
});
}
}
}
self.allLoadedLocalPackagesWatchSet.merge(isopack.getMergedWatchSet());
self._isopacks[name] = isopack;
});
},
_checkUpToDate: function (isopackBuildInfoJson) {
var self = this;
// If there isn't an isopack-buildinfo.json file, then we definitely aren't
// up to date!
if (! isopackBuildInfoJson)
return false;
// If we include Cordova but this Isopack doesn't, or via versa, then we're
// not up to date.
if (self._includeCordovaUnibuild !==
isopackBuildInfoJson.includeCordovaUnibuild) {
return false;
}
// Was the package built by a different compiler version?
if (isopackBuildInfoJson.builtBy !== compiler.BUILT_BY) {
return false;
}
// If any of the direct dependencies changed their version or location, we
// aren't up to date.
if (!self._packageMap.isSupersetOfJSON(
isopackBuildInfoJson.pluginProviderPackageMap)) {
return false;
}
// Merge in the watchsets for all unibuilds and plugins in the package, then
// check it once.
var watchSet = watch.WatchSet.fromJSON(
isopackBuildInfoJson.pluginDependencies);
_.each(isopackBuildInfoJson.unibuildDependencies, function (deps) {
watchSet.merge(watch.WatchSet.fromJSON(deps));
});
return watch.isUpToDate(watchSet);
},
_checkUpToDatePreloaded: function (previousIsopack) {
var self = this;
// If we include Cordova but this Isopack doesn't, or via versa, then we're
// not up to date.
if (self._includeCordovaUnibuild !== previousIsopack.hasCordovaUnibuild()) {
return false;
}
// We don't have to check builtBy because we don't change BUILT_BY without
// restarting the process.
// If any of the direct dependencies changed their version or location, we
// aren't up to date.
if (!self._packageMap.isSupersetOfJSON(
previousIsopack.pluginProviderPackageMap)) {
return false;
}
// Merge in the watchsets for all unibuilds and plugins in the package, then
// check it once.
var watchSet = previousIsopack.getMergedWatchSet();
return watch.isUpToDate(watchSet);
},
_isopackDir: function (packageName) {
var self = this;
return files.pathJoin(self.cacheDir, colonConverter.convert(packageName));
},
_isopackBuildInfoPath: function (packageName) {
var self = this;
return files.pathJoin(
self._isopackDir(packageName), 'isopack-buildinfo.json');
},
forgetPreviousIsopackCache: function () {
var self = this;
self._previousIsopackCache = null;
}
});