99import org .apache .commons .lang3 .StringUtils ;
1010import org .apache .commons .lang3 .SystemUtils ;
1111
12+ import io .github .fvarrui .javapackager .model .MacStartup ;
1213import io .github .fvarrui .javapackager .model .Platform ;
1314import io .github .fvarrui .javapackager .utils .CommandUtils ;
1415import io .github .fvarrui .javapackager .utils .FileUtils ;
@@ -91,37 +92,50 @@ public File doCreateApp() throws Exception {
9192 // copies jarfile to Java folder
9293 FileUtils .copyFileToFolder (jarFile , javaFolder );
9394
95+ processStartupScript ();
96+
97+ processClasspath ();
98+
99+ processInfoPlistFile ();
100+
101+ processProvisionProfileFile ();
102+
103+ codesign ();
104+
105+ return appFile ;
106+ }
107+
108+ private void processStartupScript () throws Exception {
109+
94110 if (this .administratorRequired ) {
95111
112+ // We need a helper script ("startup") in this case,
113+ // which invokes the launcher script/ executable with administrator rights.
114+ // TODO: admin script depends on launcher file name 'universalJavaApplicationStub'
115+
96116 // sets startup file
97117 this .executable = new File (macOSFolder , "startup" );
98118
99119 // creates startup file to boot java app
100120 VelocityUtils .render ("mac/startup.vtl" , executable , this );
101- executable .setExecutable (true , false );
102- Logger .info ("Startup script file created in " + executable .getAbsolutePath ());
103-
121+
104122 } else {
105123
106- // sets startup file
107- this .executable = new File (macOSFolder , "universalJavaApplicationStub" );
108- Logger .info ("Using " + executable .getAbsolutePath () + " as startup script" );
109-
110- }
111-
112- // copies universalJavaApplicationStub startup file to boot java app
113- File appStubFile = new File (macOSFolder , "universalJavaApplicationStub" );
114- String universalJavaApplicationStubResource = null ;
115- switch (macConfig .getMacStartup ()) {
116- case UNIVERSAL : universalJavaApplicationStubResource = "universalJavaApplicationStub" ; break ;
117- case X86_64 : universalJavaApplicationStubResource = "universalJavaApplicationStub.x86_64" ; break ;
118- case ARM64 : universalJavaApplicationStubResource = "universalJavaApplicationStub.arm64" ; break ;
119- case SCRIPT : universalJavaApplicationStubResource = "universalJavaApplicationStub.sh" ; break ;
124+ File launcher = macConfig .getCustomLauncher ();
125+ if (launcher != null && launcher .canRead () && launcher .isFile ()){
126+ FileUtils .copyFileToFolder (launcher , macOSFolder );
127+ this .executable = new File (macOSFolder , launcher .getName ());
128+ } else {
129+ this .executable = preparePrecompiledStartupStub ();
130+ }
120131 }
121- FileUtils .copyResourceToFile ("/mac/" + universalJavaApplicationStubResource , appStubFile );
122- appStubFile .setExecutable (true , false );
132+
133+ executable .setExecutable (true , false );
134+ Logger .info ("Startup script file created in " + executable .getAbsolutePath ());
135+ }
123136
124- // process classpath
137+ private void processClasspath () {
138+ // TODO: Why are we doing this here? I do not see any usage of 'classpath' or 'classpaths' here.
125139 classpath = (this .macConfig .isRelocateJar () ? "Java/" : "" ) + this .jarFile .getName () + (classpath != null ? ":" + classpath : "" );
126140 classpaths = Arrays .asList (classpath .split ("[:;]" ));
127141 if (!isUseResourcesAsWorkingDir ()) {
@@ -131,29 +145,57 @@ public File doCreateApp() throws Exception {
131145 .collect (Collectors .toList ());
132146 }
133147 classpath = StringUtils .join (classpaths , ":" );
148+ }
134149
135- // creates and write the Info.plist file
150+ /**
151+ * Creates and writes the Info.plist file if no custom file is specified.
152+ * @throws Exception if anything goes wrong
153+ */
154+ private void processInfoPlistFile () throws Exception {
136155 File infoPlistFile = new File (contentsFolder , "Info.plist" );
137- VelocityUtils .render ("mac/Info.plist.vtl" , infoPlistFile , this );
138- XMLUtils .prettify (infoPlistFile );
139- Logger .info ("Info.plist file created in " + infoPlistFile .getAbsolutePath ());
140-
141- // copy provisionprofile
142- if (macConfig .getProvisionProfile () != null ) {
143- // file name must be 'embedded.provisionprofile'
144- FileUtils .copyFileToFile (macConfig .getProvisionProfile (), new File (contentsFolder , "embedded.provisionprofile" ));
156+ if (macConfig .getCustomInfoPlist () != null && macConfig .getCustomInfoPlist ().isFile () && macConfig .getCustomInfoPlist ().canRead ()){
157+ FileUtils .copyFileToFile (macConfig .getCustomInfoPlist (), infoPlistFile );
158+ } else {
159+ VelocityUtils .render ("mac/Info.plist.vtl" , infoPlistFile , this );
160+ XMLUtils .prettify (infoPlistFile );
145161 }
162+ Logger .info ("Info.plist file created in " + infoPlistFile .getAbsolutePath ());
163+ }
146164
147- // codesigns app folder
165+ private void codesign () throws Exception {
148166 if (!Platform .mac .isCurrentPlatform ()) {
149167 Logger .warn ("Generated app could not be signed due to current platform is " + Platform .getCurrentPlatform ());
150168 } else if (!getMacConfig ().isCodesignApp ()) {
151169 Logger .warn ("App codesigning disabled" );
152170 } else {
153171 codesign (this .macConfig .getDeveloperId (), this .macConfig .getEntitlements (), this .appFile );
154172 }
173+ }
155174
156- return appFile ;
175+ private void processProvisionProfileFile () throws Exception {
176+ if (macConfig .getProvisionProfile () != null && macConfig .getProvisionProfile ().isFile () && macConfig .getProvisionProfile ().canRead ()) {
177+ // file name must be 'embedded.provisionprofile'
178+ File provisionProfile = new File (contentsFolder , "embedded.provisionprofile" );
179+ FileUtils .copyFileToFile (macConfig .getProvisionProfile (), provisionProfile );
180+ Logger .info ("Provision profile file created from " + "\n " +
181+ macConfig .getProvisionProfile () + " to \n " +
182+ provisionProfile .getAbsolutePath ());
183+ }
184+ }
185+
186+ private File preparePrecompiledStartupStub () throws Exception {
187+ // sets startup file
188+ File appStubFile = new File (macOSFolder , "universalJavaApplicationStub" );
189+ String universalJavaApplicationStubResource = null ;
190+ switch (macConfig .getMacStartup ()) {
191+ case UNIVERSAL : universalJavaApplicationStubResource = "universalJavaApplicationStub" ; break ;
192+ case X86_64 : universalJavaApplicationStubResource = "universalJavaApplicationStub.x86_64" ; break ;
193+ case ARM64 : universalJavaApplicationStubResource = "universalJavaApplicationStub.arm64" ; break ;
194+ case SCRIPT : universalJavaApplicationStubResource = "universalJavaApplicationStub.sh" ; break ;
195+ }
196+ // unixStyleNewLinux=true if startup is a script (this will replace '\r\n' with '\n')
197+ FileUtils .copyResourceToFile ("/mac/" + universalJavaApplicationStubResource , appStubFile , macConfig .getMacStartup () == MacStartup .SCRIPT );
198+ return appStubFile ;
157199 }
158200
159201 private void codesign (String developerId , File entitlements , File appFile ) throws Exception {
0 commit comments