forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpledriver.ts
More file actions
236 lines (206 loc) · 8.73 KB
/
simpledriver.ts
File metadata and controls
236 lines (206 loc) · 8.73 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
namespace pxt {
export interface SimpleDriverCallbacks {
cacheGet: (key: string) => Promise<string>
cacheSet: (key: string, val: string) => Promise<void>
httpRequestAsync?: (options: Util.HttpRequestOptions) => Promise<Util.HttpResponse>
pkgOverrideAsync?: (id: string) => Promise<Map<string>>
}
let callbacks: SimpleDriverCallbacks
let ghSetup = false
class CachedGithubDb implements pxt.github.IGithubDb {
constructor(public readonly db: pxt.github.IGithubDb) { }
private loadAsync<T>(repopath: string, tag: string, suffix: string,
loader: (r: string, t: string) => Promise<T>): Promise<T> {
// only cache releases
if (!/^v\d+\.\d+\.\d+$/.test(tag))
return loader(repopath, tag);
const key = `gh-${suffix}-${repopath}#${tag}`
return callbacks.cacheGet(key)
.then(json => {
if (json) {
const p = pxt.Util.jsonTryParse(json) as T;
if (p) {
pxt.debug(`cache hit ${key}`)
return Promise.resolve(p);
}
}
// download and cache
return loader(repopath, tag)
.then(p => {
if (p) {
pxt.debug(`cached ${key}`)
return callbacks.cacheSet(key, JSON.stringify(p))
.then(() => p)
}
return p;
})
})
}
loadConfigAsync(repopath: string, tag: string): Promise<pxt.PackageConfig> {
return this.loadAsync(repopath, tag, "pxt", (r, t) => this.db.loadConfigAsync(r, t));
}
loadPackageAsync(repopath: string, tag: string): Promise<pxt.github.CachedPackage> {
return this.loadAsync(repopath, tag, "pkg", (r, t) => this.db.loadPackageAsync(r, t));
}
}
function pkgOverrideAsync(pkg: pxt.Package) {
const f = callbacks?.pkgOverrideAsync
const v = f ? f(pkg.id) : Promise.resolve(undefined as Map<string>)
return v.then(r => r ? r : pkg.commonDownloadAsync())
}
export class SimpleHost implements pxt.Host {
constructor(public packageFiles: pxt.Map<string>) { }
resolve(module: pxt.Package, filename: string): string {
return ""
}
readFile(module: pxt.Package, filename: string): string {
const fid = module.id == "this" ? filename :
"pxt_modules/" + module.id + "/" + filename
if (this.packageFiles[fid] !== undefined) {
return this.packageFiles[fid]
} else if (pxt.appTarget.bundledpkgs[module.id]) {
return pxt.appTarget.bundledpkgs[module.id][filename];
} else {
return null;
}
}
writeFile(module: pxt.Package, filename: string, contents: string) {
const pref = module.id == "this" ? "" : "pxt_modules/" + module.id + "/"
pxt.debug(`write file ${pref + filename}`)
this.packageFiles[pref + filename] = contents
}
getHexInfoAsync(extInfo: pxtc.ExtensionInfo): Promise<pxtc.HexInfo> {
//console.log(`getHexInfoAsync(${extInfo})`);
return Promise.resolve<any>({ hex: ["SKIP"] })
}
cacheStoreAsync(id: string, val: string): Promise<void> {
//console.log(`cacheStoreAsync(${id}, ${val})`)
if (callbacks?.cacheSet)
return callbacks.cacheSet(id, val)
return Promise.resolve()
}
cacheGetAsync(id: string): Promise<string> {
//console.log(`cacheGetAsync(${id})`)
if (callbacks?.cacheGet)
return callbacks.cacheGet(id)
return Promise.resolve("")
}
downloadPackageAsync(pkg: pxt.Package): Promise<void> {
if (ghSetup)
return pkgOverrideAsync(pkg)
.then(resp => {
if (resp) {
U.iterMap(resp, (fn: string, cont: string) => {
this.writeFile(pkg, fn, cont)
})
}
})
//console.log(`downloadPackageAsync(${pkg.id})`)
return Promise.resolve()
}
resolveVersionAsync(pkg: pxt.Package): Promise<string> {
//console.log(`resolveVersionAsync(${pkg.id})`)
return Promise.resolve("*")
}
}
export function prepPythonOptions(opts: pxtc.CompileOptions) {
// this is suboptimal, but we need apisInfo for the python converter
if (opts.target.preferredEditor == pxt.PYTHON_PROJECT_NAME) {
const opts2 = U.clone(opts)
opts2.ast = true
opts2.target.preferredEditor = pxt.JAVASCRIPT_PROJECT_NAME
//opts2.noEmit = true
// remove previously converted .ts files, so they don't end up in apisinfo
for (let f of opts2.sourceFiles) {
if (U.endsWith(f, ".py"))
opts2.fileSystem[f.slice(0, -3) + ".ts"] = " "
}
const res = pxtc.compile(opts2)
opts.apisInfo = pxtc.getApiInfo(res.ast, opts2.jres)
}
}
export interface CompileResultWithErrors extends pxtc.CompileResult {
errors?: string;
}
export interface SimpleCompileOptions {
native?: boolean;
}
export function simpleInstallPackagesAsync(files: pxt.Map<string>) {
const host = new SimpleHost(files)
const mainPkg = new MainPackage(host)
return mainPkg.loadAsync(true)
}
export function simpleGetCompileOptionsAsync(
files: pxt.Map<string>,
simpleOptions: SimpleCompileOptions
) {
const host = new SimpleHost(files)
const mainPkg = new MainPackage(host)
return mainPkg.loadAsync()
.then(() => {
let target = mainPkg.getTargetOptions()
if (target.hasHex)
target.isNative = simpleOptions.native
return mainPkg.getCompileOptionsAsync(target)
}).then(opts => {
patchTS(mainPkg.targetVersion(), opts)
prepPythonOptions(opts)
return opts
})
}
export function simpleCompileAsync(
files: pxt.Map<string>,
optionsOrNative?: SimpleCompileOptions | boolean
) {
const options: SimpleCompileOptions =
typeof optionsOrNative == "boolean" ? { native: optionsOrNative }
: optionsOrNative || {}
return simpleGetCompileOptionsAsync(files, options)
.then(opts => pxtc.compile(opts))
.then((r: CompileResultWithErrors) => {
if (!r.success)
r.errors = r.diagnostics.map(ts.pxtc.getDiagnosticString).join("") || "Unknown error."
return r
})
}
export function patchTS(version: string, opts: pxtc.CompileOptions) {
if (!version)
return
pxt.debug(`applying TS patches relative to ${version}`)
for (let fn of Object.keys(opts.fileSystem)) {
if (fn.indexOf("/") == -1 && U.endsWith(fn, ".ts")) {
const ts = opts.fileSystem[fn]
const ts2 = pxt.patching.patchJavaScript(version, ts)
if (ts != ts2) {
pxt.debug(`applying TS patch to ${fn}`)
opts.fileSystem[fn] = ts2
}
}
}
}
// eslint-disable-next-line no-var
declare var global: any;
// eslint-disable-next-line no-var
declare var Buffer: any;
// eslint-disable-next-line no-var
declare var pxtTargetBundle: TargetBundle;
export function setupSimpleCompile(cfg?: SimpleDriverCallbacks) {
if (typeof global != "undefined" && !global.btoa) {
global.btoa = function (str: string) { return Buffer.from(str, "binary").toString("base64"); }
global.atob = function (str: string) { return Buffer.from(str, "base64").toString("binary"); }
}
if (typeof pxtTargetBundle != "undefined") {
pxt.debug("setup app bundle")
pxt.setAppTarget(pxtTargetBundle)
}
if (cfg) {
callbacks = cfg
ghSetup = true
if (cfg.httpRequestAsync)
Util.httpRequestCoreAsync = cfg.httpRequestAsync
pxt.github.forceProxy = true
pxt.github.db = new CachedGithubDb(new pxt.github.MemoryGithubDb())
}
pxt.debug("simple setup done")
}
}