Skip to content

Commit

Permalink
[enhance] opa-create: Support for multiple templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
akoprow authored and cedricss committed Jul 27, 2012
1 parent ca7e971 commit 8878a9e
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions tools/opa-create/src/opa-create.opa
Expand Up @@ -15,14 +15,24 @@
*
* @category tools
* @author Cedric Soulas
* @author Adam Koprowski (added multiple templates)
* @destination PUBLIC
* @stability experimental
*/
import-plugin unix
import stdlib.{system, io.file}

SRC_DIR = "tools/opa-create/template/mvc"
resources = @static_content_directory("tools/opa-create/template/mvc")
type Template.t = { string name, string dir, map(string, -> binary) resources }

function Template.t mk_template(name, dir, resources) {
~{ name, dir, ~resources }
}

mvc_template =
mk_template("mvc", "tools/opa-create/template/mvc",
@static_content_directory("tools/opa-create/template/mvc"))

list(Template.t) templates = [ mvc_template ]

function CommandLine_ident(names,description,param_doc)(up) {
{ CommandLine.default_parser with
Expand All @@ -31,41 +41,50 @@ function CommandLine_ident(names,description,param_doc)(up) {
}
}

list(CommandLine.parser({option(string) name})) cmdline_parsers = [
CommandLine_ident(["--name", "-n"],
"Application name, without spaces, \"/\" or any other special characters",
"app_name (no special characters)")(function(name, r){ {r with name:some(name)} })
]
list(CommandLine.parser({oprion(string) name, Template.t template})) options_parsers =
template_names = List.map(_.name, templates)
[
CommandLine_ident(["--name", "-n"],
"Application name, without spaces, \"/\" or any other special characters",
"app_name (no special characters)")({ function(name, r){ r with name:some(name)}}
),
CommandLine_ident(["--template", "-t"],
"Template to be used for the application",
"template_name [{template_names}]")(
function(template_name, r) {
{ r with template: List.find(function (t) {t.name == template_name}, templates) ?
error("Wrong template name: {template_name} (known templates: {template_names})") }
}
)
]

options =
cmdline = {
init : { name : none },
parsers : cmdline_parsers,
anonymous : [],
title : "Opa Application Generator"
}
init: { name: none, template: mvc_template },
parsers: options_parsers,
anonymous: [],
title: "Opa Application Generator"
}
CommandLine.filter(cmdline)

function write(file, content) {
%%BslFile.of_string%%("./{file}", binary_of_string(content))
%%BslFile.of_string%%("./{file}", binary_of_string(content))
}

function process_resource(file_name, file_content) {
n = String.length(options.template.dir)
file = "{options.name}{String.sub(n, String.length(file_name) - n, file_name)}"
if (File.exists(file)) { warning("File {file_name} already exists. \nPlease delete it and try again."); System.exit(1) }
jlog("Generating {file_name}")
content = String.replace("application_name", options.name, string_of_binary(file_content()))
write(file, content)
}

Scheduler.push(function () { // hack for node.js toplevel instruction
match (options.name) {
case {none}: System.exit(0);
case {some:name}:
function iter(file, f_content) {
n = String.length(SRC_DIR)
file = "{name}{String.sub(n, String.length(file) - n, file)}"
if (File.exists(file)) {
Log.warning("OpaCreate", "File {file} already exists. \nPlease delete it and try again.");
System.exit(1)
}
jlog("Generating {file}")
content = String.replace("application_name", name, string_of_binary(f_content()))
write(file, content)
}
StringMap.iter(iter, resources)
jlog("\nNow you can type:\n$ cd {name}\n$ make run")
StringMap.iter(process_resource, options.template.resources)
jlog("\nNow you can type:\n$ cd {options.name}\n$ make run")
}
}
})

0 comments on commit 8878a9e

Please sign in to comment.