...
1function newTemporaryDirectory {
2 $parent = [System.IO.Path]::GetTempPath()
3 [string] $name = [System.Guid]::NewGuid()
4 New-Item -ItemType Directory -Path (Join-Path $parent $name)
5}
6
7function updateVendor {
8 param (
9 [string]$path
10 )
11 $currentPath = (Get-Location).Path
12 Set-Location $path
13 go mod vendor
14 go mod tidy
15 Set-Location $currentPath
16}
17
18function matchingHashes {
19 param (
20 [string]$rootPath,
21 [string]$tempPath
22 )
23 $rootHashes = Get-ChildItem -Recurse -Path $rootPath | foreach {Get-FileHash -Path $_.FullName -Algorithm SHA256}
24 if (-not $?) {
25 return $false
26 }
27 $tempHashes = Get-ChildItem -Recurse -Path $tempPath | foreach {Get-FileHash -Path $_.FullName -Algorithm SHA256}
28 if (-not $?) {
29 return $false
30 }
31 $diff = Compare-Object -ReferenceObject $rootHashes.Hash -DifferenceObject $tempHashes.Hash
32 if ($diff.Count -eq 0) {
33 return $true
34 }
35 return $false
36}
37
38$rootPath = $args[0]
39$subdir = $args[1]
40
41$tempDir = newTemporaryDirectory
42Copy-Item -Path $rootPath/* -Destination $tempDir -Recurse
43if (-not $?) {
44 Remove-Item $tempDir -Recurse
45 exit 1
46}
47
48updateVendor $tempDir/$subdir
49if (-not $?) {
50 Remove-Item $tempDir -Recurse
51 exit 1
52}
53
54$hashesMatch = matchingHashes $rootPath/$subdir $tempDir/$subdir
55if (-not $?) {
56 Remove-Item $tempDir -Recurse
57 exit 1
58}
59
60Remove-Item $tempDir -Recurse
61if ($hashesMatch -ne $true) {
62 exit 1
63}
View as plain text