Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit d8c6b09

Browse files
committed
[refactoring] use new functions 'plist_get' and 'plist_get_java' and avoid duplicate PlistBuddy calls
also: get rid of backtick `command` executions and use $() instead
1 parent 2a4f6e7 commit d8c6b09

1 file changed

Lines changed: 63 additions & 51 deletions

File tree

src/universalJavaApplicationStub

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# #
1212
# @author Tobias Fischer #
1313
# @url https://github.com/tofi86/universalJavaApplicationStub #
14-
# @date 2017-10-31 #
14+
# @date 2017-11-01 #
1515
# @version 2.1.0 #
1616
# #
1717
##################################################################################
@@ -109,25 +109,39 @@ JVMVersion=""
109109

110110

111111

112-
# read Info.plist and extract JVM options
113-
############################################
114-
115112
# function 'plist_get()'
116113
#
117114
# read a specific Plist key with 'PlistBuddy' utility
118115
#
119-
# @param1 the Plist key without leading colon ':'
116+
# @param1 the Plist key with leading colon ':'
120117
# @return the value as String or Array
121118
################################################################################
122-
+plist_get(){
123-
/usr/libexec/PlistBuddy -c "print :$1" "${InfoPlistFile}"
124-
+}
119+
plist_get(){
120+
/usr/libexec/PlistBuddy -c "print $1" "${InfoPlistFile}" 2> /dev/null
121+
}
122+
123+
# function 'plist_get_java()'
124+
#
125+
# read a specific Plist key with 'PlistBuddy' utility
126+
# in the 'Java' or 'JavaX' dictionary (<dict>)
127+
#
128+
# @param1 the Plist :Java(X):Key with leading colon ':'
129+
# @return the value as String or Array
130+
################################################################################
131+
plist_get_java(){
132+
plist_get ${JavaKey:-":Java"}$1
133+
}
134+
135+
136+
137+
# read Info.plist and extract JVM options
138+
############################################
125139

126140
# read the program name from CFBundleName
127-
CFBundleName=`/usr/libexec/PlistBuddy -c "print :CFBundleName" "${InfoPlistFile}"`
141+
CFBundleName=$(plist_get ':CFBundleName')
128142

129143
# read the icon file name
130-
CFBundleIconFile=`/usr/libexec/PlistBuddy -c "print :CFBundleIconFile" "${InfoPlistFile}"`
144+
CFBundleIconFile=$(plist_get ':CFBundleIconFile')
131145

132146

133147
# check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
@@ -143,7 +157,7 @@ if [ $exitcode -ne 0 ]; then
143157
fi
144158

145159

146-
# read Info.plist in Apple style if exit code returns 0 (true, :Java key is present)
160+
# read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)
147161
if [ $exitcode -eq 0 ]; then
148162
stub_logger "[PlistStyle] Apple"
149163

@@ -157,65 +171,63 @@ if [ $exitcode -eq 0 ]; then
157171

158172

159173
# read the Java WorkingDirectory
160-
JVMWorkDir=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:WorkingDirectory" "${InfoPlistFile}" 2> /dev/null | xargs`
161-
162-
# set Working Directory based upon Plist info
174+
JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
175+
# set Working Directory based upon PList value
163176
if [[ ! -z ${JVMWorkDir} ]]; then
164177
WorkingDirectory="${JVMWorkDir}"
165178
else
166179
# AppPackageRoot is the standard WorkingDirectory when the script is started
167180
WorkingDirectory="${AppPackageRoot}"
168181
fi
169-
170182
# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
171-
WorkingDirectory=`eval "echo ${WorkingDirectory}"`
183+
WorkingDirectory=$(eval echo "${WorkingDirectory}")
172184

173185

174186
# read the MainClass name
175-
JVMMainClass=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:MainClass" "${InfoPlistFile}" 2> /dev/null`
187+
JVMMainClass=$(plist_get_java ':MainClass')
176188

177189
# read the SplashFile name
178-
JVMSplashFile=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:SplashFile" "${InfoPlistFile}" 2> /dev/null`
190+
JVMSplashFile=$(plist_get_java ':SplashFile')
179191

180-
# read the JVM Options
181-
JVMOptions=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:Properties" "${InfoPlistFile}" 2> /dev/null | grep " =" | sed 's/^ */-D/g' | tr '\n' ' ' | sed 's/ */ /g' | sed 's/ = /=/g' | xargs`
192+
# read the JVM Properties
193+
JVMOptions=$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | tr '\n' ' ' | sed 's/ */ /g' | sed 's/ = /=/g' | xargs)
182194
# replace occurences of $APP_ROOT with its content
183-
JVMOptions=`eval "echo ${JVMOptions}"`
184-
185-
# read StartOnMainThread
186-
JVMStartOnMainThread=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:StartOnMainThread" "${InfoPlistFile}" 2> /dev/null`
187-
if [ "${JVMStartOnMainThread}" == "true" ]; then
188-
JVMOptions+=" -XstartOnFirstThread"
189-
fi
195+
JVMOptions=$(eval echo "${JVMOptions}")
190196

191197
# read the ClassPath in either Array or String style
192-
JVMClassPath_RAW=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:ClassPath" "${InfoPlistFile}" 2> /dev/null`
198+
JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
193199
if [[ $JVMClassPath_RAW == *Array* ]] ; then
194-
JVMClassPath=.`/usr/libexec/PlistBuddy -c "print ${JavaKey}:ClassPath" "${InfoPlistFile}" 2> /dev/null | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs`
200+
JVMClassPath=.$(plist_get_java ':ClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
195201
else
196202
JVMClassPath=${JVMClassPath_RAW}
197203
fi
198204
# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
199-
JVMClassPath=`eval "echo ${JVMClassPath}"`
205+
JVMClassPath=$(eval echo "${JVMClassPath}")
200206

201-
# read the JVM Default Options in either Array or String style
202-
JVMDefaultOptions_RAW=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:VMOptions" "${InfoPlistFile}" 2> /dev/null | xargs`
207+
# read the JVM Options in either Array or String style
208+
JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
203209
if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
204-
JVMDefaultOptions=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:VMOptions" "${InfoPlistFile}" 2> /dev/null | grep " " | sed 's/^ */ /g' | tr -d '\n' | xargs`
210+
JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep " " | sed 's/^ */ /g' | tr -d '\n' | xargs)
205211
else
206212
JVMDefaultOptions=${JVMDefaultOptions_RAW}
207213
fi
208214

215+
# read StartOnMainThread and add as -XstartOnFirstThread
216+
JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
217+
if [ "${JVMStartOnMainThread}" == "true" ]; then
218+
JVMDefaultOptions+=" -XstartOnFirstThread"
219+
fi
220+
209221
# read the JVM Arguments
210-
MainArgs=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:Arguments" "${InfoPlistFile}" 2> /dev/null | xargs`
222+
MainArgs=$(plist_get_java ':Arguments')
211223
# replace occurences of $APP_ROOT with its content
212-
MainArgs=`eval "echo ${MainArgs}"`
224+
MainArgs=$(eval echo "${MainArgs}")
213225

214226
# read the Java version we want to find
215-
JVMVersion=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:JVMVersion" "${InfoPlistFile}" 2> /dev/null | xargs`
227+
JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
216228

217229

218-
# read Info.plist in Oracle style
230+
# read 'Info.plist' file in Oracle style
219231
else
220232
stub_logger "[PlistStyle] Oracle"
221233

@@ -227,41 +239,41 @@ else
227239
APP_ROOT="${AppPackageFolder}"
228240

229241
# read the MainClass name
230-
JVMMainClass=`/usr/libexec/PlistBuddy -c "print :JVMMainClassName" "${InfoPlistFile}" 2> /dev/null`
242+
JVMMainClass=$(plist_get ':JVMMainClassName')
231243

232244
# read the SplashFile name
233-
JVMSplashFile=`/usr/libexec/PlistBuddy -c "print :JVMSplashFile" "${InfoPlistFile}" 2> /dev/null`
245+
JVMSplashFile=$(plist_get ':JVMSplashFile')
234246

235247
# read the JVM Options
236-
JVMOptions=`/usr/libexec/PlistBuddy -c "print :JVMOptions" "${InfoPlistFile}" 2> /dev/null | grep " -" | tr -d '\n' | sed 's/ */ /g' | xargs`
248+
JVMOptions=$(plist_get ':JVMOptions' | grep " -" | tr -d '\n' | sed 's/ */ /g' | xargs)
237249
# replace occurences of $APP_ROOT with its content
238-
JVMOptions=`eval "echo ${JVMOptions}"`
250+
JVMOptions=$(eval echo "${JVMOptions}")
239251

240252
# read the ClassPath in either Array or String style
241-
JVMClassPath_RAW=`/usr/libexec/PlistBuddy -c "print JVMClassPath" "${InfoPlistFile}" 2> /dev/null`
253+
JVMClassPath_RAW=$(plist_get ':JVMClassPath')
242254
if [[ $JVMClassPath_RAW == *Array* ]] ; then
243-
JVMClassPath=.`/usr/libexec/PlistBuddy -c "print JVMClassPath" "${InfoPlistFile}" 2> /dev/null | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs`
255+
JVMClassPath=.$(plist_get ':JVMClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
244256
# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
245-
JVMClassPath=`eval "echo ${JVMClassPath}"`
257+
JVMClassPath=$(eval echo "${JVMClassPath}")
246258

247259
elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
248260
JVMClassPath=${JVMClassPath_RAW}
249261
# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
250-
JVMClassPath=`eval "echo ${JVMClassPath}"`
262+
JVMClassPath=$(eval echo "${JVMClassPath}")
251263

252264
else
253265
#default: fallback to OracleJavaFolder
254266
JVMClassPath="${JavaFolder}/*"
255-
# Do NOT expand the default App.app/Contents/Java/* classpath (#42)
267+
# Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
256268
fi
257269

258270
# read the JVM Default Options
259-
JVMDefaultOptions=`/usr/libexec/PlistBuddy -c "print :JVMDefaultOptions" "${InfoPlistFile}" 2> /dev/null | grep -o " \-.*" | tr -d '\n' | xargs`
271+
JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
260272

261273
# read the Main Arguments from JVMArguments key (see #46 for naming details)
262-
MainArgs=`/usr/libexec/PlistBuddy -c "print :JVMArguments" "${InfoPlistFile}" 2> /dev/null | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g' | xargs`
274+
MainArgs=$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g' | xargs)
263275
# replace occurences of $APP_ROOT with its content
264-
MainArgs=`eval "echo ${MainArgs}"`
276+
MainArgs=$(eval echo "${MainArgs}")
265277
fi
266278

267279

@@ -276,7 +288,7 @@ stub_logger "[Language] $LANG"
276288
if [[ $LANG == fr* ]] ; then
277289
MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
278290
MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
279-
MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version Java demandée est invalide: '%s'\nVeuillez contacter le développeur de l'application."
291+
MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
280292
MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s."
281293
MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
282294
MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
@@ -302,7 +314,7 @@ elif [[ $LANG == de* ]] ; then
302314
else
303315
MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
304316
MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
305-
MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: '%s'\nPlease contact the App developer."
317+
MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
306318
MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s."
307319
MSG_JAVA_VERSION_OR_LATER="or later"
308320
MSG_JAVA_VERSION_LATEST="(latest update)"

0 commit comments

Comments
 (0)