-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-watch-app-service.ts
174 lines (157 loc) · 4.54 KB
/
ios-watch-app-service.ts
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
import * as path from "path";
import {
IOSDeviceTargets,
IOS_WATCHAPP_FOLDER,
IOS_WATCHAPP_EXTENSION_FOLDER,
IOSNativeTargetProductTypes,
IOSNativeTargetTypes,
} from "../constants";
import {
IIOSWatchAppService,
IIOSNativeTargetService,
IAddWatchAppFromPathOptions,
IRemoveWatchAppOptions,
IProjectData,
} from "../definitions/project";
import { IPlatformData } from "../definitions/platform";
import { IFileSystem } from "../common/declarations";
import { injector } from "../common/yok";
export class IOSWatchAppService implements IIOSWatchAppService {
private static WATCH_APP_IDENTIFIER = "watchkitapp";
private static WACTCH_EXTENSION_IDENTIFIER = "watchkitextension";
constructor(
protected $fs: IFileSystem,
protected $pbxprojDomXcode: IPbxprojDomXcode,
protected $xcode: IXcode,
private $iOSNativeTargetService: IIOSNativeTargetService
) {}
public async addWatchAppFromPath({
watchAppFolderPath,
projectData,
platformData,
pbxProjPath,
}: IAddWatchAppFromPathOptions): Promise<boolean> {
const targetUuids: string[] = [];
const appPath = path.join(watchAppFolderPath, IOS_WATCHAPP_FOLDER);
const extensionPath = path.join(
watchAppFolderPath,
IOS_WATCHAPP_EXTENSION_FOLDER
);
if (!this.$fs.exists(appPath) || !this.$fs.exists(extensionPath)) {
return false;
}
const appFolder = this.$iOSNativeTargetService.getTargetDirectories(
appPath
)[0];
const extensionFolder = this.$iOSNativeTargetService.getTargetDirectories(
extensionPath
)[0];
const project = new this.$xcode.project(pbxProjPath);
project.parseSync();
const watchApptarget = this.$iOSNativeTargetService.addTargetToProject(
appPath,
appFolder,
IOSNativeTargetTypes.watchApp,
project,
platformData,
project.getFirstTarget().uuid
);
this.configureTarget(
appFolder,
path.join(appPath, appFolder),
`${projectData.projectIdentifiers.ios}.${IOSWatchAppService.WATCH_APP_IDENTIFIER}`,
"watchapp.json",
watchApptarget,
project
);
targetUuids.push(watchApptarget.uuid);
const watchExtensionTarget = this.$iOSNativeTargetService.addTargetToProject(
extensionPath,
extensionFolder,
IOSNativeTargetTypes.watchExtension,
project,
platformData,
watchApptarget.uuid
);
this.configureTarget(
extensionFolder,
path.join(extensionPath, extensionFolder),
`${projectData.projectIdentifiers.ios}.${IOSWatchAppService.WATCH_APP_IDENTIFIER}.${IOSWatchAppService.WACTCH_EXTENSION_IDENTIFIER}`,
"extension.json",
watchExtensionTarget,
project
);
targetUuids.push(watchExtensionTarget.uuid);
this.$fs.writeFile(
pbxProjPath,
project.writeSync({ omitEmptyValues: true })
);
this.$iOSNativeTargetService.prepareSigning(
targetUuids,
projectData,
pbxProjPath
);
return true;
}
public removeWatchApp({ pbxProjPath }: IRemoveWatchAppOptions): void {
const project = new this.$xcode.project(pbxProjPath);
project.parseSync();
project.removeTargetsByProductType(IOSNativeTargetProductTypes.watchApp);
project.removeTargetsByProductType(
IOSNativeTargetProductTypes.watchExtension
);
this.$fs.writeFile(
pbxProjPath,
project.writeSync({ omitEmptyValues: true })
);
}
public hasWatchApp(
platformData: IPlatformData,
projectData: IProjectData
): boolean {
const watchAppPath = path.join(
projectData.getAppResourcesDirectoryPath(),
platformData.normalizedPlatformName,
IOS_WATCHAPP_FOLDER
);
return this.$fs.exists(watchAppPath);
}
private configureTarget(
targetName: string,
targetPath: string,
identifier: string,
configurationFileName: string,
target: IXcode.target,
project: IXcode.project
) {
const targetConfigurationJsonPath = path.join(
targetPath,
configurationFileName
);
const identifierParts = identifier.split(".");
identifierParts.pop();
const wkAppBundleIdentifier = identifierParts.join(".");
this.$iOSNativeTargetService.setXcodeTargetBuildConfigurationProperties(
[
{ name: "PRODUCT_BUNDLE_IDENTIFIER", value: identifier },
{ name: "SDKROOT", value: "watchos" },
{ name: "TARGETED_DEVICE_FAMILY", value: IOSDeviceTargets.watchos },
{ name: "WATCHOS_DEPLOYMENT_TARGET", value: 5.2 },
{ name: "WK_APP_BUNDLE_IDENTIFIER", value: wkAppBundleIdentifier },
],
targetName,
project
);
this.$iOSNativeTargetService.setConfigurationsFromJsonFile(
targetConfigurationJsonPath,
target.uuid,
targetName,
project
);
project.addToHeaderSearchPaths(
targetPath,
target.pbxNativeTarget.productName
);
}
}
injector.register("iOSWatchAppService", IOSWatchAppService);