forked from dataliterate/data-populator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·172 lines (139 loc) · 5.94 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
var gulp = require('gulp');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var fs = require('fs');
var path = require('path');
var runSequence = require('run-sequence');
var del = require('del');
var async = require('async');
var fse = require('fs-extra');
var expandTilde = require('expand-tilde');
var release = require('gulp-github-release');
var zip = require('gulp-zip');
var SKETCH_PLUGINS_FOLDER = path.join(expandTilde('~'),'/Library/Application Support/com.bohemiancoding.sketch3/Plugins');
var ManifestProcessorOptions = {
pluginManifestDescriberName: 'HKSketchFusionExtension',
startingManifestTag: '__$begin_of_manifest_\n',
endingManifestTag: '__$end_of_manifest_\n',
scriptFileName: 'plugin.js',
globalVarName: '__globals'
};
var currentManifest = {};
function extractManifestObject() {
var data = fs.readFileSync(path.join(__dirname,'build',ManifestProcessorOptions.scriptFileName),'utf8');
var startTag = ManifestProcessorOptions.startingManifestTag;
var endTag = ManifestProcessorOptions.endingManifestTag;
var startIndex = data.indexOf(startTag);
var endIndex = data.indexOf(endTag);
if(startIndex === -1 || endIndex === -1) {
return;
}
return JSON.parse(data.substring(startIndex+startTag.length,endIndex));
}
gulp.task('clean', function () {
return del(['build/**','dist/**']);
});
gulp.task('prepare-manifest',function(callback) {
var manifest = extractManifestObject();
fse.outputJsonSync(path.join(__dirname,'build/manifest.json'),manifest);
currentManifest = manifest;
callback(null);
});
gulp.task('prepare-folders',function(callback) {
async.parallel({
build: function(callback) {
fse.ensureDir(path.join(__dirname,'build'),callback);
},
dist: function(callback) {
fse.ensureDir(path.join(__dirname,'dist'),callback);
}
},callback);
});
gulp.task('assemble-plugin-bundle',function(callback) {
function normalizePluginFileName(name) {
return name;
}
var bundlePath = path.join(__dirname,'dist',normalizePluginFileName(currentManifest.bundleName || currentManifest.name) + '.sketchplugin');
async.parallel({
manifest: function(callback) {
fse.outputJson(path.join(bundlePath,'Contents','Sketch','manifest.json'),currentManifest,callback);
},
runtime: function(callback) {
var script = fs.readFileSync(path.join(__dirname,'build',ManifestProcessorOptions.scriptFileName),'utf8');
script = ["var "+ManifestProcessorOptions.globalVarName+" = this;",script].join("");
fse.outputFile(path.join(bundlePath,'Contents','Sketch',ManifestProcessorOptions.scriptFileName),script,callback);
}
},function(err,data) {
callback(null);
});
});
gulp.task('assemble-plugin-resources',function(callback) {
function normalizePluginFileName(name) {
return name;
}
return gulp.src('src/resources/**/*.*')
.pipe(gulp.dest(path.join(__dirname,'dist',normalizePluginFileName(currentManifest.bundleName || currentManifest.name)+'.sketchplugin','Contents/Resources')));
});
gulp.task('assemble-plugin-presets',function(callback) {
function normalizePluginFileName(name) {
return name;
}
return gulp.src('src/presets/**/*.*')
.pipe(gulp.dest(path.join(__dirname,'dist',normalizePluginFileName(currentManifest.bundleName || currentManifest.name)+'.sketchplugin','Presets')));
});
gulp.task('install-plugin',function(){
return gulp.src("dist/**/*.*")
.pipe(gulp.dest(SKETCH_PLUGINS_FOLDER));
});
gulp.task('build',function(callback) {
runSequence('clean','prepare-folders','bundle','prepare-manifest','assemble-plugin-bundle','assemble-plugin-resources','assemble-plugin-presets','install-plugin',callback);
});
gulp.task('bundle',function() {
var filePath = './src/plugin.js';
var extensions = ['.js'];
var bundler = browserify({
entries: [filePath],
extensions: extensions,
debug: false
});
bundler.transform({ global: true }, babelify.configure({
presets: ["es2015"],
plugins: ["transform-object-rest-spread", ["babel-plugin-sketch-manifest-processor",ManifestProcessorOptions]]
}));
return bundler.bundle()
.pipe(source(ManifestProcessorOptions.scriptFileName))
.pipe(gulp.dest('./build/'));
});
gulp.task('watch', function(){
runSequence('build', function() {
gulp.watch('./src/**/*.*',function() {
console.log("Watching...");
runSequence('clean','build',function(){
console.log("Rebuild complete!");
});
});
});
});
gulp.task('default',function(callback) {
runSequence('build', callback);
});
gulp.task('zip', ['build'], function() {
return gulp.src('./dist/*.sketchplugin/**/*')
.pipe(zip('Sketch Data Populator.zip'))
.pipe(gulp.dest('dist'))
});
gulp.task('release', ['zip'], function() {
return gulp.src('./dist/Sketch Data Populator.zip')
.pipe(release({
//token: 'token', // or you can set an env var called GITHUB_TOKEN instead
owner: '', // if missing, it will be extracted from manifest (the repository.url field)
repo: '', // if missing, it will be extracted from manifest (the repository.url field)
//tag: 'v1.0.0', // if missing, the version will be extracted from manifest and prepended by a 'v'
//name: 'publish-release v1.0.0', // if missing, it will be the same as the tag
//notes: 'very good!', // if missing it will be left undefined
draft: false, // if missing it's false
prerelease: true, // if missing it's false
manifest: require('./build/manifest.json') // package.json from which default values will be extracted if they're missing
}));
});