-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
227 lines (190 loc) · 5.76 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use strict';
var gulp = require('gulp');
var swig = require('gulp-swig');
var _ = require('lodash');
var path = require('path');
var sqlite = require('sql.js');
var eventStream = require('event-stream');
var gutil = require('gulp-util');
var BufferStreams = require('bufferstreams');
var foreach = require('gulp-foreach');
var concat = require('gulp-concat');
var fileBuffer = require('gulp-buffer');
var cheerio = require('cheerio');
var fs = require('fs');
var rename = require("gulp-rename");
var program = require('commander');
var yaml = require('js-yaml');
program.version('0.0.2')
.option('-p, --plan <plan>', 'The plan to generate docsets.', 'hive')
.parse(process.argv);
var plans = [ 'plans/*.yaml' ];
var plan = yaml.safeLoad(fs.readFileSync('plans/' + program.plan + '.yaml', 'utf8'));
var prod = plan.prod;
var name = plan.name;
var packageName = prod.replace(' ', '_')
var docsetPath = packageName + '.docset/';
var contentsPath = path.join(docsetPath, 'Contents/');
var infoPlistPath = contentsPath;
var docpath = path.join(contentsPath, 'Resources/Documents/');
var sqlitePath = path.join(contentsPath, 'Resources/docSet.dsidx');
var website = 'website/' + name + '/';
var iconSrc = path.join(website, plan.icon || 'icon.png');
var iconDst = docsetPath;
var websiteSrc = [ website + '**' ];
var htmlSrc = _.map(plan.htmlSrcPadding, function(pad) {
return website + pad;
});
var otherSrc = _.map(plan.otherSrcPadding, function(pad) {
return website + pad;
});
var sqls = [
'CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);',
'CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);'
];
var indexes = [];
var Type = {
INSTRUCTION: 'Instruction',
FUNCTION: 'Function',
CLASS: 'Class',
};
gulp.task('plist', function() {
return gulp.src('templates/Info.j2')
.pipe(swig({
data: {
CFBundleIdentifier: name,
CFBundleName: prod,
DocSetPlatformFamily: name,
isJavaScriptEnabled: true,
dashIndexFilePath: plan.index || undefined
},
ext: '.plist'
}))
.pipe(gulp.dest(infoPlistPath));
});
gulp.task('sqlite', ['webpages'], function() {
var db = new sqlite.Database();
db.run(sqls.join('\n'));
_(indexes)
.chunk(10)
.forEach(function(subIndexes) {
var values = _(subIndexes)
.map(function(v) {
var value = "('" + v.name + "','" + v.type + "','" + v.path + "')";
//var value = '("' + v.name + '","' + v.type + '","' + v.path + '")';
//console.log(value);
return value;
})
.value()
.join(',');
var indexSql = 'INSERT OR REPLACE INTO searchIndex(name, type, path) VALUES ' + values + ';';
db.run(indexSql);
})
.value();
var data = db.export();
var buffer = new Buffer(data);
return fs.writeFileSync(sqlitePath, buffer);
});
gulp.task('other', function() {
return gulp.src(otherSrc)
.pipe(gulp.dest(docpath));
});
gulp.task('icon', function() {
return gulp.src(iconSrc)
.pipe(gulp.dest(iconDst));
});
gulp.task('webpages', ['other', 'icon'], function() {
return gulp.src(htmlSrc)
.pipe(rename(function(filepath) {
if (filepath.extname !== '.html') {
filepath.extname = '.html';
}
}))
.pipe(handlePage)
.pipe(gulp.dest(docpath));
});
gulp.task('pack', ['plist', 'sqlite'], function () {
var tar = require('gulp-tar');
var gzip = require('gulp-gzip');
return gulp.src(['!**/+(\.DS_Store)/**', docsetPath + '**'], { base: './' } )
.pipe(tar(name + '.tar'))
.pipe(gzip())
.pipe(rename(packageName + '.tgz'))
.pipe(gulp.dest('target'));
});
gulp.task('default', ['plist', 'sqlite', 'pack']);
//var handlePage = eventStream.map(function(buffer, callback) {
// console.log(buffer);
// callback(null, 'hello world');
//});
var handlePage = eventStream.map(function(file, callback) {
var relativePath = path.relative(path.join(process.cwd(), website), file.path);
if (file.isNull()) {
// Nothing to do if no contents
callback(null, file);
} else if (file.isStream()) {
file.contents = file.contents.pipe(new BufferStreams(function(err, buf, cb) {
try {
if (err) {
return cb(new gutil.PluginError('handlePage', err.message));
}
cb(null, handleDom(buf, relativePath));
} catch (err) {
cb(new gutil.PluginError('handlePage', err.message));
}
}));
callback(null, file);
} else if (file.isBuffer()) {
try {
file.contents = handleDom(file.contents, relativePath);
callback(null, file);
}
catch (err) {
callback(err, null);
}
}
});
var handleDom = function(buffer, filepath) {
var $ = cheerio.load(buffer);
// remove
plan.selectors.remove.forEach(function(selector) {
$(selector).remove();
});
// Remove splitter class
plan.selectors.removeClass.forEach(function(item) {
$(item.selector).removeClass(item['class']);
});
// Add keyword
plan.selectors.keyword.forEach(function(keyword) {
if (keyword.url && filepath !== keyword.url) {
// url doesn't match
return;
}
$(keyword.selector).each(function(i, elem) {
var name = $(elem).text();
name = name.replace(/'/g, "\'\'").trim();
var target;
switch(keyword.targetAttribute) {
case 'href':
var basepath = path.dirname(filepath);
target = path.join(basepath, $(elem).attr('href'));
break;
case 'id': // falling down
default:
target = filepath + '#' + $(elem).attr('id');
}
target = target.replace(/'/g, "\'\'");
if (name) {
index(name, keyword.type, target);
}
});
});
return Buffer($.html());
};
var index = function(name, type, target) {
indexes.push({
name: name,
type: type,
path: target
});
};