-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathplugin.js
106 lines (105 loc) · 3.31 KB
/
plugin.js
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
const { spawnSync } = require("child_process");
const xcode = require("@raydeck/xcode");
const { join, basename, isAbsolute } = require("path");
const {
existsSync,
renameSync,
writeFileSync,
createWriteStream
} = require("fs");
const { sync } = require("glob");
const request = require("request");
const rimraf = require("rimraf");
module.exports = [
{
name: "add-mlmodel <modelpath>",
description: "Add and compile mlmodel into your IOS project assets",
func: async (argv, _, options) => {
let tempPath;
const outPath = options.outPath ? options.outPath : ".";
const finalLocation = join(outPath, basename(argv[0]) + "c");
const projectPath = sync(
join(process.cwd(), "ios", "**", "project.pbxproj")
)[0];
if (!projectPath) {
console.error("Cannot find an XCode project to modify: aborting");
return;
}
if (existsSync(finalLocation)) {
console.log(
"Aborting compile: The mlmodelc directory already exists at ",
finalLocation
);
addToProject(finalLocation, projectPath);
const base = basename(finalLocation);
const parts = base.split(".");
parts.pop();
const newBase = parts.join("."); //.replace("-", "_");
console.log(
`Model added. You may refer to it as ${newBase} in your code.`
);
return;
}
const finish = tempPath => {
if (tempPath) {
renameSync(tempPath, finalLocation);
}
addToProject(finalLocation, projectPath);
const base = basename(finalLocation);
const parts = base.split(".");
parts.pop();
const newBase = parts.join("."); //.replace("-", "_");
console.log(
`Model added. You may refer to it as ${newBase} in your code.`
);
};
if (argv[0].includes("://")) {
//URL!
//transfer URL to our temp Path
console.log(
"I was passed a URL - attempting download. Big models can take a little time"
);
const outFile = join(process.env.TMPDIR, basename(argv[0]));
request(argv[0])
.pipe(createWriteStream(outFile))
.on("finish", () => {
tempPath = compileMLModel(outFile);
finish(tempPath);
});
} else {
tempPath = compileMLModel(argv[0]);
finish(tempPath);
}
}
}
];
const compileMLModel = (originPath, destinationPath = process.env.TMPDIR) => {
const result = join(destinationPath, basename(originPath) + "c");
try {
if (existsSync(result)) rimraf.sync(result);
} catch (e) {}
spawnSync(
"xcrun",
["coremlcompiler", "compile", originPath, destinationPath],
{
stdio: "inherit"
}
);
//We know what the tail result will be
return existsSync(result) ? result : false;
};
const addToProject = (fileToAdd, projectPath) => {
if (!isAbsolute(fileToAdd)) fileToAdd = join(process.cwd(), fileToAdd);
const project = xcode.project(projectPath);
project.parseSync();
console.log("Adding file ", fileToAdd);
try {
project.removeResourceFile(fileToAdd);
} catch (e) {}
project.addResourceFile(fileToAdd, {
target: project.getFirstTarget().uuid,
lastKnownFileType: "folder"
});
const outText = project.writeSync();
writeFileSync(projectPath, outText);
};