|
1 | | -import { Uri, workspace, window, TextDocument } from 'vscode'; |
2 | | - |
| 1 | +import { Uri, workspace, window, TextDocument, FileType } from 'vscode'; |
3 | 2 | import { Config } from './configration'; |
4 | | -import { exec } from 'child_process'; |
| 3 | +import { exec, ExecOptions } from 'child_process'; |
5 | 4 | import { AssemblerDiag } from './language/diagnose'; |
6 | 5 | import * as nls from 'vscode-nls'; |
7 | 6 | const localize = nls.loadMessageBundle(); |
8 | | -export class DOSBox { |
9 | | - constructor() { |
10 | | - } |
11 | | - /**打开dosbox,操作文件 |
12 | | - * @param conf 配置信息 |
13 | | - * @param more 需要执行的额外命令 |
14 | | - * @param doc 需要处理的文件,假如有会清理工作文件夹,复制该文件到工作文件夹 |
15 | | - * @param diag 如果有则诊断输出信息 |
16 | | - */ |
17 | | - public openDOSBox(conf: Config, more?: string, doc?: TextDocument, diag?: AssemblerDiag) { |
18 | | - let boxcmd: string = '@echo off\n'; |
19 | | - //mount the necessary path |
20 | | - boxcmd += `mount c \\\"${conf.path}\\\"\nmount d \\\"${conf.workpath}\\\"\n`; |
21 | | - //switch to the working space and add path\ |
22 | | - boxcmd += "d:\nset PATH=%%PATH%%;c:\\tasm;c:\\masm\n"; |
23 | | - if (doc) { boxcmd += "echo Your file has been copied as D:\\T.ASM\n"; }; |
24 | | - boxcmd += "@echo on"; |
25 | | - //add extra commands |
26 | | - if (more) { boxcmd += "\n" + more; } |
27 | | - //change string to needed form as dosbox parameter |
28 | | - boxcmd = boxcmd.replace(/\n/g, '" -c "'); |
29 | | - let boxcommand = '-c "' + boxcmd + '"'; |
30 | | - //command for open dosbox |
31 | | - let command = conf.OpenDosbox + ' -conf "' + conf.dosboxconfuri.fsPath + '" '; |
32 | | - //exec command by terminal |
33 | | - let callback = (error: any, stdout: string, stderr: string) => { |
34 | | - if (error) { console.error(error); } |
35 | | - else { |
36 | | - if (diag && doc) { this.BOXdiag(conf, diag, doc); } |
37 | | - console.log(stderr, stdout); |
38 | | - } |
39 | | - }; |
40 | | - if (process.platform === 'win32') { |
41 | | - if (more) { command = "copy/Y ..\\boxasm.bat boxasm.bat & " + command; } |
42 | | - if (doc) { command = 'del/Q T*.* & copy "' + doc.fileName + '" "T.ASM" & ' + command; } |
43 | | - exec(command + boxcommand, { cwd: conf.workpath, shell: 'cmd.exe' }, callback); |
44 | | - } |
45 | | - else { |
46 | | - if (more) { command = "cp ../boxasm.bat ./; " + command; } |
47 | | - if (doc) { command = 'rm -f [Tt]*.*;cp "' + doc.fileName + '" T.ASM;' + command; } |
48 | | - exec(command + boxcommand, { cwd: conf.workpath }, callback); |
| 7 | +/** |
| 8 | + * A function used when using boxasm.bat |
| 9 | + * @param conf The config information |
| 10 | + * @param runOrDebug true for run ASM code,false for debug |
| 11 | + * @param doc the source ASM file |
| 12 | + * @param diag using its `diag.ErrMsgProcess` to process the assembler's output |
| 13 | + */ |
| 14 | +export function runDosbox2(conf: Config, runOrDebug: boolean, doc: TextDocument, diag: AssemblerDiag) { |
| 15 | + let fs = workspace.fs; |
| 16 | + let src: Uri = Uri.joinPath(conf.extScriptsUri, "./boxasm.bat"); |
| 17 | + let target: Uri = Uri.joinPath(conf.toolsUri, "./boxasm.bat"); |
| 18 | + fs.copy(src, target, { overwrite: conf.toolsBuiltin }).then( |
| 19 | + () => { |
| 20 | + runDosbox(conf, conf.boxasmCommand(runOrDebug), doc).then( |
| 21 | + (stdout) => { BOXdiag(conf, diag, doc); } |
| 22 | + ); |
| 23 | + }, |
| 24 | + (reason) => { |
| 25 | + console.log(reason); |
| 26 | + runDosbox(conf, conf.boxasmCommand(runOrDebug), doc).then( |
| 27 | + (stdout) => { BOXdiag(conf, diag, doc); } |
| 28 | + ); |
49 | 29 | } |
50 | | - } |
51 | | - public BoxOpenCurrentFolder(conf: Config, doc: TextDocument) { |
52 | | - let folderpath: string = Uri.joinPath(doc.uri, '../').fsPath; |
53 | | - let Ecmd: string = '-noautoexec -c "mount e \\\"' + folderpath + '\\\"" -c "mount c \\\"' + conf.path + '\\\"" -c "set PATH=%%PATH%%;c:\\masm;c:\\tasm" -c "e:"'; |
54 | | - let command = conf.OpenDosbox + ' -conf "' + conf.dosboxconfuri.fsPath + '" '; |
| 30 | + ); |
| 31 | +} |
| 32 | +/**open dosbox and do things about it |
| 33 | + * @param conf The config information |
| 34 | + * @param more The commands needed to exec in dosbox |
| 35 | + * @param doc If defined, copy this file to workspace as T.xxx(xxx is the files extension) |
| 36 | + */ |
| 37 | +export function runDosbox(conf: Config, more?: string, doc?: TextDocument): Promise<string> { |
| 38 | + let filename = doc?.fileName; |
| 39 | + let fileext = filename?.substring(filename.lastIndexOf("."), filename.length); |
| 40 | + let preCommand: string = ""; |
| 41 | + let boxcmd: string = '@echo off\n'; |
| 42 | + boxcmd += `mount c \\\"${conf.path}\\\"\nmount d \\\"${conf.workpath}\\\"\n`;//mount the necessary path |
| 43 | + boxcmd += "d:\nset PATH=%%PATH%%;c:\\tasm;c:\\masm\n";//switch to the working space and add path\ |
| 44 | + if (doc) { |
| 45 | + boxcmd += `echo Your file has been copied as D:\\T${fileext}\n`; |
55 | 46 | if (process.platform === 'win32') { |
56 | | - exec(command + Ecmd, { cwd: conf.workpath, shell: 'cmd.exe' }); |
| 47 | + preCommand = `del/Q T*.* & copy "${filename}" "T${fileext}" & `; |
57 | 48 | } |
58 | 49 | else { |
59 | | - exec(command + Ecmd, { cwd: conf.workpath }); |
| 50 | + preCommand = `rm -f [Tt]*.*;cp "${filename}" T${fileext};'`; |
60 | 51 | } |
61 | | - |
| 52 | + }; |
| 53 | + boxcmd += "@echo on"; |
| 54 | + if (more) { boxcmd += "\n" + more; }//add extra commands |
| 55 | + let opt: OPTS = { |
| 56 | + cwd: conf.workpath, |
| 57 | + preOpen: preCommand, |
| 58 | + core: conf.OpenDosbox, |
| 59 | + boxcmd: boxcmd, |
| 60 | + parameter: ' -conf "' + conf.dosboxconfuri.fsPath + '" ' |
| 61 | + }; |
| 62 | + return openDosbox(opt); |
| 63 | +} |
| 64 | +export function BoxOpenCurrentFolder(conf: Config, doc: TextDocument) { |
| 65 | + let folderpath: string = Uri.joinPath(doc.uri, '../').fsPath; |
| 66 | + let opt: OPTS = { |
| 67 | + cwd: conf.workpath, |
| 68 | + core: conf.OpenDosbox, |
| 69 | + boxcmd: `mount e \\\"${folderpath}\\\"\nmount c \\\"${conf.path}\\\"\nset PATH=%%PATH%%;c:\\masm;c:\\tasm\ne:`, |
| 70 | + parameter: ' -conf "' + conf.dosboxconfuri.fsPath + '" ' |
| 71 | + }; |
| 72 | + openDosbox(opt); |
| 73 | +} |
| 74 | +interface OPTS { |
| 75 | + cwd: string, |
| 76 | + core: string |
| 77 | + preOpen?: string, |
| 78 | + parameter?: string, |
| 79 | + boxcmd?: string |
| 80 | +} |
| 81 | +function openDosbox(opt: OPTS): Promise<string> { |
| 82 | + let str = opt.core + opt.parameter; |
| 83 | + if (opt.preOpen) { |
| 84 | + str = opt.preOpen + str; |
62 | 85 | } |
63 | | - private BOXdiag(conf: Config, diag: AssemblerDiag, doc: TextDocument): string { |
64 | | - let info: string = ' ', content: string; |
65 | | - let document = doc; |
66 | | - if (document) { |
67 | | - content = document.getText(); |
68 | | - workspace.fs.readFile(conf.workloguri).then( |
69 | | - (text) => { |
70 | | - info = text.toString(); |
71 | | - if (diag.ErrMsgProcess(content, info, doc.uri, conf.MASMorTASM) === 0) { |
72 | | - let Errmsgwindow = localize("dosbox.errmsg", '{0} Failed to compile. See the output for more information', conf.MASMorTASM); |
73 | | - window.showErrorMessage(Errmsgwindow); |
74 | | - } |
75 | | - }, |
76 | | - () => { console.error('read dosbox mode T.txt FAILED'); } |
77 | | - ); |
| 86 | + if (opt.boxcmd) { |
| 87 | + let cmd = opt.boxcmd.replace(/\n/g, '" -c "'); |
| 88 | + cmd = '-c "' + cmd + '"'; |
| 89 | + str += cmd; |
| 90 | + } |
| 91 | + let execOption: ExecOptions; |
| 92 | + if (process.platform === 'win32') { |
| 93 | + execOption = { cwd: opt.cwd, shell: 'cmd.exe' }; |
| 94 | + } |
| 95 | + else { |
| 96 | + execOption = { cwd: opt.cwd }; |
| 97 | + } |
| 98 | + return new Promise( |
| 99 | + (resolve, reject) => { |
| 100 | + exec(str, execOption, (error: any, stdout: string, stderr: string) => { |
| 101 | + if (error) { |
| 102 | + reject(error); |
| 103 | + } |
| 104 | + else { |
| 105 | + resolve(stdout); |
| 106 | + } |
| 107 | + }); |
78 | 108 | } |
79 | | - return info; |
| 109 | + ); |
| 110 | +} |
| 111 | +function BOXdiag(conf: Config, diag: AssemblerDiag, doc: TextDocument): string { |
| 112 | + let info: string = ' ', content: string; |
| 113 | + if (doc) { |
| 114 | + content = doc.getText(); |
| 115 | + workspace.fs.readFile(conf.workloguri).then( |
| 116 | + (text) => { |
| 117 | + info = text.toString(); |
| 118 | + if (diag.ErrMsgProcess(content, info, doc.uri, conf.MASMorTASM) === 0) { |
| 119 | + let Errmsgwindow = localize("dosbox.errmsg", '{0} Failed to compile. See the output for more information', conf.MASMorTASM); |
| 120 | + window.showErrorMessage(Errmsgwindow); |
| 121 | + } |
| 122 | + }, |
| 123 | + () => { console.error('read dosbox mode T.txt FAILED'); } |
| 124 | + ); |
80 | 125 | } |
| 126 | + return info; |
81 | 127 | } |
82 | 128 |
|
83 | 129 |
|
|
0 commit comments