-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto-run.task.js
265 lines (233 loc) · 8.4 KB
/
auto-run.task.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
const vscode = require('vscode');
const cp = require('child_process');
const { tmpdir, platform } = require('os');
const { sep, join, dirname } = require('path');
const { writeFileSync, readdirSync } = require('fs');
const { file2bas, validateTxt, tokens } = require('txt2bas');
const parseError = require('./parse-error');
let subProcess = null;
let launching = false;
module.exports = main;
function exec(command, options) {
console.log(command);
return new Promise((resolve, reject) => {
subProcess = cp.exec(command, options, (error, stdout, stderr) => {
if (error) {
if (!command.includes('mkdir')) {
console.log('exec fail', error);
}
reject(new Error(`${stderr.trim()}\n\n${stdout.trim()}`));
}
resolve({ stdout, stderr });
});
});
}
function main(extensionPath) {
return vscode.commands.registerCommand('nextbasic.run', async () => {
try {
const editor = vscode.window.activeTextEditor;
const document = editor.document;
launching = true;
// try to stop previous instance of cspect
try {
process.kill(subProcess.pid, 9);
} catch (e) {}
const {
cspect,
nextImagePath,
monoPath = 'mono',
hdfmonkeyPath: hdfmonkey,
cspectOptions,
copyLimit: limit,
stripComments,
parserVersion,
} = vscode.workspace.getConfiguration('nextbasic');
let commandLine = `${hdfmonkey} mkdir "${nextImagePath}" /devel`;
try {
await exec(commandLine, { cwd: extensionPath }, false);
} catch (e) {
// ignore if exists
}
// push file into sdcard
const filename = tmpdir() + '/index.bas';
let text = document.getText().replace(/\r/g, '');
let autostartAdded = false;
if (!text.includes('#autostart')) {
text = `#autostart\n` + text;
autostartAdded = true;
}
// first make a backup of their autoexec
let autoexec = tmpdir() + '/autoexec.bas';
commandLine = `${hdfmonkey} get "${nextImagePath}" /nextzxos/autoexec.bas "${autoexec}"`;
try {
await exec(commandLine, { cwd: extensionPath });
// getting this far means the file exists, so let's back it up.
} catch (e) {
if (
e.message.toLowerCase().includes('error opening file: file not found')
) {
// cspect worked but there's no autoexec.bas
autoexec = null;
} else if (e.message.includes('CYGWIN')) {
vscode.window.showErrorMessage(
'hdfmonkey failed (possibly using cygwin build - use hdfmonkey version linked in this extension readme):\n' +
e.message
);
} else if (e.message.includes('open()')) {
vscode.window.showErrorMessage(
'hdfmonkey failed (possible path to Next img file):\n' + e.message
);
} else {
vscode.window.showErrorMessage(
'hdfmonkey failed (possible bad path to hdfmonkey):\n' + e.message
);
}
// this means that the problem wasn't that the file was missing, but a fatal
// cspect exec problem - so we bail.
if (autoexec) {
return;
}
}
commandLine = `${hdfmonkey} put "${nextImagePath}" "${join(
extensionPath,
sep,
'autoexec-pre-compiled.bas'
)}" /nextzxos/autoexec.bas`;
await exec(commandLine, { cwd: extensionPath });
const root = dirname(document.fileName);
let cwd = extensionPath;
if (root && root !== '.') {
cwd = root;
const files = readdirSync(root);
if (files.length > limit) {
console.error(
`Not copying files from ${root}, too many files: ${files.length}`
);
vscode.window.showErrorMessage(
`Not copying files from ${root} to cspect image, too many files: ${files.length} (limit is user configured to ${limit})`
);
} else if (root === '/') {
console.error(`Not copying files from ${root}, root directory`);
vscode.window.showWarningMessage(
`Only copying working file across as you're current in your root directory.`
);
} else {
// TODO skip massive files
const commands = files
// skip space files, no worth the hassle
.filter((_) => !_.startsWith('.') && !_.includes(' '))
// .filter((path) => {
// const src = join(root, sep, path);
// const stat = statSync(src);
// return stat.isFile() && stat.size < 100000;
// })
.map((path) => {
const src = join(root, sep, path);
return `${hdfmonkey} put "${nextImagePath}" "${src}" /devel/`;
});
// console.log(`Copying ${commands.length} files across`);
for (let i = 0; i < commands.length; i++) {
await exec(commands[i], { cwd: extensionPath });
}
}
}
const errors = validateTxt(text);
if (errors.length) {
vscode.window
.showWarningMessage(
'Some errors found in your NextBASIC',
...errors[0].split('\n').filter((_) => _.startsWith('>'))
)
.then((selected) => {
if (selected) {
const e = parseError(selected);
if (autostartAdded) {
e.line -= 1;
}
const start = new vscode.Position(e.line, e.start);
const end = new vscode.Position(e.line, e.end);
var newSelection = new vscode.Selection(start, end);
editor.selection = newSelection;
}
});
}
// finally send the source
writeFileSync(
filename,
file2bas(text, {
validate: false,
defines: true,
stripComments,
bankOutputDir: tmpdir(),
parser: parserVersion || 'LATEST',
}),
'utf8'
);
commandLine = `"${hdfmonkey}" put "${nextImagePath}" "${join(
tmpdir(),
sep,
'index.bas'
)}" /devel/index.bas`;
let banksSent = 0;
if (text.includes('#bank')) {
// reparse the code to find the banks
// then move those across too
const res = tokens(text, { validate: false, defines: true });
banksSent = res.bankSplits.length;
for (let i = 0; i < res.bankSplits.length; i++) {
const bank = res.bankSplits[i];
const src = join(tmpdir(), sep, bank.filename);
const dest = `/devel/${bank.filename}`;
const commandLine = `${hdfmonkey} put "${nextImagePath}" "${src}" ${dest}`;
await exec(commandLine, { cwd: extensionPath });
}
}
await exec(commandLine, { cwd: extensionPath });
if (platform() === 'win32') {
commandLine = `"${cspect}" ${cspectOptions} -mmc=${nextImagePath}`;
} else {
commandLine = `"${monoPath}" "${cspect}" ${cspectOptions} -mmc=${nextImagePath}`;
}
try {
launching = false;
let msg = 'Sent index.bas to cspect';
if (stripComments) {
msg += ' (comments stripped)';
}
if (banksSent) {
msg += ` and ${banksSent} bank${banksSent > 1 ? 's' : ''}`;
}
vscode.window.showInformationMessage(msg);
await exec(commandLine, { cwd });
// console.log(`restoring ${autoexec}`);
// now put their original autoexec back
if (autoexec) {
commandLine = `${hdfmonkey} put "${nextImagePath}" "${autoexec}" /nextzxos/autoexec.bas`;
await exec(commandLine, { cwd: extensionPath });
}
} catch (e) {
if (!launching) {
if (e.message.includes('Error loading ROM')) {
vscode.window.showErrorMessage(
'cspect failed - error loading ROM. Ensure the ROM is in the same directory your Next .img file.'
);
return;
}
if (nextImagePath.includes(' ')) {
vscode.window.showErrorMessage(
'cspect failed - try moving the Next image path to a directory *without* spaces.'
);
return;
}
// this was an exec error, not a forced shutdown
console.log(e.stack);
vscode.window.showErrorMessage('cspect exec failed: ' + e);
}
return;
}
} catch (e) {
console.log(e.stack);
throw e;
}
});
}