-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
executable file
·163 lines (138 loc) · 4.44 KB
/
gulpfile.js
File metadata and controls
executable file
·163 lines (138 loc) · 4.44 KB
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
'use strict';
var path = require('path');
var fs = require('fs');
var gulp = require('gulp');
var argv = require('yargs').default('environment', 'stage').argv;
var $ = require('gulp-load-plugins')();
var merge = require('merge-stream');
var runSequence = require('run-sequence');
var RELEASE_NAME = 'quirkbot-arduino-hardware';
var PACKAGE_INDEX_NAME = 'quirkbot.com';
var PACKAGE = JSON.parse(fs.readFileSync('package.json'));
var VERSION_FILENAME = `${RELEASE_NAME}-${PACKAGE.version}.zip`;
var LATEST_FILENAME = `${RELEASE_NAME}-latest.zip`;
var PACKAGE_INDEX_FILENAME = `package_${PACKAGE_INDEX_NAME}_index.json`;
/**
* Cleans all the generated files
*/
gulp.task('clean', function () {
return gulp.src([
LATEST_FILENAME,
RELEASE_NAME,
RELEASE_NAME + '-*.zip'
])
.pipe($.clean());
});
/**
* Gives a warning in case the current release you are building overwrites an
* existing release.
*/
gulp.task('check-release-overwrite', [], function () {
var overwrite = false;
var oldIndex = JSON.parse(fs.readFileSync(PACKAGE_INDEX_FILENAME));
var platforms = oldIndex.packages[0].platforms;
for (var i = platforms.length - 1; i >= 0; i--) {
if(platforms[i].version == PACKAGE.version){
platforms.splice(i, 1);
overwrite = true;
}
}
if(overwrite){
return gulp.src('')
.pipe($.prompt.confirm(`There is already a release with the version ${PACKAGE.version}. Do you want to overwrite it (Y)?\n (If you want to create a new release, answer (N) and updated the version in package.json first)`))
}
});
/**
* Bundles the source code into a versioned zip file
*/
gulp.task('bundle:copy', function (gulpCallBack){
return gulp.src([ 'avr/**' ])
.pipe(gulp.dest(path.join(RELEASE_NAME, 'avr')))
});
gulp.task('bundle:zip', function (cb){
var exec = require('child_process').exec;
exec(
`cd ${RELEASE_NAME}`
+ ` && zip -vr ${VERSION_FILENAME} avr -x "*.DS_Store"`
+ ` && cd ..`
+ ` && mv ${path.join(RELEASE_NAME, VERSION_FILENAME)} ${VERSION_FILENAME}`
+ ` && cp ${VERSION_FILENAME} ${LATEST_FILENAME}`
+ ` && rm -r ${RELEASE_NAME}`,
(error, stdout, stderr) => {
cb(error);
}
);
});
gulp.task('bundle', function (cb) {
runSequence(
'check-release-overwrite',
'clean',
'bundle:copy',
'bundle:zip',
cb);
});
/**
* Generate the "platform" entry on the package index
*/
gulp.task('build', ['bundle'], function (cb) {
// Check if there is already a release with this version
var oldIndex = JSON.parse(fs.readFileSync(PACKAGE_INDEX_FILENAME));
var platforms = oldIndex.packages[0].platforms;
for (var i = platforms.length - 1; i >= 0; i--) {
if(platforms[i].version == PACKAGE.version){
platforms.splice(i, 1);
}
}
// Compute the size and checksum of the bundle
var execSync = require('child_process').execSync;
var size = execSync(`stat -f%z ${VERSION_FILENAME}`).toString().replace(/\W/g, '');
var checksum = execSync(`shasum -a 256 ${VERSION_FILENAME}`).toString()
.split(VERSION_FILENAME).join('').replace(/\W/g, '');
// Create the platform entry
var newPlatform = fs.readFileSync('index_platform.template.json').toString()
.split('{{VERSION}}').join(PACKAGE.version)
.split('{{FILENAME}}').join(VERSION_FILENAME)
.split('{{SIZE}}').join(size)
.split('{{CHECKSUM}}').join(checksum);
newPlatform = JSON.parse(newPlatform);
// Apend the new platform entry to the current platforms on the package
platforms.unshift(newPlatform);
var newIndex = fs.readFileSync('index.template.json').toString()
.split('{{PLATFORMS}}').join(JSON.stringify(platforms));
newIndex = JSON.parse(newIndex);
// Save the new index
fs.writeFileSync(PACKAGE_INDEX_FILENAME, JSON.stringify(newIndex, null, '\t'));
// Update the avr/version.txt
fs.writeFileSync(path.join('avr', 'version.txt'), PACKAGE.version);
cb();
});
/**
* Builds and publish to s3
*/
gulp.task('s3', ['build'], function () {
var aws = JSON.parse(fs.readFileSync(path.join('aws-config', `${argv.environment}.json`)));
return gulp.src([
PACKAGE_INDEX_FILENAME,
VERSION_FILENAME,
LATEST_FILENAME
])
.pipe($.s3(aws, {
uploadPath: 'downloads/'
}));
});
/**
* Deploys the release. Asks for confirmation if deploying to production
*/
gulp.task('confirm-deploy', [], function () {
if(argv.environment == 'production'){
return gulp.src('')
.pipe($.prompt.confirm('You are about to deploy TO PRODUCTION! Are you sure you want to continue)'))
.pipe($.prompt.confirm('Really sure?!'))
}
});
gulp.task('deploy', function (cb) {
runSequence(
'confirm-deploy',
's3',
cb);
});