Merge pull request #1192 from atom/grunt-download-atom-shell

Use grunt-download-atom-shell to download and update atom-shell.
This commit is contained in:
Cheng Zhao 2013-12-03 22:07:34 -08:00
commit 9b0f8ccee0
4 changed files with 18 additions and 214 deletions

View File

@ -20,6 +20,7 @@ module.exports = (grunt) ->
buildDir = grunt.option('build-dir') ? path.join(tmpDir, 'atom-build')
shellAppDir = path.join(buildDir, appName)
appDir = path.join(shellAppDir, 'resources', 'app')
atomShellDownloadDir = path.join(os.tmpdir(), 'atom-cached-atom-shells')
else
appName = 'Atom.app'
tmpDir = '/tmp'
@ -28,6 +29,7 @@ module.exports = (grunt) ->
shellAppDir = path.join(buildDir, appName)
contentsDir = path.join(shellAppDir, 'Contents')
appDir = path.join(contentsDir, 'Resources', 'app')
atomShellDownloadDir = '/tmp/atom-cached-atom-shells'
installDir = path.join(installRoot, appName)
@ -168,6 +170,12 @@ module.exports = (grunt) ->
_.extend(context, parsed.attributes)
parsed.body
'download-atom-shell':
version: packageJson.atomShellVersion
outputDir: 'atom-shell'
downloadDir: atomShellDownloadDir
rebuild: true # rebuild native modules after atom-shell is updated
shell:
'kill-atom':
command: 'pkill -9 Atom'
@ -183,13 +191,14 @@ module.exports = (grunt) ->
grunt.loadNpmTasks('grunt-contrib-coffee')
grunt.loadNpmTasks('grunt-contrib-less')
grunt.loadNpmTasks('grunt-markdown')
grunt.loadNpmTasks('grunt-download-atom-shell')
grunt.loadNpmTasks('grunt-shell')
grunt.loadTasks('tasks')
grunt.registerTask('compile', ['coffee', 'prebuild-less', 'cson'])
grunt.registerTask('lint', ['coffeelint', 'csslint', 'lesslint'])
grunt.registerTask('test', ['shell:kill-atom', 'run-specs'])
grunt.registerTask('ci', ['update-atom-shell', 'build', 'set-development-version', 'lint', 'test'])
grunt.registerTask('deploy', ['partial-clean', 'update-atom-shell', 'build', 'codesign'])
grunt.registerTask('ci', ['download-atom-shell', 'build', 'set-development-version', 'lint', 'test'])
grunt.registerTask('deploy', ['partial-clean', 'download-atom-shell', 'build', 'codesign'])
grunt.registerTask('docs', ['markdown:guides', 'build-docs'])
grunt.registerTask('default', ['update-atom-shell', 'build', 'set-development-version', 'install'])
grunt.registerTask('default', ['download-atom-shell', 'build', 'set-development-version', 'install'])

View File

@ -149,7 +149,8 @@
"language-todo": "0.2.0",
"language-toml": "0.7.0",
"language-xml": "0.2.0",
"language-yaml": "0.1.0"
"language-yaml": "0.1.0",
"grunt-download-atom-shell": "0.1.1"
},
"private": true,
"scripts": {

View File

@ -226,6 +226,10 @@ class PackageManager
{@packageDependencies} = JSON.parse(fs.readFileSync(metadataPath)) ? {}
@packageDependencies ?= {}
# Temporarily ignore 'grunt-download-atom-shell' here, should remove this
# when it became a public npm module.
delete @packageDependencies['grunt-download-atom-shell']
@packageDependencies
# Public: Get an array of all the available package paths.

View File

@ -1,210 +0,0 @@
fs = require 'fs'
path = require 'path'
os = require 'os'
request = require 'request'
formidable = require 'formidable'
unzip = require 'unzip'
module.exports = (grunt) ->
{spawn, mkdir, rm, cp} = require('./task-helpers')(grunt)
accessToken = null
getTokenFromKeychain = (callback) ->
accessToken ?= process.env['ATOM_ACCESS_TOKEN']
if accessToken
callback(null, accessToken)
return
spawn {cmd: 'security', args: ['-q', 'find-generic-password', '-ws', 'GitHub API Token']}, (error, result, code) ->
accessToken = result.stdout unless error?
callback(error, accessToken)
callAtomShellReposApi = (path, callback) ->
getTokenFromKeychain (error, accessToken) ->
if error
callback(error)
return
options =
url: "https://api.github.com/repos/atom/atom-shell#{path}"
proxy: process.env.http_proxy || process.env.https_proxy
headers:
authorization: "token #{accessToken}"
accept: 'application/vnd.github.manifold-preview'
'user-agent': 'Atom'
request options, (error, response, body) ->
if not error?
body = JSON.parse(body)
error = new Error(body.message) if response.statusCode != 200
callback(error, response, body)
findReleaseIdFromAtomShellVersion = (version, callback) ->
callAtomShellReposApi '/releases', (error, response, data) ->
if error?
grunt.log.error('GitHub API failed to access atom-shell releases')
callback(error)
else
for release in data when release.tag_name is version
callback(null, release.id)
return
grunt.log.error("There is no #{version} release of atom-shell")
callback(false)
getAtomShellDownloadUrl = (version, releaseId, callback) ->
callAtomShellReposApi "/releases/#{releaseId}/assets", (error, response, data) ->
if error?
grunt.log.error("Cannot get assets of atom-shell's #{version} release")
callback(error)
else
filename = "atom-shell-#{version}-#{process.platform}.zip"
for asset in data when asset.name is filename and asset.state is 'uploaded'
callback(null, asset.url)
return
grunt.log.error("Cannot get url of atom-shell's release asset")
callback(false)
getAtomShellVersion = ->
versionPath = path.join('atom-shell', 'version')
if grunt.file.isFile(versionPath)
grunt.file.read(versionPath).trim()
else
null
getTempDir = ->
if process.platform is 'win32' then os.tmpdir() else '/tmp'
getCachePath = (version) ->
path.join(getTempDir(), 'atom-cached-atom-shells', version)
isAtomShellVersionCached = (version) ->
grunt.file.isFile(getCachePath(version), 'version')
getDownloadOptions = (version, url, callback) ->
options =
url: url
followRedirect: false
proxy: process.env.http_proxy || process.env.https_proxy
# Only set headers for GitHub host, the url could also be a S3 link and
# setting headers for it would make the request fail.
if require('url').parse(url).hostname is 'api.github.com'
getTokenFromKeychain (error, accessToken) ->
options.headers =
authorization: "token #{accessToken}"
accept: 'application/octet-stream'
'user-agent': 'Atom'
callback(error, options)
else
callback(null, options)
downloadAtomShell = (version, url, callback) ->
getDownloadOptions version, url, (error, options) ->
if error
callback(error)
return
inputStream = request(options)
inputStream.on 'response', (response) ->
if response.statusCode is 302
# Manually handle redirection so headers would not be sent for S3.
downloadAtomShell(version, response.headers.location, callback)
else if response.statusCode is 200
grunt.verbose.writeln("Downloading atom-shell version #{version.cyan}")
cacheDirectory = getCachePath(version)
rm(cacheDirectory)
mkdir(cacheDirectory)
form = new formidable.IncomingForm()
form.uploadDir = cacheDirectory
form.maxFieldsSize = 100 * 1024 * 1024
form.on 'file', (name, file) ->
cacheFile = path.join(cacheDirectory, 'atom-shell.zip')
fs.renameSync(file.path, cacheFile)
callback(null, cacheFile)
form.parse response, (error) ->
if error
grunt.log.error("atom-shell #{version.cyan} failed to download")
else
if response.statusCode is 404
grunt.log.error("atom-shell #{version.cyan} not found")
else
grunt.log.error("atom-shell #{version.cyan} request failed")
callback(false)
downloadAtomShellOfVersion = (version, callback) ->
findReleaseIdFromAtomShellVersion version, (error, releaseId) ->
if error?
callback(error)
else
getAtomShellDownloadUrl version, releaseId, (error, url) ->
if error?
callback(error)
else
downloadAtomShell version, url, callback
unzipAtomShell = (zipPath, callback) ->
grunt.verbose.writeln('Unzipping atom-shell')
directoryPath = path.dirname(zipPath)
if process.platform is 'darwin'
# The zip archive of darwin build contains symbol links, only the "unzip"
# command can handle it correctly.
spawn {cmd: 'unzip', args: [zipPath, '-d', directoryPath]}, (error) ->
rm(zipPath)
callback(error)
else
fileStream = fs.createReadStream(zipPath)
fileStream.on('error', callback)
zipStream = fileStream.pipe(unzip.Extract(path: directoryPath))
zipStream.on('error', callback)
zipStream.on 'close', ->
rm(zipPath)
callback(null)
rebuildNativeModules = (previousVersion, callback) ->
newVersion = getAtomShellVersion()
if newVersion and newVersion isnt previousVersion
grunt.verbose.writeln("Rebuilding native modules for new atom-shell version #{newVersion.cyan}.")
cmd = path.join('node_modules', '.bin', 'apm')
cmd += ".cmd" if process.platform is 'win32'
spawn {cmd, args: ['rebuild']}, (error) -> callback(error)
else
callback()
installAtomShell = (version) ->
rm('atom-shell')
cp(getCachePath(version), 'atom-shell')
grunt.registerTask 'update-atom-shell', 'Update atom-shell', ->
done = @async()
{atomShellVersion} = grunt.file.readJSON('package.json')
if atomShellVersion
atomShellVersion = "v#{atomShellVersion}"
currentAtomShellVersion = getAtomShellVersion()
if atomShellVersion isnt currentAtomShellVersion
if isAtomShellVersionCached(atomShellVersion)
grunt.verbose.writeln("Installing cached atom-shell #{atomShellVersion.cyan}")
installAtomShell(atomShellVersion)
rebuildNativeModules(currentAtomShellVersion, done)
else
downloadAtomShellOfVersion atomShellVersion, (error, zipPath) ->
if error?
done(error)
else if zipPath?
unzipAtomShell zipPath, (error) ->
if error?
done(error)
else
grunt.verbose.writeln("Installing atom-shell #{atomShellVersion.cyan}")
installAtomShell(atomShellVersion)
rebuildNativeModules(currentAtomShellVersion, done)
else
done(false)
else
done()
else
grunt.log.error("atom-shell version missing from package.json")
done(false)