-
Notifications
You must be signed in to change notification settings - Fork 111
/
iosUtil.ts
335 lines (311 loc) · 11.3 KB
/
iosUtil.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
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
import { PackagePath } from './PackagePath'
import * as mustacheUtils from './mustacheUtils'
import shell from './shell'
import { manifest } from './Manifest'
import handleCopyDirective from './handleCopyDirective'
import log from './log'
import { downloadPluginSource, isDependencyJsApiImpl } from './utils'
import path from 'path'
import xcode from 'xcode-ern'
import fs from 'fs'
import _ from 'lodash'
import readDir = require('fs-readdir-recursive')
import kax from './kax'
export async function fillProjectHull(
pathSpec: {
rootDir: string
projectHullDir: string
outputDir: string
pluginsDownloadDirectory: string
},
projectSpec: {
projectName: string
},
plugins: PackagePath[],
mustacheView?: any,
composite?: any
) {
log.debug(`[=== Starting iOS framework project hull filling ===]`)
shell.pushd(pathSpec.rootDir)
try {
if (!fs.existsSync(pathSpec.outputDir)) {
shell.mkdir(pathSpec.outputDir)
}
shell.cp('-R', pathSpec.projectHullDir, pathSpec.outputDir)
if (mustacheView) {
log.debug(`iOS: reading template files to be rendered for plugins`)
const files = readDir(pathSpec.outputDir)
for (const file of files) {
if (file.endsWith('.h') || file.endsWith('.m')) {
const pathToOutputFile = path.join(pathSpec.outputDir, file)
await mustacheUtils.mustacheRenderToOutputFileUsingTemplateFile(
pathToOutputFile,
mustacheView,
pathToOutputFile
)
}
}
}
const projectPath = path.join(
pathSpec.outputDir,
`${projectSpec.projectName}.xcodeproj`,
'project.pbxproj'
)
const librariesPath = path.join(
pathSpec.outputDir,
projectSpec.projectName,
'Libraries'
)
const iosProject = await getIosProject(projectPath)
const target = iosProject.findTargetKey(projectSpec.projectName)
const injectPluginsTaskMsg = 'Injecting Native Dependencies'
const injectPluginsKaxTask = kax.task(injectPluginsTaskMsg)
for (const plugin of plugins) {
if (await isDependencyJsApiImpl(plugin)) {
log.debug('JS api implementation identified, skipping fill hull.')
continue
}
const pluginConfig = await manifest.getPluginConfig(
plugin,
projectSpec.projectName
)
if (!pluginConfig) {
continue
}
shell.pushd(pathSpec.pluginsDownloadDirectory)
try {
if (pluginConfig.ios) {
const nativeDependencyPathInComposite =
composite && (await composite.getNativeDependencyPath(plugin))
const pluginSourcePath =
nativeDependencyPathInComposite ||
(await downloadPluginSource(pluginConfig.origin))
if (!pluginSourcePath) {
throw new Error(`Was not able to download ${plugin.basePath}`)
}
injectPluginsKaxTask.text = `${injectPluginsTaskMsg} [${
plugin.basePath
}]`
if (pluginConfig.ios.copy) {
for (const copy of pluginConfig.ios.copy) {
if (
switchToOldDirectoryStructure(pluginSourcePath, copy.source)
) {
log.debug(
`Handling copy directive: Falling back to old directory structure for API(Backward compatibility)`
)
copy.source = path.join('IOS', 'IOS', 'Classes', 'SwaggersAPIs')
}
}
handleCopyDirective(
pluginSourcePath,
pathSpec.outputDir,
pluginConfig.ios.copy
)
}
if (pluginConfig.ios.replaceInFile) {
for (const r of pluginConfig.ios.replaceInFile) {
const pathToFile = path.join(pathSpec.outputDir, r.path)
const fileContent = fs.readFileSync(pathToFile, 'utf8')
const patchedFileContent = fileContent.replace(
RegExp(r.string, 'g'),
r.replaceWith
)
fs.writeFileSync(pathToFile, patchedFileContent, {
encoding: 'utf8',
})
}
}
if (pluginConfig.ios.pbxproj) {
if (pluginConfig.ios.pbxproj.addSource) {
for (const source of pluginConfig.ios.pbxproj.addSource) {
// Multiple source files
if (source.from) {
if (
switchToOldDirectoryStructure(pluginSourcePath, source.from)
) {
log.debug(
`Source Copy: Falling back to old directory structure for API(Backward compatibility)`
)
source.from = path.join(
'IOS',
'IOS',
'Classes',
'SwaggersAPIs',
'*.swift'
)
}
const relativeSourcePath = path.dirname(source.from)
const pathToSourceFiles = path.join(
pluginSourcePath,
relativeSourcePath
)
const fileNames = _.filter(
fs.readdirSync(pathToSourceFiles),
f => f.endsWith(path.extname(source.from!))
)
for (const fileName of fileNames) {
const fileNamePath = path.join(source.path, fileName)
iosProject.addSourceFile(
fileNamePath,
null,
iosProject.findPBXGroupKey({ name: source.group })
)
}
} else {
// Single source file
iosProject.addSourceFile(
source.path,
null,
iosProject.findPBXGroupKey({ name: source.group })
)
}
}
}
if (pluginConfig.ios.pbxproj.addHeader) {
for (const header of pluginConfig.ios.pbxproj.addHeader) {
// Multiple header files
if (header.from) {
if (
switchToOldDirectoryStructure(pluginSourcePath, header.from)
) {
log.debug(
`Header Copy: Falling back to old directory structure for API(Backward compatibility)`
)
header.from = path.join(
'IOS',
'IOS',
'Classes',
'SwaggersAPIs',
'*.swift'
)
}
const relativeHeaderPath = path.dirname(header.from)
const pathToHeaderFiles = path.join(
pluginSourcePath,
relativeHeaderPath
)
const fileNames = _.filter(
fs.readdirSync(pathToHeaderFiles),
f => f.endsWith(path.extname(header.from!))
)
for (const fileName of fileNames) {
const fileNamePath = path.join(header.path, fileName)
iosProject.addHeaderFile(
fileNamePath,
{ public: header.public },
iosProject.findPBXGroupKey({ name: header.group })
)
}
} else {
const headerPath = header.path
iosProject.addHeaderFile(
headerPath,
{ public: header.public },
iosProject.findPBXGroupKey({ name: header.group })
)
}
}
}
if (pluginConfig.ios.pbxproj.addFile) {
for (const file of pluginConfig.ios.pbxproj.addFile) {
iosProject.addFile(
file.path,
iosProject.findPBXGroupKey({ name: file.group })
)
// Add target dep in any case for now, will rework later
iosProject.addTargetDependency(target, [
`"${path.basename(file.path)}"`,
])
}
}
if (pluginConfig.ios.pbxproj.addFramework) {
for (const framework of pluginConfig.ios.pbxproj.addFramework) {
iosProject.addFramework(framework, {
customFramework: true,
})
}
}
if (pluginConfig.ios.pbxproj.addProject) {
for (const project of pluginConfig.ios.pbxproj.addProject) {
const projectAbsolutePath = path.join(
librariesPath,
project.path,
'project.pbxproj'
)
const options = {
addAsTargetDependency: project.addAsTargetDependency,
frameworks: project.frameworks,
projectAbsolutePath,
staticLibs: project.staticLibs,
}
iosProject.addProject(
project.path,
project.group,
target,
options
)
}
}
if (pluginConfig.ios.pbxproj.addStaticLibrary) {
for (const lib of pluginConfig.ios.pbxproj.addStaticLibrary) {
iosProject.addStaticLibrary(lib)
}
}
if (pluginConfig.ios.pbxproj.addHeaderSearchPath) {
for (const p of pluginConfig.ios.pbxproj.addHeaderSearchPath) {
iosProject.addToHeaderSearchPaths(p)
}
}
if (pluginConfig.ios.pbxproj.addFrameworkReference) {
for (const frameworkReference of pluginConfig.ios.pbxproj
.addFrameworkReference) {
iosProject.addFramework(frameworkReference, {
customFramework: true,
})
}
}
if (pluginConfig.ios.pbxproj.addFrameworkSearchPath) {
for (const p of pluginConfig.ios.pbxproj.addFrameworkSearchPath) {
iosProject.addToFrameworkSearchPaths(p)
}
}
}
}
} finally {
shell.popd()
}
}
injectPluginsKaxTask.succeed(injectPluginsTaskMsg)
log.debug(`[=== Completed framework generation ===]`)
return { iosProject, projectPath }
} finally {
shell.popd()
}
}
async function getIosProject(projectPath: string): Promise<any> {
const project = xcode.project(projectPath)
return new Promise((resolve, reject) => {
project.parse(err => {
if (err) {
log.error(`failed to get ios project : ${JSON.stringify(err)}`)
reject(err)
}
resolve(project)
})
})
}
function switchToOldDirectoryStructure(
pluginSourcePath: string,
tail: string
): boolean {
// This is to check if the api referenced during container generation is created using the old or new directory structure to help keep the backward compatibility.
const pathToSwaggersAPIs = path.join('IOS', 'IOS', 'Classes', 'SwaggersAPIs')
if (
path.dirname(tail) === `IOS` &&
fs.existsSync(path.join(pluginSourcePath, path.dirname(pathToSwaggersAPIs)))
) {
return true
}
return false
}