forked from ZephyrJung/keenwrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.sh
executable file
·281 lines (228 loc) · 8.94 KB
/
installer.sh
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# This script cross-compiles application launchers for different platforms.
#
# The application binaries are self-contained launchers that do not need
# to be installed.
# ---------------------------------------------------------------------------
source $HOME/bin/build-template
readonly APP_NAME=$(find "${SCRIPT_DIR}/src" -type f -name "settings.properties" -exec cat {} \; | grep "application.title=" | cut -d'=' -f2)
readonly FILE_APP_JAR="${APP_NAME}.jar"
readonly OPT_JAVA=$(cat << END_OF_ARGS
--add-opens=javafx.controls/javafx.scene.control=ALL-UNNAMED \
--add-opens=javafx.controls/javafx.scene.control.skin=ALL-UNNAMED \
--add-opens=javafx.graphics/javafx.scene.text=ALL-UNNAMED \
--add-opens=javafx.graphics/com.sun.javafx.css=ALL-UNNAMED \
--add-opens=javafx.graphics/com.sun.javafx.text=ALL-UNNAMED \
--add-exports=javafx.base/com.sun.javafx.event=ALL-UNNAMED \
--add-exports=javafx.graphics/com.sun.javafx.application=ALL-UNNAMED \
--add-exports=javafx.graphics/com.sun.javafx.geom=ALL-UNNAMED \
--add-exports=javafx.graphics/com.sun.javafx.text=ALL-UNNAMED \
--add-exports=javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED \
--add-exports=javafx.graphics/com.sun.javafx.scene.text=ALL-UNNAMED \
--add-exports=javafx.graphics/com.sun.javafx.scene.traversal=ALL-UNNAMED
END_OF_ARGS
)
ARG_JAVA_OS="linux"
ARG_JAVA_ARCH="amd64"
ARG_JAVA_VERSION="20.0.1"
ARG_JAVA_UPDATE="10"
ARG_JAVA_DIR="java"
ARG_DIR_DIST="dist"
FILE_DIST_EXEC="run.sh"
ARG_PATH_DIST_JAR="${SCRIPT_DIR}/build/libs/${FILE_APP_JAR}"
DEPENDENCIES=(
"gradle,https://gradle.org"
"warp-packer,https://github.com/Reisz/warp/releases"
"tar,https://www.gnu.org/software/tar"
"wine,https://www.winehq.org"
"unzip,http://infozip.sourceforge.net"
)
ARGUMENTS+=(
"a,arch,Target operating system architecture (amd64)"
"o,os,Target operating system (linux, windows, mac)"
"u,update,Java update version number (${ARG_JAVA_UPDATE})"
"v,version,Full Java version (${ARG_JAVA_VERSION})"
)
ARCHIVE_EXT="tar.gz"
ARCHIVE_APP="tar xf"
APP_EXTENSION="bin"
# ---------------------------------------------------------------------------
# Generates
# ---------------------------------------------------------------------------
execute() {
$do_configure_target
$do_build
$do_clean
pushd "${ARG_DIR_DIST}" > /dev/null 2>&1
$do_extract_java
$do_create_launch_script
$do_copy_archive
popd > /dev/null 2>&1
$do_create_launcher
$do_brand_windows
return 1
}
# ---------------------------------------------------------------------------
# Configure platform-specific commands and file names.
# ---------------------------------------------------------------------------
utile_configure_target() {
if [ "${ARG_JAVA_OS}" = "windows" ]; then
ARCHIVE_EXT="zip"
ARCHIVE_APP="unzip -qq"
FILE_DIST_EXEC="run.bat"
APP_EXTENSION="exe"
do_create_launch_script=utile_create_launch_script_windows
do_brand_windows=utile_brand_windows
fi
}
# ---------------------------------------------------------------------------
# Build platform-specific überjar.
# ---------------------------------------------------------------------------
utile_build() {
$log "Delete ${ARG_PATH_DIST_JAR}"
rm -f "${ARG_PATH_DIST_JAR}"
$log "Build application for ${ARG_JAVA_OS}"
gradle clean jar -PtargetOs="${ARG_JAVA_OS}"
}
# ---------------------------------------------------------------------------
# Purges the existing distribution directory to recreate the launcher.
# This refreshes the JRE from the downloaded archive.
# ---------------------------------------------------------------------------
utile_clean() {
$log "Recreate ${ARG_DIR_DIST}"
rm -rf "${ARG_DIR_DIST}"
mkdir -p "${ARG_DIR_DIST}"
}
# ---------------------------------------------------------------------------
# Extract platform-specific Java Runtime Environment. This will download
# and cache the required Java Runtime Environment for the target platform.
# On subsequent runs, the cached version is used, instead of issuing another
# download.
# ---------------------------------------------------------------------------
utile_extract_java() {
$log "Extract Java"
local -r java_vm="jre"
local -r java_version="${ARG_JAVA_VERSION}+${ARG_JAVA_UPDATE}"
local -r url_java="https://download.bell-sw.com/java/${java_version}/bellsoft-${java_vm}${java_version}-${ARG_JAVA_OS}-${ARG_JAVA_ARCH}-full.${ARCHIVE_EXT}"
local -r file_java="${java_vm}-${java_version}-${ARG_JAVA_OS}-${ARG_JAVA_ARCH}.${ARCHIVE_EXT}"
local -r path_java="/tmp/${file_java}"
# File must have contents.
if [ ! -s ${path_java} ]; then
$log "Download ${url_java} to ${path_java}"
wget -q "${url_java}" -O "${path_java}"
fi
$log "Unpack ${path_java}"
$ARCHIVE_APP "${path_java}"
local -r dir_java="${java_vm}-${ARG_JAVA_VERSION}-full"
$log "Rename ${dir_java} to ${ARG_JAVA_DIR}"
mv "${dir_java}" "${ARG_JAVA_DIR}"
}
# ---------------------------------------------------------------------------
# Create Linux-specific launch script.
# ---------------------------------------------------------------------------
utile_create_launch_script_linux() {
$log "Create Linux launch script"
cat > "${FILE_DIST_EXEC}" << __EOT
#!/usr/bin/env bash
readonly SCRIPT_SRC="\$(dirname "\${BASH_SOURCE[\${#BASH_SOURCE[@]} - 1]}")"
"\${SCRIPT_SRC}/${ARG_JAVA_DIR}/bin/java" ${OPT_JAVA} -jar "\${SCRIPT_SRC}/${FILE_APP_JAR}" "\$@" 2>/dev/null
__EOT
chmod +x "${FILE_DIST_EXEC}"
}
# ---------------------------------------------------------------------------
# Create Windows-specific launch script.
# ---------------------------------------------------------------------------
utile_create_launch_script_windows() {
$log "Create Windows launch script"
cat > "${FILE_DIST_EXEC}" << __EOT
@echo off
set SCRIPT_DIR=%~dp0
"%SCRIPT_DIR%\\${ARG_JAVA_DIR}\\bin\\java" ${OPT_JAVA} -jar "%SCRIPT_DIR%\\${APP_NAME}.jar" %* 2>nul
__EOT
# Convert Unix end of line characters (\n) to Windows format (\r\n).
# This avoids any potential line conversion issues with the repository.
sed -i 's/$/\r/' "${FILE_DIST_EXEC}"
}
# ---------------------------------------------------------------------------
# Modify the binary to include icon and identifying information.
# ---------------------------------------------------------------------------
utile_brand_windows() {
# Read the properties file to get the application name (case sensitvely).
while IFS='=' read -r key value
do
key=$(echo $key | tr '.' '_')
eval ${key}=\${value}
done < "src/main/resources/bootstrap.properties"
readonly BINARY="${APP_NAME}.exe"
readonly VERSION=$(git describe --tags)
readonly COMPANY="White Magic Software, Ltd."
readonly YEAR=$(date +%Y)
readonly DESCRIPTION="Markdown editor with live preview, variables, and math."
readonly SIZE=$(stat --format="%s" ${BINARY})
wine ${SCRIPT_DIR}/scripts/rcedit-x64.exe "${BINARY}" \
--set-icon "scripts/logo.ico" \
--set-version-string "OriginalFilename" "${application_title}.exe" \
--set-version-string "CompanyName" "${COMPANY}" \
--set-version-string "ProductName" "${application_title}" \
--set-version-string "LegalCopyright" "Copyright ${YEAR} ${COMPANY}" \
--set-version-string "FileDescription" "${DESCRIPTION}" \
--set-version-string "Size" "${DESCRIPTION}" \
--set-product-version "${VERSION}" \
--set-file-version "${VERSION}"
mv -f "${BINARY}" "${application_title}.exe"
}
# ---------------------------------------------------------------------------
# Copy application überjar.
# ---------------------------------------------------------------------------
utile_copy_archive() {
$log "Create copy of ${FILE_APP_JAR}"
cp "${ARG_PATH_DIST_JAR}" "${FILE_APP_JAR}"
}
# ---------------------------------------------------------------------------
# Create platform-specific launcher binary.
# ---------------------------------------------------------------------------
utile_create_launcher() {
local -r FILE_APP_NAME="${APP_NAME}.${APP_EXTENSION}"
$log "Create ${FILE_APP_NAME}"
# Warp-packer does not seem to overwrite the file.
rm -f "${FILE_APP_NAME}"
# Download uses amd64, but warp-packer differs.
if [ "${ARG_JAVA_ARCH}" = "amd64" ]; then
ARG_JAVA_ARCH="x64"
fi
warp-packer \
pack \
--arch "${ARG_JAVA_OS}-${ARG_JAVA_ARCH}" \
--input-dir "${ARG_DIR_DIST}" \
--exec "${FILE_DIST_EXEC}" \
--output "${FILE_APP_NAME}" > /dev/null
chmod +x "${FILE_APP_NAME}"
}
argument() {
local consume=2
case "$1" in
-a|--arch)
ARG_JAVA_ARCH="$2"
;;
-o|--os)
ARG_JAVA_OS="$2"
;;
-u|--update)
ARG_JAVA_UPDATE="$2"
;;
-v|--version)
ARG_JAVA_VERSION="$2"
;;
esac
return ${consume}
}
do_configure_target=utile_configure_target
do_build=utile_build
do_clean=utile_clean
do_extract_java=utile_extract_java
do_create_launch_script=utile_create_launch_script_linux
do_copy_archive=utile_copy_archive
do_create_launcher=utile_create_launcher
do_brand_windows=:
main "$@"