]> git.gir.st - VimFx.git/blob - gulpfile.coffee
Clean up gulpfile.coffee slightly
[VimFx.git] / gulpfile.coffee
1 ###
2 # Copyright Simon Lydell 2014.
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 tap = require('gulp-tap')
29 zip = require('gulp-zip')
30 marked = require('marked')
31 merge = require('merge2')
32 precompute = require('require-precompute')
33 request = require('request')
34 rimraf = require('rimraf')
35 runSequence = require('run-sequence')
36 pkg = require('./package.json')
37
38 DEST = 'build'
39 XPI = 'VimFx.xpi'
40 LOCALE = 'extension/locale'
41 TEST = 'extension/test'
42
43 BASE_LOCALE = 'en-US'
44
45 test = '--test' in process.argv or '-t' in process.argv
46 ifTest = (value) -> if test then [value] else []
47
48 { join } = path
49 read = (filepath) -> fs.readFileSync(filepath).toString()
50 template = (data) -> mustache(data, {extension: ''})
51
52 gulp.task('default', ['push'])
53
54 gulp.task('clean', (callback) ->
55 rimraf(DEST, callback)
56 )
57
58 gulp.task('copy', ->
59 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'COPYING', 'LICENSE'])
60 .pipe(gulp.dest(DEST))
61 )
62
63 gulp.task('node_modules', ->
64 dependencies = (name for name of pkg.dependencies)
65 # Note! When installing or updating node modules, make sure that the following
66 # glob does not include too much or too little!
67 gulp.src("node_modules/+(#{ dependencies.join('|') })/\
68 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}")
69 .pipe(gulp.dest("#{ DEST }/node_modules"))
70 )
71
72 gulp.task('coffee', ->
73 gulp.src([
74 'extension/bootstrap.coffee'
75 'extension/lib/**/*.coffee'
76 ifTest('extension/test/**/*.coffee')...
77 ], {base: 'extension'})
78 .pipe(coffee({bare: true}))
79 .pipe(gulp.dest(DEST))
80 )
81
82 gulp.task('chrome.manifest', ->
83 gulp.src('extension/chrome.manifest.tmpl')
84 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
85 .pipe(gulp.dest(DEST))
86 )
87
88 gulp.task('install.rdf', ->
89 [ [ { name: creator } ], developers, contributors, translators ] =
90 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
91 .map((block) -> block.split('\n').map((name) -> {name}))
92
93 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
94
95 descriptions = fs.readdirSync(LOCALE)
96 .filter((locale) -> locale != BASE_LOCALE)
97 .map((locale) -> {locale, description: getDescription(locale)})
98
99 gulp.src('extension/install.rdf.tmpl')
100 .pipe(template({
101 version: pkg.version
102 minVersion: pkg.firefoxVersions.min
103 maxVersion: pkg.firefoxVersions.max
104 creator, developers, contributors, translators
105 defaultDescription: getDescription(BASE_LOCALE)
106 descriptions
107 }))
108 .pipe(gulp.dest(DEST))
109 )
110
111 gulp.task('require-data', ['node_modules'], ->
112 data = JSON.stringify(precompute('.'), null, 2)
113 gulp.src('extension/require-data.js.tmpl')
114 .pipe(template({data}))
115 .pipe(gulp.dest(DEST))
116 )
117
118 gulp.task('tests-list', ->
119 list = JSON.stringify(fs.readdirSync(TEST)
120 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
121 .filter(Boolean)
122 )
123 gulp.src("#{ TEST }/tests-list.js.tmpl", {base: 'extension'})
124 .pipe(template({list}))
125 .pipe(gulp.dest(DEST))
126 )
127
128 gulp.task('templates', [
129 'chrome.manifest'
130 'install.rdf'
131 'require-data'
132 ifTest('tests-list')...
133 ])
134
135 gulp.task('build', (callback) ->
136 runSequence(
137 'clean',
138 ['copy', 'node_modules', 'coffee', 'templates'],
139 callback
140 )
141 )
142
143 gulp.task('xpi', ['build'], ->
144 gulp.src("#{ DEST }/**/*")
145 .pipe(zip(XPI, {compress: false}))
146 .pipe(gulp.dest(DEST))
147 )
148
149 gulp.task('push', ['xpi'], ->
150 body = fs.readFileSync(join(DEST, XPI))
151 request.post({url: 'http://localhost:8888', body})
152 )
153
154 gulp.task('lint', ->
155 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
156 .pipe(coffeelint())
157 .pipe(coffeelint.reporter())
158 )
159
160 gulp.task('release', (callback) ->
161 { version } = pkg
162 message = "VimFx v#{ version }"
163 today = new Date().toISOString()[...10]
164 merge([
165 gulp.src('package.json'),
166 gulp.src('CHANGELOG.md')
167 .pipe(header("### #{ version } (#{ today })\n\n"))
168 .pipe(gulp.dest('.'))
169 ])
170 .pipe(git.commit(message))
171 .on('end', ->
172 git.tag("v#{ version }", message, callback)
173 )
174 )
175
176 gulp.task('changelog', ->
177 num = 1
178 for arg in process.argv when /^-[1-9]$/.test(arg)
179 num = Number(arg[1])
180 entries = read('CHANGELOG.md').split(/^### .+/m)[1..num].join('')
181 process.stdout.write(marked(entries))
182 )
183
184 gulp.task('readme', ->
185 process.stdout.write(marked(read('README.md')))
186 )
187
188 gulp.task('faster', ->
189 gulp.src('gulpfile.coffee')
190 .pipe(coffee({bare: true}))
191 .pipe(gulp.dest('.'))
192 )
193
194 gulp.task('sync-locales', ->
195 baseLocale = BASE_LOCALE
196 for arg in process.argv when arg[...2] == '--'
197 baseLocale = arg[2..]
198 results = fs.readdirSync(join(LOCALE, baseLocale))
199 .filter((file) -> path.extname(file) == '.properties')
200 .map(syncLocale.bind(null, baseLocale))
201 if baseLocale == BASE_LOCALE
202 report = []
203 for {fileName, translatedCount, total} in results
204 report.push("#{ fileName }:")
205 for localeName, count of translatedCount
206 paddedName = "#{ localeName }: "[...6]
207 percentage = Math.round((count / total) * 100)
208 report.push(" #{ paddedName } #{ percentage }%")
209 process.stdout.write(report.join('\n') + '\n')
210 )
211
212 syncLocale = (baseLocaleName, fileName) ->
213 basePath = join(LOCALE, baseLocaleName, fileName)
214 base = parseLocaleFile(read(basePath))
215 oldBasePath = "#{basePath}.old"
216 if fs.existsSync(oldBasePath)
217 oldBase = parseLocaleFile(read(oldBasePath))
218 translatedCount = {}
219 for localeName in fs.readdirSync(LOCALE) when localeName != baseLocaleName
220 localePath = join(LOCALE, localeName, fileName)
221 locale = parseLocaleFile(read(localePath))
222 translatedCount[localeName] = 0
223 newLocale = base.template.map((line) ->
224 if Array.isArray(line)
225 [ key ] = line
226 oldValue = oldBase?.keys[key]
227 value =
228 if (oldValue? and oldValue != base.keys[key]) or
229 key not of locale.keys
230 base.keys[key]
231 else
232 locale.keys[key]
233 translatedCount[localeName]++ if value != base.keys[key] or value == ''
234 return "#{ key }=#{ value }"
235 else
236 return line
237 )
238 fs.writeFileSync(localePath, newLocale.join(base.newline))
239 return {fileName, translatedCount, total: Object.keys(base.keys).length}
240
241 parseLocaleFile = (fileContents) ->
242 keys = {}
243 lines = []
244 [ newline ] = fileContents.match(/\r?\n/)
245 for line in fileContents.split(newline)
246 line = line.trim()
247 [ match, key, value ] = line.match(///^ ([^=]+) = (.*) $///) ? []
248 if match
249 keys[key] = value
250 lines.push([key])
251 else
252 lines.push(line)
253 return {keys, template: lines, newline}
254
255 helpHTMLFile = 'help.html'
256 gulp.task(helpHTMLFile, ->
257 unless fs.existsSync(helpHTMLFile)
258 console.log("""
259 First enable the “Copy to clipboard” line in help.coffee, show the help
260 dialog and finally dump the clipboard into #{ helpHTMLFile }.
261 """)
262 return
263 gulp.src('help.html')
264 .pipe(tap((file) ->
265 file.contents = new Buffer(generateHelpHTML(file.contents.toString()))
266 ))
267 .pipe(gulp.dest('.'))
268 )
269
270 helpHTMLPrelude = '''
271 <!doctype html>
272 <meta charset=utf-8>
273 <title>VimFx help</title>
274 <style>
275 * { margin: 0; }
276 body > :first-child { min-height: 100vh; }
277 </style>
278 <link rel=stylesheet href=extension/skin/style.css>
279 '''
280
281 generateHelpHTML = (dumpedHTML) ->
282 return helpHTMLPrelude + dumpedHTML
283 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
284 .replace(/\w+>$/, 'div>')
285 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')
Imprint / Impressum