-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathpackage.ts
1452 lines (1304 loc) · 63.9 KB
/
package.ts
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
/// <reference path="../localtypings/pxtpackage.d.ts"/>
/// <reference path="../localtypings/pxtparts.d.ts"/>
/// <reference path="../localtypings/pxtarget.d.ts"/>
/// <reference path="util.ts"/>
namespace pxt {
const CONFIG_FIELDS_ORDER = [
"name",
"version",
"description",
"license",
"dependencies",
"files",
"testFiles",
"testDependencies",
"fileDependencies",
"public",
"targetVersions",
"supportedTargets",
"preferredEditor",
"languageRestriction",
"additionalFilePath",
"additionalFilePaths"
]
export class Package {
static stringifyConfig(config: pxt.PackageConfig): string {
// reorg fields
const configMap: Map<string> = config as any
const newCfg: any = {}
for (const f of CONFIG_FIELDS_ORDER) {
if (configMap.hasOwnProperty(f))
newCfg[f] = configMap[f]
}
for (const f of Object.keys(configMap)) {
if (!newCfg.hasOwnProperty(f))
newCfg[f] = configMap[f]
}
// github adds a newline when web editing
return JSON.stringify(newCfg, null, 4) + "\n"
}
static parseAndValidConfig(configStr: string): pxt.PackageConfig {
const json = Util.jsonTryParse(configStr) as pxt.PackageConfig;
return json
&& json.name !== undefined && typeof json.name === "string"
// && json.version && typeof json.version === "string", default to 0.0.0
&& json.files && Array.isArray(json.files) && json.files.every(f => typeof f === "string")
&& json.dependencies && Object.keys(json.dependencies).every(k => typeof json.dependencies[k] === "string")
&& json;
}
static async getConfigAsync(pkgTargetVersion: string, id: string, fullVers: string): Promise<pxt.PackageConfig> {
if (pxt.github.isGithubId(fullVers)) {
const repoInfo = pxt.github.parseRepoId(fullVers);
const packagesConfig = await pxt.packagesConfigAsync()
const gitRepo = await pxt.github.repoAsync(repoInfo.fullName, packagesConfig) // Make sure repo exists and is allowlisted
return gitRepo ? await pxt.github.pkgConfigAsync(repoInfo.fullName, repoInfo.tag, packagesConfig) : null
} else if (fullVers.startsWith("workspace:")) {
// It's a local package
const projId = fullVers.slice("workspace:".length);
// TODO: Fetch pxt.json from the workspace project
return null;
} else if (fullVers.startsWith("pub:")) {
const id = fullVers.slice("pub:".length);
try {
const files = await Cloud.downloadScriptFilesAsync(id);
return JSON.parse(files[CONFIG_NAME]);
}
catch (e) {
pxt.log(`Unable to fetch files for published script '${fullVers}'`);
return null;
}
} else {
// If it's not from GH, assume it's a bundled package
// TODO: Add logic for shared packages if we enable that
const updatedRef = pxt.patching.upgradePackageReference(pkgTargetVersion, id, fullVers);
const bundledPkg = pxt.appTarget.bundledpkgs[updatedRef];
return JSON.parse(bundledPkg[CONFIG_NAME]) as pxt.PackageConfig;
}
}
static corePackages(): pxt.PackageConfig[] {
const pkgs = pxt.appTarget.bundledpkgs;
return Object.keys(pkgs).map(id => JSON.parse(pkgs[id][pxt.CONFIG_NAME]) as pxt.PackageConfig)
.filter(cfg => !!cfg);
}
public addedBy: Package[];
public config: PackageConfig;
public level = -1; // main package = 0, first children = 1, etc
public isLoaded = false;
private resolvedVersion: string;
public ignoreTests = false;
public cppOnly = false;
public installedVersion: string; // resolve version
protected assetPackFiles: pxt.Map<string>;
constructor(public id: string, public _verspec: string, public parent: MainPackage, addedBy: Package, public depName: string) {
if (addedBy) {
this.level = addedBy.level + 1
}
this.addedBy = [addedBy];
}
disablesVariant(v: string) {
return this.config && this.config.disablesVariants && this.config.disablesVariants.indexOf(v) >= 0
}
invalid(): boolean {
return /^invalid:/.test(this.version());
}
version() {
return this.resolvedVersion || this._verspec;
}
verProtocol() {
let spl = this.version().split(':')
if (spl.length > 1) return spl[0]
else return ""
}
verArgument() {
let p = this.verProtocol()
if (p) return this.version().slice(p.length + 1)
return this.version()
}
targetVersion(): string {
return (this.parent && this.parent != <Package>this)
? this.parent.targetVersion()
: this.config.targetVersions
? this.config.targetVersions.target
: undefined;
}
async commonDownloadAsync(): Promise<Map<string>> {
const proto = this.verProtocol()
let files: Map<string>;
if (proto == "pub") {
files = await Cloud.downloadScriptFilesAsync(this.verArgument())
} else if (proto == "github") {
const config = await pxt.packagesConfigAsync();
const resp = await pxt.github.downloadPackageAsync(this.verArgument(), config)
files = resp.files;
} else if (proto == "embed") {
files = pxt.getEmbeddedScript(this.verArgument())
} else if (proto == "pkg") {
// the package source is serialized in a file in the package itself
const src = this.parent || this; // fall back to current package if no parent
const pkgFilesSrc = src.readFile(this.verArgument());
const pkgFilesJson = U.jsonTryParse(pkgFilesSrc) as Map<string>;
if (!pkgFilesJson)
pxt.log(`unable to find ${this.verArgument()}`)
files = pkgFilesJson
}
return files;
}
writeAssetPackFiles() {
const config = JSON.parse(JSON.stringify(this.config)) as pxt.PackageConfig;
config.files = config.files.filter(f => f.endsWith(".jres"));
config.files.push("gallery.ts");
config.dependencies = {};
config.assetPack = true;
this.config = config;
this.saveConfig();
let galleryTS = "";
for (const file of config.files) {
const content = this.readFile(file);
const parsed = U.jsonTryParse(content);
if (parsed) {
const [jres, ts] = pxt.emitGalleryDeclarations(parsed, this.getNamespaceName());
this.writeFile(file, JSON.stringify(jres, null, 4));
galleryTS += ts;
}
}
this.writeFile("gallery.ts", galleryTS);
}
protected getNamespaceName() {
let child: Package = this;
let parent = child.addedBy[0];
const parts: string[] = [];
while (parent && parent !== child) {
if (child.depName) parts.push(child.depName);
child = parent;
parent = child.addedBy[0];
}
return parts.reverse().map(n => ts.pxtc.escapeIdentifier(n)).join(".");
}
host() { return this.parent._host }
readFile(fn: string) {
return this.host().readFile(this, fn)
}
writeFile(fn: string, content: string) {
this.host().writeFile(this, fn, content, true);
}
readGitJson(): pxt.github.GitJson {
const gitJsonText = this.readFile(pxt.github.GIT_JSON);
return pxt.Util.jsonTryParse(gitJsonText) as pxt.github.GitJson;
}
resolveDep(id: string) {
if (this.parent.deps.hasOwnProperty(id))
return this.parent.deps[id];
return null
}
saveConfig() {
const cfg = U.clone(this.config)
delete cfg.additionalFilePaths
const text = pxt.Package.stringifyConfig(cfg);
this.host().writeFile(this, pxt.CONFIG_NAME, text)
}
setPreferredEditor(editor: string) {
if (this.config.preferredEditor != editor) {
this.config.preferredEditor = editor
this.saveConfig()
}
}
getPreferredEditor() {
let editor = this.config.preferredEditor
if (!editor) {
// older editors do not have this field set so we need to apply our
// language resolution logic here
// note that the preferredEditor field will be set automatically on the first save
// 1. no main.blocks in project, open javascript
const hasMainBlocks = this.getFiles().indexOf(pxt.MAIN_BLOCKS) >= 0;
if (!hasMainBlocks)
return pxt.JAVASCRIPT_PROJECT_NAME;
// 2. if main.blocks is empty and main.ts is non-empty
// open typescript
// https://github.com/microsoft/pxt/blob/master/webapp/src/app.tsx#L1032
const mainBlocks = this.readFile(pxt.MAIN_BLOCKS);
const mainTs = this.readFile(pxt.MAIN_TS);
if (!mainBlocks && mainTs)
return pxt.JAVASCRIPT_PROJECT_NAME;
// 3. default ot blocks
return pxt.BLOCKS_PROJECT_NAME;
}
return editor
}
parseJRes(allres: Map<JRes> = {}) {
for (const f of this.getFiles()) {
if (U.endsWith(f, ".jres")) {
inflateJRes(JSON.parse(this.readFile(f)), allres)
}
}
return allres
}
isAssetPack() {
return this.level > 0 && !!this.config?.assetPack;
}
private resolveVersionAsync() {
let v = this._verspec
if (getEmbeddedScript(this.id)) {
this.resolvedVersion = v = "embed:" + this.id
} else if (!v || v == "*") {
// don't hard crash, instead ignore dependency
// U.userError(lf("version not specified for {0}", this.id))
this.configureAsInvalidPackage(lf("version not specified for {0}", this.id));
v = this._verspec;
}
// patch github version numbers
else if (this.verProtocol() == "github") {
const ghid = pxt.github.parseRepoId(this._verspec);
if (ghid && !ghid.tag && this.parent) {
// we have a valid repo but no tag
pxt.debug(`dep: unbound github extensions, trying to resolve tag`)
// check if we've already loaded this slug in the project, in which case we use that version number
const others = pxt.semver.sortLatestTags(Util.values(this.parent?.deps || {})
.map(dep => pxt.github.parseRepoId(dep.version()))
.filter(v => v?.slug === ghid.slug)
.map(v => v.tag));
const best = others[0];
if (best) {
ghid.tag = best;
this.resolvedVersion = v = pxt.github.stringifyRepo(ghid);
pxt.debug(`dep: github patched ${this._verspec} to ${v}`)
}
}
}
return Promise.resolve(v)
}
private downloadAsync() {
return this.resolveVersionAsync()
.then(verNo => {
if (this.invalid()) {
pxt.debug(`skip download of invalid package ${this.id}`);
return undefined;
}
if (!/^embed:/.test(verNo) && this.installedVersion == verNo)
return undefined;
pxt.debug('downloading ' + verNo)
return this.host().downloadPackageAsync(this)
.then(() => {
this.loadConfig();
if (this.isAssetPack()) {
this.writeAssetPackFiles();
}
pxt.debug(`installed ${this.id} /${verNo}`)
})
})
}
loadConfig() {
if (this.level != 0 && this.invalid())
return; // don't try load invalid dependency
const confStr = this.readFile(pxt.CONFIG_NAME)
if (!confStr)
U.userError(`extension ${this.id} is missing ${pxt.CONFIG_NAME}`)
this.parseConfig(confStr);
if (this.level != 0)
this.installedVersion = this.version()
this.saveConfig()
}
protected validateConfig() {
if (!this.config.dependencies)
U.userError("Missing dependencies in config of: " + this.id)
if (!Array.isArray(this.config.files))
U.userError("Missing files in config of: " + this.id)
if (typeof this.config.name != "string" || !this.config.name)
this.config.name = lf("Untitled");
// don't be so uptight about project names,
// handle invalid names downstream
if (this.config.targetVersions
&& this.config.targetVersions.target
&& this.config.targetVersions.targetId === pxt.appTarget.id // make sure it's the same target
&& appTarget.versions
&& semver.majorCmp(this.config.targetVersions.target, appTarget.versions.target) > 0)
U.userError(lf("{0} requires target version {1} (you are running {2})",
this.config.name, this.config.targetVersions.target, appTarget.versions.target))
}
isPackageInUse(pkgId: string, ts: string = this.readFile(pxt.MAIN_TS)): boolean {
// Build the RegExp that will determine whether the dependency is in use. Try to use upgrade rules,
// otherwise fallback to the package's name
let regex: RegExp = null;
const upgrades = pxt.patching.computePatches(this.targetVersion(), "missingPackage");
if (upgrades) {
upgrades.forEach((rule) => {
Object.keys(rule.map).forEach((match) => {
if (rule.map[match] === pkgId) {
regex = new RegExp(match, "g");
}
});
});
}
if (!regex) {
regex = new RegExp(pkgId + "\\.", "g");
}
return regex.test(ts);
}
private upgradePackagesAsync(): Promise<pxt.Map<string>> {
if (!this.config)
this.loadConfig();
return pxt.packagesConfigAsync()
.then(packagesConfig => {
let numfixes = 0
let fixes: pxt.Map<string> = {};
U.iterMap(this.config.dependencies, (pkg, ver) => {
if (pxt.github.isGithubId(ver)) {
const upgraded = pxt.github.upgradedPackageReference(packagesConfig, ver)
if (upgraded && upgraded != ver) {
pxt.log(`upgrading dep ${pkg}: ${ver} -> ${upgraded}`);
fixes[ver] = upgraded;
this.config.dependencies[pkg] = upgraded
numfixes++
}
}
})
if (numfixes)
this.saveConfig()
return numfixes && fixes;
})
}
private getMissingPackages(config: pxt.PackageConfig, ts: string): Map<string> {
const upgrades = pxt.patching.computePatches(this.targetVersion(), "missingPackage");
const missing: Map<string> = {};
if (ts && upgrades)
upgrades.forEach(rule => {
Object.keys(rule.map).forEach(match => {
const regex = new RegExp(match, 'g');
const pkg = rule.map[match];
ts.replace(regex, (m) => {
if (!config.dependencies[pkg]) {
missing[pkg] = "*";
}
return "";
})
});
})
return missing;
}
/**
* For the given package config or ID, looks through all the currently installed packages to find conflicts in
* Yotta settings and version spec
*/
findConflictsAsync(pkgOrId: string | PackageConfig, version: string): Promise<cpp.PkgConflictError[]> {
let conflicts: cpp.PkgConflictError[] = [];
let pkgCfg: PackageConfig;
return Promise.resolve()
.then(() => {
// Get the package config if it's not already provided
if (typeof pkgOrId === "string") {
return Package.getConfigAsync(this.targetVersion(), pkgOrId, version);
} else {
return Promise.resolve(pkgOrId as PackageConfig);
}
})
.then((cfg) => {
pkgCfg = cfg;
// Iterate through all installed packages and check for conflicting settings
if (pkgCfg) {
const yottaCfg = pkgCfg.yotta ? U.jsonFlatten(pkgCfg.yotta.config) : null;
this.parent.sortedDeps().forEach((depPkg) => {
if (pkgCfg.core && depPkg.config.core &&
pkgCfg.name != depPkg.config.name) {
const conflict = new cpp.PkgConflictError(lf("conflict between core extensions {0} and {1}", pkgCfg.name, depPkg.id));
conflict.pkg0 = depPkg;
conflicts.push(conflict);
return;
}
let foundYottaConflict = false;
if (yottaCfg) {
const depConfig = depPkg.config || JSON.parse(depPkg.readFile(CONFIG_NAME)) as PackageConfig;
const hasYottaSettings = !!depConfig && !!depConfig.yotta && !!depPkg.config.yotta.config;
if (hasYottaSettings) {
const depYottaCfg = U.jsonFlatten(depConfig.yotta.config);
for (const settingName of Object.keys(yottaCfg)) {
const depSetting = depYottaCfg[settingName];
const isJustDefaults = pkgCfg.yotta.configIsJustDefaults || depConfig.yotta.configIsJustDefaults;
if (depYottaCfg.hasOwnProperty(settingName) && depSetting !== yottaCfg[settingName] && !isJustDefaults && (!depPkg.parent.config.yotta || !depPkg.parent.config.yotta.ignoreConflicts)) {
const conflict = new cpp.PkgConflictError(lf("conflict on yotta setting {0} between extensions {1} and {2}", settingName, pkgCfg.name, depPkg.id));
conflict.pkg0 = depPkg;
conflict.settingName = settingName;
conflicts.push(conflict);
foundYottaConflict = true;
}
}
}
}
if (!foundYottaConflict
&& pkgCfg.name === depPkg.id
&& depPkg._verspec !== version
&& !/^file:/.test(depPkg._verspec)
&& !/^file:/.test(version)) {
// we have a potential version mistmatch here
// check if versions are semver compatible for github refs
const ghCurrent = /^github:/.test(depPkg._verspec)
&& pxt.github.parseRepoId(depPkg._verspec);
const ghNew = /^github:/.test(version)
&& pxt.github.parseRepoId(version);
if (!ghCurrent || !ghNew // only for github refs
|| ghCurrent.fullName !== ghNew.fullName // must be same extension
// if newversion does not have tag, it's ok
// note: we are upgrade major versions as well
|| (ghNew.tag && pxt.semver.strcmp(ghCurrent.tag, ghNew.tag) < 0)) {
const conflict = new cpp.PkgConflictError(lf("version mismatch for extension {0} (added: {1}, adding: {2})",
depPkg.id,
depPkg._verspec,
version));
conflict.pkg0 = depPkg;
conflict.isVersionConflict = true;
conflicts.push(conflict);
}
}
});
}
// Also check for conflicts for all the specified package's dependencies (recursively)
return Object.keys(pkgCfg?.dependencies ?? {}).reduce((soFar, pkgDep) => {
return soFar
.then(() => this.findConflictsAsync(pkgDep, pkgCfg.dependencies[pkgDep]))
.then((childConflicts) => conflicts.push.apply(conflicts, childConflicts));
}, Promise.resolve());
})
.then(() => {
// For each conflicting package, we need to include their ancestor tree in the list of conflicts
// For example, if package A depends on package B, and package B is in conflict with package C,
// then package A is also technically in conflict with C
const allAncestors = (p: Package): Package[] => {
const ancestors: Package[] = [];
p.addedBy.forEach((a) => {
if (a.id !== this.id) {
ancestors.push.apply(allAncestors(a));
ancestors.push(a);
}
});
return ancestors;
}
const additionalConflicts: cpp.PkgConflictError[] = [];
conflicts.forEach((c) => {
additionalConflicts.push.apply(additionalConflicts, allAncestors(c.pkg0).map((anc) => {
const confl = new cpp.PkgConflictError(c.isVersionConflict ?
lf("a dependency of {0} has a version mismatch with extension {1} (added: {1}, adding: {2})", anc.id, pkgCfg.name, c.pkg0._verspec, version) :
lf("conflict on yotta setting {0} between extensions {1} and {2}", c.settingName, pkgCfg.name, c.pkg0.id));
confl.pkg0 = anc;
return confl;
}));
});
conflicts.push.apply(conflicts, additionalConflicts);
// Remove duplicate conflicts (happens if more than one package had the same ancestor)
conflicts = conflicts.filter((c, index) => {
for (let i = 0; i < index; ++i) {
if (c.pkg0.id === conflicts[i].pkg0.id) {
return false;
}
}
return true;
});
return conflicts;
});
}
public configureAsInvalidPackage(reason: string) {
pxt.log(`invalid package ${this.id}: ${reason}`);
this._verspec = "invalid:" + this.id;
this.config = <PackageConfig>{
name: this.id,
description: reason,
dependencies: {},
files: []
}
}
private parseConfig(cfgSrc: string, targetVersion?: string) {
try {
const cfg = <PackageConfig>JSON.parse(cfgSrc);
this.config = cfg;
} catch (e) {
this.configureAsInvalidPackage(lf("Syntax error in pxt.json"));
pxt.tickEvent("package.invalidConfigEncountered")
}
const currentConfig = JSON.stringify(this.config);
for (const dep in this.config.dependencies) {
const value = pxt.patching.upgradePackageReference(this.targetVersion(), dep, this.config.dependencies[dep]);
if (value != dep) {
delete this.config.dependencies[dep];
if (value) {
this.config.dependencies[value] = "*";
}
}
}
if (targetVersion) {
this.config.targetVersions = {
target: targetVersion
};
}
if (JSON.stringify(this.config) != currentConfig) {
this.saveConfig();
}
this.validateConfig();
}
private patchCorePackage() {
Util.assert(appTarget.simulator && appTarget.simulator.dynamicBoardDefinition);
Util.assert(this.level == 0);
// find all core packages in target
const corePackages = Object.keys(this.config.dependencies)
.filter(dep => !!dep && (
dep == pxt.BLOCKS_PROJECT_NAME || dep == pxt.JAVASCRIPT_PROJECT_NAME ||
(<pxt.PackageConfig>JSON.parse((pxt.appTarget.bundledpkgs[dep] || {})[pxt.CONFIG_NAME] || "{}").core)
));
// no core package? add the first one
if (corePackages.length == 0) {
const allCorePkgs = pxt.Package.corePackages();
/* eslint-disable @typescript-eslint/no-unused-expressions */
if (allCorePkgs.length)
this.config.dependencies[allCorePkgs[0].name];
/* eslint-enable @typescript-eslint/no-unused-expressions */
} else if (corePackages.length > 1) {
// keep last package
corePackages.pop();
corePackages.forEach(dep => {
pxt.log(`removing core package ${dep}`)
delete this.config.dependencies[dep];
});
}
}
resolvedDependencies(): Package[] {
return Object.keys(this.dependencies()).map(n => this.resolveDep(n))
}
dependencies(includeCpp = false): pxt.Map<string> {
if (!this.config) return {};
const dependencies = Util.clone(this.config.dependencies || {});
// add test dependencies if nedeed
if (this.level == 0 && this.config.testDependencies) {
// only add testDepdencies that can be resolved
Util.iterMap(this.config.testDependencies, (k, v) => {
if (v != "*" || pxt.appTarget.bundledpkgs[k])
dependencies[k] = v;
})
}
if (includeCpp && this.config.cppDependencies) {
Util.jsonMergeFrom(dependencies, this.config.cppDependencies);
}
return dependencies;
}
async loadAsync(isInstall = false, targetVersion?: string): Promise<void> {
if (this.isLoaded) return;
if (this.level == 0 && !pxt.appTarget.multiVariants)
pxt.setAppTargetVariant(null)
this.isLoaded = true;
const str = this.readFile(pxt.CONFIG_NAME);
if (str == null) {
if (!isInstall)
U.userError("Package not installed: " + this.id + ", did you forget to run `pxt install`?")
} else {
this.parseConfig(str);
}
// if we are installing this script, we haven't yet downloaded the config
// do upgrade later
if (this.level == 0 && !isInstall) {
await this.upgradePackagesAsync();
}
if (isInstall) {
await this.downloadAsync();
if (this.level !== 0 && !this.isAssetPack()) {
for (const parent of this.addedBy) {
if (parent.config.assetPacks?.[this.id]) {
this.writeAssetPackFiles();
break;
}
}
}
}
// we are installing the script, and we've download the original version and we haven't upgraded it yet
// do upgrade and reload as needed
if (this.level == 0 && isInstall) {
const fixes = await this.upgradePackagesAsync();
if (fixes) {
// worst case scenario with double load
Object.keys(fixes).forEach(key => pxt.tickEvent("package.doubleload", { "extension": key }))
pxt.log(`upgraded, downloading again`);
pxt.debug(fixes);
await this.downloadAsync();
}
}
if (appTarget.simulator && appTarget.simulator.dynamicBoardDefinition) {
if (this.level == 0) {
this.patchCorePackage();
}
if (this.config.compileServiceVariant) {
pxt.setAppTargetVariant(this.config.compileServiceVariant)
}
if (this.config.files.indexOf("board.json") >= 0) {
const def = appTarget.simulator.boardDefinition = JSON.parse(this.readFile("board.json")) as pxsim.BoardDefinition;
def.id = this.config.name;
appTarget.appTheme.boardName = def.boardName || lf("board");
appTarget.appTheme.driveDisplayName = def.driveDisplayName || lf("DRIVE");
let expandPkg = (v: string) => {
let m = /^pkg:\/\/(.*)/.exec(v)
if (m) {
let fn = m[1]
let content = this.readFile(fn)
return U.toDataUri(content, U.getMime(fn))
} else {
return v
}
}
let bd = appTarget.simulator.boardDefinition
if (typeof bd.visual == "object") {
let vis = bd.visual as pxsim.BoardImageDefinition
vis.image = expandPkg(vis.image)
vis.outlineImage = expandPkg(vis.outlineImage)
}
}
}
const handleVerMismatch = (mod: Package, ver: string) => {
pxt.debug(`version spec mismatch: ${mod._verspec} != ${ver}`)
// if both are github, try to pick the higher semver
if (/^github:/.test(mod._verspec) && /^github:/.test(ver)) {
const modid = pxt.github.parseRepoId(mod._verspec);
const verid = pxt.github.parseRepoId(ver);
// same repo
if (modid?.slug && modid?.slug === verid?.slug) {
// if modid does not have a tag, try sniffing it from config
// this may be an issue if the user does not create releases
// and pulls from master
const modtag = modid?.tag || mod.config?.version;
const vertag = verid.tag
// if there is no tag on the current dependency,
// assume same as existing module version if any
if (modtag && !vertag) {
pxt.debug(`unversioned ${ver}, using ${modtag}`)
return
}
const c = pxt.semver.strcmp(modtag, verid.tag);
if (c == 0) {
// turns out to be the same versions
pxt.debug(`resolved version are ${modtag}`)
return;
}
else if (c > 0) {
// already loaded version of dependencies (modtag) is greater
// than current version (ver), use it instead
pxt.debug(`auto-upgraded ${ver} to ${modtag}`)
return;
}
}
}
// ignore, file: protocol
if (/^file:/.test(mod._verspec) || /^file:/.test(ver)) {
pxt.debug(`ignore file: mismatch issues`)
return;
}
// crashing if really really not good
// so instead we just ignore and continute
mod.configureAsInvalidPackage(lf("version mismatch"))
}
const loadDepsRecursive = async (deps: pxt.Map<string>, from: Package, isCpp = false) => {
if (!deps) deps = from.dependencies(isCpp);
pxt.debug(`deps: ${from.id}->${Object.keys(deps).join(", ")}`);
deps = pxt.github.resolveMonoRepoVersions(deps);
pxt.debug(`deps: resolved ${from.id}->${Object.keys(deps).join(", ")}`);
for (let id of Object.keys(deps)) {
let ver = deps[id] || "*"
pxt.debug(`dep: load ${from.id}.${id}${isCpp ? "++" : ""}: ${ver}`)
if (id == "hw" && pxt.hwVariant)
id = "hw---" + pxt.hwVariant
// for github references, make sure the version is compatible with previously
// loaded references, regardless of the id
const ghver = github.parseRepoId(ver)
if (ghver?.slug) {
// let's start by resolving the maximum version
// number of the parent repo already loaded
const repoVersions = Object.values(from.parent.deps)
.map(p => github.parseRepoId(p._verspec))
.filter(v => v?.slug === ghver.slug)
.map(v => v.tag)
const repoVersion = repoVersions
.reduce((v1, v2) => semver.strcmp(v1, v2) > 0 ? v1 : v2, "0.0.0")
pxt.debug(`dep: common repo ${ghver.slug} version found ${repoVersion}`)
if (semver.strcmp(repoVersion, "0.0.0") > 0) {
// now let's check if we have a higher version to use
if (!ghver.tag || semver.strcmp(repoVersion, ghver.tag) > 0) {
pxt.debug(`dep: upgrade from ${ghver.tag} to ${repoVersion}`)
ghver.tag = repoVersion
ver = github.stringifyRepo(ghver, true)
}
}
}
let mod = from.resolveDep(id)
if (mod) {
// check if the current dependecy matches the ones
// loaded in parent
if (!mod.invalid() && mod._verspec !== ver)
handleVerMismatch(mod, ver);
// bail out if invalid
if (mod.invalid()) {
// failed to resolve dependency, ignore
mod.level = Math.min(mod.level, from.level + 1)
mod.addedBy.push(from)
continue
}
if (!isCpp) {
mod.level = Math.min(mod.level, from.level + 1)
mod.addedBy.push(from)
}
} else {
mod = new Package(id, ver, from.parent, from, id)
if (isCpp)
mod.cppOnly = true
from.parent.deps[id] = mod
// we can have "core---nrf52" to be used instead of "core" in other packages
from.parent.deps[id.replace(/---.*/, "")] = mod
await mod.loadAsync(isInstall)
}
}
}
await loadDepsRecursive(null, this);
// get paletter config loading deps, so the more higher level packages take precedence
this.patchAppTargetPalette();
// get screen size loading deps, so the more higher level packages take precedence
if (this.config.screenSize && appTarget.runtime)
appTarget.runtime.screenSize = U.clone(this.config.screenSize);
if (this.level === 0) {
// Check for missing packages. We need to add them 1 by 1 in case they conflict with eachother.
const mainTs = this.readFile(pxt.MAIN_TS);
if (mainTs) {
const missingPackages = this.getMissingPackages(this.config, mainTs);
let didAddPackages = false;
for (const missing of Object.keys(missingPackages)) {
const conflicts = await this.findConflictsAsync(missing, missingPackages[missing]);
if (conflicts.length) {
const conflictNames = conflicts.map((c) => c.pkg0.id).join(", ");
const settingNames = conflicts.map((c) => c.settingName).filter((s) => !!s).join(", ");
pxt.log(`skipping missing package ${missing} because it conflicts with the following packages: ${conflictNames} (conflicting settings: ${settingNames})`);
} else {
pxt.log(`adding missing package ${missing}`);
didAddPackages = true;
this.config.dependencies[missing] = "*"
const addDependency: Map<string> = {};
addDependency[missing] = missingPackages[missing];
await loadDepsRecursive(addDependency, this);
}
}
if (didAddPackages) {
this.saveConfig();
this.validateConfig();
}
}
await Promise.all(U.values(this.parent.deps).map(pkg => loadDepsRecursive(null, pkg, true)));
}
pxt.debug(` installed ${this.id}`);
}
static depWarnings: Map<boolean> = {}
getFiles() {
let res: string[]
if (this.level == 0 && !this.ignoreTests)
res = this.config.files.concat(this.config.testFiles || [])
else
res = this.config.files.slice(0);
const fd = this.config.fileDependencies
if (this.config.fileDependencies)
res = res.filter(fn => {
const evalCond = (cond: string): boolean => {
cond = cond.trim()
if (cond[0] == '!')
return !evalCond(cond.slice(1))
if (/^[\w-]+$/.test(cond)) {
const dep = this.parent.resolveDep(cond)
if (dep && !dep.cppOnly)
return true
return false
}
const m = /^target:(\w+)$/.exec(cond)
if (m)
return m[1] == pxt.appTarget.id || m[1] == pxt.appTarget.platformid
if (!Package.depWarnings[cond]) {
Package.depWarnings[cond] = true
pxt.log(`invalid dependency expression: ${cond} in ${this.id}/${fn}`)
}
return false
}
const cond = U.lookup(fd, fn)
if (!cond || !cond.trim()) return true
return cond.split('||').some(c => c.split('&&').every(evalCond))
})
return res
}
addSnapshot(files: Map<string>, exts: string[] = [""]) {
for (let fn of this.getFiles()) {
if (exts.some(e => U.endsWith(fn, e))) {
files[this.id + "/" + fn] = this.readFile(fn)
}
}
files[this.id + "/" + pxt.CONFIG_NAME] = this.readFile(pxt.CONFIG_NAME)
}
/**
* Returns localized strings qName (+ some additional identification data) -> translation
*/
packageLocalizationStringsAsync(lang: string): Promise<Map<string>> {
const targetId = pxt.appTarget.id;
const filenames = [this.config.name + "-jsdoc", this.config.name];
const r: Map<string> = {};
const theme = pxt.appTarget.appTheme || {};
if (this.config.skipLocalization)
return Promise.resolve(r);
// live loc of bundled packages
if (pxt.Util.liveLocalizationEnabled() && this.id != "this" && pxt.appTarget.bundledpkgs[this.id]) {
pxt.debug(`loading live translations for ${this.id}`)
return Promise.all(filenames.map(
fn => pxt.Util.downloadLiveTranslationsAsync(lang, `${targetId}/${fn}-strings.json`)
.then(tr => {
if (tr && Object.keys(tr).length) {
Util.jsonMergeFrom(r, tr);
} else {
pxt.tickEvent("translations.livetranslationsfailed", { "filename": fn });
Util.jsonMergeFrom(r, this.bundledStringsForFile(lang, fn));
}
})
.catch(e => {
pxt.tickEvent("translations.livetranslationsfailed", { "filename": fn });
pxt.log(`error while downloading ${targetId}/${fn}-strings.json`);
Util.jsonMergeFrom(r, this.bundledStringsForFile(lang, fn));
}))
).then(() => r);
}
else {
filenames.map(name => {
return this.bundledStringsForFile(lang, name);
}).filter(d => !!d).forEach(d => Util.jsonMergeFrom(r, d));
return Promise.resolve(r);
}
}
bundledStringsForFile(lang: string, filename: string): Map<string> {
let r: Map<string> = {};
let [initialLang, baseLang, initialLangLowerCase] = pxt.Util.normalizeLanguageCode(lang);
const files = this.config.files;
let fn = `_locales/${initialLang}/${filename}-strings.json`;
if (initialLangLowerCase && files.indexOf(fn) === -1) {
fn = `_locales/${initialLangLowerCase}/${filename}-strings.json`;
}
if (baseLang && files.indexOf(fn) === -1) {
fn = `_locales/${baseLang}/${filename}-strings.json`;
}
if (files.indexOf(fn) > -1) {
try {
r = JSON.parse(this.readFile(fn));
} catch (e) {
pxt.reportError("extension", "Extension localization JSON failed to parse", {
fileName: fn,
lang: lang,
extension: this.verArgument(),
});
const isGithubRepo = this.verProtocol() === "github";
if (isGithubRepo) {
pxt.tickEvent("loc.errors.json", {
repo: this.verArgument(),
file: fn,
});
}
}
}
return r;
}
patchAppTargetPalette() {
if (this.config.palette && appTarget.runtime) {