Skip to content

Instantly share code, notes, and snippets.

@armornick
Last active March 30, 2018 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armornick/5330d3b25cbdb21fb3083f050f9d4f0f to your computer and use it in GitHub Desktop.
Save armornick/5330d3b25cbdb21fb3083f050f9d4f0f to your computer and use it in GitHub Desktop.
Node-based Project Generator (templates not included)
var Generator = (function () {
var Mustache = require("./lib/mustache.min.js"),
fs = require("fs")
function Generator() {
this.templateDir = null
this.config = null
}
Generator.prototype.init = function(projectDir) {
process.chdir(projectDir)
console.log("project directory: ", process.cwd())
}
Generator.prototype.addDirectory = function(path) {
console.log("adding directory: ", path)
fs.mkdirSync(path)
}
Generator.prototype.addFile = function(template, outpath) {
var templatePath = this.templateDir + "/" + template
var templateSource = fs.readFileSync(templatePath, "utf8")
var output = Mustache.render(templateSource, this.config)
console.log("adding file:", outpath)
fs.writeFileSync(outpath, output)
}
return Generator
}())
var PROJECT_TYPES = (function () {
var types = {}
types.cmake = function () {
console.log("generating CMake project")
this.addDirectory("src")
if (this.config.duktape) {
this.addDirectory("vendor")
this.addDirectory("vendor/duktape")
}
this.addFile("CMakeLists.txt", "CMakeLists.txt")
if (this.config.duktape) {
this.addFile("duktape-main.cpp", "src/main.cpp")
}
}
types.duktape = types.cmake
types.csharp = function () {
console.log("generating C# project")
var mainfile = this.config.name + ".cs"
this.addFile("main.cs", mainfile)
this.config.mainfile = mainfile
this.config.compiler = "csc"
this.addFile("build.bat", "build.bat")
}
types.cs = types.csharp
types.VisualBasic = function () {
console.log("generating Visual Basic project")
var mainfile = this.config.name + ".vb"
this.addFile("main.vb", mainfile)
this.config.mainfile = mainfile
this.config.compiler = "vbc"
this.config.vb = true
this.addFile("build.bat", "build.bat")
}
types.VB = types.VisualBasic
types.vb = types.VisualBasic
return types
}())
var generator = new Generator()
var targetDir;
if (process.argv.length > 2) {
targetDir = process.argv[2]
} else {
targetDir = process.cwd()
}
generator.init(targetDir)
// no error checking
// because we would fail if no project.json was found
var config = require(process.cwd() + "/project.json")
generator.config = config
generator.templateDir = __dirname + "/templates"
if (!config.type || !config.name) {
console.log("project type and name are required")
return
}
var generate = PROJECT_TYPES[config.type]
if (!generate) {
console.log("project type was not recognized")
return
}
generate.call(generator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment