forked from Araxeus/Youtube-Volume-Scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.ps1
142 lines (130 loc) · 6.49 KB
/
zip.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
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env pwsh
#Requires -Version 7
# Specifying no parameter will result in current working directory ($pwd) being archived into $pwd\$pwd.zip
param (
# The following paths can be relative or absolute:
# path to folder/s containing the files to be archived
[Alias("i","input","from")][string[]][ValidateScript({ Test-Path -LiteralPath $_ })] $FolderPaths = @($PWD),
# path to zipfile / zipFolder if $ZipNameFromJson is specified (will be created if it doesn't exist)
[Alias("t","to","output")][string] $ZipPath = "", # defaults to $PWD.zip
# set $ZipPath to end with .zip or set this to an empty string to disable this feature
[Alias("j","json")][string][ValidatePattern('.json$')] $ZipNameFromJson = "", # set to a json file containing name and version, output will be $name_v$version.zip
# update scss->css in the directories, leave empty to disable
[Alias("scss")][string[]][ValidateScript({ Test-Path -LiteralPath $_ })] $ScssPaths = @(),
# ie "*.*" to only include files that have a .extension
[Alias("f")][string] $Filter = "",
# filterScript has more options than eclude
[Alias("e")][string[]] $Exclude = @(),
# { ($_.FullName -notlike "*\node_modules\*") -and ($_.Name -notlike "*.scss")}, # ignore .scss and nodeModules folder
[Alias("fs","script")][ScriptBlock] $FilterScript = { $_ },
# overwrite zip (if there is a zip with the same name, delete it creating a new one)
[Alias("o","overwrite")][switch] $OverwriteZip,
# keep sync between folder and zip (doesn"t do anything if OverwriteZip=true) - delete surplus files from zip
[Alias("s")][switch] $Sync,
# pause script when done to allow reading output
[Alias("p","pause")][switch] $PauseOnDone,
# verbose output
[Alias("v")][switch] $Verbose,
# show all parameter and exit
[Alias("h")][switch] $Help,
# default settings for youtube-volume-scroll, applied by default if current folder = 'Youtube-Volume-Scroll'
[Alias("d")][switch] $Default = (Split-Path -Path $PWD -Leaf) -eq 'Youtube-Volume-Scroll'
)
"`n"
if ($Help) {
# $MyInvocation.MyCommand.Source -eq $PSCommandPath
"Available flags:`n" | Write-Host -ForegroundColor Yellow
(Get-Help $PSCommandPath).TrimStart($MyInvocation.MyCommand.Name+' ').replace('] [', '], [').split(', ') | ForEach-Object { "`t$($_)" } | Write-Host -ForegroundColor Cyan
Exit
}
if ($Default) {
$FolderPaths = @("unpacked")
$ZipNameFromJson = "unpacked\manifest.json"
$Exclude = @("*.scss", "_*")
$Sync = $true
$PauseOnDone = $true
$ScssPaths = @("unpacked/popup")
$Verbose = $true
}
$VerbosePreference = $Verbose ? "Continue" : "SilentlyContinue"
if ($ScssPaths.Length -gt 0) {
try { # transform scss to css
sass --update $ScssPaths
} catch {
Write-Error "`n Error when calling sass:`n $($_.Exception.Message)"
}
}
if ($ZipNameFromJson -and !$ZipPath.EndsWith('.zip')) {
$jsonFile = Get-Content $ZipNameFromJson
$jsonObj = $jsonFile | ConvertFrom-Json
$ZipName = "$($jsonObj.name.Trim().Replace(' ', '-'))_v$($jsonObj.version.Trim()).zip"
if ($ZipPath) {
try {
[System.IO.Directory]::CreateDirectory($ZipPath) | Out-Null
$ZipPath = [IO.Path]::Combine($ZipPath, $ZipName)
} catch {
Write-Error("`n Error creating ZipPath:`n $($_.Exception.Message)")
$ZipPath = $ZipName
}
} else {
$ZipPath = $ZipName
}
} elseif (!$ZipPath) {
$ZipPath = [IO.Path]::Combine($PWD, "$(Split-Path -Path $PWD -Leaf).zip")
}
if ($OverwriteZip) {
Remove-item -literalpath $ZipPath -force -ErrorAction SilentlyContinue
}
$AllFiles = New-Object System.Collections.Generic.List[System.Object]
$ChangesCount = 0;
try {
$ZipArchive = [IO.Compression.ZipFile]::Open( $ZipPath, "Update" )
foreach ($FolderPath in $FolderPaths) {
$FileList = (Get-ChildItem -LiteralPath $FolderPath -Filter $Filter -Exclude $Exclude -File -Recurse | Where-Object $FilterScript) #use the -File argument because empty folders can"t be stored
foreach ($File in $FileList) {
if ($File.FullName.endsWith($ZipPath)) { continue }
# get relative path and trim leading .\ from it
$File | Add-Member RelativePath ([System.IO.Path]::GetRelativePath($FolderPath, $File.FullName) -replace "^.\\")
$AllFiles.Add($File)
try { # zip will store multiple copies of the exact same file - prevent this by checking if already archived.
if (!$OverwriteZip) {
$AlreadyArchivedFile = $ZipArchive.GetEntry($File.RelativePath)
# $AlreadyArchivedFile = ($ZipArchive.Entries | Where-Object { $_.FullName -eq $File.RelativePath })
if ($AlreadyArchivedFile) {
if (($AlreadyArchivedFile.Length -eq $File.Length) -and
#ZipFileExtensions timestamps are only precise within 2 seconds.
([math]::Abs(($AlreadyArchivedFile.LastWriteTime.UtcDateTime - $File.LastWriteTimeUtc).Seconds) -le 2)) {
continue
}
$AlreadyArchivedFile.Delete()
}
}
$ZipArchiveEntry = [IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipArchive, $File.FullName, $File.RelativePath, 'Optimal')
$ChangesCount++
Write-Verbose "Archived \$($ZipArchiveEntry.FullName)"
} catch { # single file failed - usually inaccessible or in use
Write-Warning "`n $($File.FullName) could not be archived.`n $($_.Exception.Message)"
}
}
}
if ($Sync -and !$OverwriteZip) {
$UnsyncedFiles = $ZipArchive.Entries | Where-Object -Property FullName -NotIn ($AllFiles | ForEach-Object { $_.RelativePath })
foreach ($File in $UnsyncedFiles) {
try {
$File.Delete()
$ChangesCount++
Write-Verbose "Deleted $($ZipPath)\$($File.FullName)"
} catch {
Write-Warning "$($ZipPath)\$($File.FullName) is not in sync but couldn't be deleted"
}
}
}
} catch { # failure to open the zip file
Write-Error $_.Exception
} finally { # always close the zip file so it can be read later
$ZipArchive.Dispose()
Write-Host "$(Resolve-Path $ZipPath) was succesfully updated ($($ChangesCount) files changed)" -ForegroundColor Green
}
if ($PauseOnDone) {
cmd /c "pause"
}