]> git.gir.st - VimFx.git/blob - gulpfile.coffee
Merge branch 'master' into develop
[VimFx.git] / gulpfile.coffee
1 ###
2 # Copyright Simon Lydell 2014, 2015.
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 sloc = require('gulp-sloc')
29 tap = require('gulp-tap')
30 zip = require('gulp-zip')
31 marked = require('marked')
32 merge = require('merge2')
33 precompute = require('require-precompute')
34 request = require('request')
35 rimraf = require('rimraf')
36 runSequence = require('run-sequence')
37 pkg = require('./package.json')
38
39 DEST = 'build'
40 XPI = 'VimFx.xpi'
41 LOCALE = 'extension/locale'
42 TEST = 'extension/test'
43
44 BASE_LOCALE = 'en-US'
45
46 argv = process.argv.slice(2)
47
48 test = '--test' in argv or '-t' in argv
49 ifTest = (value) -> if test then [value] else []
50
51 {join} = path
52 read = (filepath) -> fs.readFileSync(filepath).toString()
53 template = (data) -> mustache(data, {extension: ''})
54
55 gulp.task('default', ['push'])
56
57 gulp.task('clean', (callback) ->
58 rimraf(DEST, callback)
59 )
60
61 gulp.task('copy', ->
62 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'COPYING', 'LICENSE'])
63 .pipe(gulp.dest(DEST))
64 )
65
66 gulp.task('node_modules', ->
67 dependencies = (name for name of pkg.dependencies)
68 # Note! When installing or updating node modules, make sure that the following
69 # glob does not include too much or too little!
70 gulp.src("node_modules/+(#{dependencies.join('|')})/\
71 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}")
72 .pipe(gulp.dest("#{DEST}/node_modules"))
73 )
74
75 gulp.task('coffee', ->
76 gulp.src([
77 'extension/bootstrap.coffee'
78 'extension/lib/**/*.coffee'
79 ifTest('extension/test/**/*.coffee')...
80 ], {base: 'extension'})
81 .pipe(coffee({bare: true}))
82 .pipe(gulp.dest(DEST))
83 )
84
85 gulp.task('chrome.manifest', ->
86 gulp.src('extension/chrome.manifest.tmpl')
87 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
88 .pipe(gulp.dest(DEST))
89 )
90
91 gulp.task('install.rdf', ->
92 [[{name: creator}], developers, contributors, translators] =
93 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
94 .map((block) -> block.split('\n').map((name) -> {name}))
95
96 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
97
98 descriptions = fs.readdirSync(LOCALE)
99 .filter((locale) -> locale != BASE_LOCALE)
100 .map((locale) -> {locale, description: getDescription(locale)})
101
102 gulp.src('extension/install.rdf.tmpl')
103 .pipe(template({
104 idSuffix: if '--unlisted' in argv or '-u' in argv then '-unlisted' else ''
105 version: pkg.version
106 minVersion: pkg.firefoxVersions.min
107 maxVersion: pkg.firefoxVersions.max
108 creator, developers, contributors, translators
109 defaultDescription: getDescription(BASE_LOCALE)
110 descriptions
111 }))
112 .pipe(gulp.dest(DEST))
113 )
114
115 gulp.task('require-data', ['node_modules'], ->
116 data = JSON.stringify(precompute('.'), null, 2)
117 gulp.src('extension/require-data.js.tmpl')
118 .pipe(template({data}))
119 .pipe(gulp.dest(DEST))
120 )
121
122 gulp.task('tests-list', ->
123 list = JSON.stringify(fs.readdirSync(TEST)
124 .map((name) -> name.match(/^(?!.+-frame\.)(test-.+)\.coffee$/)?[1])
125 .filter(Boolean)
126 )
127 gulp.src("#{TEST}/tests-list.js.tmpl", {base: 'extension'})
128 .pipe(template({list}))
129 .pipe(gulp.dest(DEST))
130 )
131
132 gulp.task('tests-list-frame', ->
133 list = JSON.stringify(fs.readdirSync(TEST)
134 .map((name) -> name.match(/^(test-.+-frame)\.coffee$/)?[1])
135 .filter(Boolean)
136 )
137 gulp.src("#{TEST}/tests-list-frame.js.tmpl", {base: 'extension'})
138 .pipe(template({list}))
139 .pipe(gulp.dest(DEST))
140 )
141
142 gulp.task('templates', [
143 'chrome.manifest'
144 'install.rdf'
145 'require-data'
146 ifTest('tests-list')...
147 ifTest('tests-list-frame')...
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 oldBasePath = "#{basePath}.old"
264 if fs.existsSync(oldBasePath)
265 oldBase = parseLocaleFile(read(oldBasePath))
266 untranslated = {}
267 for localeName in fs.readdirSync(LOCALE) when localeName != baseLocaleName
268 localePath = join(LOCALE, localeName, fileName)
269 locale = parseLocaleFile(read(localePath))
270 untranslated[localeName] = []
271 newLocale = base.template.map((line, index) ->
272 if Array.isArray(line)
273 [key] = line
274 oldValue = oldBase?.keys[key]
275 value =
276 if (oldValue? and oldValue != base.keys[key]) or
277 key not of locale.keys
278 base.keys[key]
279 else
280 locale.keys[key]
281 result = "#{key}=#{value}"
282 if value == base.keys[key] and value != ''
283 untranslated[localeName].push("#{index + 1}: #{result}")
284 return result
285 else
286 return line
287 )
288 fs.writeFileSync(localePath, newLocale.join(base.newline))
289 return {fileName, untranslated, total: Object.keys(base.keys).length}
290
291 parseLocaleFile = (fileContents) ->
292 keys = {}
293 lines = []
294 [newline] = fileContents.match(/\r?\n/)
295 for line in fileContents.split(newline)
296 line = line.trim()
297 [match, key, value] = line.match(///^ ([^=]+) = (.*) $///) ? []
298 if match
299 keys[key] = value
300 lines.push([key])
301 else
302 lines.push(line)
303 return {keys, template: lines, newline}
304
305 helpHTMLFile = 'help.html'
306 gulp.task(helpHTMLFile, ->
307 unless fs.existsSync(helpHTMLFile)
308 process.stdout.write("""
309 First enable the “Copy to clipboard” line in help.coffee, show the help
310 dialog and finally dump the clipboard into #{helpHTMLFile}.
311 """)
312 return
313 gulp.src('help.html')
314 .pipe(tap((file) ->
315 file.contents = new Buffer(generateHelpHTML(file.contents.toString()))
316 ))
317 .pipe(gulp.dest('.'))
318 )
319
320 helpHTMLPrelude = '''
321 <!doctype html>
322 <meta charset=utf-8>
323 <title>VimFx help</title>
324 <style>
325 * {margin: 0;}
326 body > :first-child {min-height: 100vh; width: 100vw;}
327 </style>
328 <link rel=stylesheet href=extension/skin/style.css>
329 '''
330
331 generateHelpHTML = (dumpedHTML) ->
332 return helpHTMLPrelude + dumpedHTML
333 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
334 .replace(/\w+>$/, 'div>')
335 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')
Imprint / Impressum