text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
      value: true
  });
  exports.createPublisher = exports.build = undefined;
  
  var _bluebirdLstC;
  
  function _load_bluebirdLstC() {
      return _bluebirdLstC = require("bluebird-lst-c");
  }
  
  var _bluebirdLstC2;
  
  function _load_bluebirdLstC2() {
      return _bluebirdLstC2 = _interopRequireDefault(require("bluebird-lst-c"));
  }
  
  let build = exports.build = (() => {
      var _ref = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (rawOptions) {
          const options = normalizeOptions(rawOptions || {});
          if (options.cscLink === undefined && !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.CSC_LINK)) {
              options.cscLink = process.env.CSC_LINK;
          }
          if (options.cscInstallerLink === undefined && !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.CSC_INSTALLER_LINK)) {
              options.cscInstallerLink = process.env.CSC_INSTALLER_LINK;
          }
          if (options.cscKeyPassword === undefined && !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.CSC_KEY_PASSWORD)) {
              options.cscKeyPassword = process.env.CSC_KEY_PASSWORD;
          }
          if (options.cscInstallerKeyPassword === undefined && !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.CSC_INSTALLER_KEY_PASSWORD)) {
              options.cscInstallerKeyPassword = process.env.CSC_INSTALLER_KEY_PASSWORD;
          }
          if (options.draft === undefined && !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.EP_DRAFT)) {
              options.draft = process.env.EP_DRAFT.toLowerCase() === "true";
          }
          if (options.prerelease === undefined && !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.EP_PRELEASE)) {
              options.prerelease = process.env.EP_PRELEASE.toLowerCase() === "true";
          }
          let isPublishOptionGuessed = false;
          if (options.publish === undefined) {
              if (process.env.npm_lifecycle_event === "release") {
                  options.publish = "always";
              } else if (isAuthTokenSet()) {
                  const tag = process.env.TRAVIS_TAG || process.env.APPVEYOR_REPO_TAG_NAME || process.env.CIRCLE_TAG;
                  if (!(0, (_util || _load_util()).isEmptyOrSpaces)(tag)) {
                      (0, (_log || _load_log()).log)(`Tag ${ tag } is defined, so artifacts will be published`);
                      options.publish = "onTag";
                      isPublishOptionGuessed = true;
                  } else if ((_isCi || _load_isCi()).default) {
                      (0, (_log || _load_log()).log)("CI detected, so artifacts will be published if draft release exists");
                      options.publish = "onTagOrDraft";
                      isPublishOptionGuessed = true;
                  }
              }
          }
          const packager = new (_packager || _load_packager()).Packager(options);
          const publishTasks = [];
          if (options.publish != null && options.publish !== "never") {
              // todo if token set as option
              if (isAuthTokenSet()) {
                  publishManager(packager, publishTasks, options, isPublishOptionGuessed);
              } else if ((_isCi || _load_isCi()).default) {
                  (0, (_log || _load_log()).log)(`CI detected, publish is set to ${ options.publish }, but neither GH_TOKEN nor BT_TOKEN is not set, so artifacts will be not published`);
              }
          }
          const artifactPaths = [];
          packager.artifactCreated(function (event) {
              if (event.file != null) {
                  artifactPaths.push(event.file);
              }
          });
          return yield (0, (_promise || _load_promise()).executeFinally)(packager.build().then(function () {
              return artifactPaths;
          }), function (errorOccurred) {
              if (errorOccurred) {
                  for (let task of publishTasks) {
                      task.cancel();
                  }
                  return (_bluebirdLstC2 || _load_bluebirdLstC2()).default.resolve(null);
              } else {
                  return (_bluebirdLstC2 || _load_bluebirdLstC2()).default.all(publishTasks);
              }
          });
      });
  
      return function build(_x) {
          return _ref.apply(this, arguments);
      };
  })();
  
  // visible only for tests
  // call only from this file or from tests
  let createPublisher = exports.createPublisher = (() => {
      var _ref2 = (0, (_bluebirdLstC || _load_bluebirdLstC()).coroutine)(function* (packager, publishConfig, options) {
          let isPublishOptionGuessed = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  
          const config = yield (0, (_platformPackager || _load_platformPackager()).getResolvedPublishConfig)(packager, publishConfig, isPublishOptionGuessed);
          if (config == null) {
              return null;
          }
          const version = packager.metadata.version;
          if (publishConfig.provider === "github") {
              const githubInfo = config;
              (0, (_log || _load_log()).log)(`Creating Github Publisher — owner: ${ githubInfo.owner }, project: ${ githubInfo.repo }, version: ${ version }`);
              return new (_gitHubPublisher || _load_gitHubPublisher()).GitHubPublisher(githubInfo, version, options, isPublishOptionGuessed);
          }
          if (publishConfig.provider === "bintray") {
              const bintrayInfo = config;
              (0, (_log || _load_log()).log)(`Creating Bintray Publisher — user: ${ bintrayInfo.user || bintrayInfo.owner }, owner: ${ bintrayInfo.owner },  package: ${ bintrayInfo.package }, repository: ${ bintrayInfo.repo }, version: ${ version }`);
              return new (_BintrayPublisher || _load_BintrayPublisher()).BintrayPublisher(bintrayInfo, version, options);
          }
          return null;
      });
  
      return function createPublisher(_x2, _x3, _x4, _x5) {
          return _ref2.apply(this, arguments);
      };
  })();
  //# sourceMappingURL=builder.js.map
  
  
  exports.normalizeOptions = normalizeOptions;
  exports.createTargets = createTargets;
  
  var _packager;
  
  function _load_packager() {
      return _packager = require("./packager");
  }
  
  var _platformPackager;
  
  function _load_platformPackager() {
      return _platformPackager = require("./platformPackager");
  }
  
  var _gitHubPublisher;
  
  function _load_gitHubPublisher() {
      return _gitHubPublisher = require("./publish/gitHubPublisher");
  }
  
  var _promise;
  
  function _load_promise() {
      return _promise = require("./util/promise");
  }
  
  var _util;
  
  function _load_util() {
      return _util = require("./util/util");
  }
  
  var _log;
  
  function _load_log() {
      return _log = require("./util/log");
  }
  
  var _metadata;
  
  function _load_metadata() {
      return _metadata = require("./metadata");
  }
  
  var _targetFactory;
  
  function _load_targetFactory() {
      return _targetFactory = require("./targets/targetFactory");
  }
  
  var _BintrayPublisher;
  
  function _load_BintrayPublisher() {
      return _BintrayPublisher = require("./publish/BintrayPublisher");
  }
  
  var _isCi;
  
  function _load_isCi() {
      return _isCi = _interopRequireDefault(require("is-ci"));
  }
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function addValue(map, key, value) {
      const list = map.get(key);
      if (list == null) {
          map.set(key, [value]);
      } else {
          list.push(value);
      }
  }
  function normalizeOptions(args) {
      if (args.targets != null) {
          return args;
      }
      let targets = new Map();
      function processTargets(platform, types) {
          if (args.platform != null) {
              throw new Error(`--platform cannot be used if --${ platform.buildConfigurationKey } is passed`);
          }
          if (args.arch != null) {
              throw new Error(`--arch cannot be used if --${ platform.buildConfigurationKey } is passed`);
          }
          function commonArch() {
              const result = Array();
              if (args.x64) {
                  result.push((_metadata || _load_metadata()).Arch.x64);
              }
              if (args.armv7l) {
                  result.push((_metadata || _load_metadata()).Arch.armv7l);
              }
              if (args.ia32) {
                  result.push((_metadata || _load_metadata()).Arch.ia32);
              }
              return result.length === 0 ? [(0, (_metadata || _load_metadata()).archFromString)(process.arch)] : result;
          }
          let archToType = targets.get(platform);
          if (archToType == null) {
              archToType = new Map();
              targets.set(platform, archToType);
          }
          if (types.length === 0) {
              const defaultTargetValue = args.dir ? [(_targetFactory || _load_targetFactory()).DIR_TARGET] : [];
              if (platform === (_metadata || _load_metadata()).Platform.MAC) {
                  archToType.set((_metadata || _load_metadata()).Arch.x64, defaultTargetValue);
              } else {
                  for (let arch of commonArch()) {
                      archToType.set(arch, defaultTargetValue);
                  }
              }
              return;
          }
          for (let type of types) {
              let arch;
              if (platform === (_metadata || _load_metadata()).Platform.MAC) {
                  arch = "x64";
                  addValue(archToType, (_metadata || _load_metadata()).Arch.x64, type);
              } else {
                  const suffixPos = type.lastIndexOf(":");
                  if (suffixPos > 0) {
                      addValue(archToType, (0, (_metadata || _load_metadata()).archFromString)(type.substring(suffixPos + 1)), type.substring(0, suffixPos));
                  } else {
                      for (let arch of commonArch()) {
                          addValue(archToType, arch, type);
                      }
                  }
              }
          }
      }
      if (args.mac != null) {
          processTargets((_metadata || _load_metadata()).Platform.MAC, args.mac);
      }
      if (args.linux != null) {
          processTargets((_metadata || _load_metadata()).Platform.LINUX, args.linux);
      }
      if (args.win != null) {
          processTargets((_metadata || _load_metadata()).Platform.WINDOWS, args.win);
      }
      if (targets.size === 0) {
          if (args.platform == null && args.arch == null) {
              processTargets((_metadata || _load_metadata()).Platform.current(), []);
          } else {
              targets = createTargets((0, (_packager || _load_packager()).normalizePlatforms)(args.platform), args.dir ? (_targetFactory || _load_targetFactory()).DIR_TARGET : null, args.arch);
          }
      }
      const result = Object.assign({}, args);
      result.targets = targets;
      delete result.dir;
      delete result.mac;
      delete result.linux;
      delete result.win;
      delete result.platform;
      delete result.arch;
      const r = result;
      delete r.em;
      delete r.m;
      delete r.o;
      delete r.l;
      delete r.w;
      delete r.windows;
      delete r.macos;
      delete r.$0;
      delete r._;
      delete r.version;
      delete r.help;
      delete result.ia32;
      delete result.x64;
      delete result.armv7l;
      return result;
  }
  function createTargets(platforms, type, arch) {
      const targets = new Map();
      for (let platform of platforms) {
          const archs = platform === (_metadata || _load_metadata()).Platform.MAC ? [(_metadata || _load_metadata()).Arch.x64] : arch === "all" ? [(_metadata || _load_metadata()).Arch.x64, (_metadata || _load_metadata()).Arch.ia32] : [(0, (_metadata || _load_metadata()).archFromString)(arch == null ? process.arch : arch)];
          const archToType = new Map();
          targets.set(platform, archToType);
          for (let arch of archs) {
              archToType.set(arch, type == null ? [] : [type]);
          }
      }
      return targets;
  }
  
  function isAuthTokenSet() {
      return !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.GH_TOKEN) || !(0, (_util || _load_util()).isEmptyOrSpaces)(process.env.BT_TOKEN);
  }
  function publishManager(packager, publishTasks, options, isPublishOptionGuessed) {
      const nameToPublisher = new Map();
      function getOrCreatePublisher(publishConfig) {
          let publisher = nameToPublisher.get(publishConfig.provider);
          if (publisher == null) {
              publisher = createPublisher(packager, publishConfig, options, isPublishOptionGuessed);
              nameToPublisher.set(publishConfig.provider, publisher);
          }
          return publisher;
      }
      packager.artifactCreated(event => {
          const publishers = event.publishConfig == null ? (0, (_platformPackager || _load_platformPackager()).getPublishConfigs)(event.packager, event.packager.platformSpecificBuildOptions) : [event.publishConfig];
          // if explicitly set to null - do not publish
          if (publishers === null) {
              (0, (_util || _load_util()).debug)(`${ event.file } is not published: publish is set to null`);
              return;
          }
          for (let publishConfig of publishers) {
              const publisher = getOrCreatePublisher(publishConfig);
              if (publisher != null) {
                  publisher.then(it => {
                      if (it == null) {
                          return null;
                      }
                      if (event.file == null) {
                          return publishTasks.push(it.uploadData(event.data, event.artifactName));
                      } else {
                          return publishTasks.push(it.upload(event.file, event.artifactName));
                      }
                  });
              }
          }
      });
  }