forked from adafruit/adafruit-bluefruit-le-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
171 lines (153 loc) · 6.37 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
// Load dependencies.
var del = require('del'),
gulp = require('gulp'),
babel = require("gulp-babel"),
os = require('os'),
packager = require('electron-packager'),
replace = require('gulp-replace'),
shell = require('gulp-run'),
sourcemaps = require("gulp-sourcemaps"),
zip = require('gulp-zip');
// Load the app package.json to read its version.
var app = require('./app/package.json')
// Global variables to control build and package.
var electronVersion = '0.30.4',
packageName = 'able',
fullName = 'Adafruit Bluefruit LE'
platform = os.platform(),
arch = os.arch(),
appVersion = app.version;
function packageFullName() {
// Return full package name including platform and architecture.
// Note that this depends on how electron-packager outputs results
// so you can't just change this algorithm.
return packageName + '-' + platform + '-' + arch;
}
// Define tasks.
gulp.task('dist-clean', function() {
// Delete any transformed javascript & JSX files.
return del(['./app/dist/']);
});
gulp.task('js-build', ['dist-clean'], function() {
// Convert all the ES6 & JSX files to plain ES5 using babel.
return gulp.src(['src/**/*.jsx', 'src/**/*.js'])
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(sourcemaps.write("."))
.pipe(gulp.dest('app/dist'));
});
gulp.task('dependency-clean', function() {
// Delete the app's dependencies so they can be installed fresh again.
return del(['./app/node_modules/']);
});
gulp.task('install-dependencies', ['dependency-clean'], function() {
// Install the app's dependencies using npm.
return shell('cd app && npm install').exec();
});
gulp.task('fix-node-usb', ['install-dependencies'], function() {
// Mega hack to patch the node-usb module to not use node-pre-gyp by adding
// the variables that node-pre-gyp normally adds. This is necessary because
// node-pre-gyp doesn't work with electron. See this issue:
// https://github.com/mapbox/node-pre-gyp/issues/166
// Only run this task on windows and linux where the bluetooth-hci-socket
// module is used by noble.
if (platform === 'win32' || platform === 'linux') {
return gulp.src('app/node_modules/noble/node_modules/bluetooth-hci-socket/node_modules/usb/binding.gyp')
.pipe(replace("'use_system_libusb%': 'false',", "'use_system_libusb%': 'false',\n'module_name': 'usb_bindings',\n'module_path': './src/binding',"))
.pipe(gulp.dest('./app/node_modules/noble/node_modules/bluetooth-hci-socket/node_modules/usb/'));
}
});
gulp.task('rebuild-usb', ['fix-node-usb'], function() {
// Rebuild noble's usb module with the correct electron version.
// Only run this task on windows and linux.
if (platform === 'win32' || platform === 'linux') {
return shell('cd app/node_modules/noble/node_modules/bluetooth-hci-socket/node_modules/usb && node-gyp rebuild --target=' + electronVersion + ' --dist-url=https://atom.io/download/atom-shell').exec();
}
});
gulp.task('rebuild-bluetooth-hci-socket', ['rebuild-usb'], function() {
// Rebuild noble's bluetooth-hci-socket module with the correct electron version.
// Only run this task on windows and linux.
if (platform === 'win32' || platform === 'linux') {
return shell('cd app/node_modules/noble/node_modules/bluetooth-hci-socket && node-gyp rebuild --target=' + electronVersion + ' --dist-url=https://atom.io/download/atom-shell').exec();
}
});
gulp.task('native-build', ['install-dependencies', 'rebuild-bluetooth-hci-socket'], function() {
// Compile any native dependencies.
// For windows this depends on a convoluted process to patch node-usb to not use
// node-pre-gyp (since node-pre-gyp doesn't currently work with electron)
// and then manually rebuild each native noble dependency for the right
// electron version. Gulp is horrible at representing this multi-step
// syncronous process as it has to be encoded in the dependencies for this
// task, therefore nothing needs to be done at this point for windows.
if (platform === 'darwin') {
// For Mac OSX just run electron-rebuild as it works fine on noble's OSX dependencies.
return shell('electron-rebuild -v ' + electronVersion + ' -m ./app/node_modules/').exec();
}
});
gulp.task('build', ['js-build', 'native-build'], function() {
// Build task just kicks off a build of the JS source and native dependencies.
});
gulp.task('package-clean', function() {
return del([packageFullName() + '.zip', packageFullName() + '/**/*']);
});
gulp.task('electron-package', ['package-clean', 'build'], function(cb) {
// Package task uses electron-package to build the final app package.
// Set general options.
var opts = {
dir: 'app',
name: packageName,
platform: platform,
arch: arch,
version: electronVersion,
prune: true,
overwrite: true,
asar: true,
};
// Set platform-specific options.
if (platform === 'darwin') {
opts.icon = 'app/assets/adafruit.icns';
opts['app-bundle-id'] = 'com.adafruit.BluefruitLE';
opts['app-version'] = appVersion;
opts['helper-bundle-id'] = 'com.adafruit.BluefruitLE.Helper';
}
else if (platform === 'win32') {
opts.icon = 'app/assets/adafruit.ico';
opts['version-string'] = {
CompanyName: 'Adafruit',
LegalCopyright: '2015',
FileDescription: 'Adafruit Bluefruit LE application.',
FileVersion: appVersion,
OriginalFilename: packageName + '.exe',
ProductVersion: appVersion,
ProductName: fullName
};
}
// Call packager.
packager(opts, function(error, appPath) {
if (error) {
cb(error);
}
else {
cb();
}
});
});
gulp.task('copy-license-readme', ['electron-package'], function() {
// Copy the root LICENSE and README.md file into the application directory that will be zipped up.
return gulp.src(['LICENSE', 'README.md'])
.pipe(gulp.dest(packageFullName() + '/'));
});
gulp.task('package', ['copy-license-readme'], function() {
// Zip up the built package.
if (platform === 'darwin') {
// For some reason zipping the app on OSX is broken because of gulp.src
// not following symlinks. Just invoke the zip command line tool.
return shell('zip -r ' + packageFullName() + '-' + appVersion + '.zip ' + packageFullName() + '/').exec();
}
else {
// Use gulp-zip to zip up the app.
return gulp.src(packageFullName() + '/**/*')
.pipe(zip(packageFullName() + '-' + appVersion + '.zip'))
.pipe(gulp.dest('.'));
}
});