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