-
Notifications
You must be signed in to change notification settings - Fork 0
/
github2choco.psm1
314 lines (261 loc) · 10.6 KB
/
github2choco.psm1
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# load all the helpers
Get-ChildItem $PSScriptRoot/Helpers | ForEach-Object {. "$PSScriptRoot/Helpers/$_"}
# load all the pakcage Writers
Get-ChildItem $PSScriptRoot/PackageWriters | ForEach-Object {. "$PSScriptRoot/PackageWriters/$_"}
function Update-GTCPackage {
<#
.SYNOPSIS
Update a choco package
.DESCRIPTION
Update a package that is in the `profile.json` and return whether the package is updated
.OUTPUTS
A boolean value indicate whether the package is updated
.PARAMETER packageName
The name (id) of the package, it is the keys in `profile.json`
.PARAMETER Force
Whether to force execute the update.
Normal update stop when the remote version matches the local version,
but a force update will update the package to the latest release regardless of the version number
Notice if this parameter is applied, the output of this cmdlet will always be $true
.EXAMPLE
PS C:\> Update-GTCPackage you-get
update the package with name 'you-get' (if local is already on the latest release, this will just exit)
.EXAMPLE
PS C:\> Update-GTCPackage you-get -Force
update the package with name 'you-get' to the latest release regardless of the version number
.NOTES
This will only write the latest version, so it is possible that you may miss versions.
For example if your local version is on 1.0,
and on github there is 2.0 and 3.0, this cmdlet will update the package to 3.0 and miss 2.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string] $packageName,
[Parameter(Mandatory = $false)]
[switch] $Force
)
begin
{
$profile = Read-GTCProfile
}
process
{
# regular log
Write-Host ''
Write-Host ''
# verbose log
Write-Verbose "updating Package $packageName"
Write-Verbose "the package Type is: $($profile.$packageName.packageType)"
Write-Verbose "the package Local Version is: $($profile.$packageName.version)"
Write-Verbose "the package github repo is: $($profile.$packageName.githubRepo)"
try
{
switch ($profile.$packageName.packageType)
{
'installer' {$packageUpdated = Update-InstallerChocoPackage -packageName $packageName -Force $Force -ErrorAction Stop}
'vsix' {$packageUpdated = Update-VsixChocoPackage -packageName $packageName -Force $Force -ErrorAction Stop}
'webFile' {$packageUpdated = Update-WebFileChocoPackage -packageName $packageName -Force $Force -ErrorAction Stop}
'zip' {$packageUpdated = Update-ZipChocoPackage -packageName $packageName -Force $Force -ErrorAction Stop}
Default {Write-Error "Package type not valid for $packageName"}
}
}
catch
{
Write-Host ""
Write-Host "the following Error encounterd while updating $packageName :" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
$packageUpdated = $false
Write-Host "package update fail, see more info using parameter verbose" -ForegroundColor Yellow
}
}
end
{
return $packageUpdated
}
}
function Update-AllGTCPackage {
<#
.SYNOPSIS
Update all the choco package you created
.DESCRIPTION
Update all the package inside `profile.json` and give you a list of package name of the package that is updated
.PARAMETER Force
Whether to force execute the update for all package.
See the doc for `Update-GTCPackage` for more detail
.OUTPUTS
A list of package names of the package that has been updated
.EXAMPLE
PS C:\> Update-AllChocoPackage
This will just update all the choco package that is in your profile
.NOTES
This just goes through the profile and invoke `Update-GTCPackage` on each one.
Therefore reading the doc on `Update-GTCPackage` may be helpfull
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[switch] $Force
)
begin
{
$profile = Read-GTCProfile
$packageNames = $profile | Get-Member -MemberType NoteProperty | ForEach-Object {$_.Name}
}
process
{
# a list contain all the name of the updated packages
$UpdatedPackagesName = @()
# force execute
if ($Force)
{
foreach ($packageName in $packageNames)
{
#update package
$packageUpdated = Update-GTCPackage -packageName $packageName -Force
# add to the updated packages if the package is updated
if ($packageUpdated)
{
$UpdatedPackagesName += $packageName
}
}
}
# not force execute
else
{
foreach ($packageName in $packageNames)
{
#update package
$packageUpdated = Update-GTCPackage -packageName $packageName
# add to the updated packages if the package is updated
if ($packageUpdated)
{
$UpdatedPackagesName += $packageName
}
}
}
}
end
{
# tell the upstream which is updated
return $UpdatedPackagesName
}
}
function New-GTCPackage
{
[CmdletBinding(DefaultParameterSetName = 'general')]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $githubRepo,
[Parameter(Mandatory = $true, Position = 1)]
[string] $packageType,
[Parameter(Mandatory = $false)]
[string] $packageName,
[Parameter(Mandatory = $false)]
[string] $packagePath,
[Parameter(Mandatory = $false)]
[string] $templatePath,
[Parameter(Mandatory = $false)]
[string] $Regex32Bit,
[Parameter(Mandatory = $false)]
[string] $Regex64Bit,
[Parameter(Mandatory = $false, ParameterSetName = 'zip')]
[switch] $isSourceCode,
[Parameter(Mandatory = $false, ParameterSetName = 'installer')]
[string] $installerType,
[Parameter(Mandatory = $false, ParameterSetName = 'installer')]
[string] $silentArg
)
begin
{
$GTCProfile = Read-GTCProfile
$GTCProfileLocation = Get-GTCProfileLocation
$Owner, $RepoName = Split-GithubRepoName -GithubRepo $githubRepo
}
process
{
try
{
###### finish the profile setup ########
# get package name
if (-Not ($packageName))
{
$packageName = $RepoName
Write-Verbose "package name not provided using the repo name: $RepoName as the package name"
}
# get package path
if (-Not ($packagePath))
{
$packagePath = Join-Path -Path $(Get-GTCPackagePath) -ChildPath "$packageName-choco"
Write-Verbose "package path not provided using the Default:"
Write-Verbose $packagePath
}
else
{
$packagePath = $(Resolve-Path -Path $packagePath).Path
}
# get template path
if ( -Not ($templatePath))
{
$templatePath = Join-Path -Path $packagePath -ChildPath 'template'
Write-Verbose "template path not provided using the Default:"
Write-Verbose $templatePath
}
else
{
$templatePath = $(Resolve-Path -Path $templatePath).Path
}
New-ProfileItem -githubRepo $githubRepo -packageType $packageType `
-packageName $packageName -packagePath $packagePath `
-templatePath $templatePath -Regex32Bit $Regex32Bit -Regex64Bit $Regex64Bit `
-isSourceCode $isSourceCode -silentArg $silentArg -installerType $installerType -ErrorAction Stop
##### finish the new template #######
# get the path name and the folder name
$templateParentPath = Split-Path $templatePath -Parent
$templateFolderName = Split-Path $templatePath -Leaf
# create the package and template path
Write-Verbose "creating package path and the parent folder of templatePath"
if ($packagePath -ne $templateParentPath)
{
New-Item -Path $packagePath -ItemType Directory | Out-Null
New-Item -Path $templateParentPath -ItemType Directory | Out-Null
}
else
{
New-Item -Path $packagePath -ItemType Directory | Out-Null
}
# create the template folder
$CurrentLocation = Get-Location
Set-Location $templateParentPath
Write-Verbose "change directory to $templateParentPath"
Write-Verbose "starts to run `choco new` command"
choco.exe new $packageName | Out-Null
Write-Verbose "renaming the folder $templateParentPath\$packageName to $templatePath"
Rename-Item -LiteralPath "$templateParentPath\$packageName" -NewName $templateFolderName
Write-Verbose "change the directory back to $CurrentLocation"
Set-Location $CurrentLocation
Write-Host "the template folder is sucessfully created" -ForegroundColor Green
# complete the nuspec file
Complete-NuspecTemplateFile `
-NuspecFilePath "$templatePath/$packageName.nuspec" -GithubRepo $githubRepo -packageName $packageName
Write-Host "the nuspec file is completed" -ForegroundColor Green
### end ###
Write-Host "the new package creation is completed" -ForegroundColor Green
Write-Warning "Please go to $templatePath to make sure the template is okay."
Write-Warning "Please go to $GTCProfileLocation to make sure profile is okay."
Write-Host "if both is okay for you, run `Update-AllChocoPackage` command to update your new package"
}
catch
{
Write-Host ""
Write-Host "the following Error encounterd while creating package $packageName :" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Write-Host "package creation fail, see more info using parameter verbose" -ForegroundColor Yellow
return
}
}
end
{
}
}
Export-ModuleMember -Function *