-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pipes to write pretty PowerShell #53
This commit introduces two pipes: `inlinePowerShell`, `escapeDoubleQuotes`. The types when used together allows writing adding clean and real PowerShell scripts as they are (without inlinining or escaping them), removing the need to have hard-coded inlining/escaping. It enables writing better PowerShell, makes it easier to maintain and extend PowerShell scripts. Also allows writing more stable code with less "unseen" bugs due to manual escaping/inlining. This commit naturally reveals and fixes double quotes not being escaped in "Empty trash bin" script. This is solved by unifying the use of RunPowerShell function by all scripts using PowerShell. The function inlines and escapes the scripts as compile time to be send them to PowerShell.exe as an argument and then invokes PowerShell.exe with generated ugly code.
- Loading branch information
1 parent
ddf417a
commit 5217b0b
Showing
8 changed files
with
196 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...pplication/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IPipe } from '../IPipe'; | ||
|
||
export class EscapeDoubleQuotes implements IPipe { | ||
public readonly name: string = 'escapeDoubleQuotes'; | ||
public apply(raw: string): string { | ||
return raw?.replaceAll('"', '\\"'); | ||
/* | ||
In batch, it also works with 4 double quotes but looks bloated | ||
An easy test: PowerShell -Command "Write-Host '\"Hello World\"'" | ||
*/ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/InlinePowerShell.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IPipe } from '../IPipe'; | ||
|
||
export class InlinePowerShell implements IPipe { | ||
public readonly name: string = 'inlinePowerShell'; | ||
public apply(raw: string): string { | ||
return raw | ||
?.split(/\r\n|\r|\n/) | ||
.map((line) => line.trim()) | ||
.filter((line) => line.length > 0) | ||
.join('; '); | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
src/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ation/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'mocha'; | ||
import { runPipeTests } from './PipeTestRunner'; | ||
import { EscapeDoubleQuotes } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes'; | ||
|
||
describe('EscapeDoubleQuotes', () => { | ||
// arrange | ||
const sut = new EscapeDoubleQuotes(); | ||
// act | ||
runPipeTests(sut, [ | ||
{ | ||
name: 'using "', | ||
input: 'hello "world"', | ||
expectedOutput: 'hello \\"world\\"', | ||
}, | ||
{ | ||
name: 'not using any double quotes', | ||
input: 'hello world', | ||
expectedOutput: 'hello world', | ||
}, | ||
{ | ||
name: 'consecutive double quotes', | ||
input: '""hello world""', | ||
expectedOutput: '\\"\\"hello world\\"\\"', | ||
}, | ||
{ | ||
name: 'returns undefined when if input is undefined', | ||
input: undefined, | ||
expectedOutput: undefined, | ||
}, | ||
]); | ||
}); |
47 changes: 47 additions & 0 deletions
47
...ication/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/InlinePowerShell.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'mocha'; | ||
import { InlinePowerShell } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/InlinePowerShell'; | ||
import { runPipeTests } from './PipeTestRunner'; | ||
|
||
describe('InlinePowerShell', () => { | ||
// arrange | ||
const sut = new InlinePowerShell(); | ||
// act | ||
runPipeTests(sut, [ | ||
{ | ||
name: 'no new line', | ||
input: 'Write-Host \'Hello, World!\'', | ||
expectedOutput: 'Write-Host \'Hello, World!\'', | ||
}, | ||
{ | ||
name: '\\n new line', | ||
input: '$things = Get-ChildItem C:\\Windows\\\nforeach ($thing in $things) {\nWrite-Host $thing.Name -ForegroundColor Magenta\n}', | ||
expectedOutput: '$things = Get-ChildItem C:\\Windows\\; foreach ($thing in $things) {; Write-Host $thing.Name -ForegroundColor Magenta; }', | ||
}, | ||
{ | ||
name: '\\n double empty lines are ignored', | ||
input: '$things = Get-ChildItem C:\\Windows\\\n\nforeach ($thing in $things) {\n\nWrite-Host $thing.Name -ForegroundColor Magenta\n\n\n}', | ||
expectedOutput: '$things = Get-ChildItem C:\\Windows\\; foreach ($thing in $things) {; Write-Host $thing.Name -ForegroundColor Magenta; }', | ||
}, | ||
{ | ||
name: '\\r new line', | ||
input: '$things = Get-ChildItem C:\\Windows\\\rforeach ($thing in $things) {\rWrite-Host $thing.Name -ForegroundColor Magenta\r}', | ||
expectedOutput: '$things = Get-ChildItem C:\\Windows\\; foreach ($thing in $things) {; Write-Host $thing.Name -ForegroundColor Magenta; }', | ||
}, | ||
{ | ||
name: '\\r and \\n newlines combined', | ||
input: '$things = Get-ChildItem C:\\Windows\\\r\nforeach ($thing in $things) {\n\rWrite-Host $thing.Name -ForegroundColor Magenta\n\r}', | ||
expectedOutput: '$things = Get-ChildItem C:\\Windows\\; foreach ($thing in $things) {; Write-Host $thing.Name -ForegroundColor Magenta; }', | ||
}, | ||
{ | ||
name: 'trims whitespaces on lines', | ||
input: ' $things = Get-ChildItem C:\\Windows\\ \nforeach ($thing in $things) {\n\tWrite-Host $thing.Name -ForegroundColor Magenta\r \n}', | ||
expectedOutput: '$things = Get-ChildItem C:\\Windows\\; foreach ($thing in $things) {; Write-Host $thing.Name -ForegroundColor Magenta; }', | ||
}, | ||
{ | ||
name: 'returns undefined when if input is undefined', | ||
input: undefined, | ||
expectedOutput: undefined, | ||
}, | ||
]); | ||
}); | ||
|
20 changes: 20 additions & 0 deletions
20
...it/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/PipeTestRunner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'mocha'; | ||
import { expect } from 'chai'; | ||
import { IPipe } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipe'; | ||
|
||
interface ITestCase { | ||
readonly name: string; | ||
readonly input: string; | ||
readonly expectedOutput: string; | ||
} | ||
|
||
export function runPipeTests(sut: IPipe, testCases: ITestCase[]) { | ||
for (const testCase of testCases) { | ||
it(testCase.name, () => { | ||
// act | ||
const actual = sut.apply(testCase.input); | ||
// assert | ||
expect(actual).to.equal(testCase.expectedOutput); | ||
}); | ||
} | ||
} |