forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.ts
More file actions
1292 lines (1153 loc) · 43.5 KB
/
workspace.ts
File metadata and controls
1292 lines (1153 loc) · 43.5 KB
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="../../built/pxtlib.d.ts" />
/// <reference path="../../built/pxteditor.d.ts" />
/// <reference path="../../built/pxtwinrt.d.ts" />
import * as db from "./db";
import * as core from "./core";
import * as data from "./data";
import * as browserworkspace from "./browserworkspace"
import * as fileworkspace from "./fileworkspace"
import * as memoryworkspace from "./memoryworkspace"
import * as iframeworkspace from "./iframeworkspace"
import * as cloudsync from "./cloudsync"
import * as indexedDBWorkspace from "./idbworkspace";
import * as compiler from "./compiler"
import U = pxt.Util;
import Cloud = pxt.Cloud;
// Avoid importing entire crypto-js
/* tslint:disable:no-submodule-imports */
const sha1 = require("crypto-js/sha1");
type Header = pxt.workspace.Header;
type ScriptText = pxt.workspace.ScriptText;
type WorkspaceProvider = pxt.workspace.WorkspaceProvider;
type InstallHeader = pxt.workspace.InstallHeader;
interface HeaderWithScript {
header: Header;
text: ScriptText;
version: pxt.workspace.Version;
}
let allScripts: HeaderWithScript[] = [];
let headerQ = new U.PromiseQueue();
let impl: WorkspaceProvider;
let implType: string;
function lookup(id: string) {
return allScripts.filter(x => x.header.id == id || x.header.path == id)[0]
}
export function gitsha(data: string, encoding: "utf-8" | "base64" = "utf-8") {
if (encoding == "base64") {
const d = atob(data);
return (sha1("blob " + d.length + "\u0000" + d) + "")
} else
return (sha1("blob " + U.toUTF8(data).length + "\u0000" + data) + "")
}
export function copyProjectToLegacyEditor(header: Header, majorVersion: number): Promise<Header> {
if (!isBrowserWorkspace()) {
return Promise.reject("Copy operation only works in browser workspace");
}
return browserworkspace.copyProjectToLegacyEditor(header, majorVersion);
}
export function setupWorkspace(id: string) {
U.assert(!impl, "workspace set twice");
pxt.log(`workspace: ${id}`);
implType = id;
switch (id) {
case "fs":
case "file":
// Local file workspace, serializes data under target/projects/
impl = fileworkspace.provider;
break;
case "mem":
case "memory":
impl = memoryworkspace.provider;
break;
case "iframe":
// Iframe workspace, the editor relays sync messages back and forth when hosted in an iframe
impl = iframeworkspace.provider;
break;
case "uwp":
fileworkspace.setApiAsync(pxt.winrt.workspace.fileApiAsync);
impl = pxt.winrt.workspace.getProvider(fileworkspace.provider);
break;
case "idb":
impl = indexedDBWorkspace.provider;
break;
case "cloud":
case "browser":
default:
impl = browserworkspace.provider
break;
}
}
export function getHeaders(withDeleted = false) {
checkSession();
let r = allScripts.map(e => e.header).filter(h => (withDeleted || !h.isDeleted) && !h.isBackup)
r.sort((a, b) => b.recentUse - a.recentUse)
return r
}
export function makeBackupAsync(h: Header, text: ScriptText): Promise<Header> {
let h2 = U.flatClone(h)
h2.id = U.guidGen()
delete h2._rev
delete (h2 as any)._id
h2.isBackup = true;
return importAsync(h2, text)
.then(() => {
h.backupRef = h2.id;
return saveAsync(h2);
})
.then(() => h2)
}
export function restoreFromBackupAsync(h: Header) {
if (!h.backupRef || h.isDeleted) return Promise.resolve();
const refId = h.backupRef;
return getTextAsync(refId)
.then(files => {
delete h.backupRef;
return saveAsync(h, files);
})
.then(() => {
const backup = getHeader(refId);
backup.isDeleted = true;
return saveAsync(backup);
})
.catch(() => {
delete h.backupRef;
return saveAsync(h);
});
}
export function cleanupBackupsAsync() {
checkSession();
const allHeaders = allScripts.map(e => e.header);
const refMap: pxt.Map<boolean> = {};
// Figure out which scripts have backups
allHeaders.filter(h => h.backupRef).forEach(h => refMap[h.backupRef] = true);
// Delete the backups that don't have any scripts referencing them
return Promise.all(allHeaders.filter(h => (h.isBackup && !refMap[h.id])).map(h => {
h.isDeleted = true;
return saveAsync(h);
}));
}
export function getHeader(id: string) {
checkSession();
let e = lookup(id)
if (e && !e.header.isDeleted)
return e.header
return null
}
let sessionID: string;
export function isSessionOutdated() {
return pxt.storage.getLocal('pxt_workspace_session_id') != sessionID;
}
function checkSession() {
if (isSessionOutdated()) {
pxt.Util.assert(false, "trying to access outdated session")
}
}
export function initAsync() {
if (!impl) impl = browserworkspace.provider;
// generate new workspace session id to avoid races with other tabs
sessionID = ts.pxtc.Util.guidGen();
pxt.storage.setLocal('pxt_workspace_session_id', sessionID);
pxt.debug(`workspace session: ${sessionID}`);
allScripts = []
return syncAsync()
.then(state => cleanupBackupsAsync().then(() => state))
.then(_ => {
pxt.perf.recordMilestone("workspace init finished")
return _
})
}
export function getTextAsync(id: string): Promise<ScriptText> {
checkSession();
let e = lookup(id)
if (!e)
return Promise.resolve(null as ScriptText)
if (e.text)
return Promise.resolve(e.text)
return headerQ.enqueue(id, () => impl.getAsync(e.header)
.then(resp => {
if (!e.text) {
// otherwise we were beaten to it
e.text = fixupFileNames(resp.text);
}
e.version = resp.version;
return e.text
}))
}
export interface ScriptMeta {
description: string;
blocksWidth?: number;
blocksHeight?: number;
}
// https://github.com/Microsoft/pxt-backend/blob/master/docs/sharing.md#anonymous-publishing
export function anonymousPublishAsync(h: Header, text: ScriptText, meta: ScriptMeta, screenshotUri?: string) {
const saveId = {}
h.saveId = saveId
let thumbnailBuffer: string;
let thumbnailMimeType: string;
if (screenshotUri) {
const m = /^data:(image\/(png|gif));base64,([a-zA-Z0-9+/]+=*)$/.exec(screenshotUri);
if (m) {
thumbnailBuffer = m[3];
thumbnailMimeType = m[1];
}
}
const stext = JSON.stringify(text, null, 2) + "\n"
const scrReq = {
name: h.name,
target: h.target,
targetVersion: h.targetVersion,
description: meta.description || lf("Made with ❤️ in {0}.", pxt.appTarget.title || pxt.appTarget.name),
editor: h.editor,
text: text,
meta: {
versions: pxt.appTarget.versions,
blocksHeight: meta.blocksHeight,
blocksWidth: meta.blocksWidth
},
thumbnailBuffer,
thumbnailMimeType
}
pxt.debug(`publishing script; ${stext.length} bytes`)
return Cloud.privatePostAsync("scripts", scrReq, /* forceLiveEndpoint */ true)
.then((inf: Cloud.JsonScript) => {
if (inf.shortid) inf.id = inf.shortid;
h.pubId = inf.shortid
h.pubCurrent = h.saveId === saveId
h.meta = inf.meta;
pxt.debug(`published; id /${h.pubId}`)
return saveAsync(h)
.then(() => inf)
})
}
function fixupVersionAsync(e: HeaderWithScript) {
if (e.version !== undefined)
return Promise.resolve()
return impl.getAsync(e.header)
.then(resp => {
e.version = resp.version;
})
}
export function saveAsync(h: Header, text?: ScriptText, isCloud?: boolean): Promise<void> {
checkSession();
U.assert(h.target == pxt.appTarget.id);
if (h.temporary)
return Promise.resolve()
let e = lookup(h.id)
//U.assert(e.header === h)
if (!isCloud)
h.recentUse = U.nowSeconds()
if (text || h.isDeleted) {
if (text)
e.text = text
if (!isCloud) {
h.pubCurrent = false
h.blobCurrent = false
h.modificationTime = U.nowSeconds();
h.targetVersion = h.targetVersion || "0.0.0";
}
h.saveId = null
// update version on save
}
// perma-delete
if (h.isDeleted && h.blobVersion == "DELETED") {
let idx = allScripts.indexOf(e)
U.assert(idx >= 0)
allScripts.splice(idx, 1)
return headerQ.enqueue(h.id, () =>
fixupVersionAsync(e).then(() =>
impl.deleteAsync ? impl.deleteAsync(h, e.version) : impl.setAsync(h, e.version, {})))
}
// check if we have dynamic boards, store board info for home page rendering
if (text && pxt.appTarget.simulator && pxt.appTarget.simulator.dynamicBoardDefinition) {
const pxtjson = pxt.Package.parseAndValidConfig(text[pxt.CONFIG_NAME]);
if (pxtjson && pxtjson.dependencies)
h.board = Object.keys(pxtjson.dependencies)
.filter(p => !!pxt.bundledSvg(p))[0];
}
return headerQ.enqueue<void>(h.id, () =>
fixupVersionAsync(e).then(() =>
impl.setAsync(h, e.version, text ? e.text : null)
.then(ver => {
if (text)
e.version = ver
if ((text && !isCloud) || h.isDeleted) {
h.pubCurrent = false
h.blobCurrent = false
h.saveId = null
data.invalidate("text:" + h.id)
data.invalidate("pkg-git-status:" + h.id)
}
data.invalidate("header:" + h.id)
data.invalidate("header:*")
})))
}
function computePath(h: Header) {
let path = h.name.replace(/[^a-zA-Z0-9]+/g, " ").trim().replace(/ /g, "-")
if (!path)
path = "Untitled"; // do not translate
if (lookup(path)) {
let n = 2
while (lookup(path + "-" + n))
n++;
path += "-" + n
}
return path
}
export function importAsync(h: Header, text: ScriptText, isCloud = false) {
h.path = computePath(h)
const e: HeaderWithScript = {
header: h,
text: text,
version: null
}
allScripts.push(e)
return saveAsync(h, text, isCloud)
}
export function installAsync(h0: InstallHeader, text: ScriptText) {
checkSession();
U.assert(h0.target == pxt.appTarget.id);
const h = <Header>h0
h.id = ts.pxtc.Util.guidGen();
h.recentUse = U.nowSeconds()
h.modificationTime = h.recentUse;
const cfg: pxt.PackageConfig = pxt.Package.parseAndValidConfig(text[pxt.CONFIG_NAME]);
if (cfg && cfg.preferredEditor) {
h.editor = cfg.preferredEditor
pxt.Util.setEditorLanguagePref(cfg.preferredEditor);
}
return importAsync(h, text)
.then(() => h)
}
export function renameAsync(h: Header, newName: string) {
checkSession();
return cloudsync.renameAsync(h, newName);
}
export function duplicateAsync(h: Header, text: ScriptText, rename?: boolean): Promise<Header> {
let e = lookup(h.id)
U.assert(e.header === h)
let h2 = U.flatClone(h)
e.header = h2
h.id = U.guidGen()
if (rename) {
h.name = createDuplicateName(h);
let cfg = JSON.parse(text[pxt.CONFIG_NAME]) as pxt.PackageConfig
cfg.name = h.name
text[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
}
delete h._rev
delete (h as any)._id
return importAsync(h, text)
.then(() => h)
}
export function createDuplicateName(h: Header) {
let reducedName = h.name.indexOf("#") > -1 ?
h.name.substring(0, h.name.lastIndexOf('#')).trim() : h.name;
let names = U.toDictionary(allScripts.filter(e => !e.header.isDeleted), e => e.header.name)
let n = 2
while (names.hasOwnProperty(reducedName + " #" + n))
n++
return reducedName + " #" + n;
}
export function saveScreenshotAsync(h: Header, data: string, icon: string) {
checkSession();
return impl.saveScreenshotAsync
? impl.saveScreenshotAsync(h, data, icon)
: Promise.resolve();
}
export function fixupFileNames(txt: ScriptText) {
if (!txt) return txt;
["kind.json", "yelm.json"].forEach(oldName => {
if (!txt[pxt.CONFIG_NAME] && txt[oldName]) {
txt[pxt.CONFIG_NAME] = txt[oldName]
delete txt[oldName]
}
})
return txt
}
const scriptDlQ = new U.PromiseQueue();
const scripts = new db.Table("script"); // cache for published scripts
//let scriptCache:any = {}
export function getPublishedScriptAsync(id: string) {
checkSession();
//if (scriptCache.hasOwnProperty(id)) return Promise.resolve(scriptCache[id])
if (pxt.github.isGithubId(id))
id = pxt.github.normalizeRepoId(id)
let eid = encodeURIComponent(id)
return pxt.packagesConfigAsync()
.then(config => scriptDlQ.enqueue(id, () => scripts.getAsync(eid)
.then(v => v.files, e =>
(pxt.github.isGithubId(id) ?
pxt.github.downloadPackageAsync(id, config).then(v => v.files) :
Cloud.downloadScriptFilesAsync(id))
.catch(core.handleNetworkError)
.then(files => scripts.setAsync({ id: eid, files: files })
.then(() => {
//return (scriptCache[id] = files)
return files
})))
.then(fixupFileNames))
);
}
export enum PullStatus {
NoSourceControl,
UpToDate,
GotChanges,
NeedsCommit,
BranchNotFound
}
const GIT_JSON = pxt.github.GIT_JSON
type GitJson = pxt.github.GitJson
export async function hasPullAsync(hd: Header) {
return (await pullAsync(hd, true)) == PullStatus.GotChanges
}
export async function pullAsync(hd: Header, checkOnly = false) {
let files = await getTextAsync(hd.id)
await recomputeHeaderFlagsAsync(hd, files)
let gitjsontext = files[GIT_JSON]
if (!gitjsontext)
return PullStatus.NoSourceControl
let gitjson = JSON.parse(gitjsontext) as GitJson
let parsed = pxt.github.parseRepoId(gitjson.repo)
const sha = await pxt.github.getRefAsync(parsed.fullName, parsed.tag)
if (!sha) {
// 404: branch does not exist, repo is gone or no rights to access repo
// try to get the list of heads to see if we can access the project
const heads = await pxt.github.listRefsAsync(parsed.fullName, "heads");
if (heads && heads.length)
return PullStatus.BranchNotFound;
else
return PullStatus.NoSourceControl; // something is wrong
} else if (sha == gitjson.commit.sha)
return PullStatus.UpToDate
if (checkOnly)
return PullStatus.GotChanges
try {
await githubUpdateToAsync(hd, { repo: gitjson.repo, sha, files, tryDiff3: true })
return PullStatus.GotChanges
} catch (e) {
if (e.isMergeError)
return PullStatus.NeedsCommit
else throw e
}
}
export async function hasMergeConflictMarkers(hd: Header): Promise<boolean> {
const files = await getTextAsync(hd.id)
return !!Object.keys(files).find(k => pxt.diff.hasMergeConflictMarker(files[k]));
}
export async function prAsync(hd: Header, commitId: string, msg: string) {
let parsed = pxt.github.parseRepoId(hd.githubId)
// merge conflict - create a Pull Request
const branchName = await pxt.github.getNewBranchNameAsync(parsed.fullName, "merge-")
await pxt.github.createNewBranchAsync(parsed.fullName, branchName, commitId)
const url = await pxt.github.createPRFromBranchAsync(parsed.fullName, parsed.tag, branchName, msg)
// force user back to master - we will instruct them to merge PR in github.com website
// and sync here to get the changes
let headCommit = await pxt.github.getRefAsync(parsed.fullName, parsed.tag)
await githubUpdateToAsync(hd, {
repo: hd.githubId,
sha: headCommit,
files: {}
})
return url
}
export function bumpedVersion(cfg: pxt.PackageConfig) {
let v = pxt.semver.parse(cfg.version || "0.0.0")
v.patch++
return pxt.semver.stringify(v)
}
export async function bumpAsync(hd: Header, newVer = "") {
let files = await getTextAsync(hd.id)
let cfg = pxt.Package.parseAndValidConfig(files[pxt.CONFIG_NAME]);
cfg.version = newVer || bumpedVersion(cfg)
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
await saveAsync(hd, files)
return await commitAsync(hd, {
message: cfg.version,
createTag: "v" + cfg.version,
binaryJs: true
})
}
export interface CommitOptions {
message?: string;
createTag?: string;
filenamesToCommit?: string[];
binaryJs?: boolean;
// render blocks to png
blocksScreenshotAsync?: () => Promise<string>;
// render blocks diff to png
blocksDiffScreenshotAsync?: () => Promise<string>;
}
const BLOCKS_PREVIEW_PATH = ".github/makecode/blocks.png";
const BLOCKSDIFF_PREVIEW_PATH = ".github/makecode/blocksdiff.png";
const BINARY_JS_PATH = "assets/js/binary.js";
const VERSION_TXT_PATH = "assets/version.txt";
export async function commitAsync(hd: Header, options: CommitOptions = {}) {
await cloudsync.ensureGitHubTokenAsync();
let files = await getTextAsync(hd.id)
let gitjsontext = files[GIT_JSON]
if (!gitjsontext)
U.userError(lf("Not a git extension."))
let gitjson = JSON.parse(gitjsontext) as GitJson
let parsed = pxt.github.parseRepoId(gitjson.repo)
let cfg = pxt.Package.parseAndValidConfig(files[pxt.CONFIG_NAME]);
let treeUpdate: pxt.github.CreateTreeReq = {
base_tree: gitjson.commit.tree.sha,
tree: []
}
const filenames = options.filenamesToCommit || pxt.allPkgFiles(cfg)
const ignoredFiles = [GIT_JSON, pxt.SIMSTATE_JSON, pxt.SERIAL_EDITOR_FILE];
for (const path of filenames.filter(f => ignoredFiles.indexOf(f) < 0)) {
const fileContent = files[path];
await addToTree(path, fileContent);
}
if (treeUpdate.tree.length == 0)
U.userError(lf("Nothing to commit!"))
// add screenshots
let blocksDiffSha: string;
if (options
&& treeUpdate.tree.find(e => e.path == "main.blocks")) {
if (options.blocksScreenshotAsync) {
const png = await options.blocksScreenshotAsync();
if (png)
await addToTree(BLOCKS_PREVIEW_PATH, png);
}
if (options.blocksDiffScreenshotAsync) {
const png = await options.blocksDiffScreenshotAsync();
if (png)
blocksDiffSha = await addToTree(BLOCKSDIFF_PREVIEW_PATH, png);
}
}
// add compiled javascript to be run in github pages
if (pxt.appTarget.appTheme.githubCompiledJs
&& options.binaryJs
&& (!parsed.tag || parsed.tag == "master")) {
const v = cfg.version || "0.0.0";
const opts: compiler.CompileOptions = {
jsMetaVersion: v
}
const compileResp = await compiler.compileAsync(opts);
if (compileResp && compileResp.success && compileResp.outfiles[pxtc.BINARY_JS]) {
await addToTree(BINARY_JS_PATH, compileResp.outfiles[pxtc.BINARY_JS]);
await addToTree(VERSION_TXT_PATH, v);
}
}
// create tree
let treeId = await pxt.github.createObjectAsync(parsed.fullName, "tree", treeUpdate)
let commit: pxt.github.CreateCommitReq = {
message: options.message || lf("Update {0}", treeUpdate.tree.map(e => e.path).filter(f => !/\.github\/makecode\//.test(f)).join(", ")),
parents: [gitjson.commit.sha],
tree: treeId
}
let commitId = await pxt.github.createObjectAsync(parsed.fullName, "commit", commit)
let ok = await pxt.github.fastForwardAsync(parsed.fullName, parsed.tag, commitId)
let newCommit = commitId
if (!ok)
newCommit = await pxt.github.mergeAsync(parsed.fullName, parsed.tag, commitId)
if (newCommit == null) {
return commitId
} else {
// if we created a block preview, add as comment
if (blocksDiffSha)
await pxt.github.postCommitComment(
parsed.fullName,
commitId,
``);
await githubUpdateToAsync(hd, {
repo: gitjson.repo,
sha: newCommit,
files,
saveTag: options.createTag
})
if (options.createTag)
await pxt.github.createTagAsync(parsed.fullName, options.createTag, newCommit)
return ""
}
async function addToTree(path: string, content: string): Promise<string> {
const data = {
content: content,
encoding: "utf-8"
} as pxt.github.CreateBlobReq;
if (path == pxt.CONFIG_NAME)
data.content = prepareConfigForGithub(data.content, !!options.createTag);
const m = /^data:([^;]+);base64,/.exec(content);
if (m) {
data.encoding = "base64";
data.content = content.substr(m[0].length);
}
const sha = gitsha(data.content, data.encoding)
const ex = pxt.github.lookupFile(gitjson.commit, path)
let res: string;
if (!ex || ex.sha != sha) {
// look for unfinished merges
if (data.encoding == "utf-8" &&
pxt.diff.hasMergeConflictMarker(data.content))
throw mergeConflictMarkerError();
res = await pxt.github.createObjectAsync(parsed.fullName, "blob", data)
if (data.encoding == "utf-8")
U.assert(res == sha, `sha not matching ${res} != ${sha}`)
treeUpdate.tree.push({
"path": path,
"mode": "100644",
"type": "blob",
"sha": res,
"url": undefined
})
}
return res;
}
}
interface UpdateOptions {
repo: string;
sha: string;
files: pxt.Map<string>;
saveTag?: string;
justJSON?: boolean;
tryDiff3?: boolean;
}
function mergeError() {
const e = new Error("Merge error");
(e as any).isMergeError = true
return e
}
function mergeConflictMarkerError() {
const e = new Error("Merge conflict marker error");
(e as any).isMergeConflictMarkerError = true
return e
}
async function githubUpdateToAsync(hd: Header, options: UpdateOptions) {
const { repo, sha, files, justJSON } = options
const parsed = pxt.github.parseRepoId(repo)
const commit = await pxt.github.getCommitAsync(parsed.fullName, sha)
let gitjson: GitJson = JSON.parse(files[GIT_JSON] || "{}")
if (!gitjson.commit) {
gitjson = {
repo: repo,
commit: null
}
}
const downloadedFiles: pxt.Map<boolean> = {}
let conflicts = 0;
let blocksNeedDecompilation = false;
const downloadAsync = async (path: string) => {
downloadedFiles[path] = true
const treeEnt = pxt.github.lookupFile(commit, path)
const oldEnt = pxt.github.lookupFile(gitjson.commit, path)
const hasChanges = files[path] != null && (!oldEnt || oldEnt.blobContent != files[path])
if (!treeEnt) {
// file in pxt.json but not in git:
// changes were merged from the cloud but not pushed yet
if (options.tryDiff3 && hasChanges)
return files[path];
if (!justJSON)
files[path] = ""
return ""
}
let text = oldEnt ? oldEnt.blobContent : files[path]
if (text != null && gitsha(text) == treeEnt.sha) {
treeEnt.blobContent = text
if (!options.tryDiff3 && !options.justJSON)
files[path] = text
return text
}
text = await pxt.github.downloadTextAsync(parsed.fullName, sha, path)
treeEnt.blobContent = text
if (gitsha(text) != treeEnt.sha)
U.userError(lf("Corrupt SHA1 on download of '{0}'.", path))
if (options.tryDiff3 && hasChanges) {
if (path == pxt.CONFIG_NAME) {
text = pxt.diff.mergeDiff3Config(files[path], oldEnt.blobContent, treeEnt.blobContent);
if (!text) // merge failed?
throw mergeError()
} else if (/\.blocks$/.test(path)) {
// blocks file, try merging the blocks or clear it so that ts merge picks it up
const d3 = pxt.blocks.mergeXml(files[path], oldEnt.blobContent, treeEnt.blobContent);
// if xml merge fails, leave an empty xml payload to force decompilation
blocksNeedDecompilation = blocksNeedDecompilation || !d3;
text = d3 || "";
} else if (path == BINARY_JS_PATH || path == VERSION_TXT_PATH) {
// local build wins, does not matter
text = files[path];
} else {
const d3 = pxt.diff.diff3(files[path], oldEnt.blobContent, treeEnt.blobContent, lf("local changes"), lf("remote changes (pulled from Github)"))
if (!d3) // merge failed?
throw mergeError()
conflicts += d3.numConflicts
if (d3.numConflicts && !/\.ts$/.test(path)) // only allow conflict markers in typescript files
throw mergeError()
text = d3.merged
}
}
if (!justJSON)
files[path] = text
return text
}
// we need to keep the old cfg to maintain the github id -> local workspace id
const oldCfg = pxt.Package.parseAndValidConfig(files[pxt.CONFIG_NAME])
const cfgText = await downloadAsync(pxt.CONFIG_NAME)
let cfg = pxt.Package.parseAndValidConfig(cfgText);
// invalid cfg or no TypeScript files
if (!cfg || !cfg.files.find(f => /\.ts$/.test(f))) {
if (hd) // not importing
U.userError(lf("Invalid pxt.json file."));
pxt.log(`github: reconstructing pxt.json`)
cfg = pxt.diff.reconstructConfig(files, commit, pxt.appTarget.blocksprj || pxt.appTarget.tsprj);
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
}
// patch the github references back to local workspaces
if (oldCfg) {
let ghupdated = 0;
Object.keys(cfg.dependencies)
// find github references that are also in the original version
.filter(k => /^github:/.test(cfg.dependencies[k]) && oldCfg.dependencies[k])
.forEach(k => {
const gid = pxt.github.parseRepoId(cfg.dependencies[k]);
if (gid) {
const wks = oldCfg.dependencies[k];
cfg.dependencies[k] = wks;
ghupdated++;
}
})
if (ghupdated)
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
}
for (const fn of pxt.allPkgFiles(cfg).slice(1))
await downloadAsync(fn)
if (!cfg.name) {
cfg.name = (parsed.project || parsed.fullName).replace(/[^\w\-]/g, "");
if (!justJSON)
files[pxt.CONFIG_NAME] = pxt.Package.stringifyConfig(cfg);
}
if (!justJSON) {
// if any block needs decompilation, don't allow merge error markers
if (blocksNeedDecompilation && conflicts)
throw mergeError()
for (let k of Object.keys(files)) {
if (k[0] != "." && !downloadedFiles[k])
delete files[k]
}
}
commit.tag = options.saveTag
gitjson.commit = commit
files[GIT_JSON] = JSON.stringify(gitjson, null, 4)
if (!hd) {
hd = await installAsync({
name: cfg.name,
githubId: repo,
pubId: "",
pubCurrent: false,
meta: {},
editor: pxt.BLOCKS_PROJECT_NAME,
target: pxt.appTarget.id,
targetVersion: pxt.appTarget.versions.target,
}, files)
} else {
hd.name = cfg.name
await saveAsync(hd, files)
}
return hd
}
export async function exportToGithubAsync(hd: Header, repoid: string) {
const parsed = pxt.github.parseRepoId(repoid);
const pfiles = pxt.template.packageFiles(hd.name);
await pxt.github.putFileAsync(parsed.fullName, ".gitignore", pfiles[".gitignore"]);
const sha = await pxt.github.getRefAsync(parsed.fullName, parsed.tag)
const commit = await pxt.github.getCommitAsync(parsed.fullName, sha)
const files = await getTextAsync(hd.id)
files[GIT_JSON] = JSON.stringify({
repo: repoid,
commit
})
// assign ids to blockly blocks
const mainBlocks = files["main.blocks"];
if (mainBlocks) {
const ws = pxt.blocks.loadWorkspaceXml(mainBlocks, true);
if (ws) {
const mainBlocksWithIds = pxt.blocks.saveWorkspaceXml(ws, true);
if (mainBlocksWithIds)
files["main.blocks"] = mainBlocksWithIds;
}
}
// save updated files
await saveAsync(hd, files);
await initializeGithubRepoAsync(hd, repoid, false, true);
// race condition, don't pull right away
// await pullAsync(hd);
}
// to be called after loading header in a editor
export async function recomputeHeaderFlagsAsync(h: Header, files: ScriptText) {
h.githubCurrent = false
const gitjson: GitJson = JSON.parse(files[GIT_JSON] || "{}")
h.githubId = gitjson && gitjson.repo
h.githubTag = gitjson && gitjson.commit && gitjson.commit.tag
if (!h.githubId)
return
if (!gitjson.commit || !gitjson.commit.tree)
return
let isCurrent = true
let needsBlobs = false
for (let k of Object.keys(files)) {
if (k == GIT_JSON || k == pxt.SIMSTATE_JSON || k == pxt.SERIAL_EDITOR_FILE)
continue
let treeEnt = pxt.github.lookupFile(gitjson.commit, k)
if (!treeEnt || treeEnt.type != "blob") {
isCurrent = false
continue
}
if (treeEnt.blobContent == null)
needsBlobs = true
let content = files[k];
if (k == pxt.CONFIG_NAME)
content = prepareConfigForGithub(content);
if (content && treeEnt.sha != gitsha(content)) {
isCurrent = false
continue
}
}
h.githubCurrent = isCurrent
// this happens for older projects
if (needsBlobs)
await githubUpdateToAsync(h, {
repo: gitjson.repo,
sha: gitjson.commit.sha,
files,
justJSON: true
})
if (gitjson.isFork == null) {
const p = pxt.github.parseRepoId(gitjson.repo)
const r = await pxt.github.repoAsync(p.fullName, null);
if (r) {
gitjson.isFork = !!r.fork
files[GIT_JSON] = JSON.stringify(gitjson, null, 4)
await saveAsync(h, files)
}
}
// automatically update project name with github name
const ghid = pxt.github.parseRepoId(h.githubId);
if (ghid.project) {
const ghname = ghid.project.replace(/^pxt-/, '').replace(/-+/g, ' ')
if (ghname != h.name) {
const cfg = pxt.Package.parseAndValidConfig(files[pxt.CONFIG_NAME]);
cfg.name = ghname;
h.name = ghname;
await saveAsync(h, files);
}
}
}
// replace all file|worspace references with github sha
// createTag: determine if tags need to be enforced
export function prepareConfigForGithub(content: string, createTag?: boolean): string {
// replace workspace: references with resolve github sha/tags.
const cfg = pxt.Package.parseAndValidConfig(content);
if (!cfg) return content;
// cleanup
delete (<any>cfg).installedVersion // cleanup old pxt.json files
delete cfg.additionalFilePath
delete cfg.additionalFilePaths
delete cfg.targetVersions;
// add list of supported targets
const supportedTargets = cfg.supportedTargets || [];
if (supportedTargets.indexOf(pxt.appTarget.id) < 0) {
supportedTargets.push(pxt.appTarget.id);
supportedTargets.sort(); // keep list stable
cfg.supportedTargets = supportedTargets;
}
// patch dependencies
const localDependencies = Object.keys(cfg.dependencies)
.filter(d => /^(file|workspace):/.test(cfg.dependencies[d]));
for (const d of localDependencies)
resolveDependencyAsync(d);
return pxt.Package.stringifyConfig(cfg);
function resolveDependencyAsync(d: string) {
const v = cfg.dependencies[d];
const hid = v.substring(v.indexOf(':') + 1);
const header = getHeader(hid);
if (!header.githubId) {
if (createTag)
U.userError(lf("Dependency {0} is a local project.", d))
} else {
const gid = pxt.github.parseRepoId(header.githubId);
if (createTag && !/^v\d+/.test(header.githubTag))
U.userError(lf("You need to create a release for dependency {0}.", d))
const tag = header.githubTag || gid.tag;
cfg.dependencies[d] = `github:${gid.fullName}#${tag}`;
}
}
}
export async function initializeGithubRepoAsync(hd: Header, repoid: string, forceTemplateFiles: boolean, binaryJs: boolean) {
await cloudsync.ensureGitHubTokenAsync();
let parsed = pxt.github.parseRepoId(repoid)
let name = parsed.fullName.replace(/.*\//, "")
let currFiles = await getTextAsync(hd.id);
const templateFiles = pxt.template.packageFiles(name);
pxt.template.packageFilesFixup(templateFiles, {
repo: parsed.fullName,
repoowner: parsed.owner,
reponame: parsed.project,
repotag: parsed.tag
});
if (forceTemplateFiles) {
U.jsonMergeFrom(currFiles, templateFiles);