-
Notifications
You must be signed in to change notification settings - Fork 0
/
archiveDownloader.ps1
96 lines (70 loc) · 2.96 KB
/
archiveDownloader.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
<#
Archive downloader for GitHub - Created by w33zl (Open Modding Alliance)
USAGE:
> powershell -command "& {Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/open-modding-alliance/install/master/fsScriptLibrary.ps1' -OutFile 'fsScriptLibrary.ps1'; ./fsScriptLibrary.ps1}"
#>
#NOTE: the following five lines should be put in the top of the archive specific script
###[ USER SETTINGS ]###########################################################
# $appName = "Your Appliction"
# $githubUser = "open-modding-alliance"
# $githubRepo = "install"
###############################################################################
###[ SCRIPT CONFIG ]###########################################################
$outputFolder = ".\temp"
$tempFolder = ".\.oma"
$tempArchivePath = "$tempFolder\archive.zip"
$latestReleaseUrl = "https://api.github.com/repos/$githubUser/$githubRepo/releases/latest"
###############################################################################
try {
$response = Invoke-RestMethod -Uri $latestReleaseUrl
$downloadUrl = $response.assets[0].browser_download_url
$manualDownloadUrl = "https://github.com/$githubUser/$githubRepo/releases/latest"
}
catch {
Write-Host "Failed to get latest release, you need to manually download it from $manualDownloadUrl"
exit 1
}
# Write-Host "Repo url: $downloadUrl"
if (!(Test-Path $tempFolder)) {
New-Item -Path $tempFolder -ItemType Directory
}
Set-ItemProperty -Path $tempFolder -Name Attributes -Value ([System.IO.FileAttributes]::Hidden)
Write-Host "`nDownloading archive from $downloadUrl"
try {
$oldProgressPreference = $progressPreference
$ProgressPreference = 'SilentlyContinue' # Subsequent calls do not display UI.
Invoke-WebRequest -Uri $downloadUrl -OutFile $tempArchivePath
}
catch {
<#Do this if a terminating exception happens#>
Write-Host "Failed to download file from $downloadUrl"
}
finally {
$ProgressPreference = $oldProgressPreference # Subsequent calls do display UI.
<#Do this after the try block regardless of whether an exception occurred or not#>
}
$success = 0
if ((Test-Path $tempArchivePath)) {
Write-Host "Extracting archive to $outputFolder"
try {
Expand-Archive -Path $tempArchivePath -DestinationPath $outputFolder -ErrorAction Inquire # Stop
Write-Host "Archive '$tempArchivePath' successfully extracted"
$success = 1
}
catch {
Write-Host "Failed to extract archive, make sure the folder doesn't already contains the '$appName'"
}
Remove-Item $tempArchivePath
if ((Get-ChildItem $tempFolder | Measure-Object).Count -eq 0) {
Remove-Item $tempFolder -Force -Recurse
} else {
Write-Host "Temp folder not empty, leaving it there"
}
}
if ($success -eq 0) {
Write-Host "Could not install $appName, please download it manually from $manualDownloadUrl"
exit 1
} else {
Write-Host "Successfully installed $appName"
exit 0
}