dmg.js 15.2 KB
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DmgTarget = void 0;

function _appBuilderLib() {
  const data = require("app-builder-lib");

  _appBuilderLib = function () {
    return data;
  };

  return data;
}

function _macCodeSign() {
  const data = require("app-builder-lib/out/codeSign/macCodeSign");

  _macCodeSign = function () {
    return data;
  };

  return data;
}

function _differentialUpdateInfoBuilder() {
  const data = require("app-builder-lib/out/targets/differentialUpdateInfoBuilder");

  _differentialUpdateInfoBuilder = function () {
    return data;
  };

  return data;
}

function _appBuilder() {
  const data = require("app-builder-lib/out/util/appBuilder");

  _appBuilder = function () {
    return data;
  };

  return data;
}

function _builderUtil() {
  const data = require("builder-util");

  _builderUtil = function () {
    return data;
  };

  return data;
}

function _fs() {
  const data = require("builder-util/out/fs");

  _fs = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = require("fs-extra");

  _fsExtra = function () {
    return data;
  };

  return data;
}

var path = _interopRequireWildcard(require("path"));

function _sanitizeFilename() {
  const data = _interopRequireDefault(require("sanitize-filename"));

  _sanitizeFilename = function () {
    return data;
  };

  return data;
}

function _dmgLicense() {
  const data = require("./dmgLicense");

  _dmgLicense = function () {
    return data;
  };

  return data;
}

function _dmgUtil() {
  const data = require("./dmgUtil");

  _dmgUtil = function () {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

class DmgTarget extends _appBuilderLib().Target {
  constructor(packager, outDir) {
    super("dmg");
    this.packager = packager;
    this.outDir = outDir;
    this.options = this.packager.config.dmg || Object.create(null);
  }

  async build(appPath, arch) {
    const packager = this.packager; // tslint:disable-next-line:no-invalid-template-strings

    const artifactName = packager.expandArtifactNamePattern(this.options, "dmg", arch, "${productName}-" + (packager.platformSpecificBuildOptions.bundleShortVersion || "${version}") + "-${arch}.${ext}", true, packager.platformSpecificBuildOptions.defaultArch);
    const artifactPath = path.join(this.outDir, artifactName);
    await packager.info.callArtifactBuildStarted({
      targetPresentableName: "DMG",
      file: artifactPath,
      arch
    });
    const volumeName = (0, _sanitizeFilename().default)(this.computeVolumeName(arch, this.options.title));
    const tempDmg = await createStageDmg(await packager.getTempFile(".dmg"), appPath, volumeName);
    const specification = await this.computeDmgOptions(); // https://github.com/electron-userland/electron-builder/issues/2115

    const backgroundFile = specification.background == null ? null : await transformBackgroundFileIfNeed(specification.background, packager.info.tempDirManager);
    const finalSize = await computeAssetSize(packager.info.cancellationToken, tempDmg, specification, backgroundFile);
    const expandingFinalSize = finalSize * 0.1 + finalSize;
    await (0, _builderUtil().exec)("hdiutil", ["resize", "-size", expandingFinalSize.toString(), tempDmg]);
    const volumePath = path.join("/Volumes", volumeName);

    if (await (0, _fs().exists)(volumePath)) {
      _builderUtil().log.debug({
        volumePath
      }, "unmounting previous disk image");

      await (0, _dmgUtil().detach)(volumePath);
    }

    if (!(await (0, _dmgUtil().attachAndExecute)(tempDmg, true, () => customizeDmg(volumePath, specification, packager, backgroundFile)))) {
      return;
    } // dmg file must not exist otherwise hdiutil failed (https://github.com/electron-userland/electron-builder/issues/1308#issuecomment-282847594), so, -ov must be specified


    const args = ["convert", tempDmg, "-ov", "-format", specification.format, "-o", artifactPath];

    if (specification.format === "UDZO") {
      args.push("-imagekey", `zlib-level=${process.env.ELECTRON_BUILDER_COMPRESSION_LEVEL || "9"}`);
    }

    await (0, _builderUtil().spawn)("hdiutil", addLogLevel(args));

    if (this.options.internetEnabled && parseInt(require("os").release().split(".")[0], 10) < 19) {
      await (0, _builderUtil().exec)("hdiutil", addLogLevel(["internet-enable"]).concat(artifactPath));
    }

    const licenseData = await (0, _dmgLicense().addLicenseToDmg)(packager, artifactPath);

    if (packager.packagerOptions.effectiveOptionComputed != null) {
      await packager.packagerOptions.effectiveOptionComputed({
        licenseData
      });
    }

    if (this.options.sign === true) {
      await this.signDmg(artifactPath);
    }

    const safeArtifactName = packager.computeSafeArtifactName(artifactName, "dmg");
    const updateInfo = this.options.writeUpdateInfo === false ? null : await (0, _differentialUpdateInfoBuilder().createBlockmap)(artifactPath, this, packager, safeArtifactName);
    await packager.info.callArtifactBuildCompleted({
      file: artifactPath,
      safeArtifactName,
      target: this,
      arch,
      packager,
      isWriteUpdateInfo: updateInfo != null,
      updateInfo
    });
  }

  async signDmg(artifactPath) {
    if (!(0, _macCodeSign().isSignAllowed)(false)) {
      return;
    }

    const packager = this.packager;
    const qualifier = packager.platformSpecificBuildOptions.identity; // explicitly disabled if set to null

    if (qualifier === null) {
      // macPackager already somehow handle this situation, so, here just return
      return;
    }

    const keychainFile = (await packager.codeSigningInfo.value).keychainFile;
    const certificateType = "Developer ID Application";
    let identity = await (0, _macCodeSign().findIdentity)(certificateType, qualifier, keychainFile);

    if (identity == null) {
      identity = await (0, _macCodeSign().findIdentity)("Mac Developer", qualifier, keychainFile);

      if (identity == null) {
        return;
      }
    }

    const args = ["--sign", identity.hash];

    if (keychainFile != null) {
      args.push("--keychain", keychainFile);
    }

    args.push(artifactPath);
    await (0, _builderUtil().exec)("codesign", args);
  }

  computeVolumeName(arch, custom) {
    const appInfo = this.packager.appInfo;
    const shortVersion = this.packager.platformSpecificBuildOptions.bundleShortVersion || appInfo.version;
    const archString = (0, _builderUtil().getArchSuffix)(arch, this.packager.platformSpecificBuildOptions.defaultArch);

    if (custom == null) {
      return `${appInfo.productFilename} ${shortVersion}${archString}`;
    }

    return custom.replace(/\${arch}/g, archString).replace(/\${shortVersion}/g, shortVersion).replace(/\${version}/g, appInfo.version).replace(/\${name}/g, appInfo.name).replace(/\${productName}/g, appInfo.productName);
  } // public to test


  async computeDmgOptions() {
    const packager = this.packager;
    const specification = { ...this.options
    };

    if (specification.icon == null && specification.icon !== null) {
      specification.icon = await packager.getIconPath();
    }

    if (specification.icon != null && (0, _builderUtil().isEmptyOrSpaces)(specification.icon)) {
      throw new (_builderUtil().InvalidConfigurationError)("dmg.icon cannot be specified as empty string");
    }

    const background = specification.background;

    if (specification.backgroundColor != null) {
      if (background != null) {
        throw new (_builderUtil().InvalidConfigurationError)("Both dmg.backgroundColor and dmg.background are specified — please set the only one");
      }
    } else if (background == null) {
      specification.background = await (0, _dmgUtil().computeBackground)(packager);
    } else {
      specification.background = path.resolve(packager.info.projectDir, background);
    }

    if (specification.format == null) {
      if (process.env.ELECTRON_BUILDER_COMPRESSION_LEVEL != null) {
        specification.format = "UDZO";
      } else if (packager.compression === "store") {
        specification.format = "UDRO";
      } else {
        specification.format = packager.compression === "maximum" ? "UDBZ" : "UDZO";
      }
    }

    if (specification.contents == null) {
      specification.contents = [{
        x: 130,
        y: 220
      }, {
        x: 410,
        y: 220,
        type: "link",
        path: "/Applications"
      }];
    }

    return specification;
  }

}

exports.DmgTarget = DmgTarget;

async function createStageDmg(tempDmg, appPath, volumeName) {
  //noinspection SpellCheckingInspection
  const imageArgs = addLogLevel(["create", "-srcfolder", appPath, "-volname", volumeName, "-anyowners", "-nospotlight", "-format", "UDRW"]);

  if (_builderUtil().log.isDebugEnabled) {
    imageArgs.push("-debug");
  }

  imageArgs.push("-fs", "HFS+", "-fsargs", "-c c=64,a=16,e=16");
  imageArgs.push(tempDmg); // The reason for retrying up to ten times is that hdiutil create in some cases fail to unmount due to "resource busy".
  // https://github.com/electron-userland/electron-builder/issues/5431

  await (0, _builderUtil().retry)(() => (0, _builderUtil().spawn)("hdiutil", imageArgs), 5, 1000);
  return tempDmg;
}

function addLogLevel(args) {
  args.push(process.env.DEBUG_DMG === "true" ? "-verbose" : "-quiet");
  return args;
}

async function computeAssetSize(cancellationToken, dmgFile, specification, backgroundFile) {
  const asyncTaskManager = new (_builderUtil().AsyncTaskManager)(cancellationToken);
  asyncTaskManager.addTask((0, _fsExtra().stat)(dmgFile));

  if (specification.icon != null) {
    asyncTaskManager.addTask((0, _fs().statOrNull)(specification.icon));
  }

  if (backgroundFile != null) {
    asyncTaskManager.addTask((0, _fsExtra().stat)(backgroundFile));
  }

  let result = 32 * 1024;

  for (const stat of await asyncTaskManager.awaitTasks()) {
    if (stat != null) {
      result += stat.size;
    }
  }

  return result;
}

async function customizeDmg(volumePath, specification, packager, backgroundFile) {
  const window = specification.window;
  const env = { ...process.env,
    volumePath,
    appFileName: `${packager.appInfo.productFilename}.app`,
    iconSize: specification.iconSize || 80,
    iconTextSize: specification.iconTextSize || 12,
    PYTHONIOENCODING: "utf8"
  };

  if (specification.backgroundColor != null || specification.background == null) {
    env.backgroundColor = specification.backgroundColor || "#ffffff";

    if (window != null) {
      env.windowX = (window.x == null ? 100 : window.x).toString();
      env.windowY = (window.y == null ? 400 : window.y).toString();
      env.windowWidth = (window.width || 540).toString();
      env.windowHeight = (window.height || 380).toString();
    }
  } else {
    delete env.backgroundColor;
  }

  const args = ["dmg", "--volume", volumePath];

  if (specification.icon != null) {
    args.push("--icon", await packager.getResource(specification.icon));
  }

  if (backgroundFile != null) {
    args.push("--background", backgroundFile);
  }

  const data = await (0, _appBuilder().executeAppBuilderAsJson)(args);

  if (data.backgroundWidth != null) {
    env.windowWidth = window == null ? null : window.width;
    env.windowHeight = window == null ? null : window.height;

    if (env.windowWidth == null) {
      env.windowWidth = data.backgroundWidth.toString();
    }

    if (env.windowHeight == null) {
      env.windowHeight = data.backgroundHeight.toString();
    }

    if (env.windowX == null) {
      env.windowX = 400;
    }

    if (env.windowY == null) {
      env.windowY = Math.round((1440 - env.windowHeight) / 2).toString();
    }
  }

  Object.assign(env, data);
  const asyncTaskManager = new (_builderUtil().AsyncTaskManager)(packager.info.cancellationToken);
  env.iconLocations = await computeDmgEntries(specification, volumePath, packager, asyncTaskManager);
  await asyncTaskManager.awaitTasks();
  await (0, _builderUtil().exec)("/usr/bin/python", [path.join((0, _dmgUtil().getDmgVendorPath)(), "dmgbuild/core.py")], {
    cwd: (0, _dmgUtil().getDmgVendorPath)(),
    env
  });
  return packager.packagerOptions.effectiveOptionComputed == null || !(await packager.packagerOptions.effectiveOptionComputed({
    volumePath,
    specification,
    packager
  }));
}

async function computeDmgEntries(specification, volumePath, packager, asyncTaskManager) {
  let result = "";

  for (const c of specification.contents) {
    if (c.path != null && c.path.endsWith(".app") && c.type !== "link") {
      _builderUtil().log.warn({
        path: c.path,
        reason: "actual path to app will be used instead"
      }, "do not specify path for application");
    }

    const entryPath = c.path || `${packager.appInfo.productFilename}.app`;
    const entryName = c.name || path.basename(entryPath);

    if (result.length !== 0) {
      result += ",\n";
    }

    result += `'${entryName}': (${c.x}, ${c.y})`;

    if (c.type === "link") {
      asyncTaskManager.addTask((0, _builderUtil().exec)("ln", ["-s", `/${entryPath.startsWith("/") ? entryPath.substring(1) : entryPath}`, `${volumePath}/${entryName}`]));
    } // use c.path instead of entryPath (to be sure that this logic is not applied to .app bundle) https://github.com/electron-userland/electron-builder/issues/2147
    else if (!(0, _builderUtil().isEmptyOrSpaces)(c.path) && (c.type === "file" || c.type === "dir")) {
        const source = await packager.getResource(c.path);

        if (source == null) {
          _builderUtil().log.warn({
            entryPath,
            reason: "doesn't exist"
          }, "skipped DMG item copying");

          continue;
        }

        const destination = `${volumePath}/${entryName}`;
        asyncTaskManager.addTask(c.type === "dir" || (await (0, _fsExtra().stat)(source)).isDirectory() ? (0, _fs().copyDir)(source, destination) : (0, _fs().copyFile)(source, destination));
      }
  }

  return result;
}

async function transformBackgroundFileIfNeed(file, tmpDir) {
  if (file.endsWith(".tiff") || file.endsWith(".TIFF")) {
    return file;
  }

  const retinaFile = file.replace(/\.([a-z]+)$/, "@2x.$1");

  if (await (0, _fs().exists)(retinaFile)) {
    const tiffFile = await tmpDir.getTempFile({
      suffix: ".tiff"
    });
    await (0, _builderUtil().exec)("tiffutil", ["-cathidpicheck", file, retinaFile, "-out", tiffFile]);
    return tiffFile;
  }

  return file;
} 
// __ts-babel@6.0.4
//# sourceMappingURL=dmg.js.map