forked from testforstephen/serialport.node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
176 lines (172 loc) · 7.97 KB
/
gulpfile.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require('shelljs/global');
const gulp = require('gulp');
const fs = require('fs');
const path = require('path');
const async = require('async');
const cliArgs = require('yargs').argv;
const linuxDistro = require('linux-distro');
const github = require('octonode');
const git = require('simple-git')();
function getRepoName(gitUrl) {
const fields = gitUrl.split(':');
if (fields.length < 2) return '';
const segments = fields[1].split('/');
const userName = segments[segments.length-2];
const repoName = segments[segments.length-1];
const fullRepoName = `${userName}/${repoName}`;
const position = fullRepoName.length - '.git'.length;
const lastIndex = fullRepoName.lastIndexOf('.git');
if (lastIndex !== -1 && lastIndex === position) {
return fullRepoName.substring(0, position);
} else {
return fullRepoName;
}
}
function uploadAssets(client, tagName, filePath, distName, callback) {
async.waterfall([
// parse repo name from git repository configuration
(callback) => {
git.listRemote(['--get-url'], function(err, data) {
if (!err) {
console.log('Remote url for repository at ' + __dirname + ':');
const repoName = getRepoName(data.trim());
console.log(repoName);
if (repoName) {
callback(null, repoName);
} else {
callback('Cannot get repo name for this repository.');
}
} else {
callback(err);
}
});
},
// get release by tag
(repoName, callback) => {
client.get(`/repos/${repoName}/releases/tags/${tagName}`, (err, res, body) => {
if (!err) {
console.log(`release id: ${body.id}`);
callback(null, repoName, body.id);
} else {
callback(`The release via tag ${tagName} not found!`);
}
});
},
// check if asset exist or not.
(repoName, releaseId, callback) => {
client.get(`/repos/${repoName}/releases/${releaseId}/assets`, (err, res, body) => {
if (!err) {
const find = body.find((element) => {
return element.name === distName;
});
if (find) {
console.log(`Finded an existing asset '${distName} in github release and delete it first.`);
client.del(`/repos/${repoName}/releases/assets/${find.id}`, null, (err1, res1, body1) => {
if (err1) {
callback(`Cannot delete assets '${distName}'. See the error '${err1}'`);
} else {
callback(null, repoName, releaseId);
}
});
} else {
callback(null, repoName, releaseId);
}
} else {
callback(null, repoName, releaseId, null);
}
});
},
// upload assets to releases.
(repoName, releaseId, callback) => {
const ghRelease = client.release(repoName, releaseId);
const archive = fs.readFileSync(filePath);
ghRelease.uploadAssets(archive, {
name: distName,
contentType: 'application/octet-stream',
uploadHost: 'uploads.github.com',
}, (err, res, body) => {
if (!err) {
console.log(`Succeeded to upload assets '${distName}' to github release '${tagName}'`);
}
callback(err);
});
}
], (error, results) => {
callback(error);
});
}
gulp.task('buildDll', (done) => {
if (!cliArgs.repoUrl || !cliArgs.token || !cliArgs.tag) {
done('Missing repoUrl, token, tag parameters!');
return ;
}
console.log('Repo URL: ', Buffer.from(cliArgs.repoUrl).toString('base64'));
const client = github.client(cliArgs.token);
const tagName = cliArgs.tag;
async.waterfall([
// Pulling package source code from GITHUB.
(callback) => {
const tmpDir = path.normalize('./tmp');
if (fs.existsSync(tmpDir)) {
if (process.platform === 'win32') {
rm('-rf', tmpDir);
} else {
exec(`sudo rm -rf ${tmpDir}`);
}
}
mkdir('-p', tmpDir);
const gitClone = exec(`git clone -b master ${decodeURIComponent(cliArgs.repoUrl)} usb-native`, {
cwd: tmpDir
});
if (gitClone.code) {
callback('Pulling node package failed.');
} else {
callback();
}
},
// Using node-gyp to compile CPP source code.
(callback) => {
const platform = require('os').platform();
const platformConfig = JSON.parse(fs.readFileSync(path.normalize('./platform.json')));
const versions = platformConfig[platform];
const electrons = (cliArgs['electronVersion'] && cliArgs['electronVersion'].split(',')) || (versions ? versions.electron : ['1.4.6']);
const archs = (cliArgs['arch'] && cliArgs['arch'].split(',')) || (versions ? versions.arch : ['ia32', "x64"]);
const tasks = [];
electrons.forEach((electron) => {
archs.forEach((arch) => {
// Compile node-usb-native native code.
tasks.push((callback) => {
console.log(`[node-gyp] Starting to build node-usb-usb binary version for electron ${electron} and arch ${arch}.`);
const compile = exec(`node-gyp rebuild --target=${electron} --arch=${arch} --dist-url=https://atom.io/download/electron`, {
cwd: path.normalize('./tmp/usb-native/vendor/node-usb-native')
});
if (compile.code) {
callback('[node-gyp] Compiling node-usb-native native code failed.');
} else {
console.log('[node-gyp] Build complete.');
console.log(`Generate dll at ${path.normalize('./tmp/usb-native/vendor/node-usb-native/build/Release/usb-native.node')}`);
if (platform === 'linux') {
linuxDistro().then(data => {
const packageName = `usb-native_${data.os}${data.release || data.code}_${electron}_${arch}.node`;
console.log(packageName);
uploadAssets(client, tagName, path.normalize('./tmp/usb-native/vendor/node-usb-native/build/Release/usb-native.node'), packageName, callback);
}, () => {
const packageName = `usb-native_${platform}_${electron}_${arch}.node`;
console.log(packageName);
uploadAssets(client, tagName, path.normalize('./tmp/usb-native/vendor/node-usb-native/build/Release/usb-native.node'), packageName, callback);
});
} else {
const packageName = `usb-native_${platform}_${electron}_${arch}.node`;
console.log(packageName);
uploadAssets(client, tagName, path.normalize('./tmp/usb-native/vendor/node-usb-native/build/Release/usb-native.node'), packageName, callback);
}
}
});
});
});
async.series(tasks, callback);
},
], (error, result) => {
done(error);
});
});