-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathGruntfile.js
153 lines (141 loc) · 4.8 KB
/
Gruntfile.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
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
const xcode = require('appium-xcode');
const appPaths = require('./');
const B = require('bluebird');
const fs = require('fs');
const renameFile = B.promisify(fs.rename);
const rimraf = require('rimraf');
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
});
const MAX_BUFFER_SIZE = 524288;
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
const cleanApp = function(appRoot, sdk, done) {
const cmd = `xcodebuild -sdk ${sdk} clean`;
const xcode = exec(cmd, {cwd: appRoot, maxBuffer: MAX_BUFFER_SIZE}, function (err, stdout, stderr) {
if (err) {
grunt.log.error("Failed cleaning app");
grunt.warn(stderr);
} else {
done();
}
});
}
const buildApp = function(appRoot, sdk, done) {
grunt.log.write("Building app...");
let args = ['-sdk', sdk];
if (process.env.XCCONFIG_FILE) {
args.push('-xcconfig', process.env.XCCONFIG_FILE);
}
const xcode = spawn('xcodebuild', args, {
cwd: appRoot,
});
xcode.on("error", function (err) {
grunt.warn("Failed spawning xcodebuild: " + err.message);
});
let output = '';
const collect = function (data) { output += data; };
xcode.stdout.on('data', collect);
xcode.stderr.on('data', collect);
xcode.on('exit', function (code) {
if (code != 0) {
process.env.PRODUCT_NAME;
grunt.log.error("Failed building app");
grunt.log.warn(output); // TODO: better error handling
done();
} else {
grunt.log.write('done building ios app for sdk', sdk);
done();
}
});
};
const renameAll = function (done) {
const safeRenameFile = function (file, newFile) {
return renameFile(file, newFile).catch(function (err) {
if (!err.message.includes('ENOENT')) {
throw err;
}
});
}
const renamePromises = [
safeRenameFile('build/Release-iphonesimulator/UICatalog.app', 'build/Release-iphonesimulator/UICatalog-iphonesimulator.app'),
safeRenameFile('build/Release-iphoneos/UICatalog.app', 'build/Release-iphoneos/UICatalog-iphoneos.app')
];
B.all(
renamePromises,
function () {
grunt.log.write("finished renaming apps");
done();
},
function (e) {
grunt.log.error("could not rename apps");
done(e);
}
);
}
const getSdks = function () {
let sdks = ['iphonesimulator'];
if (process.env.IOS_REAL_DEVICE || process.env.REAL_DEVICE) {
sdks.push('iphoneos');
}
return sdks;
}
grunt.registerTask('build', 'build ios app', function (sdk) { buildApp('.', sdk, this.async()) } );
grunt.registerTask('clean', 'cleaning', function (sdk) { cleanApp('.', sdk, this.async()) } );
grunt.registerTask('renameAll', 'renaming apps', function (path) { renameAll(this.async()) } );
grunt.registerTask('deleteBuild', 'deleting build dir', function () { rimraf('./build', this.async()) });
grunt.registerTask('cleanAll', 'cleaning', function () {
const done = this.async();
function runTasks (sdkVer) {
getSdks().forEach(function (sdk) {
sdk = sdk + sdkVer;
grunt.task.run(`clean:${sdk}`);
});
}
xcode.getMaxIOSSDK()
.then(function (sdkVer) {
runTasks(sdkVer);
done();
}).catch(function (err) {
// sometimes this fails on the first try for unknown reasons
console.log(`Error getting max iOS SDK: ${err.message}`);
console.log('Trying again...');
xcode.getMaxIOSSDK()
.then(function (sdkVer) {
runTasks(sdkVer);
done();
}).catch(function (err) {
console.log(`Error getting max iOS SDK: ${err.message}`);
if (process.env.PLATFORM_VERSION) {
console.log(`Using process.env.PLATFORM_VERSION version: ${process.env.PLATFORM_VERSION}`);
let sdkVer = process.env.PLATFORM_VERSION;
runTasks(sdkVer);
done();
}
});
});
});
grunt.registerTask('buildAll', 'building', function () {
const done = this.async();
function runTasks (sdkVer) {
getSdks().forEach(function (sdk) {
sdk = sdk + sdkVer;
grunt.task.run(`build:${sdk}`);
});
}
xcode.getMaxIOSSDK().then(function (sdkVer) {
runTasks(sdkVer);
done();
}).catch(function (err) {
console.log(`Error getting max iOS SDK: ${err.message}`);
if (process.env.PLATFORM_VERSION) {
console.log(`Using process.env.PLATFORM_VERSION version: ${process.env.PLATFORM_VERSION}`);
let sdkVer = process.env.PLATFORM_VERSION;
runTasks(sdkVer);
done();
}
});
});
grunt.registerTask('default', ['cleanAll', 'deleteBuild', 'buildAll', 'renameAll']);
}