1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.bundlers.mac;
  27 
  28 import com.oracle.bundlers.BundlerParamInfo;
  29 import com.sun.javafx.tools.packager.Log;
  30 import com.sun.javafx.tools.packager.bundlers.ConfigException;
  31 import com.sun.javafx.tools.packager.bundlers.IOUtils;
  32 import com.sun.javafx.tools.packager.bundlers.UnsupportedPlatformException;
  33 
  34 import java.io.*;
  35 import java.util.Collection;
  36 import java.util.Map;
  37 
  38 import static com.oracle.bundlers.StandardBundlerParam.NAME;
  39 
  40 public class MacPKGBundler extends MacBaseInstallerBundler {
  41 
  42 
  43     //@Override
  44     public File bundle(Map<String, ? super Object> p, File outdir) {
  45         Log.info("Building PKG package for " + NAME.fetchFrom(p));
  46 
  47         File appImageDir = APP_IMAGE_BUILD_ROOT.fetchFrom(p);
  48         try {
  49             appImageDir.mkdirs();
  50             prepareAppBundle(p);
  51             return createPKG(p, outdir);
  52         } catch (Exception ex) {
  53             return null;
  54         }
  55     }
  56 
  57     private File createPKG(Map<String, ? super Object> params, File outdir) {
  58         //generic find attempt
  59         try {
  60             String appLocation =
  61                     APP_IMAGE_BUILD_ROOT.fetchFrom(params) + "/" + NAME.fetchFrom(params) + ".app";
  62             File predefinedImage = getPredefinedImage(params);
  63             if (predefinedImage != null) {
  64                 appLocation = predefinedImage.getAbsolutePath();
  65             }
  66 
  67             //productbuild --component Smoke.app /Applications  --product Smoke.app/Contents/Info.plist Smoke.pkg
  68             File finalPKG = new File(outdir, NAME.fetchFrom(params)+".pkg");
  69             outdir.mkdirs();
  70 
  71             ProcessBuilder pb = new ProcessBuilder("productbuild",
  72                     "--component",
  73                     appLocation,
  74                     "/Applications",
  75                     "--product",
  76                     appLocation+"/Contents/Info.plist",
  77                     finalPKG.getAbsolutePath());
  78             IOUtils.exec(pb, verbose);
  79             return finalPKG;
  80         } catch (Exception ignored) {
  81             Log.debug("PKG Failed: " + ignored.getMessage());
  82         }
  83         return null;
  84     }
  85 
  86     //////////////////////////////////////////////////////////////////////////////////
  87     // Implement Bundler
  88     //////////////////////////////////////////////////////////////////////////////////
  89 
  90     @Override
  91     public String getName() {
  92         return "PKG Installer";
  93     }
  94 
  95     @Override
  96     public String getDescription() {
  97         return "Mac PKG Installer Bundle.";
  98     }
  99 
 100     @Override
 101     public String getID() {
 102         return "pkg";
 103     }
 104 
 105     @Override
 106     public Collection<BundlerParamInfo<?>> getBundleParameters() {
 107         //Add PKG Specific parameters as required
 108         return super.getBundleParameters();
 109     }
 110 
 111     @Override
 112     public boolean validate(Map<String, ? super Object> params) throws UnsupportedPlatformException, ConfigException {
 113         if (params == null) throw new ConfigException("Parameters map is null.", "Pass in a non-null parameters map.");
 114 
 115         // hdiutil is always available so there's no need to test for availability.
 116         //run basic validation to ensure requirements are met
 117 
 118         //run basic validation to ensure requirements are met
 119         //we are not interested in return code, only possible exception
 120         APP_BUNDLER.fetchFrom(params).doValidate(params);
 121         return true;
 122     }
 123 
 124     @Override
 125     public File execute(Map<String, ? super Object> params, File outputParentDir) {
 126         return bundle(params, outputParentDir);
 127     }
 128 
 129 }