1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.TargetType = void 0;
7exports.findApplicationTargetWithDependenciesAsync = findApplicationTargetWithDependenciesAsync;
8exports.findFirstNativeTarget = findFirstNativeTarget;
9exports.findNativeTargetByName = findNativeTargetByName;
10exports.findSignableTargets = findSignableTargets;
11exports.getNativeTargets = getNativeTargets;
12exports.getXCBuildConfigurationFromPbxproj = getXCBuildConfigurationFromPbxproj;
13exports.isTargetOfType = isTargetOfType;
14function _BuildScheme() {
15  const data = require("./BuildScheme");
16  _BuildScheme = function () {
17    return data;
18  };
19  return data;
20}
21function _Xcodeproj() {
22  const data = require("./utils/Xcodeproj");
23  _Xcodeproj = function () {
24    return data;
25  };
26  return data;
27}
28function _string() {
29  const data = require("./utils/string");
30  _string = function () {
31    return data;
32  };
33  return data;
34}
35let TargetType;
36exports.TargetType = TargetType;
37(function (TargetType) {
38  TargetType["APPLICATION"] = "com.apple.product-type.application";
39  TargetType["EXTENSION"] = "com.apple.product-type.app-extension";
40  TargetType["WATCH"] = "com.apple.product-type.application.watchapp";
41  TargetType["APP_CLIP"] = "com.apple.product-type.application.on-demand-install-capable";
42  TargetType["STICKER_PACK_EXTENSION"] = "com.apple.product-type.app-extension.messages-sticker-pack";
43  TargetType["OTHER"] = "other";
44})(TargetType || (exports.TargetType = TargetType = {}));
45function getXCBuildConfigurationFromPbxproj(project, {
46  targetName,
47  buildConfiguration = 'Release'
48} = {}) {
49  const [, nativeTarget] = targetName ? findNativeTargetByName(project, targetName) : findFirstNativeTarget(project);
50  const [, xcBuildConfiguration] = (0, _Xcodeproj().getBuildConfigurationForListIdAndName)(project, {
51    configurationListId: nativeTarget.buildConfigurationList,
52    buildConfiguration
53  });
54  return xcBuildConfiguration !== null && xcBuildConfiguration !== void 0 ? xcBuildConfiguration : null;
55}
56async function findApplicationTargetWithDependenciesAsync(projectRoot, scheme) {
57  const applicationTargetName = await (0, _BuildScheme().getApplicationTargetNameForSchemeAsync)(projectRoot, scheme);
58  const project = (0, _Xcodeproj().getPbxproj)(projectRoot);
59  const [, applicationTarget] = findNativeTargetByName(project, applicationTargetName);
60  const dependencies = getTargetDependencies(project, applicationTarget);
61  return {
62    name: (0, _string().trimQuotes)(applicationTarget.name),
63    type: TargetType.APPLICATION,
64    dependencies
65  };
66}
67function getTargetDependencies(project, parentTarget) {
68  if (!parentTarget.dependencies || parentTarget.dependencies.length === 0) {
69    return undefined;
70  }
71  return parentTarget.dependencies.map(({
72    value
73  }) => {
74    const {
75      target: targetId
76    } = project.getPBXGroupByKeyAndType(value, 'PBXTargetDependency');
77    const [, target] = findNativeTargetById(project, targetId);
78    const type = isTargetOfType(target, TargetType.EXTENSION) ? TargetType.EXTENSION : TargetType.OTHER;
79    return {
80      name: (0, _string().trimQuotes)(target.name),
81      type,
82      dependencies: getTargetDependencies(project, target)
83    };
84  });
85}
86function isTargetOfType(target, targetType) {
87  return (0, _string().trimQuotes)(target.productType) === targetType;
88}
89function getNativeTargets(project) {
90  const section = project.pbxNativeTargetSection();
91  return Object.entries(section).filter(_Xcodeproj().isNotComment);
92}
93function findSignableTargets(project) {
94  const targets = getNativeTargets(project);
95  const signableTargetTypes = [TargetType.APPLICATION, TargetType.APP_CLIP, TargetType.EXTENSION, TargetType.WATCH, TargetType.STICKER_PACK_EXTENSION];
96  const applicationTargets = targets.filter(([, target]) => {
97    for (const targetType of signableTargetTypes) {
98      if (isTargetOfType(target, targetType)) {
99        return true;
100      }
101    }
102    return false;
103  });
104  if (applicationTargets.length === 0) {
105    throw new Error(`Could not find any signable targets in project.pbxproj`);
106  }
107  return applicationTargets;
108}
109function findFirstNativeTarget(project) {
110  const targets = getNativeTargets(project);
111  const applicationTargets = targets.filter(([, target]) => isTargetOfType(target, TargetType.APPLICATION));
112  if (applicationTargets.length === 0) {
113    throw new Error(`Could not find any application target in project.pbxproj`);
114  }
115  return applicationTargets[0];
116}
117function findNativeTargetByName(project, targetName) {
118  const nativeTargets = getNativeTargets(project);
119  const nativeTargetEntry = nativeTargets.find(([, i]) => (0, _string().trimQuotes)(i.name) === targetName);
120  if (!nativeTargetEntry) {
121    throw new Error(`Could not find target '${targetName}' in project.pbxproj`);
122  }
123  return nativeTargetEntry;
124}
125function findNativeTargetById(project, targetId) {
126  const nativeTargets = getNativeTargets(project);
127  const nativeTargetEntry = nativeTargets.find(([key]) => key === targetId);
128  if (!nativeTargetEntry) {
129    throw new Error(`Could not find target with id '${targetId}' in project.pbxproj`);
130  }
131  return nativeTargetEntry;
132}
133//# sourceMappingURL=Target.js.map