-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppMaker_CLI.ps1
More file actions
250 lines (214 loc) · 7.19 KB
/
AppMaker_CLI.ps1
File metadata and controls
250 lines (214 loc) · 7.19 KB
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
Clear-Host
function promptMessage($message, $type){
switch($type)
{
'error' { Write-Host $message -ForegroundColor Red }
'warning' { Write-Host $message -ForegroundColor Yellow }
'success' { Write-Host $message -ForegroundColor Green }
default { Write-Host $message }
}
}
# Logging
$logPath = "$PSScriptRoot" # Log file folder in same location as the script
$logFileName = "AppMaker.log" # Log filename
$logFile = "$logPath\$logFileName" # Combine LPath and LFile to make one full path
if(!(Test-Path $logFile)){
New-Item $logFile -Type file | Out-Null
}
function LogWrite {
Param ([string]$logstring)
$logTime = Get-Date -format "yyyy-MM-dd HH:mm:ss"
$logText = $logTime + " - " + $logstring
Add-content $logFile -value $logText
}
# Setup vars
$buildLocation = "$PSSCRIPTROOT\DropFilesHere"
$outputLocation = "$PSSCRIPTROOT\COMPLETE"
$templateLocation ="$PSSCRIPTROOT\bin"
# Create folders if not already exists
if(!(Test-Path $buildLocation)){
New-Item $buildLocation -Type Directory | Out-Null
}
if(!(Test-Path $outputLocation)){
New-Item $outputLocation -Type Directory | Out-Null
}
Write-Host "===== AppMaker ====="
Write-Host " "
LogWrite "AppMaker started!"
# Select installer if multiple
$validFiles = @(Get-ChildItem "$($buildLocation)\*" -Include *.ps1,*.vbs,*.bat,*.exe,*.msi)
$validFilesCount = 0
$installerCount = 0
foreach ($validFile in $validFiles){
Write-Host "[$($validFilesCount)] - $($validFile.name)"
$validFilesCount++
}
if($validFilesCount -gt 0){
while(!($installerID)){
Write-Host " "
$installerID = Read-Host 'Choose your installer'
}
# Set installer
$appInstaller = $validFiles[$installerID].name
$appExt = $validFiles[0].Extension
LogWrite "Selected $($validFiles[$installerID])!"
}
# Check if installer is existing at all
if($appInstaller -and $installerCount -lt 2) {
$appName = Read-Host 'App name? '
$appVersion = Read-Host 'App version? '
Clear-Host
Write-Host "===== AppMaker ====="
Write-Host " "
while(!($appName)){
$appName = Read-Host 'App name?'
}
while(!($appVersion)){
$appVersion = Read-Host 'App version?'
}
# Set installer type
if($appExt){
LogWrite "Found extension $appExt, setting installer type."
switch($appExt)
{
'.ps1' {
$appInstType = "powershell.exe"
$appInstParam = "-file $appInstaller"
}
'.vbs' {
$appInstType = "cscript.exe"
$appInstParam = "$appInstaller"
}
'.msi' {
$appInstType = "msiexec.exe"
$appInstOption = Read-Host 'Enter MSI options (example /silent /s)'
$appInstParam = "/i $appInstaller $appInstOption"
}
'.exe' {
$appInstType = "$appInstaller"
$appInstOption = Read-Host 'Enter EXE options (example /silent /s)'
$appInstParam = "$appInstOption"
}
'.bat' {
$appInstType = "cmd.exe"
$appInstParam = "/c $appInstaller"
}
'.bat' {
$appInstType = "cmd.exe"
$appInstParam = "/c $appInstaller"
}
}
} else {
LogWrite "No file extension detected, exiting..."
Break
}
Clear-Host
Write-Host "===== AppMaker ====="
Write-Host " "
promptMessage "App name: $($appName)"
LogWrite "App name: $($appName)"
promptMessage "App version: $($appVersion)"
LogWrite "App version: $($appVersion)"
if($appInstOption){
promptMessage "App options: $($appInstOption)"
LogWrite "App options: $($appInstOption)"
}
Write-Host " "
$continueMW = Read-Host 'Continue? [Y/N]'
} else {
# Check if multiple installers exists
if($installerCount -gt 1){
promptMessage "Multiple installers found! We only need one..." "warning"
promptMessage "Remove unwanted installers!" "warning"
LogWrite "Multiple installers found! We only need one..."
LogWrite "Remove unwanted installers!"
Write-Host " "
} else {
promptMessage "No valid installer found!" "warning"
LogWrite "No valid installer found!"
Write-Host " "
}
}
if($continueMW -contains "Y"){
Clear-Host
Write-Host "===== AppMaker ====="
Write-Host " "
promptMessage "Please wait while creating the EXE..." "success"
LogWrite "Preparing job"
Write-Host " "
# Get input for package
$applicationName = $appName.replace(" ","_")
$applicationVersion = $appVersion
$date = Get-Date -format "yyyy-MM-dd"
# Set folder for "complete"
$applicationPath = "$outputLocation\$applicationName\$applicationVersion - $date"
if(!(Test-path $applicationPath)){
LogWrite "Creating output folder - $applicationPath"
New-Item $applicationPath -Type Directory | Out-Null
} else {
LogWrite "Output folder already exists - $applicationPath"
}
################################## BUILD ########################################################################################
$date = Get-Date -format "yyyy-MM-dd"
$folderLoc = $buildLocation
if(!(Test-path $folderLoc)){
New-Item "$folderLoc" -Type Directory | Out-Null
}
# Copy config for EXE installer to build folder
$config = @"
;!@Install@!UTF-8!
Title="$applicationName"
Progress="no"
ExecuteFile="$appInstType"
ExecuteParameters="$appInstParam"
;!@InstallEnd@!
"@
$config | Out-File -encoding ascii "$folderLoc\config.txt"
LogWrite "Creating config - $folderLoc\config.txt"
# Copy required sfx file to build folder
LogWrite "Copy sfx - $folderLoc\7zS.sfx"
Copy-Item "$templateLocation\7zS.sfx" -Destination "$folderLoc\7zS.sfx"
# Compress raw installer files
LogWrite "Compressing files"
if (-not (test-path "$templateLocation\7-Zip\7z.exe")) {throw "$templateLocation\7-Zip\7z.exe needed"}
set-alias sz "$templateLocation\7-Zip\7z.exe"
$zipFile = "temp.7z"
sz a -r "$folderLoc\$zipFile" "$folderLoc\*.*" | Out-Null
# Create EXE installer from compressed archive
LogWrite "Creating EXE file"
$command = @'
cmd.exe /C copy /b "$folderLoc\7zS.sfx" + "$folderLoc\config.txt" + "$folderLoc\$zipFile" "$folderLoc\$($applicationName)_$($applicationVersion).exe"
'@
Invoke-Expression -Command:$command | Out-Null
# Remove unnecessary files
if(Test-Path "$folderLoc\$($applicationName)_$($applicationVersion).exe"){
LogWrite "Removing unwanted files"
Get-ChildItem -Path "$folderLoc" -Recurse -exclude "$($applicationName)_$($applicationVersion).exe" | Remove-Item -force -recurse
LogWrite "Moving EXE to output folders"
Move-Item "$folderLoc\$($applicationName)_$($applicationVersion).exe" -Destination "$applicationPath\$($applicationName)_$($applicationVersion).exe"
}
#################################################################################################################################
# If build is success prompt and exit..
if(Test-path "$applicationPath\$($applicationName)_$($applicationVersion).exe"){
Clear-Host
Write-Host "===== AppMaker ====="
Write-Host " "
promptMessage "The app $applicationName was created successfully!" "success"
LogWrite "The app $applicationName was created successfully!"
LogWrite "=================================================="
Write-Host " "
exit
} else {
Clear-Host
Write-Host "===== AppMaker ====="
Write-Host " "
promptMessage "ERROR! Something went wrong with the build - $applicationName could not be created!" "error"
LogWrite "ERROR! Something went wrong with the build - $applicationName could not be created!"
Write-Host " "
exit
}
} else {
promptMessage "Exiting..." "warning"
LogWrite "Exiting..."
Write-Host " "
}