2 # Copyright Simon Lydell 2014, 2015, 2016.
4 # This file is part of VimFx.
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.
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.
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/>.
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')
42 LOCALE = 'extension/locale'
43 TEST = 'extension/test'
46 UPDATE_ALL = /\s*UPDATE_ALL$/
48 ADDON_PATH = 'chrome://vimfx'
49 BUILD_TIME = Date.now()
51 argv = process.argv.slice(2)
54 read = (filepath) -> fs.readFileSync(filepath).toString()
55 template = (data) -> mustache(data, {extension: ''})
57 gulp.task('default', ['push'])
59 gulp.task('clean', (callback) ->
60 rimraf(DEST, callback)
64 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'COPYING', 'LICENSE'])
65 .pipe(gulp.dest(DEST))
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!
73 "node_modules/+(#{dependencies.join('|')})/\
74 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}"
76 .pipe(gulp.dest("#{DEST}/node_modules"))
79 gulp.task('coffee', ->
80 test = '--test' in argv or '-t' in argv
83 'extension/bootstrap.coffee'
84 'extension/lib/**/*.coffee'
85 ].concat(if test then 'extension/test/**/*.coffee' else []),
88 .pipe(preprocess({context: {
90 ADDON_PATH: JSON.stringify(ADDON_PATH)
91 REQUIRE_DATA: JSON.stringify(precompute('.'), null, 2)
94 JSON.stringify(fs.readdirSync(TEST)
95 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
101 .pipe(coffee({bare: true}))
102 .pipe(gulp.dest(DEST))
105 gulp.task('bootstrap-frame.js', ->
106 gulp.src('extension/bootstrap-frame.js.tmpl')
107 .pipe(mustache({ADDON_PATH}))
109 file.path = file.path.replace(/\.js\.tmpl$/, "-#{BUILD_TIME}.js")
111 .pipe(gulp.dest(DEST))
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))
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}))
125 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
127 descriptions = fs.readdirSync(LOCALE)
128 .filter((locale) -> locale != BASE_LOCALE)
129 .map((locale) -> {locale, description: getDescription(locale)})
131 gulp.src('extension/install.rdf.tmpl')
133 idSuffix: if '--unlisted' in argv or '-u' in argv then '-unlisted' else ''
135 minVersion: pkg.firefoxVersions.min
136 maxVersion: pkg.firefoxVersions.max
137 creator, developers, contributors, translators
138 defaultDescription: getDescription(BASE_LOCALE)
141 .pipe(gulp.dest(DEST))
144 gulp.task('templates', [
150 gulp.task('build', (callback) ->
153 ['copy', 'node_modules', 'coffee', 'templates'],
158 gulp.task('xpi', ['build'], ->
159 gulp.src("#{DEST}/**/*")
160 .pipe(zip(XPI, {compress: false}))
161 .pipe(gulp.dest(DEST))
164 gulp.task('push', ['xpi'], ->
165 body = fs.readFileSync(join(DEST, XPI))
166 request.post({url: 'http://localhost:8888', body})
170 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
172 .pipe(coffeelint.reporter())
177 'extension/bootstrap.coffee'
178 'extension/lib/!(migrations|legacy).coffee'
183 gulp.task('release', (callback) ->
185 message = "VimFx v#{version}"
186 today = new Date().toISOString()[...10]
188 gulp.src('package.json')
189 gulp.src('CHANGELOG.md')
190 .pipe(header("### #{version} (#{today})\n\n"))
191 .pipe(gulp.dest('.'))
193 .pipe(git.commit(message))
195 git.tag("v#{version}", message, callback)
200 gulp.task('changelog', ->
202 for arg in argv when /^-[1-9]$/.test(arg)
204 entries = read('CHANGELOG.md').split(/^### .+/m)[1..num].join('')
205 process.stdout.write(html(entries))
208 gulp.task('readme', ->
209 process.stdout.write(html(read('README.md')))
212 # Reduce markdown to the small subset of HTML that AMO allows. Note that AMO
213 # converts newlines to `<br>`.
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"
220 .replace(///<li> ((?: [^<] | <(?!/li>) )+) </li>///g, (match, text) ->
221 return "<li>#{text.replace(/\s*\n\s*/g, ' ')}</li>"
223 .replace(/<br>/g, '\n')
224 .replace(///<(/?)kbd>///g, '<$1code>')
225 .replace(/<img[^>]*>\s*/g, '')
226 .replace(/\n\s*\n/g, '\n\n')
229 gulp.task('faster', ->
230 gulp.src('gulpfile.coffee')
231 .pipe(coffee({bare: true}))
232 .pipe(gulp.dest('.'))
235 gulp.task('sync-locales', ->
236 baseLocale = BASE_LOCALE
238 for arg in argv when arg[...2] == '--'
240 if name[-1..] == '?' then compareLocale = name[...-1] else baseLocale = name
242 results = fs.readdirSync(join(LOCALE, baseLocale))
243 .filter((file) -> path.extname(file) == '.properties')
244 .map(syncLocale.bind(null, baseLocale))
246 if baseLocale == BASE_LOCALE
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')
260 syncLocale = (baseLocaleName, fileName) ->
261 basePath = join(LOCALE, baseLocaleName, fileName)
262 base = parseLocaleFile(read(basePath))
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)
271 baseValue = base.keys[key]
273 if UPDATE_ALL.test(baseValue) or key not of locale.keys
274 baseValue.replace(UPDATE_ALL, '')
277 result = "#{key}=#{value}"
278 if value == base.keys[key] and value != ''
279 untranslated[localeName].push("#{index + 1}: #{result}")
284 fs.writeFileSync(localePath, newLocale.join(base.newline))
285 delete untranslated[baseLocaleName]
286 return {fileName, untranslated, total: Object.keys(base.keys).length}
288 parseLocaleFile = (fileContents) ->
291 [newline] = fileContents.match(/\r?\n/)
292 for line in fileContents.split(newline)
294 [match, key, value] = line.match(///^ ([^=]+) = (.*) $///) ? []
300 return {keys, template: lines, newline}
302 helpHTMLFile = 'help.html'
303 gulp.task(helpHTMLFile, ->
304 unless fs.existsSync(helpHTMLFile)
305 process.stdout.write("""
306 First enable the “Copy to clipboard” line in help.coffee, show the help
307 dialog and finally dump the clipboard into #{helpHTMLFile}.
310 gulp.src('help.html')
312 file.contents = new Buffer(generateHelpHTML(file.contents.toString()))
314 .pipe(gulp.dest('.'))
317 helpHTMLPrelude = '''
320 <title>VimFx help</title>
323 body > :first-child {min-height: 100vh; width: 100vw;}
325 <link rel=stylesheet href=extension/skin/style.css>
328 generateHelpHTML = (dumpedHTML) ->
329 return helpHTMLPrelude + dumpedHTML
330 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
331 .replace(/\w+>$/, 'div>')
332 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')