@@ -150,113 +150,115 @@ public static PListWrapper readPList(Stream<String> lines) {
150150 }
151151
152152 static PackageHandlers createDmgPackageHandlers () {
153- PackageHandlers dmg = new PackageHandlers ();
153+ return new PackageHandlers (MacHelper ::installDmg , MacHelper ::uninstallDmg , MacHelper ::unpackDmg );
154+ }
154155
155- dmg .installHandler = cmd -> {
156- withExplodedDmg (cmd , dmgImage -> {
157- Executor .of ("sudo" , "cp" , "-r" )
158- .addArgument (dmgImage )
159- .addArgument (getInstallationDirectory (cmd ).getParent ())
160- .execute ();
161- });
162- };
163- dmg .unpackHandler = (cmd , destinationDir ) -> {
164- Path unpackDir = destinationDir .resolve (
165- TKit .removeRootFromAbsolutePath (
166- getInstallationDirectory (cmd )).getParent ());
167- try {
168- Files .createDirectories (unpackDir );
169- } catch (IOException ex ) {
170- throw new RuntimeException (ex );
171- }
156+ private static int installDmg (JPackageCommand cmd ) {
157+ cmd .verifyIsOfType (PackageType .MAC_DMG );
158+ withExplodedDmg (cmd , dmgImage -> {
159+ Executor .of ("sudo" , "cp" , "-r" )
160+ .addArgument (dmgImage )
161+ .addArgument (getInstallationDirectory (cmd ).getParent ())
162+ .execute (0 );
163+ });
164+ return 0 ;
165+ }
172166
173- withExplodedDmg (cmd , dmgImage -> {
174- Executor .of ("cp" , "-r" )
175- .addArgument (dmgImage )
176- .addArgument (unpackDir )
177- .execute ();
178- });
179- return destinationDir ;
180- };
181- dmg .uninstallHandler = cmd -> {
182- cmd .verifyIsOfType (PackageType .MAC_DMG );
183- Executor .of ("sudo" , "rm" , "-rf" )
184- .addArgument (cmd .appInstallationDirectory ())
185- .execute ();
186- };
167+ private static void uninstallDmg (JPackageCommand cmd ) {
168+ cmd .verifyIsOfType (PackageType .MAC_DMG );
169+ Executor .of ("sudo" , "rm" , "-rf" )
170+ .addArgument (cmd .appInstallationDirectory ())
171+ .execute ();
172+ }
173+
174+ private static Path unpackDmg (JPackageCommand cmd , Path destinationDir ) {
175+ cmd .verifyIsOfType (PackageType .MAC_DMG );
176+ Path unpackDir = destinationDir .resolve (
177+ TKit .removeRootFromAbsolutePath (
178+ getInstallationDirectory (cmd )).getParent ());
179+ try {
180+ Files .createDirectories (unpackDir );
181+ } catch (IOException ex ) {
182+ throw new RuntimeException (ex );
183+ }
187184
188- return dmg ;
185+ withExplodedDmg (cmd , dmgImage -> {
186+ Executor .of ("cp" , "-r" )
187+ .addArgument (dmgImage )
188+ .addArgument (unpackDir )
189+ .execute ();
190+ });
191+ return destinationDir ;
189192 }
190193
191194 static PackageHandlers createPkgPackageHandlers () {
192- PackageHandlers pkg = new PackageHandlers ();
195+ return new PackageHandlers (MacHelper ::installPkg , MacHelper ::uninstallPkg , MacHelper ::unpackPkg );
196+ }
197+
198+ private static int installPkg (JPackageCommand cmd ) {
199+ cmd .verifyIsOfType (PackageType .MAC_PKG );
200+ return Executor .of ("sudo" , "/usr/sbin/installer" , "-allowUntrusted" , "-pkg" )
201+ .addArgument (cmd .outputBundle ())
202+ .addArguments ("-target" , "/" )
203+ .execute ().getExitCode ();
204+ }
193205
194- pkg .installHandler = cmd -> {
195- cmd .verifyIsOfType (PackageType .MAC_PKG );
196- Executor .of ("sudo" , "/usr/sbin/installer" , "-allowUntrusted" , "-pkg" )
197- .addArgument (cmd .outputBundle ())
198- .addArguments ("-target" , "/" )
206+ private static void uninstallPkg (JPackageCommand cmd ) {
207+ cmd .verifyIsOfType (PackageType .MAC_PKG );
208+ if (Files .exists (getUninstallCommand (cmd ))) {
209+ Executor .of ("sudo" , "/bin/sh" ,
210+ getUninstallCommand (cmd ).toString ()).execute ();
211+ } else {
212+ Executor .of ("sudo" , "rm" , "-rf" )
213+ .addArgument (cmd .appInstallationDirectory ())
199214 .execute ();
200- };
201- pkg .unpackHandler = (cmd , destinationDir ) -> {
202- cmd .verifyIsOfType (PackageType .MAC_PKG );
215+ }
216+ }
203217
204- var dataDir = destinationDir .resolve ("data" );
218+ private static Path unpackPkg (JPackageCommand cmd , Path destinationDir ) {
219+ cmd .verifyIsOfType (PackageType .MAC_PKG );
205220
206- Executor .of ("pkgutil" , "--expand" )
207- .addArgument (cmd .outputBundle ())
208- .addArgument (dataDir ) // We need non-existing folder
209- .execute ();
221+ var dataDir = destinationDir .resolve ("data" );
210222
211- final Path unpackRoot = destinationDir .resolve ("unpacked" );
212-
213- // Unpack all ".pkg" files from $dataDir folder in $unpackDir folder
214- try (var dataListing = Files .list (dataDir )) {
215- dataListing .filter (file -> {
216- return ".pkg" .equals (PathUtils .getSuffix (file .getFileName ()));
217- }).forEach (ThrowingConsumer .toConsumer (pkgDir -> {
218- // Installation root of the package is stored in
219- // /pkg-info@install-location attribute in $pkgDir/PackageInfo xml file
220- var doc = createDocumentBuilder ().parse (
221- new ByteArrayInputStream (Files .readAllBytes (
222- pkgDir .resolve ("PackageInfo" ))));
223- var xPath = XPathFactory .newInstance ().newXPath ();
224-
225- final String installRoot = (String ) xPath .evaluate (
226- "/pkg-info/@install-location" , doc ,
227- XPathConstants .STRING );
228-
229- final Path unpackDir = unpackRoot .resolve (
230- TKit .removeRootFromAbsolutePath (Path .of (installRoot )));
231-
232- Files .createDirectories (unpackDir );
233-
234- Executor .of ("tar" , "-C" )
235- .addArgument (unpackDir )
236- .addArgument ("-xvf" )
237- .addArgument (pkgDir .resolve ("Payload" ))
238- .execute ();
239- }));
240- } catch (IOException ex ) {
241- throw new RuntimeException (ex );
242- }
223+ Executor .of ("pkgutil" , "--expand" )
224+ .addArgument (cmd .outputBundle ())
225+ .addArgument (dataDir ) // We need non-existing folder
226+ .execute ();
243227
244- return unpackRoot ;
245- };
246- pkg .uninstallHandler = cmd -> {
247- cmd .verifyIsOfType (PackageType .MAC_PKG );
228+ final Path unpackRoot = destinationDir .resolve ("unpacked" );
248229
249- if (Files .exists (getUninstallCommand (cmd ))) {
250- Executor .of ("sudo" , "/bin/sh" ,
251- getUninstallCommand (cmd ).toString ()).execute ();
252- } else {
253- Executor .of ("sudo" , "rm" , "-rf" )
254- .addArgument (cmd .appInstallationDirectory ())
230+ // Unpack all ".pkg" files from $dataDir folder in $unpackDir folder
231+ try (var dataListing = Files .list (dataDir )) {
232+ dataListing .filter (file -> {
233+ return ".pkg" .equals (PathUtils .getSuffix (file .getFileName ()));
234+ }).forEach (ThrowingConsumer .toConsumer (pkgDir -> {
235+ // Installation root of the package is stored in
236+ // /pkg-info@install-location attribute in $pkgDir/PackageInfo xml file
237+ var doc = createDocumentBuilder ().parse (
238+ new ByteArrayInputStream (Files .readAllBytes (
239+ pkgDir .resolve ("PackageInfo" ))));
240+ var xPath = XPathFactory .newInstance ().newXPath ();
241+
242+ final String installRoot = (String ) xPath .evaluate (
243+ "/pkg-info/@install-location" , doc ,
244+ XPathConstants .STRING );
245+
246+ final Path unpackDir = unpackRoot .resolve (
247+ TKit .removeRootFromAbsolutePath (Path .of (installRoot )));
248+
249+ Files .createDirectories (unpackDir );
250+
251+ Executor .of ("tar" , "-C" )
252+ .addArgument (unpackDir )
253+ .addArgument ("-xvf" )
254+ .addArgument (pkgDir .resolve ("Payload" ))
255255 .execute ();
256- }
257- };
256+ }));
257+ } catch (IOException ex ) {
258+ throw new RuntimeException (ex );
259+ }
258260
259- return pkg ;
261+ return unpackRoot ;
260262 }
261263
262264 static void verifyBundleStructure (JPackageCommand cmd ) {
0 commit comments