Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created October 5, 2012 23:05
Show Gist options
  • Save pmuellr/3842981 to your computer and use it in GitHub Desktop.
Save pmuellr/3842981 to your computer and use it in GitHub Desktop.
Cordova iOS Developer tool
#!/usr/bin/env node
fs = require("fs")
path = require("path")
child_process = require("child_process")
//------------------------------------------------------------------------------
PROGRAM = path.basename(__filename)
VERSION = "0.2.0.2012-10-09"
var CordovaVersion
//------------------------------------------------------------------------------
function main() {
var cmd = process.argv[2]
switch (cmd) {
case "install": return doInstall()
case "clean": return doClean()
case "build": return doBuild()
case "update": return doUpdate()
case "build-update": return doBuildUpdate()
case "wr": return doWr()
default: return doHelp()
}
}
//------------------------------------------------------------------------------
function doClean() {
ensureGitRepos()
if (dirExists("CordovaSamp")) rmR("CordovaSamp")
if (dirExists("CordovaSpec")) rmR("CordovaSpec")
}
//------------------------------------------------------------------------------
function doInstall() {
ensureGitRepos()
if (dirExists("CordovaSamp")) error("directory CordovaSamp already exists!")
if (dirExists("CordovaSpec")) error("directory CordovaSpec already exists!")
exec("jake", [], {cwd: "incubator-cordova-js"}, doInstall_2)
}
//------------------------------------------------------------------------------
function doInstall_2() {
var cmd = "incubator-cordova-ios/bin/create"
var args = ["CordovaSamp", "com.example.CordovaSamp", "CordovaSamp"]
exec(cmd, args, {}, doInstall_3)
}
//------------------------------------------------------------------------------
function doInstall_3() {
var cmd = "incubator-cordova-ios/bin/create"
var args = ["CordovaSpec", "com.example.CordovaSpec", "CordovaSpec"]
exec(cmd, args, {}, doUpdate)
}
//------------------------------------------------------------------------------
function doBuild() {
ensureGitRepos()
ensureXcodeProjects()
exec("jake", [], {cwd: "incubator-cordova-js"})
}
//------------------------------------------------------------------------------
function doUpdate() {
ensureGitRepos()
ensureXcodeProjects()
copyNativeBits("CordovaSamp")
copyNativeBits("CordovaSpec")
cp(
"incubator-cordova-js/pkg/cordova.ios.js",
"CordovaSamp/www/cordova-" + CordovaVersion + ".js"
)
rmR("CordovaSpec/www")
fs.mkdirSync("CordovaSpec/www")
cpR(
"incubator-cordova-mobile-spec",
"CordovaSpec/www"
)
cp(
"incubator-cordova-js/pkg/cordova.ios.js",
"CordovaSpec/www/cordova.js"
)
}
//------------------------------------------------------------------------------
function doBuildUpdate() {
ensureGitRepos()
ensureXcodeProjects()
exec("jake", [], {cwd: "incubator-cordova-js"}, doUpdate)
}
//------------------------------------------------------------------------------
function doWr() {
ensureGitRepos()
ensureXcodeProjects()
var sourceFiles = getSourceFiles()
sourceFiles.unshift("./cid build-update")
exec("wr", sourceFiles, {silent: true})
}
//------------------------------------------------------------------------------
function copyNativeBits(proj) {
cpR(
"incubator-cordova-ios/bin/templates/project/__TESTING__/Resources",
proj + "/" + proj + "/Resources"
)
cpR(
"incubator-cordova-ios/bin/templates/project/cordova",
proj + "/cordova"
)
cpR(
"incubator-cordova-ios/bin/templates/project/www",
proj + "/www"
)
cpR(
"incubator-cordova-ios/CordovaLib",
proj + "/CordovaLib"
)
}
//------------------------------------------------------------------------------
function getSourceFiles() {
return [].concat(
lsR2("incubator-cordova-ios/bin/templates/project"),
lsR2("incubator-cordova-ios/CordovaLib"),
lsR2("incubator-cordova-js/lib"),
lsR2("incubator-cordova-js/test"),
lsR2("incubator-cordova-mobile-spec")
)
}
//------------------------------------------------------------------------------
// returns files with dir pre-pended
//------------------------------------------------------------------------------
function lsR2(dir) {
return lsR(dir).map(function(entry) {
return path.join(dir, entry)
})
}
//------------------------------------------------------------------------------
// returns files without dir pre-pended
//------------------------------------------------------------------------------
function lsR(dir, files, currPath) {
files = files || []
currPath = currPath || ""
var entries = fs.readdirSync(dir)
entries.forEach(function(entry) {
if (entry == ".git") return
if (entry == ".gitignore") return
if (entry == "project.xcworkspace") return
if (entry == "xcuserdata") return
var fullEntry = path.join(dir, entry)
var currEntry = path.join(currPath, entry)
var stats = fs.statSync(fullEntry)
if (stats.isDirectory()) {
lsR(fullEntry, files, currEntry)
}
else {
files.push(currEntry)
}
})
return files
}
//------------------------------------------------------------------------------
function rmR(dir) {
var entries = fs.readdirSync(dir)
entries.forEach(function(entry) {
entry = path.join(dir, entry)
var stats = fs.statSync(entry)
if (stats.isDirectory()) {
rmR(entry)
}
else {
fs.unlinkSync(entry)
}
})
fs.rmdirSync(dir)
}
//------------------------------------------------------------------------------
function cp(fromFile, toFile) {
mkdirp(path.dirname(toFile))
var contents = fs.readFileSync(fromFile)
fs.writeFileSync(toFile, contents)
}
//------------------------------------------------------------------------------
function cpR(fromDir, toDir) {
var files = lsR(fromDir)
files.forEach(function(file) {
cp(path.join(fromDir, file), path.join(toDir, file))
})
}
//------------------------------------------------------------------------------
function mkdirp(pathName) {
if (dirExists(pathName)) return
mkdirp(path.join(pathName, ".."))
fs.mkdirSync(pathName)
}
//------------------------------------------------------------------------------
function ensureXcodeProjects() {
ensureDirExists("CordovaSamp")
ensureDirExists("CordovaSpec")
}
//------------------------------------------------------------------------------
function ensureGitRepos() {
ensureDirExists("incubator-cordova-js")
ensureDirExists("incubator-cordova-ios")
ensureDirExists("incubator-cordova-mobile-spec")
CordovaVersion = getCordovaVersion()
}
//------------------------------------------------------------------------------
function ensureDirExists(name) {
if (!dirExists(name)) error("expecting directory " + name + " but not found.")
}
//------------------------------------------------------------------------------
function getCordovaVersion() {
var fileName = "incubator-cordova-js/VERSION"
var content = fs.readFileSync(fileName, "utf-8")
return content.split("\n")[0]
}
//------------------------------------------------------------------------------
function dirExists(name) {
var exists = fs.existsSync || path.existsSync
if (!exists(name)) return false
var stats = fs.statSync(name)
return stats.isDirectory()
}
//------------------------------------------------------------------------------
function log(message) {
console.log(PROGRAM + ": " + message)
}
//------------------------------------------------------------------------------
function error(message) {
console.error(PROGRAM + ": " + message)
process.exit(1)
}
//------------------------------------------------------------------------------
function exec(cmd, args, opts, callback) {
opts = opts || {}
opts.stdio = ['ignore', 'pipe', 'pipe']
var silent = opts.silent
delete opts.silent
var fullCmd = cmd + " " + args.join(" ")
if (opts.cwd) {
fullCmd += " in directory " + opts.cwd
}
if (!silent) console.log("running " + fullCmd)
var proc = child_process.spawn(cmd, args, opts)
proc.stdout.on('data', function (data) {
process.stdout.write(data)
})
proc.stderr.on('data', function (data) {
process.stderr.write(data)
})
proc.on('exit', function (code, signal) {
if (code == 0) {
if (!callback) process.exit(0)
return callback()
}
console.error("error: " + code + " running " + fullCmd + '; exiting')
process.exit(1)
})
}
//------------------------------------------------------------------------------
function doHelp() {
var text = [
PROGRAM + " [cmd] -- a tool for Cordova Ios Developers",
" v " + VERSION,
"",
"[cmd]:",
" install - create Xcode projects in cwd",
" build - build the files in the git repos",
" update - copy the files from the git repos to the Xcode projects",
" build-update - do a build, then update",
" wr - when a file in the git repos change, run build/update",
"",
"'install' expects the following directories in your cwd, all of which",
"are expected to be git clones of the relevant Apache git repos:",
"",
" incubator-cordova-js",
" incubator-cordova-ios",
" incubator-cordova-mobile-spec",
"",
"'install' creates the following directories in your cwd, all of which",
"end up being Xcode projects:",
"",
" CordovaSample - the sample Cordova app",
" CordovaSpec - the mobile-spec test suite",
"",
"'build' will do whatever build-y things need to happen against the",
"source in the git repo clones.",
"",
"'update' will copy the relevant source and built files in the git repo clones",
"into the Xcode projects",
"",
"'wr' will use the 'wr' utility to watch for a change in a source file,",
"and then launch a build and then update",
""
]
console.log(text.join("\n"))
process.exit(1)
}
//------------------------------------------------------------------------------
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment