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