forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isopack.js
1210 lines (1069 loc) · 43.2 KB
/
isopack.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var compiler = require('./compiler.js');
var archinfo = require('./archinfo.js');
var _ = require('underscore');
var linker = require('./linker.js');
var buildmessage = require('./buildmessage.js');
var Builder = require('./builder.js');
var bundler = require('./bundler.js');
var watch = require('./watch.js');
var files = require('./files.js');
var isopackets = require("./isopackets.js");
var isopackCacheModule = require('./isopack-cache.js');
var packageMapModule = require('./package-map.js');
var colonConverter = require('./colon-converter.js');
var Future = require('fibers/future');
var Console = require('./console.js').Console;
var Profile = require('./profile.js').Profile;
var rejectBadPath = function (p) {
if (p.match(/\.\./))
throw new Error("bad path: " + p);
};
///////////////////////////////////////////////////////////////////////////////
// Unibuild
///////////////////////////////////////////////////////////////////////////////
// Options:
// - kind [required] (main/plugin/app)
// - arch [required]
// - uses
// - implies
// - watchSet
// - nodeModulesPath
// - prelinkFiles
// - packageVariables
// - resources
var nextBuildId = 1;
var Unibuild = function (isopack, options) {
var self = this;
options = options || {};
self.pkg = isopack;
self.kind = options.kind;
self.arch = options.arch;
self.uses = options.uses;
self.implies = options.implies || [];
// This WatchSet will end up having the watch items from the
// SourceArch (such as package.js or .meteor/packages), plus all of
// the actual source files for the unibuild (including items that we
// looked at to find the source files, such as directories we
// scanned).
self.watchSet = options.watchSet || new watch.WatchSet();
// Each Unibuild is given a unique id when it's loaded (it is
// not saved to disk). This is just a convenience to make it easier
// to keep track of Unibuilds in a map; it's used by bundler
// and compiler. We put some human readable info in here too to make
// debugging easier.
self.id = self.pkg.name + "." + self.kind + "@" + self.arch + "#" +
(nextBuildId ++);
// Prelink output.
//
// 'prelinkFiles' is the partially linked JavaScript code (an
// array of objects with keys 'source' and 'servePath', both strings -- see
// prelink() in linker.js)
//
// 'packageVariables' are variables that are syntactically globals
// in our input files and which we capture with a package-scope
// closure. A list of objects with keys 'name' (required) and
// 'export' (true, 'tests', or falsy).
//
// Both of these are saved into unibuilds on disk, and are inputs into the final
// link phase, which inserts the final JavaScript resources into
// 'resources'.
self.prelinkFiles = options.prelinkFiles;
self.packageVariables = options.packageVariables;
// All of the data provided for eventual inclusion in the bundle,
// other than JavaScript that still needs to be fed through the
// final link stage. A list of objects with these keys:
//
// type: "js", "css", "head", "body", "asset"
//
// data: The contents of this resource, as a Buffer. For example,
// for "head", the data to insert in <head>; for "js", the
// JavaScript source code (which may be subject to further
// processing such as minification); for "asset", the contents of a
// static resource such as an image.
//
// servePath: The (absolute) path at which the resource would prefer
// to be served. Interpretation varies by type. For example, always
// honored for "asset", ignored for "head" and "body", sometimes
// honored for CSS but ignored if we are concatenating.
//
// sourceMap: Allowed only for "js". If present, a string.
self.resources = options.resources;
// Absolute path to the node_modules directory to use at runtime to
// resolve Npm.require() calls in this unibuild. null if this unibuild
// does not have a node_modules.
self.nodeModulesPath = options.nodeModulesPath;
};
_.extend(Unibuild.prototype, {
// Get the resources that this function contributes to a bundle, in
// the same format as self.resources as documented above. This
// includes static assets and fully linked JavaScript.
//
// @param bundleArch The architecture targeted by the bundle. Might
// be more specific than self.arch.
//
// It is when you call this function that we read our dependent
// packages and commit to whatever versions of them we currently
// have in the library -- at least for the purpose of imports, which
// is resolved at bundle time. (On the other hand, when it comes to
// the extension handlers we'll use, we previously commited to those
// versions at package build ('compile') time.)
getResources: Profile(
"Unibuild#getResources", function (bundleArch, options) {
var self = this;
var isopackCache = options.isopackCache;
if (! isopackCache)
throw Error("no isopackCache?");
if (! archinfo.matches(bundleArch, self.arch))
throw new Error("unibuild of arch '" + self.arch + "' does not support '" +
bundleArch + "'?");
// Compute imports by merging the exports of all of the packages
// we use. Note that in the case of conflicting symbols, later
// packages get precedence.
//
// We don't get imports from unordered dependencies (since they may not be
// defined yet) or from weak/debugOnly dependencies (because the meaning of
// a name shouldn't be affected by the non-local decision of whether or not
// an unrelated package in the target depends on something).
var imports = {}; // map from symbol to supplying package name
var addImportsForUnibuild = function (depUnibuild) {
_.each(depUnibuild.packageVariables, function (symbol) {
// Slightly hacky implementation of test-only exports.
if (symbol.export === true ||
(symbol.export === "tests" && self.pkg.isTest))
imports[symbol.name] = depUnibuild.pkg.name;
});
};
compiler.eachUsedUnibuild({
dependencies: self.uses,
arch: bundleArch,
isopackCache: isopackCache,
skipUnordered: true,
skipDebugOnly: true
}, addImportsForUnibuild);
// Phase 2 link
var isApp = ! self.pkg.name;
var files = linker.link({
imports: imports,
useGlobalNamespace: isApp,
// XXX report an error if there is a package called global-imports
importStubServePath: isApp && '/packages/global-imports.js',
prelinkFiles: self.prelinkFiles,
packageVariables: self.packageVariables,
includeSourceMapInstructions: archinfo.matches(self.arch, "web"),
name: self.pkg.name || null
});
// Add each output as a resource
var jsResources = _.map(files, function (file) {
return {
type: "js",
data: new Buffer(file.source, 'utf8'), // XXX encoding
servePath: file.servePath,
sourceMap: file.sourceMap
};
});
return _.union(self.resources, jsResources); // union preserves order
})
});
///////////////////////////////////////////////////////////////////////////////
// Isopack
///////////////////////////////////////////////////////////////////////////////
// Meteor has a packaging system called "Isobuild". Isobuild knows how to
// compile the same JavaScript code-base to different architectures: browser,
// node.js-like server environment (could be Rhino or other) or a webview in a
// Cordova mobile app.
//
// Each package used by Isobuild forms an Isopack. Isopack is a package format
// containing source code for each architecture it can be ran on.
// Each separate part built for a separate architecture is called "Unibuild".
//
// There are multiple reasons why we can't call it just "build" and historically
// the name "Unibuild" has been associated with parts of Isopacks. We also can't
// call it "Isobuild" because this is the brand-name of the whole
// build/packaging system.
var Isopack = function () {
var self = this;
// These have the same meaning as in PackageSource.
self.name = null;
self.metadata = {};
self.version = null;
self.isTest = false;
self.debugOnly = false;
// Unibuilds, an array of class Unibuild.
self.unibuilds = [];
// Plugins in this package. Map from plugin name to {arch -> JsImage}.
self.plugins = {};
self.cordovaDependencies = {};
// -- Information for up-to-date checks --
// Data in this section is only set if the Isopack was directly created by
// compiler.compile or read from a package compiled by IsopackCache (with its
// isopack-buildinfo.json file). They are not set for Isopacks read from
// the tropohouse.
// XXX this is likely to change once we have build versions
//
// A WatchSet for the full transitive dependencies for all plugins in this
// package, as well as this package's package.js. If any of these dependencies
// change, our plugins need to be rebuilt... but also, any package that
// directly uses this package needs to be rebuilt in case the change to
// plugins affected compilation.
self.pluginWatchSet = new watch.WatchSet();
// -- Loaded plugin state --
// True if plugins have been initialized (if _ensurePluginsInitialized has
// been called)
self._pluginsInitialized = false;
// Source file handlers registered by plugins. Map from extension
// (without a dot) to a handler function that takes a
// CompileStep. Valid only when _pluginsInitialized is true.
self.sourceHandlers = null;
// See description in PackageSource. If this is set, then we include a copy of
// our own source, in addition to any other tools that were originally in the
// isopack.
self.includeTool = false;
// This is tools to copy from trees on disk. This is used by the
// isopack-merge code in tropohouse.
self.toolsOnDisk = [];
// XXX doc
self.pluginProviderPackageMap = null;
};
Isopack.currentFormat = "isopack-1";
Isopack.knownFormats = ["unipackage-pre2", "isopack-1"];
Isopack.convertOneStepForward = function (data, fromFormat) {
var convertedData = _.clone(data);
// XXX COMPAT WITH 0.9.3
if (fromFormat === "unipackage-pre2") {
convertedData.builds = convertedData.unibuilds;
delete convertedData.unibuilds;
return convertedData;
}
};
Isopack.convertOneStepBackward = function (data, fromFormat) {
var convertedData = _.clone(data);
if (fromFormat === "isopack-1") {
convertedData.unibuilds = convertedData.builds;
convertedData.format = "unipackage-pre2";
delete convertedData.builds;
return convertedData;
}
};
Isopack.convertIsopackFormat = Profile(
"Isopack.convertIsopackFormat", function (data, fromFormat, toFormat) {
var fromPos = _.indexOf(Isopack.knownFormats, fromFormat);
var toPos = _.indexOf(Isopack.knownFormats, toFormat);
var step = fromPos < toPos ? 1 : -1;
if (fromPos === -1)
throw new Error("Can't convert from unknown Isopack format: " + fromFormat);
if (toPos === -1)
throw new Error("Can't convert to unknown Isopack format: " + toFormat);
while (fromPos !== toPos) {
if (step > 0) {
data = Isopack.convertOneStepForward(data, fromFormat);
} else {
data = Isopack.convertOneStepBackward(data, fromFormat);
}
fromPos += step;
fromFormat = Isopack.knownFormats[fromPos];
}
return data;
});
// Read the correct file from isopackDirectory and convert to current format
// of the isopack metadata. Returns null if there is no package here.
Isopack.readMetadataFromDirectory =
Profile("Isopack.readMetadataFromDirectory", function (isopackDirectory) {
var metadata;
// deal with different versions of "isopack.json", backwards compatible
var isopackJsonPath = files.pathJoin(isopackDirectory, "isopack.json");
var unipackageJsonPath = files.pathJoin(isopackDirectory, "unipackage.json");
if (files.exists(isopackJsonPath)) {
var isopackJson = JSON.parse(files.readFile(isopackJsonPath));
if (isopackJson[Isopack.currentFormat]) {
metadata = isopackJson[Isopack.currentFormat];
} else {
// This file is from the future and no longer supports this version
throw new Error("Could not find isopack data with format " + Isopack.currentFormat + ".\n" +
"This isopack was likely built with a much newer version of Meteor.");
}
} else if (files.exists(unipackageJsonPath)) {
// super old version with different file name
// XXX COMPAT WITH 0.9.3
if (files.exists(unipackageJsonPath)) {
metadata = JSON.parse(files.readFile(unipackageJsonPath));
// in the old format, builds were called unibuilds
// use string to make sure this doesn't get caught in a find/replace
metadata.builds = metadata["unibuilds"];
metadata = Isopack.convertIsopackFormat(metadata,
"unipackage-pre2", Isopack.currentFormat);
}
if (metadata.format !== "unipackage-pre2") {
// We don't support pre-0.9.0 isopacks, but we do know enough to delete
// them if we find them in an isopack cache somehow (rather than crash).
if (metadata.format === "unipackage-pre1") {
throw new exports.OldIsopackFormatError();
}
throw new Error("Unsupported isopack format: " +
JSON.stringify(metadata.format));
}
}
return metadata;
});
_.extend(Isopack.prototype, {
// Make a dummy (empty) package that contains nothing of interest.
// XXX used?
initEmpty: function (name) {
var self = this;
self.name = name;
},
// This is primarily intended to be used by the compiler. After
// calling this, call addUnibuild to add the unibuilds.
initFromOptions: function (options) {
var self = this;
self.name = options.name;
self.metadata = options.metadata;
self.version = options.version;
self.isTest = options.isTest;
self.plugins = options.plugins;
self.cordovaDependencies = options.cordovaDependencies;
self.pluginWatchSet = options.pluginWatchSet;
self.npmDiscards = options.npmDiscards;
self.includeTool = options.includeTool;
self.debugOnly = options.debugOnly;
},
// Programmatically add a unibuild to this Isopack. Should only be
// called as part of building up a new Isopack using
// initFromOptions. 'options' are the options to the Unibuild
// constructor.
addUnibuild: function (options) {
var self = this;
self.unibuilds.push(new Unibuild(self, options));
},
setPluginProviderPackageMap: function (pluginProviderPackageMap) {
var self = this;
self.pluginProviderPackageMap = pluginProviderPackageMap;
},
getSourceFilesUnderSourceRoot: Profile(
"Isopack#getSourceFilesUnderSourceRoot", function (sourceRoot) {
var self = this;
var sourceFiles = {};
var anySourceFiles = false;
var addSourceFilesFromWatchSet = function (watchSet) {
_.each(watchSet.files, function (hash, filename) {
anySourceFiles = true;
var relativePath = files.pathRelative(sourceRoot, filename);
// We only want files that are actually under sourceRoot.
if (relativePath.substr(0, 3) === '..' + files.pathSep)
return;
sourceFiles[relativePath] = true;
});
};
addSourceFilesFromWatchSet(self.pluginWatchSet);
_.each(self.unibuilds, function (u) {
addSourceFilesFromWatchSet(u.watchSet);
});
// Were we actually built from source or loaded from an IsopackCache? If so
// then there should be at least one source file in some WatchSet. If not,
// return null.
if (! anySourceFiles)
return null;
return _.keys(sourceFiles);
}),
// An sorted array of all the architectures included in this package.
architectures: Profile("Isopack#architectures", function () {
var self = this;
var archSet = {};
_.each(self.unibuilds, function (unibuild) {
archSet[unibuild.arch] = true;
});
_.each(self._toolArchitectures(), function (arch) {
archSet[arch] = true;
});
_.each(self.plugins, function (plugin, name) {
_.each(plugin, function (plug, arch) {
archSet[arch] = true;
});
});
var arches = _.keys(archSet).sort();
// Ensure that our buildArchitectures string does not look like
// web+os+os.osx.x86_64
// This would happen if there is an 'os' unibuild but a platform-specific
// tool (eg, in meteor-tool). This would confuse catalog.getBuildsForArches
// into thinking that it would work for Linux, since the 'os' means
// 'works on any Node server'.
if (_.any(arches, function (a) { return a.match(/^os\./); })) {
arches = _.without(arches, 'os');
}
return arches;
}),
// A sorted plus-separated string of all the architectures included in this
// package.
buildArchitectures: function () {
var self = this;
return self.architectures().join('+');
},
// Returns true if we think that this isopack is platform specific (contains
// binary builds)
platformSpecific: function () {
var self = this;
return _.any(self.architectures(), function (arch) {
return arch.match(/^os\./);
});
},
tarballName: function () {
var self = this;
return colonConverter.convert(self.name) + '-' + self.version;
},
_toolArchitectures: function () {
var self = this;
var toolArches = _.pluck(self.toolsOnDisk, 'arch');
self.includeTool && toolArches.push(archinfo.host());
return _.uniq(toolArches).sort();
},
// Return the unibuild of the package to use for a given target architecture
// (eg, 'os.linux.x86_64' or 'web'), or throw an exception if that
// packages can't be loaded under these circumstances.
getUnibuildAtArch: Profile("Isopack#getUnibuildAtArch", function (arch) {
var self = this;
var chosenArch = archinfo.mostSpecificMatch(
arch, _.pluck(self.unibuilds, 'arch'));
if (! chosenArch) {
buildmessage.error(
(self.name || "this app") +
" is not compatible with architecture '" + arch + "'",
{ secondary: true });
// recover by returning by no unibuilds
return null;
}
return _.findWhere(self.unibuilds, { arch: chosenArch });
}),
// Load this package's plugins into memory, if they haven't already
// been loaded, and return the list of source file handlers
// registered by the plugins: a map from extension (without a dot)
// to a handler function that takes a CompileStep.
getSourceHandlers: function () {
var self = this;
self._ensurePluginsInitialized();
return self.sourceHandlers;
},
// If this package has plugins, initialize them (run the startup
// code in them so that they register their extensions). Idempotent.
_ensurePluginsInitialized: Profile(
"Isopack#_ensurePluginsInitialized", function () {
var self = this;
if (self._pluginsInitialized)
return;
/**
* @global
* @namespace Plugin
* @summary The namespace that is exposed inside build plugin files.
*/
var Plugin = {
// 'extension' is a file extension without the separation dot
// (eg 'js', 'coffee', 'coffee.md')
//
// 'options' can be omitted. The only known option is 'isTemplate', which
// is a bit of a hack meaning "in an app, these files should be loaded
// before non-templates".
//
// 'handler' is a function that takes a single argument, a
// CompileStep (#CompileStep)
/**
* @summary Inside a build plugin source file specified in
* [Package.registerBuildPlugin](#Package-registerBuildPlugin),
* add a handler to compile files with a certain file extension.
* @param {String} fileExtension The file extension that this plugin
* should handle, without the first dot.
* Examples: `"coffee"`, `"coffee.md"`.
* @param {Function} handler A function that takes one argument,
* a CompileStep object.
*
* Documentation for CompileStep is available [on the GitHub Wiki](https://github.com/meteor/meteor/wiki/CompileStep-API-for-Build-Plugin-Source-Handlers).
* @memberOf Plugin
* @locus Build Plugin
*/
registerSourceHandler: function (extension, options, handler) {
if (!handler) {
handler = options;
options = {};
}
if (_.has(self.sourceHandlers, extension)) {
buildmessage.error("duplicate handler for '*." +
extension + "'; may only have one per Plugin",
{ useMyCaller: true });
// recover by ignoring all but the first
return;
}
self.sourceHandlers[extension] = {
handler: handler,
isTemplate: !!options.isTemplate,
archMatching: options.archMatching
};
}
};
self.sourceHandlers = {};
_.each(self.plugins, function (pluginsByArch, name) {
var arch = archinfo.mostSpecificMatch(
archinfo.host(), _.keys(pluginsByArch));
if (! arch) {
buildmessage.error("package `" + name + "` is built for incompatible " +
"architecture");
// Recover by ignoring plugin
// XXX does this recovery work?
return;
}
var plugin = pluginsByArch[arch];
buildmessage.enterJob({
title: "loading plugin `" + name +
"` from package `" + self.name + "`"
// don't necessarily have rootPath anymore
// (XXX we do, if the isopack was locally built, which is
// the important case for debugging. it'd be nice to get this
// case right.)
}, function () {
plugin.load({ Plugin: Plugin });
});
});
self._pluginsInitialized = true;
}),
// Load a Isopack on disk.
//
// options:
// - isopackBuildInfoJson: parsed isopack-buildinfo.json object,
// if loading from an IsopackCache.
initFromPath: Profile(
"Isopack#initFromPath", function (name, dir, options) {
var self = this;
options = _.clone(options || {});
options.firstIsopack = true;
return self._loadUnibuildsFromPath(name, dir, options);
}),
_loadUnibuildsFromPath: function (name, dir, options) {
var self = this;
options = options || {};
// In the tropohouse, isopack paths are symlinks (which can be updated if
// more unibuilds are merged in). For any given call to
// _loadUnibuildsFromPath, let's ensure we see a consistent isopack by
// realpath'ing dir.
dir = files.realpath(dir);
var mainJson = Isopack.readMetadataFromDirectory(dir);
// isopacks didn't used to know their name, but they should.
if (_.has(mainJson, 'name') && name !== mainJson.name) {
throw new Error("isopack " + name + " thinks its name is " +
mainJson.name);
}
// If we're loading from an IsopackCache, we need to load the WatchSets
// which will be used by the bundler. (builtBy is only used by
// IsopackCache._checkUpToDate. pluginProviderPackageMap will actually be
// set by IsopackCache afterwards, because it has access to an appropriate
// PackageMap which can be subset to create a new PackageMap object.)
var unibuildWatchSets = {};
if (options.isopackBuildInfoJson) {
if (! options.firstIsopack)
throw Error("can't merge isopacks with buildinfo");
// XXX should comprehensively sanitize (eg, typecheck) everything
// read from json files
// Read the watch sets for each unibuild
_.each(
options.isopackBuildInfoJson.unibuildDependencies,
function (watchSetJSON, unibuildTag) {
unibuildWatchSets[unibuildTag] =
watch.WatchSet.fromJSON(watchSetJSON);
});
// Read pluginWatchSet. (In the multi-sub-isopack case, these are
// guaranteed to be trivial (since we check that there's no
// isopackBuildInfoJson), so no need to merge.)
self.pluginWatchSet = watch.WatchSet.fromJSON(
options.isopackBuildInfoJson.pluginDependencies);
}
// If we are loading multiple isopacks, only take this stuff from the
// first one.
if (options.firstIsopack) {
self.name = name;
self.metadata = {
summary: mainJson.summary
};
self.version = mainJson.version;
self.isTest = mainJson.isTest;
self.debugOnly = !!mainJson.debugOnly;
}
_.each(mainJson.plugins, function (pluginMeta) {
rejectBadPath(pluginMeta.path);
var plugin = bundler.readJsImage(files.pathJoin(dir, pluginMeta.path));
if (!_.has(self.plugins, pluginMeta.name)) {
self.plugins[pluginMeta.name] = {};
}
// If we already loaded a plugin of this name/arch, just ignore this one.
if (!_.has(self.plugins[pluginMeta.name], plugin.arch)) {
self.plugins[pluginMeta.name][plugin.arch] = plugin;
}
});
self.pluginsBuilt = true;
_.each(mainJson.builds, function (unibuildMeta) {
// aggressively sanitize path (don't let it escape to parent
// directory)
rejectBadPath(unibuildMeta.path);
// Skip unibuilds we already have.
var alreadyHaveUnibuild = _.find(self.unibuilds, function (unibuild) {
return unibuild.arch === unibuildMeta.arch;
});
if (alreadyHaveUnibuild)
return;
var unibuildJson = JSON.parse(
files.readFile(files.pathJoin(dir, unibuildMeta.path)));
var unibuildBasePath =
files.pathDirname(files.pathJoin(dir, unibuildMeta.path));
if (unibuildJson.format !== "unipackage-unibuild-pre1")
throw new Error("Unsupported isopack unibuild format: " +
JSON.stringify(unibuildJson.format));
var nodeModulesPath = null;
if (unibuildJson.node_modules) {
rejectBadPath(unibuildJson.node_modules);
nodeModulesPath =
files.pathJoin(unibuildBasePath, unibuildJson.node_modules);
}
var prelinkFiles = [];
var resources = [];
_.each(unibuildJson.resources, function (resource) {
rejectBadPath(resource.file);
var data = new Buffer(resource.length);
// Read the data from disk, if it is non-empty. Avoid doing IO for empty
// files, because (a) unnecessary and (b) fs.readSync with length 0
// throws instead of acting like POSIX read:
// https://github.com/joyent/node/issues/5685
if (resource.length > 0) {
var fd =
files.open(files.pathJoin(unibuildBasePath, resource.file), "r");
try {
var count = files.read(
fd, data, 0, resource.length, resource.offset);
} finally {
files.close(fd);
}
if (count !== resource.length)
throw new Error("couldn't read entire resource");
}
if (resource.type === "prelink") {
var prelinkFile = {
source: data.toString('utf8'),
servePath: resource.servePath
};
if (resource.sourceMap) {
rejectBadPath(resource.sourceMap);
prelinkFile.sourceMap = files.readFile(
files.pathJoin(unibuildBasePath, resource.sourceMap), 'utf8');
}
prelinkFiles.push(prelinkFile);
} else if (_.contains(["head", "body", "css", "js", "asset"],
resource.type)) {
resources.push({
type: resource.type,
data: data,
servePath: resource.servePath || undefined,
path: resource.path || undefined
});
} else
throw new Error("bad resource type in isopack: " +
JSON.stringify(resource.type));
});
self.unibuilds.push(new Unibuild(self, {
// At some point we stopped writing 'kind's to the metadata file, so
// default to main.
kind: unibuildMeta.kind || 'main',
arch: unibuildMeta.arch,
uses: unibuildJson.uses,
implies: unibuildJson.implies,
watchSet: unibuildWatchSets[unibuildMeta.path],
nodeModulesPath: nodeModulesPath,
prelinkFiles: prelinkFiles,
packageVariables: unibuildJson.packageVariables || [],
resources: resources
}));
});
self.cordovaDependencies = mainJson.cordovaDependencies || null;
_.each(mainJson.tools, function (toolMeta) {
toolMeta.rootDir = dir;
// XXX check for overlap
self.toolsOnDisk.push(toolMeta);
});
return true;
},
hasCordovaUnibuild: function () {
var self = this;
return _.any(self.unibuilds, function (unibuild) {
return unibuild.arch === 'web.cordova';
});
},
// options:
//
// - includeIsopackBuildInfo: If set, write an isopack-buildinfo.json file.
saveToPath: Profile("Isopack#saveToPath", function (outputDir, options) {
var self = this;
var outputPath = outputDir;
options = options || {};
var builder = new Builder({ outputPath: outputPath });
try {
var mainJson = {
name: self.name,
summary: self.metadata.summary,
version: self.version,
isTest: self.isTest,
builds: [],
plugins: []
};
if (self.debugOnly) {
mainJson.debugOnly = true;
}
if (! _.isEmpty(self.cordovaDependencies)) {
mainJson.cordovaDependencies = self.cordovaDependencies;
}
var isopackBuildInfoJson = null;
if (options.includeIsopackBuildInfo) {
isopackBuildInfoJson = {
builtBy: compiler.BUILT_BY,
unibuildDependencies: {},
// pluginDependencies defines a WatchSet that any package that could
// use this package as a plugin needs to watch. So it always contains
// our package.js (because modifications to package.js could add a new
// plugin), as well as any files making up plugins in our package.
pluginDependencies: self.pluginWatchSet.toJSON(),
pluginProviderPackageMap: self.pluginProviderPackageMap.toJSON(),
includeCordovaUnibuild: self.hasCordovaUnibuild()
};
}
// XXX COMPAT WITH 0.9.3
builder.reserve("unipackage.json");
builder.reserve("isopack.json");
// Reserve this even if includeIsopackBuildInfo is not set, to ensure
// nothing else writes it somehow.
builder.reserve("isopack-buildinfo.json");
builder.reserve("head");
builder.reserve("body");
// Map from absolute path to npm directory in the unibuild, to the
// generated filename in the isopack we're writing. Multiple builds
// can use the same npm modules (though maybe not any more, since tests
// have been separated?), but also there can be different sets of
// directories as well (eg, for a isopack merged with from multiple
// isopacks with _loadUnibuildsFromPath).
var npmDirectories = {};
// Pre-linker versions of Meteor expect all packages in the warehouse to
// contain a file called "package.js"; they use this as part of deciding
// whether or not they need to download a new package. Because packages
// are downloaded by the *existing* version of the tools, we need to
// include this file until we're comfortable breaking "meteor update" from
// 0.6.4. (Specifically, warehouse.packageExistsInWarehouse used to check
// to see if package.js exists instead of just looking for the package
// directory.)
// XXX Remove this once we can.
builder.write("package.js", {
data: new Buffer(
("// This file is included for compatibility with the Meteor " +
"0.6.4 package downloader.\n"),
"utf8")
});
// Unibuilds
_.each(self.unibuilds, function (unibuild) {
// Make up a filename for this unibuild
var baseUnibuildName = unibuild.arch;
var unibuildDir =
builder.generateFilename(baseUnibuildName, { directory: true });
var unibuildJsonFile =
builder.generateFilename(baseUnibuildName + ".json");
mainJson.builds.push({
kind: unibuild.kind,
arch: unibuild.arch,
path: unibuildJsonFile
});
// Save unibuild dependencies. Keyed by the json path rather than thinking
// too hard about how to encode pair (name, arch).
if (isopackBuildInfoJson) {
isopackBuildInfoJson.unibuildDependencies[unibuildJsonFile] =
unibuild.watchSet.toJSON();
}
// Figure out where the npm dependencies go.
var nodeModulesPath = undefined;
var needToCopyNodeModules = false;
if (unibuild.nodeModulesPath) {
if (_.has(npmDirectories, unibuild.nodeModulesPath)) {
// We already have this npm directory from another unibuild.
nodeModulesPath = npmDirectories[unibuild.nodeModulesPath];
} else {
// It's important not to put node_modules at the top level of the
// isopack, so that it is not visible from within plugins.
nodeModulesPath = npmDirectories[unibuild.nodeModulesPath] =
builder.generateFilename("npm/node_modules", {directory: true});
needToCopyNodeModules = true;
}
}
// Construct unibuild metadata
var unibuildJson = {
format: "unipackage-unibuild-pre1",
packageVariables: unibuild.packageVariables,
uses: _.map(unibuild.uses, function (u) {
return {
'package': u.package,
// For cosmetic value, leave false values for these options out of
// the JSON file.
constraint: u.constraint || undefined,
unordered: u.unordered || undefined,
weak: u.weak || undefined
};
}),
implies: (_.isEmpty(unibuild.implies) ? undefined : unibuild.implies),
node_modules: nodeModulesPath,
resources: []
};
// Output 'head', 'body' resources nicely
var concat = { head: [], body: [] };
var offset = { head: 0, body: 0 };
_.each(unibuild.resources, function (resource) {
if (_.contains(["head", "body"], resource.type)) {
if (concat[resource.type].length) {
concat[resource.type].push(new Buffer("\n", "utf8"));
offset[resource.type]++;
}
if (! (resource.data instanceof Buffer))
throw new Error("Resource data must be a Buffer");
unibuildJson.resources.push({
type: resource.type,
file: files.pathJoin(unibuildDir, resource.type),
length: resource.data.length,
offset: offset[resource.type]
});
concat[resource.type].push(resource.data);
offset[resource.type] += resource.data.length;
}
});
_.each(concat, function (parts, type) {
if (parts.length) {
builder.write(files.pathJoin(unibuildDir, type), {
data: Buffer.concat(concat[type], offset[type])
});
}
});
// Output other resources each to their own file
_.each(unibuild.resources, function (resource) {
if (_.contains(["head", "body"], resource.type))
return; // already did this one
unibuildJson.resources.push({
type: resource.type,
file: builder.writeToGeneratedFilename(
files.pathJoin(unibuildDir, resource.servePath),
{ data: resource.data }),
length: resource.data.length,
offset: 0,
servePath: resource.servePath || undefined,
path: resource.path || undefined
});
});
// Output prelink resources
_.each(unibuild.prelinkFiles, function (file) {
var data = new Buffer(file.source, 'utf8');
var resource = {
type: 'prelink',
file: builder.writeToGeneratedFilename(
files.pathJoin(unibuildDir, file.servePath),
{ data: data }),
length: data.length,
offset: 0,
servePath: file.servePath || undefined
};
if (file.sourceMap) {
// Write the source map.
resource.sourceMap = builder.writeToGeneratedFilename(
files.pathJoin(unibuildDir, file.servePath + '.map'),
{ data: new Buffer(file.sourceMap, 'utf8') }
);
}
unibuildJson.resources.push(resource);
});
// If unibuild has included node_modules, copy them in
if (needToCopyNodeModules) {
builder.copyDirectory({
from: unibuild.nodeModulesPath,
to: nodeModulesPath,
npmDiscards: self.npmDiscards,
symlink: false
});
}