-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMake-KLC.ps1
82 lines (67 loc) · 2.08 KB
/
Make-KLC.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = "Paths")]
[string] $path,
[Parameter(Position = 1, ParameterSetName = "Paths")]
[string] $output,
[Parameter(Mandatory = $true, ParameterSetName = "Name")]
[string] $name,
[string] $encoding = "UTF8",
[switch] $force = $false
)
BEGIN {
if ($name -ne $null){
$path = ".\$($name).klc.tpl"
$output = ".\$($name).klc"
}
$path = (Resolve-Path -Path $path).ProviderPath
$folder = Split-Path -Path $path -Parent
$klc = [IO.Path]::GetFileNameWithoutExtension($path)
if (-not $output) {
$output = Join-Path -Path $folder -ChildPath $klc
} else {
$output = (Resolve-Path -Path $output).ProviderPath
}
if (Test-Path -Path $output) {
if (-not $force.IsPresent) {
throw "The layout file `"$klc`" already exists. Please, use the -force flag to overwrite."
}
Remove-Item -Path "$output~" -EA SilentlyContinue | Out-Null
Rename-Item -Path $output -NewName "$output~"
}
$code = "\<\? (?<cmd>.*) \?\>"
Function Run-Command {
[CmdletBinding()]
param(
[string] $script
)
BEGIN {
$path = (Resolve-Path -Path $script).ProviderPath
$folder = Split-Path -Path $path -Parent
$ps = Split-Path -Path $path -Leaf
Push-Location $folder
}
PROCESS {
Write-Output (Invoke-Expression ".\$ps")
}
END {
Pop-Location
}
}
}
PROCESS {
Get-Content -Path $path -Encoding UTF8 | % {
$line = $_
$match = $line -match $code
if ($match) {
$command = $matches["cmd"]
$result = Run-Command -Script $command
$result | % {
Add-Content -Path $output -Encoding $encoding -Value $_
}
}
else {
Add-Content -Path $output -Encoding $encoding -Value $_
}
}
}