Skip to content

Instantly share code, notes, and snippets.

@jocafa
Created October 17, 2011 16:42
Show Gist options
  • Select an option

  • Save jocafa/1293042 to your computer and use it in GitHub Desktop.

Select an option

Save jocafa/1293042 to your computer and use it in GitHub Desktop.
task 'proto', 'retrieve, extract, and compile proto files', ->
version = '0.2.1.p0'
tmpPath = './tmp'
gemFile = "atlas-#{version}.gem"
defPath = path.join tmpPath, 'definitions'
protoPath = path.join defPath, 'atlas'
schemaPath = path.join defPath, 'schema.desc'
rpcPath = path.join tmpPath, 'lib', 'atlas', 'rpc', 'config', 'rpc.yml'
gemPath = path.join tmpPath, gemFile
destPath = './proto'
allProtoPath = path.join destPath, 'defs.proto'
options =
host: 'gems.moneydesktop.com'
port: 80
path: "/gems/#{gemFile}"
method: 'GET'
output = ''
request = http.request options, (res) ->
cleanupTempAndExit tmpPath, "Gem file not found on server\n" if res.statusCode != 200
res.setEncoding 'binary'
res.on 'data', (chunk) -> output += chunk
res.on 'end', ->
fs.mkdir tmpPath, 0755
fs.writeFile gemPath, output, 'binary', (err) ->
cleanupTempAndExit tmpPath, err if err
extractProtoFiles gemPath, tmpPath, (err) ->
throw err if err
path.exists schemaPath, (exists) ->
throw "Schema descriptor not found in gem" unless exists
fs.renameSync schemaPath, path.join(destPath, 'schema.desc')
fs.writeFileSync(allProtoPath, recursivelyCat(protoPath, 'proto'))
path.exists rpcPath, (exists) ->
throw "Protobuf rpc yml not found in gem" unless exists
fs.renameSync rpcPath, path.join(destPath, 'rpc.yml')
request.on 'error', (e) -> throw "Error downloading gem file"
process.on 'exit', -> childProc.exec "rm -rf #{tmpPath}"
process.on 'uncaughtException', (err) -> sys.print err + "\n"
request.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment