11import { Uri , workspace , window , TextDocument , FileType } from 'vscode' ;
22import { Config } from './configration' ;
33import { exec , ExecOptions } from 'child_process' ;
4- import { AssemblerDiag } from './language/diagnose' ;
5- import * as nls from 'vscode-nls' ;
6- const localize = nls . loadMessageBundle ( ) ;
74/**
8- * A function used when using boxasm.bat
5+ * A function used when using boxasm.bat to run or debug ASM codes in DOSBox
96 * @param conf The config information
107 * @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
138 */
14- export function runDosbox2 ( conf : Config , runOrDebug : boolean , doc : TextDocument , diag : AssemblerDiag ) {
9+ export async function runDosbox2 ( conf : Config , runOrDebug : boolean ) : Promise < string > {
1510 let fs = workspace . fs ;
1611 let src : Uri = Uri . joinPath ( conf . extScriptsUri , "./boxasm.bat" ) ;
1712 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- ) ;
29- }
30- ) ;
13+ if ( ! conf . customToolInfo ?. hasBoxasm ) { await fs . copy ( src , target , { overwrite : true } ) ; }
14+ await runDosbox ( conf , conf . boxasmCommand ( runOrDebug ) ) ;
15+ let stdout : Uint8Array = await fs . readFile ( conf . workloguri )
16+ return stdout . toString ( ) ;
3117}
3218/**open dosbox and do things about it
3319 * @param conf The config information
3420 * @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)
21+ * @param doc If defined, copy this file to workspace as T.xxx(xxx is the files extension) using terminal commands
3622 */
3723export function runDosbox ( conf : Config , more ?: string , doc ?: TextDocument ) : Promise < string > {
38- let filename = doc ?. fileName ;
39- let fileext = filename ?. substring ( filename . lastIndexOf ( "." ) , filename . length ) ;
4024 let preCommand : string = "" ;
4125 let boxcmd : string = '@echo off\n' ;
42- boxcmd += `mount c \\\"${ conf . path } \\\"\nmount d \\\"${ conf . workpath } \\\"\n` ; //mount the necessary path
26+ boxcmd += `mount c \\\"${ conf . path } \\\"\nmount d \\\"${ conf . workUri . fsPath } \\\"\n` ; //mount the necessary path
4327 boxcmd += "d:\nset PATH=%%PATH%%;c:\\tasm;c:\\masm\n" ; //switch to the working space and add path\
4428 if ( doc ) {
29+ let filename = doc ?. fileName ;
30+ let fileext = filename ?. substring ( filename . lastIndexOf ( "." ) , filename . length ) ;
4531 boxcmd += `echo Your file has been copied as D:\\T${ fileext } \n` ;
4632 if ( process . platform === 'win32' ) {
4733 preCommand = `del/Q T*.* & copy "${ filename } " "T${ fileext } " & ` ;
@@ -53,31 +39,58 @@ export function runDosbox(conf: Config, more?: string, doc?: TextDocument): Prom
5339 boxcmd += "@echo on" ;
5440 if ( more ) { boxcmd += "\n" + more ; } //add extra commands
5541 let opt : OPTS = {
56- cwd : conf . workpath ,
42+ cwd : conf . workUri . fsPath ,
5743 preOpen : preCommand ,
5844 core : conf . OpenDosbox ,
5945 boxcmd : boxcmd ,
6046 parameter : ' -conf "' + conf . dosboxconfuri . fsPath + '" '
6147 } ;
6248 return openDosbox ( opt ) ;
6349}
50+ /**
51+ * opendosbox at the Editor's file's folder
52+ * @param conf config
53+ * @param doc the doc
54+ */
6455export function BoxOpenCurrentFolder ( conf : Config , doc : TextDocument ) {
6556 let folderpath : string = Uri . joinPath ( doc . uri , '../' ) . fsPath ;
6657 let opt : OPTS = {
67- cwd : conf . workpath ,
58+ cwd : conf . workUri . fsPath ,
6859 core : conf . OpenDosbox ,
6960 boxcmd : `mount e \\\"${ folderpath } \\\"\nmount c \\\"${ conf . path } \\\"\nset PATH=%%PATH%%;c:\\masm;c:\\tasm\ne:` ,
7061 parameter : ' -conf "' + conf . dosboxconfuri . fsPath + '" '
7162 } ;
7263 openDosbox ( opt ) ;
7364}
65+ /**
66+ * options for open dosbox
67+ */
7468interface OPTS {
69+ /**
70+ * the cwd of child_process
71+ */
7572 cwd : string ,
73+ /**
74+ * the core command,usually the command for open dosbox
75+ */
7676 core : string
77+ /**
78+ * the command exec before the core command
79+ */
7780 preOpen ?: string ,
81+ /**
82+ * the parameter for dosbox command
83+ */
7884 parameter ?: string ,
85+ /**
86+ * the command need to exec inside dosbox
87+ */
7988 boxcmd ?: string
8089}
90+ /**
91+ * open DOSBox through child_process
92+ * @param opt options
93+ */
8194function openDosbox ( opt : OPTS ) : Promise < string > {
8295 let str = opt . core + opt . parameter ;
8396 if ( opt . preOpen ) {
@@ -97,34 +110,28 @@ function openDosbox(opt: OPTS): Promise<string> {
97110 }
98111 return new Promise (
99112 ( resolve , reject ) => {
100- exec ( str , execOption , ( error : any , stdout : string , stderr : string ) => {
113+ let child = exec ( str , execOption , ( error : any , stdout : string , stderr : string ) => {
101114 if ( error ) {
102115 reject ( error ) ;
103116 }
104117 else {
105118 resolve ( stdout ) ;
106119 }
107120 } ) ;
121+ child . on ( 'exit' , ( code ) => {
122+ if ( code !== 0 ) {
123+ let msg = `Open dosbox Failed\t exitcode${ code } \n` ;
124+ if ( process . platform !== "win32" ) {
125+ msg += 'PLEASE make sure DOSBox can be opened by terminal command "dosbox"' ;
126+ }
127+ window . showErrorMessage ( msg ) ;
128+ }
129+
130+ } )
108131 }
109132 ) ;
110133}
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- ) ;
125- }
126- return info ;
127- }
134+
128135
129136
130137
0 commit comments