Skip to content

Commit 0354a0b

Browse files
committed
Build, run, and build-run aliases.
1 parent 2840377 commit 0354a0b

File tree

4 files changed

+205
-28
lines changed

4 files changed

+205
-28
lines changed

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,39 @@ Must first set `$env:PatchTargetDir` to directory you want to patch. Use `vspatc
4646

4747
```json
4848
{
49-
"sourceDirectory": "directory to copy from",
49+
"variables": {
50+
"`$env:EnvVariableName": "value"
51+
},
5052
"files": {
51-
52-
"relative path to source file": "relative path to destination file",
53+
"relative source path": "relative destination path"
5354
},
5455
"commands": [
55-
"Start-Process 'myproc.exe' -Wait -ArgumentList 'args'"
56+
"Start-Process foo.exe -Wait -ArgumentsList 'So Argumentative'"
5657
]
5758
}
5859
```
5960

61+
- variables: Strings that are expanded to PowerShell variables. Use `$env:Foo to
62+
define environment variables, try `$env:GitRoot to create paths relative
63+
to the repo root, and try `$env: to reference environment variables.
64+
65+
- The following are 'special' variables that light up aliases:
66+
67+
- `$env:PatchBuildCmd - The command to build with prior to patching.
68+
69+
- `$env:PatchSourceDir - The source folder to copy bits from.
70+
71+
- `$env:PatchTargetDir - The destination to patch to. You can optionally
72+
set this with the vspath alias or your own script.
73+
74+
- `$env:PatchTargetExe - The main executable of the application being patched.
75+
76+
- files: a dictionary of source -> destination path that are backedup and patched.
77+
Can use environment variables.
78+
79+
- commands: an array of PowerShell commands to run after the patch and unpatch.
80+
81+
6082
#### Start VS
6183
- Find VS instances with `vsget`
6284
- Launch desired instance with `vsstart [instance number]`.
@@ -69,6 +91,14 @@ Must first set `$env:PatchTargetDir` to directory you want to patch. Use `vspatc
6991
- Check if your currently selected VS instance has been patched with `ptstatus`.
7092
- Unpatch a specific profile using `ptrevert [profile name]`.
7193

94+
#### Inner Dev Loop
95+
- You can run the typical inner dev loop of build, deploy, run with:
96+
- ptuse [profile name] - Chooses a profile to use for this PowerShell session. Enables you to not specify each time.
97+
- ptbuild - builds the project with the command specified by $env:PatchBuildCmd
98+
- ptrun - runs the target application in $env:PatchTargetExe
99+
- ptbuildrun - builds, patches, and runs the target application using the commands in $env:PatchBuildCmd and
100+
$env:PatchTargetExe.
101+
72102
#### F5 debug your project
73103
- Follow steps above for 'Configuring' your machine and profile.
74104
- Ensure that 'Patch and Restore' from above work.
@@ -109,6 +139,12 @@ Use with Visual Studio
109139
- ptstatus [profile]: Checks the current target application for patched files.
110140
- ptrevert [profile]: Reverts the current profile's patched binaries.
111141
- ptF5 [vsinstance] [solutionpath] [profile]: Launches the specified instance of VS with the specified solution + profile configured for one-click (F5) debugging.
142+
- ptbuild [profile]: Builds the project for the named profile using the command contained in $env:PatchBuildCmd.
143+
- ptrun [profile]: Runs the target executable for the named profile using the command contained in $env:PatchTargetExe.
144+
- ptbuildrun [profile]: Builds, patches, and runs using the $env:PatchBuildCmd and $env:PatchTargetExe variables.
145+
- ptuse [profile]: Indicates that this profile should be used for the duration of the PowerShell session. Run this once
146+
and no longer need to specify the profile each build.
147+
112148

113149
#### Process Snapshot
114150
Defines some convenient scripts and aliases for killing groups of processes
@@ -140,7 +176,7 @@ Aliases for launching Visual Studio installs developer command prompts.
140176
- vspatch [instance]: Selects an instance of VS as the target application for patching.
141177

142178
## ChangeLog
143-
- 8/15/2019 - Install to consistent location. Can now uninstall with 'Uninstall-Tools' function. Fixed issue where we'd pollute the user's path.
179+
- 8/15/2019 - Install to consistent location. Can now uninstall with 'Uninstall-Tools' function. Fixed issue where we'd pollute the user's path. Refined build, deploy, run workflow aliases.
144180
- 7/17/2019 - Support environment variables in patch paths and define $env:GitRoot for the root of the current repo.
145181
- 6/7/2019 - Fixed killing of locking processes when doing revert. Fixed overwriting of backup when revert fails. Print release notes on startup. Init submodule on build.
146182
- 5/10/2019 - Add 'scratch' to default nav locations.

src/Common/Config.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $ConfigurationValues =
1616
"DisableConsole" = $true
1717
"InstallationPath" = New-InstallationPath
1818
"IsInstalled" = $false
19-
"Version" = "0.36"
19+
"Version" = "0.4"
2020
}
2121

2222
$RegistryRootKeyPath = "HKCU:Software\Tools"

src/Features/Git.Autoload.psm1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Git utilities
2+
# By: Christian Gunderman
3+
4+
function Set-GitRoot()
5+
{
6+
$env:GitRoot = Get-GitRoot
7+
}
8+
9+
function Get-GitRoot()
10+
{
11+
$currentDirectory = Convert-Path .
12+
$lastIndex = $currentDirectory.LastIndexOf('\')
13+
14+
while ($lastIndex -ne -1)
15+
{
16+
if (Test-Path (Join-Path $currentDirectory ".git") -PathType Container)
17+
{
18+
return $currentDirectory
19+
}
20+
21+
$currentDirectory = $currentDirectory.Substring(0, $lastIndex)
22+
$lastIndex = $currentDirectory.LastIndexOf('\')
23+
}
24+
25+
Write-Host -ForegroundColor Yellow "Not a git repository"
26+
}

src/Features/Patch.Autoload.psm1

Lines changed: 137 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ function Get-PatchProfilePath($patchProfile)
88
{
99
if ([string]::IsNullOrWhiteSpace($patchProfile))
1010
{
11-
Throw "Must provide a patch profile name argument"
11+
if ([string]::IsNullOrWhiteSpace($env:PatchProfile))
12+
{
13+
Throw "Must provide a patch profile name argument"
14+
}
15+
16+
$patchProfile = $env:PatchProfile
1217
}
1318

1419
return Join-Path $Global:ScratchDir "$patchProfile.patchprofile"
@@ -26,13 +31,56 @@ function Get-PatchTargetDirectory
2631
return $env:PatchTargetDir
2732
}
2833

34+
function Get-PatchConfiguration($patchProfile)
35+
{
36+
$patchProfile = (Get-PatchProfilePath $patchProfile)
37+
$content = (Get-Content $patchProfile)
38+
$jsonContent = $content | ConvertFrom-Json
39+
40+
# Define any given environment variables.
41+
$variables = $jsonContent.variables
42+
foreach ($file in $variables)
43+
{
44+
foreach ($property in $file.PSObject.Properties)
45+
{
46+
$name = [string]$property.Name
47+
$value = [string]$ExecutionContext.InvokeCommand.ExpandString($property.Value)
48+
49+
if ([string]::IsNullOrEmpty($value))
50+
{
51+
Write-Host -ForegroundColor Yellow "Variable $name has empty value"
52+
}
53+
54+
Write-Host "Setting variable $name to '$value'..."
55+
Invoke-Expression "$name = `"$value`""
56+
}
57+
}
58+
59+
# Determine the source directory. Supports environment variables.
60+
# This line here is for compat with existing profiles. New profiles should set a variable instead.
61+
if ([string]::IsNullOrWhiteSpace($env:PatchSourceDir))
62+
{
63+
$env:PatchSourceDir = $ExecutionContext.InvokeCommand.ExpandString($jsonContent.sourceDirectory)
64+
}
65+
66+
Write-Host "Source Directory: $env:PatchSourceDir"
67+
if ([string]::IsNullOrWhiteSpace($env:PatchSourceDir) -or (-not (Test-Path $env:PatchSourceDir)))
68+
{
69+
Throw "Unspecified or inaccessible `$env:PatchSourceDir $env:PatchSourceDir"
70+
}
71+
72+
return $jsonContent
73+
}
74+
2975
function Write-PatchSchema
3076
{
3177
Write-Output @"
3278
Patch schema is as follows:"
3379
3480
{
35-
"sourceDirectory": "$env:GitRoot",
81+
"variables": {
82+
"`$env:EnvVariableName": "value"
83+
},
3684
"files": {
3785
"relative source path": "relative destination path"
3886
},
@@ -41,10 +89,24 @@ Patch schema is as follows:"
4189
]
4290
}
4391
44-
sourceDirectory: a path, possibly with Powershell variables, to patch from.
45-
Try $env:GitRoot to copy relative to the repo root.
92+
variables: Strings that are expanded to PowerShell variables. Use `$env:Foo to
93+
define environment variables, try `$env:GitRoot to create paths relative
94+
to the repo root, and try `$env: to reference environment variables.
95+
96+
The following are 'special' variables that light up aliases:
97+
98+
`$env:PatchBuildCmd - The command to build with prior to patching.
99+
100+
`$env:PatchSourceDir - The source folder to copy bits from.
101+
102+
`$env:PatchTargetDir - The destination to patch to. You can optionally
103+
set this with the vspath alias or your own script.
104+
105+
`$env:PatchTargetExe - The main executable of the application being
106+
patched.
46107
47-
files: a dictionary of source -> destination path. Can use environment variables.
108+
files: a dictionary of source -> destination path that are backedup and patched.
109+
Can use environment variables.
48110
49111
commands: an array of PowerShell commands to run after the patch and unpatch.
50112
@@ -175,17 +237,7 @@ function Invoke-PatchProfile($patchProfile)
175237

176238
Set-GitRoot
177239

178-
$patchProfile = (Get-PatchProfilePath $patchProfile)
179-
$content = (Get-Content $patchProfile)
180-
$jsonContent = $content | ConvertFrom-Json
181-
182-
# Determine the source directory. Supports environment variables.
183-
$sourceDirectory = $ExecutionContext.InvokeCommand.ExpandString($jsonContent.sourceDirectory)
184-
Write-Host "Source Directory: $sourceDirectory"
185-
if ([string]::IsNullOrWhiteSpace($sourceDirectory) -or (-not (Test-Path $sourceDirectory)))
186-
{
187-
Throw "Unspecified or inaccessible sourceDirectory $sourceDirectory"
188-
}
240+
$jsonContent = (Get-PatchConfiguration $patchProfile)
189241

190242
# Determine the destination directory.
191243
$destinationDirectory = Get-PatchTargetDirectory
@@ -196,7 +248,7 @@ function Invoke-PatchProfile($patchProfile)
196248
{
197249
foreach ($property in $file.PSObject.Properties)
198250
{
199-
$sourceFile = (Join-Path $sourceDirectory $ExecutionContext.InvokeCommand.ExpandString($property.Name))
251+
$sourceFile = (Join-Path $env:PatchSourceDir $ExecutionContext.InvokeCommand.ExpandString($property.Name))
200252
$destinationFile = (Join-Path $destinationDirectory $ExecutionContext.InvokeCommand.ExpandString($property.Value))
201253

202254
if (-not (PatchItem $sourceFile $destinationFile))
@@ -239,9 +291,7 @@ function Invoke-RevertPatchProfile($patchProfile)
239291
{
240292
Set-GitRoot
241293

242-
$patchProfile = (Get-PatchProfilePath $patchProfile)
243-
$content = (Get-Content $patchProfile)
244-
$jsonContent = $content | ConvertFrom-Json
294+
$jsonContent = (Get-PatchConfiguration $patchProfile)
245295

246296
# Determine the destination directory.
247297
$destinationDirectory = Get-PatchTargetDirectory
@@ -300,10 +350,9 @@ function Start-F5InVS($vsInstance, $solutionPath, $patchProfile)
300350
$oldPostBuildEvent = $env:PostBuildEvent
301351

302352
# Set environment variables that will be read by Microsoft common targets and perform the patch.
303-
# TODO: PatchTargetExe should be configurable in the profile.
304353
$env:StartAction="Program"
305354
$env:StartProgram=$env:PatchTargetExe
306-
$env:PatchProfileName="search"
355+
$env:PatchProfileName=$patchProfile
307356

308357
$powershellPath = (Join-Path $PsHome "powershell.exe")
309358
$toolsPath = (Join-Path (Join-Path $Global:FeatureDir "..") "Tools.ps1")
@@ -323,9 +372,75 @@ function Start-F5InVS($vsInstance, $solutionPath, $patchProfile)
323372
$env:PostBuildEvent=$oldPostBuildEvent
324373
}
325374

375+
# Invokes a patch profile build operation.
376+
function Invoke-BuildPatchProfile($patchProfile)
377+
{
378+
Set-GitRoot
379+
380+
# Read in variables.
381+
Get-PatchConfiguration $patchProfile | Out-Null
382+
383+
if ([string]::IsNullOrEmpty($env:PatchBuildCmd))
384+
{
385+
Throw "Must set `$env:PatchBuildCmd prior to using this alias. Consider setting it in your profile 'variables' section."
386+
}
387+
388+
# Run the build command.
389+
Invoke-Expression $env:PatchBuildCmd
390+
391+
if ($LASTEXITCODE -ne 0)
392+
{
393+
Throw "Build failed"
394+
}
395+
}
396+
397+
# Invokes a patch profile target executable.
398+
function Invoke-RunPatchProfileTarget($patchProfile)
399+
{
400+
Set-GitRoot
401+
402+
# Read in variables.
403+
Get-PatchConfiguration $patchProfile | Out-Null
404+
405+
if ([string]::IsNullOrEmpty($env:PatchTargetExe))
406+
{
407+
Throw "Must set `$env:PatchTargetExe prior to using this alias. Consider using 'vspatch' or setting it in your profile 'variables' section."
408+
}
409+
410+
# Run the target executable.
411+
& $env:PatchTargetExe
412+
}
413+
414+
# Builds, patches, and runs the target of a patch profile.
415+
function Invoke-BuildAndRunPatchProfile($patchProfile)
416+
{
417+
# Build project.
418+
Invoke-BuildPatchProfile $patchProfile
419+
420+
# Patch target.
421+
Invoke-PatchProfile $patchProfile
422+
423+
# Run target.
424+
Invoke-RunPatchProfileTarget $patchProfile
425+
}
426+
427+
# Enables setting a current preferred patch profile.
428+
function Set-CurrentPatchProfile($patchProfile)
429+
{
430+
# Ensure we have a profile of that name.
431+
Get-PatchProfilePath $patchProfile | Out-Null
432+
433+
# Save it for later.
434+
$env:PatchProfile = $patchProfile
435+
}
436+
326437
New-Alias -Name ptedit -Value Edit-PatchProfile
327438
New-Alias -Name ptget -Value Get-PatchProfiles
328439
New-Alias -Name ptapply -Value Invoke-PatchProfile
329440
New-Alias -Name ptstatus -Value Get-PatchStatus
330441
New-Alias -Name ptrevert -Value Invoke-RevertPatchProfile
331442
New-Alias -Name ptF5 -Value Start-F5InVS
443+
New-Alias -Name ptbuild -Value Invoke-BuildPatchProfile
444+
New-Alias -Name ptrun -Value Invoke-RunPatchProfileTarget
445+
New-Alias -Name ptbuildrun -Value Invoke-BuildAndRunPatchProfile
446+
New-Alias -Name ptuse -Value Set-CurrentPatchProfile

0 commit comments

Comments
 (0)