Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more comprehensive support for compare operators #70

Merged
merged 6 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions src/convert-bash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,77 @@ describe('convert-bash', () => {
.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 handle simple if -eq', () => {
expect(convertBashToWin('if [ "$my_var" -eq "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF "%my_var%" EQU "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle simple if -ne', () => {
expect(convertBashToWin('if [ "$my_var" -ne "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF "%my_var%" NEQ "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle simple if -lt', () => {
expect(convertBashToWin('if [ "$my_var" -lt "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF "%my_var%" LSS "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle simple if -le', () => {
expect(convertBashToWin('if [ "$my_var" -le "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF "%my_var%" LEQ "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle simple if -gt', () => {
expect(convertBashToWin('if [ "$my_var" -gt "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF "%my_var%" GTR "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle simple if -ge', () => {
expect(convertBashToWin('if [ "$my_var" -ge "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF "%my_var%" GEQ "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle simple if not equal', () => {
expect(convertBashToWin('if [ ! "$my_var" == "" ]; then\n' +
' echo "my_var is empty"\n' +
' echo "second line"\n' +
'fi'))
.toEqual('@echo off\n\nIF NOT "%my_var%" == "" (\n echo "my_var is empty"\n echo "second line"\n)');
});

test('should handle string interpolation with backticks', () => {
expect(convertBashToWin('my_var="test-`git log`"'))
.toEqual('@echo off\n' +
'setlocal EnableDelayedExpansion\n\n' +
'SET _INTERPOLATION_0=\n' +
'FOR /f "delims=" %%a in (\'git log\') DO (SET "_INTERPOLATION_0=!_INTERPOLATION_0! %%a")\n' +
'SET "my_var=test-!_INTERPOLATION_0!"');
'SET "my_var=test-!_INTERPOLATION_0:~1!"');
});
test('should echo variable correctly with delayed expansion', () => {
expect(convertBashToWin('my_var="test-`git log`"\necho $my_var'))
.toEqual('@echo off\n' +
'setlocal EnableDelayedExpansion\n\n' +
'SET _INTERPOLATION_0=\n' +
'FOR /f "delims=" %%a in (\'git log\') DO (SET "_INTERPOLATION_0=!_INTERPOLATION_0! %%a")\n' +
'SET "my_var=test-!_INTERPOLATION_0!"\n' +
'SET "my_var=test-!_INTERPOLATION_0:~1!"\n' +
'echo "!my_var!"');
});
test('should activate delayed expansion for interpolation in function', () => {
Expand All @@ -120,7 +175,7 @@ EXIT /B %ERRORLEVEL%
:my_function
SET _INTERPOLATION_0=
FOR /f "delims=" %%a in ('git log') DO (SET "_INTERPOLATION_0=!_INTERPOLATION_0! %%a")
SET "my_var=test-!_INTERPOLATION_0!"
SET "my_var=test-!_INTERPOLATION_0:~1!"
echo "hello from my_function: !my_var!"
EXIT /B 0
`);
Expand All @@ -132,7 +187,7 @@ EXIT /B 0
'setlocal EnableDelayedExpansion\n\n' +
'SET _INTERPOLATION_0=\n' +
'FOR /f "delims=" %%a in (\'git log\') DO (SET "_INTERPOLATION_0=!_INTERPOLATION_0! %%a")\n' +
'SET "my_var=test-!_INTERPOLATION_0!"');
'SET "my_var=test-!_INTERPOLATION_0:~1!"');
});

test('should handle switch case', () => {
Expand Down
34 changes: 23 additions & 11 deletions src/convert-bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,30 @@ class ConvertBash {
return '';
case 'Word':
const expandedWord = this.performExpansions(command.text, command.expansion);
const textWord = convertPaths(expandedWord);

if (textWord.startsWith('"') || ['==', '!='].includes(textWord)) {
return textWord;
let textWord = convertPaths(expandedWord);

if (textWord.startsWith('"')) {
/* Keep textWord as it is. */
} else if (['=='].includes(textWord)) {
/* Keep textWord as it is. */
} else if (['!=', '-ne'].includes(textWord)) {
textWord = 'NEQ';
} else if (['-eq'].includes(textWord)) {
textWord = 'EQU';
} else if (['-lt'].includes(textWord)) {
textWord = 'LSS';
} else if (['-le'].includes(textWord)) {
textWord = 'LEQ';
} else if (['-gt'].includes(textWord)) {
textWord = 'GTR';
} else if (['-ge'].includes(textWord)) {
textWord = 'GEQ';
} else if (['!'].includes(textWord)) {
textWord = 'NOT';
} else {
return `"${textWord}"`;
textWord = `"${textWord}"`;
}
return textWord;
case 'AssignmentWord':
const expandedAssignmentWord = this.performExpansions(command.text, command.expansion);
const textAssignmentWord = convertPaths(expandedAssignmentWord);
Expand Down Expand Up @@ -138,7 +155,7 @@ class ConvertBash {
const interpolationVar = `_INTERPOLATION_${this.interpolationCounter++}`;
this.preStatements.push(`SET ${interpolationVar}=`);
this.preStatements.push(`FOR /f "delims=" %%a in ('${expansion.command}') DO (SET "${interpolationVar}=!${interpolationVar}! %%a")`);
result = `${result.substring(0, expansion.loc.start)}!${interpolationVar}!${result.substring(expansion.loc.end + 1)}`;
result = `${result.substring(0, expansion.loc.start)}!${interpolationVar}:~1!${result.substring(expansion.loc.end + 1)}`;
break;
case 'ParameterExpansion':
// expand function parameters such as `$1` (-> `%~1`) different to regular variables `$MY`(-> `%MY%` or `!MY!` if delayed expansion is active):
Expand Down Expand Up @@ -185,8 +202,3 @@ export function convertBashToWin(script: string) {
convertedCommands +
functionDefinitions;
}





Loading