-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-project-service.ts
275 lines (228 loc) · 12.3 KB
/
ios-project-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
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
///<reference path="../.d.ts"/>
"use strict";
import Future = require("fibers/future");
import path = require("path");
import shell = require("shelljs");
import util = require("util");
import xcode = require("xcode");
import constants = require("./../constants");
import helpers = require("./../common/helpers");
import options = require("../common/options");
class IOSProjectService implements IPlatformProjectService {
private static XCODE_PROJECT_EXT_NAME = ".xcodeproj";
private static XCODEBUILD_MIN_VERSION = "6.0";
private static IOS_PROJECT_NAME_PLACEHOLDER = "__PROJECT_NAME__";
constructor(private $projectData: IProjectData,
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $errors: IErrors,
private $logger: ILogger,
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
private $npm: INodePackageManager) { }
public get platformData(): IPlatformData {
var projectRoot = path.join(this.$projectData.platformsDir, "ios");
return {
frameworkPackageName: "tns-ios",
normalizedPlatformName: "iOS",
appDestinationDirectoryPath: path.join(projectRoot, this.$projectData.projectName),
appResourcesDestinationDirectoryPath: path.join(projectRoot, this.$projectData.projectName, "Resources", "icons"),
platformProjectService: this,
emulatorServices: this.$iOSEmulatorServices,
projectRoot: projectRoot,
deviceBuildOutputPath: path.join(projectRoot, "build", "device"),
emulatorBuildOutputPath: path.join(projectRoot, "build", "emulator"),
validPackageNamesForDevice: [
this.$projectData.projectName + ".ipa"
],
validPackageNamesForEmulator: [
this.$projectData.projectName + ".app"
],
frameworkFilesExtensions: [".a", ".framework", ".bin"],
frameworkDirectoriesExtensions: [".framework"],
frameworkDirectoriesNames: ["Metadata"],
targetedOS: ['darwin']
};
}
public validate(): IFuture<void> {
return (() => {
try {
this.$childProcess.exec("which xcodebuild").wait();
} catch(error) {
this.$errors.fail("Xcode is not installed. Make sure you have Xcode installed and added to your PATH");
}
var xcodeBuildVersion = this.$childProcess.exec("xcodebuild -version | head -n 1 | sed -e 's/Xcode //'").wait();
var splitedXcodeBuildVersion = xcodeBuildVersion.split(".");
if(splitedXcodeBuildVersion.length === 3) {
xcodeBuildVersion = util.format("%s.%s", splitedXcodeBuildVersion[0], splitedXcodeBuildVersion[1]);
}
if(helpers.versionCompare(xcodeBuildVersion, IOSProjectService.XCODEBUILD_MIN_VERSION) < 0) {
this.$errors.fail("NativeScript can only run in Xcode version %s or greater", IOSProjectService.XCODEBUILD_MIN_VERSION);
}
}).future<void>()();
}
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
return (() => {
if(options.symlink) {
this.$fs.ensureDirectoryExists(path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
var xcodeProjectName = util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER);
shell.cp("-R", path.join(frameworkDir, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "*"), path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), projectRoot);
var directoryContent = this.$fs.readDirectory(frameworkDir).wait();
var frameworkFiles = _.difference(directoryContent, [IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, xcodeProjectName]);
_.each(frameworkFiles, (file: string) => {
this.$fs.symlink(path.join(frameworkDir, file), path.join(projectRoot, file)).wait();
});
} else {
shell.cp("-R", path.join(frameworkDir, "*"), projectRoot);
}
}).future<void>()();
}
public interpolateData(projectRoot: string): IFuture<void> {
return (() => {
var infoPlistFilePath = path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, util.format("%s-%s", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "Info.plist"));
shell.sed('-i', "__CFBUNDLEIDENTIFIER__", this.$projectData.projectId, infoPlistFilePath);
this.replaceFileName("-Info.plist", path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
this.replaceFileName("-Prefix.pch", path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
this.replaceFileName(IOSProjectService.XCODE_PROJECT_EXT_NAME, projectRoot).wait();
var pbxprojFilePath = path.join(projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
this.replaceFileContent(pbxprojFilePath).wait();
}).future<void>()();
}
public afterCreateProject(projectRoot: string): IFuture<void> {
return (() => {
this.$fs.rename(path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER),
path.join(projectRoot, this.$projectData.projectName)).wait();
}).future<void>()();
}
public buildProject(projectRoot: string): IFuture<void> {
return (() => {
var basicArgs = [
"-project", path.join(projectRoot, this.$projectData.projectName + ".xcodeproj"),
"-target", this.$projectData.projectName,
"-configuration", options.release ? "Release" : "Debug",
"build",
'SHARED_PRECOMPS_DIR=' + path.join(projectRoot, 'build', 'sharedpch')
];
var args: string[] = [];
if(options.forDevice) {
args = basicArgs.concat([
"-xcconfig", path.join(projectRoot, this.$projectData.projectName, "build.xcconfig"),
"-sdk", "iphoneos",
'ARCHS=armv7 arm64',
'VALID_ARCHS=armv7 arm64',
"CONFIGURATION_BUILD_DIR=" + path.join(projectRoot, "build", "device")
]);
} else {
args = basicArgs.concat([
"-sdk", "iphonesimulator",
"-arch", "i386",
"VALID_ARCHS=\"i386\"",
"CONFIGURATION_BUILD_DIR=" + path.join(projectRoot, "build", "emulator")
]);
}
this.$childProcess.spawnFromEvent("xcodebuild", args, "exit", {cwd: options, stdio: 'inherit'}).wait();
if(options.forDevice) {
var buildOutputPath = path.join(projectRoot, "build", "device");
// Produce ipa file
var xcrunArgs = [
"-sdk", "iphoneos",
"PackageApplication",
"-v", path.join(buildOutputPath, this.$projectData.projectName + ".app"),
"-o", path.join(buildOutputPath, this.$projectData.projectName + ".ipa")
];
this.$childProcess.spawnFromEvent("xcrun", xcrunArgs, "exit", {cwd: options, stdio: 'inherit'}).wait();
}
}).future<void>()();
}
public isPlatformPrepared(projectRoot: string): IFuture<boolean> {
return this.$fs.exists(path.join(projectRoot, this.$projectData.projectName, constants.APP_FOLDER_NAME));
}
public addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void> {
return (() => {
this.validateDynamicFramework(libraryPath).wait();
var umbrellaHeader = this.getUmbrellaHeaderFromDynamicFramework(libraryPath).wait();
var frameworkName = path.basename(libraryPath, path.extname(libraryPath));
var targetPath = path.join(this.$projectData.projectDir, "lib", platformData.normalizedPlatformName, frameworkName);
this.$fs.ensureDirectoryExists(targetPath).wait();
shell.cp("-R", libraryPath, targetPath);
var pbxProjPath = path.join(platformData.projectRoot, this.$projectData.projectName + ".xcodeproj", "project.pbxproj");
var project = new xcode.project(pbxProjPath);
project.parseSync();
project.addFramework(path.join(targetPath, frameworkName + ".framework"), { customFramework: true, embed: true });
project.updateBuildProperty("IPHONEOS_DEPLOYMENT_TARGET", "8.0");
this.$fs.writeFile(pbxProjPath, project.writeSync()).wait();
this.$logger.info("The iOS Deployment Target is now 8.0 in order to support Cocoa Touch Frameworks.");
}).future<void>()();
}
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
return (() => {
var currentXcodeProjectFile = this.buildPathToXcodeProjectFile(currentVersion);
var currentXcodeProjectFileContent = this.$fs.readFile(currentXcodeProjectFile).wait();
var newXcodeProjectFile = this.buildPathToXcodeProjectFile(newVersion);
var newXcodeProjectFileContent = this.$fs.readFile(newXcodeProjectFile).wait();
return currentXcodeProjectFileContent === newXcodeProjectFileContent;
}).future<boolean>()();
}
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
return (() => {
// Copy old file to options["profile-dir"]
var sourceFile = path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName));
var destinationFile = path.join(options.profileDir, "xcodeproj");
this.$fs.deleteDirectory(destinationFile).wait();
shell.cp("-R", path.join(sourceFile, "*"), destinationFile);
this.$logger.info("Backup file %s at location %s", sourceFile, destinationFile);
this.$fs.deleteDirectory(path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName))).wait();
// Copy xcodeProject file
var cachedPackagePath = path.join(this.$npm.getCachedPackagePath(this.platformData.frameworkPackageName, newVersion), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
shell.cp("-R", path.join(cachedPackagePath, "*"), path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName)));
this.$logger.info("Copied from %s at %s.", cachedPackagePath, this.platformData.projectRoot);
var pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
this.replaceFileContent(pbxprojFilePath).wait();
}).future<void>()();
}
private buildPathToXcodeProjectFile(version: string): string {
return path.join(this.$npm.getCachedPackagePath(this.platformData.frameworkPackageName, version), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER), "project.pbxproj");
}
private validateDynamicFramework(libraryPath: string): IFuture<void> {
return (() => {
var infoPlistPath = path.join(libraryPath, "Info.plist");
if (!this.$fs.exists(infoPlistPath).wait()) {
this.$errors.failWithoutHelp("The bundle at %s does not contain an Info.plist file.", libraryPath);
}
var packageType = this.$childProcess.exec(`/usr/libexec/PlistBuddy -c "Print :CFBundlePackageType" "${infoPlistPath}"`).wait().trim();
if (packageType !== "FMWK") {
this.$errors.failWithoutHelp("The bundle at %s does not appear to be a dynamic framework.", libraryPath);
}
}).future<void>()();
}
private getUmbrellaHeaderFromDynamicFramework(libraryPath: string): IFuture<string> {
return (() => {
var modulemapPath = path.join(libraryPath, "Modules", "module.modulemap");
if (!this.$fs.exists(modulemapPath).wait()) {
this.$errors.failWithoutHelp("The framework at %s does not contain a module.modulemap file.", modulemapPath);
}
var modulemap = this.$fs.readText(modulemapPath).wait();
var umbrellaRegex = /umbrella header "(.+\.h)"/g;
var match = umbrellaRegex.exec(modulemap);
if (!match) {
this.$errors.failWithoutHelp("The modulemap at %s does not specify an umbrella header.", modulemapPath);
}
return match[1];
}).future<string>()();
}
private replaceFileContent(file: string): IFuture<void> {
return (() => {
var fileContent = this.$fs.readText(file).wait();
var replacedContent = helpers.stringReplaceAll(fileContent, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, this.$projectData.projectName);
this.$fs.writeFile(file, replacedContent).wait();
}).future<void>()();
}
private replaceFileName(fileNamePart: string, fileRootLocation: string): IFuture<void> {
return (() => {
var oldFileName = IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER + fileNamePart;
var newFileName = this.$projectData.projectName + fileNamePart;
this.$fs.rename(path.join(fileRootLocation, oldFileName), path.join(fileRootLocation, newFileName)).wait();
}).future<void>()();
}
}
$injector.register("iOSProjectService", IOSProjectService);