Skip to content

Commit

Permalink
support for if-else - fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed Jan 6, 2022
1 parent c72977c commit f553185
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/convert-bash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -62,4 +79,4 @@ EXIT /B 0
`);
});
});
});
});
13 changes: 13 additions & 0 deletions src/convert-bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`;
}
Expand All @@ -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);
}
Expand All @@ -113,6 +123,9 @@ class ConvertBash {
}).join('\n')}`
}

private indent(s: string): string {
return s.split('\n').map(line => ` ${line}`).join('\n');
}
}


Expand Down
6 changes: 6 additions & 0 deletions ui/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit f553185

Please sign in to comment.