From f553185880afd0b5ce18d2a93e964bdeb8ab91fa Mon Sep 17 00:00:00 2001 From: Daniel Schreiber Date: Thu, 6 Jan 2022 12:14:15 +0100 Subject: [PATCH] support for if-else - fixes #44 --- src/convert-bash.test.ts | 27 ++++++++++++++++++++++----- src/convert-bash.ts | 13 +++++++++++++ ui/src/app/app.component.ts | 6 ++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/convert-bash.test.ts b/src/convert-bash.test.ts index c034bf0..c0ad52f 100644 --- a/src/convert-bash.test.ts +++ b/src/convert-bash.test.ts @@ -22,13 +22,30 @@ describe('convert-bash', () => { }); test('should handle "&&"', () => { - expect(convertBashToWin('echo "hi 1" && echo "there 2"')) - .toEqual('@echo off\n\necho "hi 1" && echo "there 2"'); + expect(convertBashToWin('echo "hi 1" && echo "there 2"')) + .toEqual('@echo off\n\necho "hi 1" && echo "there 2"'); }); test('should handle "||"', () => { - expect(convertBashToWin('echo "hi 1" || echo "there 2"')) - .toEqual('@echo off\n\necho "hi 1" || echo "there 2"'); + expect(convertBashToWin('echo "hi 1" || echo "there 2"')) + .toEqual('@echo off\n\necho "hi 1" || echo "there 2"'); + }); + + test('should handle simple if', () => { + expect(convertBashToWin('if [ "$my_var" == "" ]; then\n' + + ' echo "my_var is empty"\n' + + ' echo "second line"\n' + + 'fi')) + .toEqual('@echo off\n\nIF "%my_var%" == "" (\n echo "my_var is empty"\n echo "second line"\n)'); + }); + + test('should handle if-else', () => { + expect(convertBashToWin('if [ "$my_var" == "" ]; then\n' + + ' echo "my_var is empty"\n' + + 'else\n' + + ' echo "my_var is not empty"\n' + + 'fi')) + .toEqual('@echo off\n\nIF "%my_var%" == "" (\n echo "my_var is empty"\n) ELSE (\n echo "my_var is not empty"\n)'); }); test('should transform complete example', () => { @@ -62,4 +79,4 @@ EXIT /B 0 `); }); }); -}); \ No newline at end of file +}); diff --git a/src/convert-bash.ts b/src/convert-bash.ts index cf5e983..ec883e1 100644 --- a/src/convert-bash.ts +++ b/src/convert-bash.ts @@ -82,6 +82,9 @@ class ConvertBash { this.userDefinedFunctions.push(command); return ''; case 'Word': + if (text === '') { + return '""'; + } if (text && text.indexOf(' ') >= 0 && !text.startsWith('"')) { return `"${text}"`; } @@ -98,6 +101,13 @@ class ConvertBash { default: return `REM UNKNOWN operand "${command.op}" in: ${JSON.stringify(command)}`; } + case 'CompoundList': + return command.commands.map(c => this.convertCommand(c)).join('\n'); + case 'If': + // note: AND/OR is not supported with batch IF (https://stackoverflow.com/a/2143203/2544163) + const condition = this.convertCommand(command.clause.commands[0]).replace(/^\[ | ]$/g, ''); + const elseBranch = command.else ? ` ELSE (\n${this.indent(this.convertCommand(command.else))}\n)` : ''; + return `IF ${condition} (\n${this.indent(this.convertCommand(command.then))}\n)${elseBranch}`; } return 'REM UNKNOWN: ' + JSON.stringify(command); } @@ -113,6 +123,9 @@ class ConvertBash { }).join('\n')}` } + private indent(s: string): string { + return s.split('\n').map(line => ` ${line}`).join('\n'); + } } diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index 0dc453c..5efa431 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -19,6 +19,12 @@ SOME_VAR="/c/cygwin/path" rm -rf $SOME_VAR || echo "file not found" cp /c/some/file /to/another/file +if [ $SOME_VAR == "" ]; then + echo "SOME_VAR is empty" +else + echo "SOME_VAR not empty" +fi + my_function () { echo "hello from my_function: $1" }