]> git.gir.st - VimFx.git/blob - gulpfile.coffee
Update help.html/hints.html `<title>`
[VimFx.git] / gulpfile.coffee
1 ###
2 # Copyright Simon Lydell 2014, 2015, 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 fs = require('fs')
21 path = require('path')
22 gulp = require('gulp')
23 coffee = require('gulp-coffee')
24 coffeelint = require('gulp-coffeelint')
25 git = require('gulp-git')
26 header = require('gulp-header')
27 mustache = require('gulp-mustache')
28 preprocess = require('gulp-preprocess')
29 sloc = require('gulp-sloc')
30 tap = require('gulp-tap')
31 zip = require('gulp-zip')
32 marked = require('marked')
33 merge = require('merge2')
34 precompute = require('require-precompute')
35 request = require('request')
36 rimraf = require('rimraf')
37 runSequence = require('run-sequence')
38 pkg = require('./package.json')
39
40 DEST = 'build'
41 XPI = 'VimFx.xpi'
42 LOCALE = 'extension/locale'
43 TEST = 'extension/test'
44
45 BASE_LOCALE = 'en-US'
46 UPDATE_ALL = /\s*UPDATE_ALL$/
47
48 ADDON_PATH = 'chrome://vimfx'
49 BUILD_TIME = Date.now()
50
51 argv = process.argv.slice(2)
52
53 {join} = path
54 read = (filepath) -> fs.readFileSync(filepath).toString()
55 template = (data) -> mustache(data, {extension: ''})
56
57 gulp.task('default', ['push'])
58
59 gulp.task('clean', (callback) ->
60 rimraf(DEST, callback)
61 )
62
63 gulp.task('copy', ->
64 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'COPYING', 'LICENSE'])
65 .pipe(gulp.dest(DEST))
66 )
67
68 gulp.task('node_modules', ->
69 dependencies = (name for name of pkg.dependencies)
70 # Note: When installing or updating node modules, make sure that the following
71 # glob does not include too much or too little!
72 gulp.src(
73 "node_modules/+(#{dependencies.join('|')})/\
74 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}"
75 )
76 .pipe(gulp.dest("#{DEST}/node_modules"))
77 )
78
79 gulp.task('coffee', ->
80 test = '--test' in argv or '-t' in argv
81 gulp.src(
82 [
83 'extension/bootstrap.coffee'
84 'extension/lib/**/*.coffee'
85 ].concat(if test then 'extension/test/**/*.coffee' else []),
86 {base: 'extension'}
87 )
88 .pipe(preprocess({context: {
89 BUILD_TIME
90 ADDON_PATH: JSON.stringify(ADDON_PATH)
91 REQUIRE_DATA: JSON.stringify(precompute('.'), null, 2)
92 TESTS:
93 if test
94 JSON.stringify(fs.readdirSync(TEST)
95 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
96 .filter(Boolean)
97 )
98 else
99 null
100 }}))
101 .pipe(coffee({bare: true}))
102 .pipe(gulp.dest(DEST))
103 )
104
105 gulp.task('bootstrap-frame.js', ->
106 gulp.src('extension/bootstrap-frame.js.tmpl')
107 .pipe(mustache({ADDON_PATH}))
108 .pipe(tap((file) ->
109 file.path = file.path.replace(/\.js\.tmpl$/, "-#{BUILD_TIME}.js")
110 ))
111 .pipe(gulp.dest(DEST))
112 )
113
114 gulp.task('chrome.manifest', ->
115 gulp.src('extension/chrome.manifest.tmpl')
116 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
117 .pipe(gulp.dest(DEST))
118 )
119
120 gulp.task('install.rdf', ->
121 [[{name: creator}], developers, contributors, translators] =
122 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
123 .map((block) -> block.split('\n').map((name) -> {name}))
124
125 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
126
127 descriptions = fs.readdirSync(LOCALE)
128 .filter((locale) -> locale != BASE_LOCALE)
129 .map((locale) -> {locale, description: getDescription(locale)})
130
131 gulp.src('extension/install.rdf.tmpl')
132 .pipe(template({
133 idSuffix: if '--unlisted' in argv or '-u' in argv then '-unlisted' else ''
134 version: pkg.version
135 minVersion: pkg.firefoxVersions.min
136 maxVersion: pkg.firefoxVersions.max
137 creator, developers, contributors, translators
138 defaultDescription: getDescription(BASE_LOCALE)
139 descriptions
140 }))
141 .pipe(gulp.dest(DEST))
142 )
143
144 gulp.task('templates', [
145 'bootstrap-frame.js'
146 'chrome.manifest'
147 'install.rdf'
148 ])
149
150 gulp.task('build', (callback) ->
151 runSequence(
152 'clean',
153 ['copy', 'node_modules', 'coffee', 'templates'],
154 callback
155 )
156 )
157
158 gulp.task('xpi', ['build'], ->
159 gulp.src("#{DEST}/**/*")
160 .pipe(zip(XPI, {compress: false}))
161 .pipe(gulp.dest(DEST))
162 )
163
164 gulp.task('push', ['xpi'], ->
165 body = fs.readFileSync(join(DEST, XPI))
166 request.post({url: 'http://localhost:8888', body})
167 )
168
169 gulp.task('lint', ->
170 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
171 .pipe(coffeelint())
172 .pipe(coffeelint.reporter())
173 )
174
175 gulp.task('sloc', ->
176 gulp.src([
177 'extension/bootstrap.coffee'
178 'extension/lib/!(migrations|legacy).coffee'
179 ])
180 .pipe(sloc())
181 )
182
183 gulp.task('release', (callback) ->
184 {version} = pkg
185 message = "VimFx v#{version}"
186 today = new Date().toISOString()[...10]
187 merge([
188 gulp.src('package.json')
189 gulp.src('CHANGELOG.md')
190 .pipe(header("### #{version} (#{today})\n\n"))
191 .pipe(gulp.dest('.'))
192 ])
193 .pipe(git.commit(message))
194 .on('end', ->
195 git.tag("v#{version}", message, callback)
196 )
197 return
198 )
199
200 gulp.task('changelog', ->
201 num = 1
202 for arg in argv when /^-[1-9]$/.test(arg)
203 num = Number(arg[1])
204 entries = read('CHANGELOG.md').split(/^### .+/m)[1..num].join('')
205 process.stdout.write(html(entries))
206 )
207
208 gulp.task('readme', ->
209 process.stdout.write(html(read('README.md')))
210 )
211
212 # Reduce markdown to the small subset of HTML that AMO allows. Note that AMO
213 # converts newlines to `<br>`.
214 html = (string) ->
215 return marked(string)
216 .replace(/// <h\d [^>]*> ([^<>]+) </h\d> ///g, '\n\n<b>$1</b>')
217 .replace(///\s* <p> ((?: [^<] | <(?!/p>) )+) </p>///g, (match, text) ->
218 return "\n#{text.replace(/\s*\n\s*/g, ' ')}\n\n"
219 )
220 .replace(///<li> ((?: [^<] | <(?!/li>) )+) </li>///g, (match, text) ->
221 return "<li>#{text.replace(/\s*\n\s*/g, ' ')}</li>"
222 )
223 .replace(/<br>/g, '\n')
224 .replace(///<(/?)kbd>///g, '<$1code>')
225 .replace(/<img[^>]*>\s*/g, '')
226 .replace(/\n\s*\n/g, '\n\n')
227 .trim() + '\n'
228
229 gulp.task('faster', ->
230 gulp.src('gulpfile.coffee')
231 .pipe(coffee({bare: true}))
232 .pipe(gulp.dest('.'))
233 )
234
235 gulp.task('sync-locales', ->
236 baseLocale = BASE_LOCALE
237 compareLocale = null
238 for arg in argv when arg[...2] == '--'
239 name = arg[2..]
240 if name[-1..] == '?' then compareLocale = name[...-1] else baseLocale = name
241
242 results = fs.readdirSync(join(LOCALE, baseLocale))
243 .filter((file) -> path.extname(file) == '.properties')
244 .map(syncLocale.bind(null, baseLocale))
245
246 if baseLocale == BASE_LOCALE
247 report = []
248 for {fileName, untranslated, total} in results
249 report.push("#{fileName}:")
250 for localeName, strings of untranslated
251 paddedName = "#{localeName}: "[...6]
252 percentage = Math.round((1 - strings.length / total) * 100)
253 if localeName == compareLocale or compareLocale == null
254 report.push(" #{paddedName} #{percentage}%")
255 if localeName == compareLocale
256 report.push(strings.map((string) -> " #{string}")...)
257 process.stdout.write(report.join('\n') + '\n')
258 )
259
260 syncLocale = (baseLocaleName, fileName) ->
261 basePath = join(LOCALE, baseLocaleName, fileName)
262 base = parseLocaleFile(read(basePath))
263 untranslated = {}
264 for localeName in fs.readdirSync(LOCALE)
265 localePath = join(LOCALE, localeName, fileName)
266 locale = parseLocaleFile(read(localePath))
267 untranslated[localeName] = []
268 newLocale = base.template.map((line, index) ->
269 if Array.isArray(line)
270 [key] = line
271 baseValue = base.keys[key]
272 value =
273 if UPDATE_ALL.test(baseValue) or key not of locale.keys
274 baseValue.replace(UPDATE_ALL, '')
275 else
276 locale.keys[key]
277 result = "#{key}=#{value}"
278 if value == base.keys[key] and value != ''
279 untranslated[localeName].push("#{index + 1}: #{result}")
280 return result
281 else
282 return line
283 )
284 fs.writeFileSync(localePath, newLocale.join(base.newline))
285 delete untranslated[baseLocaleName]
286 return {fileName, untranslated, total: Object.keys(base.keys).length}
287
288 parseLocaleFile = (fileContents) ->
289 keys = {}
290 lines = []
291 [newline] = fileContents.match(/\r?\n/)
292 for line in fileContents.split(newline)
293 line = line.trim()
294 [match, key, value] = line.match(///^ ([^=]+) = (.*) $///) ? []
295 if match
296 keys[key] = value
297 lines.push([key])
298 else
299 lines.push(line)
300 return {keys, template: lines, newline}
301
302 generateHTMLTask = (filename, message) ->
303 gulp.task(filename, ->
304 unless fs.existsSync(filename)
305 process.stdout.write(message(filename))
306 return
307 gulp.src(filename)
308 .pipe(tap((file) ->
309 file.contents = new Buffer(generateTestHTML(file.contents.toString()))
310 ))
311 .pipe(gulp.dest('.'))
312 )
313
314 generateHTMLTask('help.html', (filename) -> """
315 First enable the “Copy to clipboard” line in help.coffee, show the help
316 dialog and finally dump the clipboard into #{filename}.
317 """)
318
319 generateHTMLTask('hints.html', (filename) -> """
320 First enable the “Copy to clipboard” line in modes.coffee, show the
321 hint markers, activate the “Increase count” command and finally dump the
322 clipboard into #{filename}.
323 """)
324
325 testHTMLPrelude = '''
326 <!doctype html>
327 <meta charset=utf-8>
328 <title>VimFx test</title>
329 <style>
330 * {margin: 0;}
331 body > :first-child {min-height: 100vh; width: 100vw;}
332 </style>
333 <link rel=stylesheet href=extension/skin/style.css>
334 '''
335
336 generateTestHTML = (dumpedHTML) ->
337 return testHTMLPrelude + dumpedHTML
338 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
339 .replace(/\w+>$/, 'div>')
340 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')
Imprint / Impressum