Add "with" expression for templating #53
Allows optionally rendering content if an argument is given. The expression is designed to be used with `optional` parameters. Goal is to allow using `RunPowerShell` function on every function that consists of PowerShell code. Before this commit, they were all required to provide revertCode, or none of them could be able to have it. It would not work because some scripts can be reverted, meanwhile some are one-way scripts that cannot be reverted (such as cleaning scripts). In this case a way to optionally render revertCode was required. `with` expression give each callee script ability to turn off `revertCode` if not needed, therefore enables using `RunPowerShell` everywhere. This commit also improves error message for script code for better debugging and refactors parser tests for more code reuse. It also adds more tests to parameter substitution, and renames some tests of both expressions for consistency.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { IExpression } from '../Expression/IExpression';
|
||||
import { IExpressionParser } from './IExpressionParser';
|
||||
import { ParameterSubstitutionParser } from '../SyntaxParsers/ParameterSubstitutionParser';
|
||||
import { WithParser } from '../SyntaxParsers/WithParser';
|
||||
|
||||
const Parsers = [
|
||||
new ParameterSubstitutionParser(),
|
||||
new WithParser(),
|
||||
];
|
||||
|
||||
export class CompositeExpressionParser implements IExpressionParser {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { RegexParser, IPrimitiveExpression } from '../Parser/RegexParser';
|
||||
import { FunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter';
|
||||
|
||||
export class WithParser extends RegexParser {
|
||||
protected readonly regex = /{{\s*with\s+\$([^}| ]+)\s*}}\s*([^)]+?)\s*{{\s*end\s*}}/g;
|
||||
protected buildExpression(match: RegExpMatchArray): IPrimitiveExpression {
|
||||
const parameterName = match[1];
|
||||
const innerText = match[2];
|
||||
return {
|
||||
parameters: [ new FunctionParameter(parameterName, true) ],
|
||||
evaluator: (args) => {
|
||||
const argumentValue = args.hasArgument(parameterName) ?
|
||||
args.getArgument(parameterName).argumentValue
|
||||
: undefined;
|
||||
if (!argumentValue) {
|
||||
return '';
|
||||
}
|
||||
const substitutionRegex = /{{\s*.\s*}}/g;
|
||||
const newText = innerText.replace(substitutionRegex, argumentValue);
|
||||
return newText;
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -389,7 +389,15 @@ actions:
|
||||
code: net user defaultuser0 /delete 2>nul
|
||||
-
|
||||
name: Empty trash bin
|
||||
code: Powershell -Command "$bin = (New-Object -ComObject Shell.Application).NameSpace(10);$bin.items() | ForEach { Write-Host "Deleting $($_.Name) from Recycle Bin"; Remove-Item $_.Path -Recurse -Force}"
|
||||
call:
|
||||
function: RunPowerShell
|
||||
parameters:
|
||||
code:
|
||||
$bin = (New-Object -ComObject Shell.Application).NameSpace(10);
|
||||
$bin.items() | ForEach {
|
||||
Write-Host "Deleting $($_.Name) from Recycle Bin";
|
||||
Remove-Item $_.Path -Recurse -Force
|
||||
}
|
||||
-
|
||||
name: Enable Reset Base in Dism Component Store
|
||||
recommend: standard
|
||||
@@ -3803,14 +3811,16 @@ actions:
|
||||
code: reg delete "HKCU\Environment" /v "OneDrive" /f
|
||||
-
|
||||
name: Uninstall Edge (chromium-based)
|
||||
code:
|
||||
PowerShell -ExecutionPolicy Unrestricted -Command "
|
||||
$installer = (Get-ChildItem \"$env:ProgramFiles*\Microsoft\Edge\Application\*\Installer\setup.exe\");
|
||||
if (!$installer) {
|
||||
Write-Host Could not find the installer;
|
||||
} else {
|
||||
& $installer.FullName -uninstall -system-level -verbose-logging -force-uninstall
|
||||
}; "
|
||||
call:
|
||||
function: RunPowerShell
|
||||
parameters:
|
||||
code:
|
||||
$installer = (Get-ChildItem \"$env:ProgramFiles*\Microsoft\Edge\Application\*\Installer\setup.exe\");
|
||||
if (!$installer) {
|
||||
Write-Host Could not find the installer;
|
||||
} else {
|
||||
& $installer.FullName -Uninstall -System-Level -Verbose-Logging -Force-Uninstall
|
||||
};
|
||||
-
|
||||
category: Disable built-in Windows features
|
||||
children:
|
||||
@@ -4522,5 +4532,9 @@ functions:
|
||||
parameters:
|
||||
- name: code
|
||||
- name: revertCode
|
||||
optional: true
|
||||
code: PowerShell -ExecutionPolicy Unrestricted -Command "{{ $code }}"
|
||||
revertCode: PowerShell -ExecutionPolicy Unrestricted -Command "{{ $revertCode }}"
|
||||
revertCode: |-
|
||||
{{ with $revertCode }}
|
||||
PowerShell -ExecutionPolicy Unrestricted -Command "{{ . }}"
|
||||
{{ end }}
|
||||
|
||||
Reference in New Issue
Block a user