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')
22 LOCALE = 'extension/locale'
23 TEST = 'extension/test'
26 UPDATE_ALL = /\s*UPDATE_ALL$/
28 ADDON_PATH = 'chrome://vimfx'
29 BUILD_TIME = Date.now()
31 argv = process.argv.slice(2)
34 read = (filepath) -> fs.readFileSync(filepath).toString()
35 template = (data) -> mustache(data, {extension: ''})
37 gulp.task('clean', (callback) ->
38 rimraf(DEST, callback)
42 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'LICENSE', 'LICENSE-MIT'])
43 .pipe(gulp.dest(DEST))
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!
51 "node_modules/+(#{dependencies.join('|')})/\
52 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}"
54 .pipe(gulp.dest("#{DEST}/node_modules"))
57 gulp.task('coffee', ->
58 test = '--test' in argv or '-t' in argv
61 'extension/bootstrap.coffee'
62 'extension/lib/**/*.coffee'
63 ].concat(if test then 'extension/test/**/*.coffee' else []),
66 .pipe(preprocess({context: {
68 ADDON_PATH: JSON.stringify(ADDON_PATH)
69 REQUIRE_DATA: JSON.stringify(precompute('.'), null, 2)
72 JSON.stringify(fs.readdirSync(TEST)
73 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
79 .pipe(coffee({bare: true}))
80 .pipe(gulp.dest(DEST))
83 gulp.task('bootstrap-frame.js', ->
84 gulp.src('extension/bootstrap-frame.js.tmpl')
85 .pipe(mustache({ADDON_PATH}))
87 file.path = file.path.replace(/\.js\.tmpl$/, "-#{BUILD_TIME}.js")
89 .pipe(gulp.dest(DEST))
92 gulp.task('chrome.manifest', ->
93 gulp.src('extension/chrome.manifest.tmpl')
94 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
95 .pipe(gulp.dest(DEST))
98 gulp.task('install.rdf', ->
99 [[{name: creator}], developers, contributors, translators] =
100 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
101 .map((block) -> block.split('\n').map((name) -> {name}))
103 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
105 descriptions = fs.readdirSync(LOCALE)
106 .filter((locale) -> locale != BASE_LOCALE)
107 .map((locale) -> {locale, description: getDescription(locale)})
109 gulp.src('extension/install.rdf.tmpl')
111 idSuffix: if '--unlisted' in argv or '-u' in argv then '-unlisted' else ''
113 minVersion: pkg.firefoxVersions.min
114 maxVersion: pkg.firefoxVersions.max
115 creator, developers, contributors, translators
116 defaultDescription: getDescription(BASE_LOCALE)
119 .pipe(gulp.dest(DEST))
122 gulp.task('templates', gulp.parallel(
128 gulp.task('build', gulp.series(
130 gulp.parallel('copy', 'node_modules', 'coffee', 'templates')
133 gulp.task('xpi-only', ->
134 gulp.src("#{DEST}/**/*")
135 .pipe(zip(XPI, {compress: false}))
136 .pipe(gulp.dest(DEST))
139 gulp.task('xpi', gulp.series('build', 'xpi-only'))
141 gulp.task('push-only', ->
142 body = fs.readFileSync(join(DEST, XPI))
143 request.post({url: 'http://localhost:8888', body})
146 gulp.task('push', gulp.series('xpi', 'push-only'))
148 gulp.task('default', gulp.series('push'))
150 # coffeelint-forbidden-keywords has `require('coffee-script/register');` in its
152 gulp.task('lint-workaround', ->
153 gulp.src('node_modules/coffeescript/')
154 .pipe(gulp.symlink('node_modules/coffee-script'))
157 gulp.task('lint-only', ->
158 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
160 .pipe(coffeelint.reporter())
163 gulp.task('lint', gulp.series('lint-workaround', 'lint-only'))
167 'extension/bootstrap.coffee'
168 'extension/lib/!(migrations|legacy).coffee'
173 gulp.task('release', (callback) ->
175 message = "VimFx v#{version}"
176 today = new Date().toISOString()[...10]
178 gulp.src('package.json')
179 gulp.src('CHANGELOG.md')
180 .pipe(header("### #{version} (#{today})\n\n"))
181 .pipe(gulp.dest('.'))
183 .pipe(git.commit(message))
185 git.tag("v#{version}", message, callback)
190 gulp.task('changelog', (callback) ->
192 for arg in argv when /^-[1-9]$/.test(arg)
194 entries = read('CHANGELOG.md').split(/^### .+/m)[1..num].join('')
195 process.stdout.write(html(entries))
199 gulp.task('readme', (callback) ->
200 process.stdout.write(html(read('README.md')))
204 # Reduce markdown to the small subset of HTML that AMO allows. Note that AMO
205 # converts newlines to `<br>`.
207 return marked(string)
208 .replace(/// <h\d [^>]*> ([^<>]+) </h\d> ///g, '\n\n<b>$1</b>')
209 .replace(///\s* <p> ((?: [^<] | <(?!/p>) )+) </p>///g, (match, text) ->
210 return "\n#{text.replace(/\s*\n\s*/g, ' ')}\n\n"
212 .replace(///<li> ((?: [^<] | <(?!/li>) )+) </li>///g, (match, text) ->
213 return "<li>#{text.replace(/\s*\n\s*/g, ' ')}</li>"
215 .replace(/<br>/g, '\n')
216 .replace(///<(/?)kbd>///g, '<$1code>')
217 .replace(/<img[^>]*>\s*/g, '')
218 .replace(/\n\s*\n/g, '\n\n')
221 gulp.task('faster', ->
222 gulp.src('gulpfile.coffee')
223 .pipe(coffee({bare: true}))
224 .pipe(gulp.dest('.'))
227 gulp.task('sync-locales', (callback) ->
228 baseLocale = BASE_LOCALE
230 for arg in argv when arg[...2] == '--'
232 if name[-1..] == '?' then compareLocale = name[...-1] else baseLocale = name
234 results = fs.readdirSync(join(LOCALE, baseLocale))
235 .filter((file) -> path.extname(file) == '.properties')
236 .map(syncLocale.bind(null, baseLocale))
238 if baseLocale == BASE_LOCALE
240 for {fileName, untranslated, total} in results
241 report.push("#{fileName}:")
242 for localeName, strings of untranslated
243 paddedName = "#{localeName}: "[...6]
244 percentage = Math.round((1 - strings.length / total) * 100)
245 if localeName == compareLocale or compareLocale == null
246 report.push(" #{paddedName} #{percentage}%")
247 if localeName == compareLocale
248 report.push(strings.map((string) -> " #{string}")...)
249 process.stdout.write(report.join('\n') + '\n')
254 syncLocale = (baseLocaleName, fileName) ->
255 basePath = join(LOCALE, baseLocaleName, fileName)
256 base = parseLocaleFile(read(basePath))
258 for localeName in fs.readdirSync(LOCALE)
259 localePath = join(LOCALE, localeName, fileName)
260 locale = parseLocaleFile(read(localePath))
261 untranslated[localeName] = []
262 newLocale = base.template.map((line, index) ->
263 if Array.isArray(line)
265 baseValue = base.keys[key]
267 if UPDATE_ALL.test(baseValue) or key not of locale.keys
268 baseValue.replace(UPDATE_ALL, '')
271 result = "#{key}=#{value}"
272 if value == base.keys[key] and value != ''
273 untranslated[localeName].push("#{index + 1}: #{result}")
278 fs.writeFileSync(localePath, newLocale.join(base.newline))
279 delete untranslated[baseLocaleName]
280 return {fileName, untranslated, total: Object.keys(base.keys).length}
282 parseLocaleFile = (fileContents) ->
285 [newline] = fileContents.match(/\r?\n/)
286 for line in fileContents.split(newline)
288 [match, key, value] = line.match(///^ ([^=]+) = (.*) $///) ? []
294 return {keys, template: lines, newline}
296 generateHTMLTask = (filename, message) ->
297 gulp.task(filename, (callback) ->
298 unless fs.existsSync(filename)
299 process.stdout.write(message(filename))
304 file.contents = new Buffer(generateTestHTML(file.contents.toString()))
306 .pipe(gulp.dest('.'))
309 generateHTMLTask('help.html', (filename) -> """
310 First enable the “Copy to clipboard” line in help.coffee, show the help
311 dialog and finally dump the clipboard into #{filename}.
314 generateHTMLTask('hints.html', (filename) -> """
315 First enable the “Copy to clipboard” line in modes.coffee, show the
316 hint markers, activate the “Increase count” command and finally dump the
317 clipboard into #{filename}.
320 testHTMLPrelude = '''
323 <title>VimFx test</title>
326 body > :first-child {min-height: 100vh; width: 100vw;}
328 <link rel=stylesheet href=extension/skin/style.css>
331 generateTestHTML = (dumpedHTML) ->
332 return testHTMLPrelude + dumpedHTML
333 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
334 .replace(/\w+>$/, 'div>')
335 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')