-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-snippets.ps1
130 lines (100 loc) · 4.1 KB
/
update-snippets.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using namespace System
param([switch]$VerboseSwitch = $false)
# $Verbose=$true -or $VerboseSwitch
$Verbose=$VerboseSwitch
# Write-Verbose "[$script] [$env:SnippetsInitialized] -not `$env:SnippetsInitialized: $(-not $env:SnippetsInitialized)" -Verbose:$Verbose
$script = $MyInvocation.MyCommand
if (-not $env:SnippetsInitialized) {
$fileInfo = New-Object System.IO.FileInfo (Get-Item $PSScriptRoot).FullName
$path = $fileInfo.Directory.FullName;
. $path/Snippets/_common.ps1;
Initialize-Snippets -Verbose:$Verbose
}
if ($env:IsWindows -ieq 'true') {
if($env:OneDrive){
$env:Snippets = "$env:OneDrive\Documents\PowerShell\Snippets"
} else {
$env:Snippets = "$env:UserProfile\Documents\PowerShell\Snippets"
}
}
else {
$env:Snippets = "$env:HOME/.config/powershell/Snippets"
}
Write-Verbose "[$script] Set `$env:Snippets to [$env:Snippets]" -Verbose:$Verbose
function Update-Profile {
param([switch]$Verbose = $false)
try{
if (Test-Path $env:Snippets) {
Push-Location
Set-Location $env:Snippets
Get-Item .version -ErrorAction SilentlyContinue -Verbose:$Verbose `
| Remove-Item -Verbose:$Verbose -ErrorAction Stop
. ./set-version.ps1 -Verbose:$Verbose
$startLine='# SNIPPETS BEGIN'
$endLine='# SNIPPETS END'
if($env:IsWindows -ieq "true") { $readmeFile = "${env:Snippets}/Windows-ReadmeTest.ps9" }
else { $readmeFile = "${env:Snippets}/Linux-ReadmeTest.ps9" }
Write-Verbose -Verbose:$Verbose -Message "[$script] Source File: [$readmeFile] Exists: $(Test-Path $readmeFile)"
$readme = Get-Content $readmeFile
$myProfile=get-content $PROFILE
$array=New-Object System.Collections.ArrayList
$array.AddRange($myProfile)
$matchStart=$array.IndexOf($startLine) + 1
$matchEnd=$array.IndexOf($endLine) - 1
if($matchStart -lt $matchEnd -and $matchStart -gt 0) {
$range = ($matchEnd..$matchStart)
foreach($index in $range){
$array.RemoveAt($index)
}
$array.InsertRange($matchStart, $readme)
} else {
# Append to End
$array.Add($startLine)
$array.AddRange($readme)
$array.Add($endLine)
}
$now=[System.DateTime]::Now.ToShortDateString()
$array.Add("# Snippets History: $now - ${env:SnippetsVersion}")
$array | Out-File $PROFILE -Encoding UTF8 -Verbose:$Verbose
return "Updated $PROFILE to version ${env:SnippetsVersion}"
}
else {
throw "Cannot locate `$env:Snippets: [$env:Snippets]"
}
}
finally {
Pop-Location
}
}
$alias = set-alias -Verbose:$Verbose -Scope Global -Description "Snippets: [snippets] Update Profile from GitHub." -Name profileup -Value Update-Profile
Write-Verbose -Verbose:$Verbose -Message "[$script] Set-Alias $alias"
function Update-Snippets {
param([switch]$Verbose = $false)
try{
if (Test-Path $env:Snippets) {
Push-Location
Set-Location $env:Snippets
& git pull
if ($LASTEXITCODE -ne 0) {
throw "git pull failed with: $LASTEXITCODE"
}
$exitCode = $LASTEXITCODE
if($exitCode -eq 0) {
Get-Item .version -ErrorAction SilentlyContinue -Verbose:$Verbose `
| Remove-Item -Verbose:$Verbose -ErrorAction Stop
. ./set-version.ps1 -Verbose:$Verbose
Update-Profile
}
}
else {
throw "Cannot find snippets folder at $env:Snippets"
}
}
finally {
Pop-Location
}
}
$alias = set-alias -Verbose:$Verbose -Scope Global -Description "Snippets: [snippets] Update Snippets from GitHub." -Name snipup -Value Update-Snippets
Write-Verbose -Verbose:$Verbose -Message "Set-Alias $alias"
$Verbose = $VerboseSwitch
return "Call ``snipup`` or ``Update-Snippets`` to update from GitHub."