Skip to content

Commit 7ea48ff

Browse files
authored
Merge pull request #10 from guitarrapc/fix/bug_installapp
fix: bug update app only run for 1st app
2 parents 267c44f + 70a581f commit 7ea48ff

File tree

3 files changed

+147
-145
lines changed

3 files changed

+147
-145
lines changed

src/ScoopPlaybook.psm1

Lines changed: 119 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,10 @@ function RuntimeCheck {
116116
$package = $state.ToString().Split(":")[0].Trim()
117117
$script:updatablePackages.Add($package)
118118
PrintChanged -Message " [!] chck: [scoop-status: (updatable) $package]"
119-
$updateSection = $false
120119
}
121120
elseif ($removeSection) {
122121
$package = $state.ToString().Trim()
123122
PrintChanged -Message " [!] info: [scoop-status: (removable) $package]"
124-
$removeSection = $false
125123
}
126124
else {
127125
PrintInfo -Message " [o] info: [scoop-status: $state]"
@@ -137,31 +135,64 @@ function RuntimeCheck {
137135
}
138136
}
139137

140-
function RunMain {
138+
function VerifyYaml {
141139
[CmdletBinding()]
142140
[OutputType([void])]
143141
param(
144-
[string]$BaseYaml = "site.yml",
145-
[RunMode]$Mode = [RunMode]::run
142+
[string]$BaseYaml = "site.yml"
146143
)
147144

145+
Write-Verbose "Validate YAML format."
146+
148147
# Verify Playbook exists
149148
if (!(Test-Path $BaseYaml)) {
150149
throw [System.IO.FileNotFoundException]::New("File not found. $BaseYaml")
151150
}
152151
# Verify Playbook is not empty
153-
$basePath = [System.IO.Path]::GetDirectoryName($BaseYaml)
154152
$definitions = Get-Content -LiteralPath $BaseYaml -Raw | ConvertFrom-Yaml
155153
if ($null -eq $definitions) {
156-
PrintInfo -Message "Nothing definied in $BaseYaml"
157-
return
154+
throw [System.FormatException]::New("Playbook format invalid. $BaseYaml is empty.")
158155
}
159156
# Verify Playbook contains roles section
160157
if ($null -eq $definitions[$([PlaybookKeys]::roles.ToString())]) {
161-
PrintInfo -Message "No roles definied in $BaseYaml"
162-
return
158+
throw [System.FormatException]::New("Playbook format invalid. $BaseYaml is empty.")
159+
}
160+
161+
$basePath = [System.IO.Path]::GetDirectoryName($BaseYaml)
162+
163+
# Verify role yaml is valid
164+
$roles = @($definitions[$([PlaybookKeys]::roles.ToString())])
165+
foreach ($role in $roles) {
166+
$taskPath = "$basePath/roles/$role/tasks/"
167+
$tasks = Get-ChildItem -LiteralPath "$taskPath" -File | Where-Object { $_.Extension -in @(".yml", ".yaml") }
168+
if ($null -eq $tasks) {
169+
PrintWarning -Message "No task file found in $taskPath"
170+
continue
171+
}
172+
173+
foreach ($task in $tasks.FullName) {
174+
$taskDef = Get-Content -LiteralPath "$task" -Raw | ConvertFrom-Yaml
175+
if ($null -eq $taskDef) {
176+
PrintWarning -Message "No valid task definied in $task"
177+
continue
178+
}
179+
}
163180
}
164181

182+
Write-Verbose "Validation passed. YAML format is valid."
183+
}
184+
185+
function RunMain {
186+
[CmdletBinding()]
187+
[OutputType([void])]
188+
param(
189+
[string]$BaseYaml = "site.yml",
190+
[RunMode]$Mode = [RunMode]::run
191+
)
192+
193+
$definitions = Get-Content -LiteralPath $BaseYaml -Raw | ConvertFrom-Yaml
194+
$basePath = [System.IO.Path]::GetDirectoryName($BaseYaml)
195+
165196
# Header
166197
$playbookName = $definitions[$([PlaybookKeys]::name.ToString())]
167198
$header = "PLAY [$playbookName]"
@@ -172,8 +203,9 @@ function RunMain {
172203
# Handle each role
173204
$roles = @($definitions[$([PlaybookKeys]::roles.ToString())])
174205
foreach ($role in $roles) {
175-
Write-Verbose "Checking role definition from [$basePath/roles/$role/tasks/]"
176-
$tasks = Get-ChildItem -LiteralPath "$basePath/roles/$role/tasks/" -File | Where-Object { $_.Extension -in @(".yml", ".yaml") }
206+
$taskPath = "$basePath/roles/$role/tasks/"
207+
Write-Verbose "Checking role definition from [$taskPath]"
208+
$tasks = Get-ChildItem -LiteralPath "$taskPath" -File | Where-Object { $_.Extension -in @(".yml", ".yaml") }
177209
if ($null -eq $tasks) {
178210
continue
179211
}
@@ -183,7 +215,6 @@ function RunMain {
183215
Write-Verbose "Read from [$task]"
184216
$taskDef = Get-Content -LiteralPath $task -Raw | ConvertFrom-Yaml
185217
if ($null -eq $taskDef) {
186-
Write-Verbose "No valid task definied in $task"
187218
continue
188219
}
189220

@@ -193,10 +224,10 @@ function RunMain {
193224
$module.Remove($([ModuleParams]::name.ToString()))
194225

195226
# check which module
196-
$containsInstall = $module.Contains([Modules]::scoop_install.ToString())
227+
$containsAppInstall = $module.Contains([Modules]::scoop_install.ToString())
197228
$containsBucketInstall = $module.Contains([Modules]::scoop_bucket_install.ToString())
198229

199-
if ($containsInstall) {
230+
if ($containsAppInstall) {
200231
# handle scoop_install
201232
$tag = [Modules]::scoop_install
202233
if ([string]::IsNullOrWhiteSpace($name)) {
@@ -205,7 +236,7 @@ function RunMain {
205236
$header = "TASK [$role : $name]"
206237
$marker = "*" * (CalulateSeparator -Message "$header ")
207238
PrintInfo -Message "$header $marker"
208-
ScoopModuleStateHandler -Module $module -Tag $tag -Mode $Mode
239+
ScoopAppStateHandler -Module $module -Tag $tag -Mode $Mode
209240
}
210241
elseif ($containsBucketInstall) {
211242
# handle scoop_bucket_install
@@ -289,7 +320,7 @@ function ScoopBucketStateHandler {
289320
}
290321
}
291322

292-
function ScoopModuleStateHandler {
323+
function ScoopAppStateHandler {
293324
[CmdletBinding()]
294325
[OutputType([void])]
295326
param(
@@ -328,10 +359,10 @@ function ScoopModuleStateHandler {
328359
$tools = $moduleDetail[$([ModuleElement]::name.ToString())]
329360
switch ($state) {
330361
$([StateElement]::present) {
331-
ScoopInstall -Tools $tools -Tag $Tag -DryRun $dryRun
362+
ScoopAppInstall -Tools $tools -Tag $Tag -DryRun $dryRun
332363
}
333364
$([StateElement]::absent) {
334-
ScoopUninstall -Tools $tools -Tag $Tag -DryRun $dryRun
365+
ScoopAppUninstall -Tools $tools -Tag $Tag -DryRun $dryRun
335366
}
336367
}
337368
}
@@ -362,15 +393,16 @@ function ScoopBucketInstall {
362393
[bool]$DryRun
363394
)
364395

396+
$prefix = "chng"
397+
if ($DryRun) {
398+
$prefix = "chck"
399+
}
400+
365401
if (!(ScoopBucketExists -Bucket $Bucket)) {
366-
if ($DryRun) {
367-
PrintCheck -Message " [!] chck: [${Tag}: $Bucket] => $Source (installed: $false)"
368-
}
369-
else {
370-
PrintChanged -Message " [!] chng: [${Tag}: $Bucket] => $Source (installed: $false)"
371-
PrintSpace
372-
scoop bucket add $Bucket $Source
373-
}
402+
PrintChanged -Message " [!] ${prefix}: [${Tag}: $Bucket] => $Source (installed: $false)"
403+
if ($DryRun) { continue }
404+
PrintSpace
405+
scoop bucket add "$Bucket" "$Source"
374406
}
375407
else {
376408
PrintSkip -Message " [o] skip: [${Tag}: $Bucket]"
@@ -389,22 +421,23 @@ function ScoopBucketUninstall {
389421
[bool]$DryRun
390422
)
391423

424+
$prefix = "chng"
425+
if ($DryRun) {
426+
$prefix = "chck"
427+
}
428+
392429
if (ScoopBucketExists -Bucket $Bucket) {
393-
if ($DryRun) {
394-
PrintCheck -Message " [!] chck: [${Tag}: $Bucket] (installed: $false)"
395-
}
396-
else {
397-
PrintChanged -Message " [!] chng: [${Tag}: $Bucket] (installed: $false)"
398-
PrintSpace
399-
scoop bucket rm $Bucket
400-
}
430+
PrintChanged -Message " [!] ${prefix}: [${Tag}: $Bucket] (installed: $false)"
431+
if ($DryRun) { continue }
432+
PrintSpace
433+
scoop bucket rm $Bucket
401434
}
402435
else {
403436
PrintSkip -Message " [o] skip: [${Tag}: $Bucket]"
404437
}
405438
}
406439

407-
function ScoopInstall {
440+
function ScoopAppInstall {
408441
[CmdletBinding()]
409442
[OutputType([void])]
410443
param(
@@ -416,80 +449,56 @@ function ScoopInstall {
416449
[bool]$DryRun
417450
)
418451

452+
$prefix = "chng"
453+
if ($DryRun) {
454+
$prefix = "chck"
455+
}
456+
419457
foreach ($tool in $Tools) {
420-
if ($DryRun) {
421-
$output = scoop info $tool *>&1
422-
# may be typo manifest should throw fast
423-
if ($output -match "Could not find manifest for") {
424-
PrintFail -Message " [x] fail: [${Tag}: $tool] => $($output)"
425-
throw "ACTION: please make sure your desired manifest '$tool' is available."
426-
}
427-
# successfully found manifest
428-
$installed = $output | Select-String -Pattern "Installed:"
429-
if ($installed.Line -match "no") {
430-
PrintCheck -Message " [!] chck: [${Tag}: $tool] => $($installed.Line)"
431-
}
432-
else {
433-
$outputStrict = scoop list $tool *>&1
434-
$installedStrictCheck = $outputStrict | Select-String -Pattern "failed"
435-
if ($null -ne $installedStrictCheck) {
436-
# previous installation was interupped
437-
PrintCheck -Message " [!] chck: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2) (installed: $false, Failed previous installation, begin reinstall.)"
438-
}
439-
else {
440-
$isUpdatable = $updatablePackages -contains $tool
441-
if (!$isUpdatable) {
442-
PrintSkip -Message " [o] skip: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2)"
443-
}
444-
else {
445-
PrintCheck -Message " [!] chck: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2) (updatable: $isUpdatable)"
446-
}
447-
Write-Verbose "$($installed.Line)$($output[$installed.LineNumber++])"
448-
}
449-
}
458+
$output = scoop info $tool *>&1
459+
# may be typo manifest should throw fast
460+
if ($output -match "Could not find manifest for") {
461+
PrintFail -Message " [x] fail: [${Tag}: $tool] => $($output)"
462+
throw "ACTION: please make sure your desired manifest '$tool' is available."
463+
}
464+
# successfully found manifest
465+
$installed = $output | Select-String -Pattern "Installed:"
466+
if ($installed.Line -match "no") {
467+
PrintChanged -Message " [!] ${prefix}: [${Tag}: $tool] => $($installed.Line)"
468+
if ($DryRun) { continue }
469+
PrintSpace
470+
scoop install $tool
450471
}
451472
else {
452-
$output = scoop info $tool *>&1
453-
# may be typo manifest should throw fast
454-
if ($output -match "Could not find manifest for") {
455-
PrintFail -Message " [x] fail: [${Tag}: $tool] => $($output)"
456-
throw "ACTION: please make sure your desired manifest '$tool' is available."
457-
}
458-
# successfully found manifest
459-
$installed = $output | Select-String -Pattern "Installed:"
460-
if ($installed.Line -match "no") {
461-
PrintChanged -Message " [!] chng: [${Tag}: $tool] => $($installed.Line)"
473+
$outputStrict = scoop list $tool *>&1
474+
$installedStrictCheck = $outputStrict | Select-String -Pattern "failed"
475+
if ($null -ne $installedStrictCheck) {
476+
# previous installation was interupped
477+
PrintChanged -Message " [!] ${prefix}: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2) (installed: $false, Failed previous installation, begin reinstall.)"
478+
if ($DryRun) { continue }
479+
PrintSpace
480+
scoop uninstall $tool
462481
PrintSpace
463482
scoop install $tool
464483
}
465484
else {
466-
$outputStrict = scoop list $tool *>&1
467-
$installedStrictCheck = $outputStrict | Select-String -Pattern "failed"
468-
if ($null -ne $installedStrictCheck) {
469-
# previous installation was interupped
470-
PrintChanged -Message " [!] chng: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2) (installed: $false, Failed previous installation, begin reinstall.)"
471-
PrintSpace
472-
scoop uninstall $tool
473-
PrintSpace
474-
scoop install $tool
485+
$updatablePackages | ForEach-Object { Write-Verbose "$_" }
486+
$isUpdatable = $updatablePackages -contains $tool
487+
if (!$isUpdatable) {
488+
PrintSkip -Message " [o] skip: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2)"
475489
}
476490
else {
477-
$isUpdatable = $updatablePackages -contains $tool
478-
if (!$isUpdatable) {
479-
PrintSkip -Message " [o] skip: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2)"
480-
}
481-
else {
482-
PrintChanged -Message " [!] chng: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2) (updatable: $isUpdatable)"
483-
PrintSpace
484-
scoop update $tool *>&1 | ForEach-Object { PrintInfo -Message $_ }
485-
}
491+
PrintChanged -Message " [!] ${prefix}: [${Tag}: $tool] => $($outputStrict | Select-Object -Skip 1 -First 2) (updatable: $isUpdatable)"
492+
if ($DryRun) { continue }
493+
PrintSpace
494+
scoop update $tool *>&1 | ForEach-Object { PrintInfo -Message $_ }
486495
}
487496
}
488497
}
489498
}
490499
}
491500

492-
function ScoopUninstall {
501+
function ScoopAppUninstall {
493502
[OutputType([void])]
494503
param(
495504
[Parameter(Mandatory = $true)]
@@ -506,29 +515,23 @@ function ScoopUninstall {
506515
continue
507516
}
508517

518+
$prefix = "chng"
519+
if ($DryRun) {
520+
$prefix = "chck"
521+
}
522+
509523
foreach ($tool in $tools) {
510-
if ($DryRun) {
511-
$output = scoop info $tool
512-
$installed = $output | Select-String -Pattern "Installed:"
513-
if ($installed.Line -match "no") {
514-
PrintSkip -Message " [o] skip: [${Tag}: $tool] => Already uninstalled"
515-
Write-Verbose $installed.Line
516-
}
517-
else {
518-
PrintCheck -Message " [!] chck: [${Tag}: $tool] => Require uninstall"
519-
PrintCheck -Message "$($installed.Line)$($output[++$installed.LineNumber])"
520-
}
524+
$output = scoop info $tool
525+
$installed = $output | Select-String -Pattern "Installed:"
526+
if ($installed.Line -match "no") {
527+
PrintSkip -Message " [o] skip: [${Tag}: $tool] => Already uninstalled"
528+
Write-Verbose $installed.Line
521529
}
522530
else {
523-
$output = scoop info $tool
524-
$installed = $output | Select-String -Pattern "Installed:"
525-
if ($installed.Line -match "no") {
526-
PrintSkip -Message " [o] skip: [${Tag}: $tool] => Already uninstalled"
527-
}
528-
else {
529-
PrintChanged -Message " [!] chng: [${Tag}: $tool] => Require uninstall"
530-
scoop uninstall $tool
531-
}
531+
PrintChanged -Message " [!] ${prefix}: [${Tag}: $tool] => Require uninstall"
532+
Write-Verbose $installed.Line
533+
if ($DryRun) { continue }
534+
scoop uninstall $tool
532535
}
533536
}
534537
}
@@ -577,6 +580,7 @@ function Invoke-ScoopPlaybook {
577580

578581
# run
579582
try {
583+
VerifyYaml -BaseYaml $LiteralPath
580584
RunMain -BaseYaml $LiteralPath -Mode $Mode
581585
}
582586
catch [Exception] {

0 commit comments

Comments
 (0)