Permalink
Browse files

Added spludo-gen (the spludo generator).

Added first code template for new project.
  • Loading branch information...
1 parent fb691d3 commit ee415ac4c757d440589b92ccc786bfa13dd8adeb @DracoBlue committed Jun 15, 2010
@@ -0,0 +1,94 @@
+/*
+ * This file is part of the Spludo Framework.
+ * Copyright (c) 2009-2010 DracoBlue, http://dracoblue.net/
+ *
+ * Licensed under the terms of MIT License. For the full copyright and license
+ * information, please see the LICENSE file in the root folder.
+ */
+
+var sys = require("sys");
+var fs = require("fs");
+var path = require("path");
+
+SpludoGenerator.addCodeTemplate("new-project", {
+ parameters: [
+ {
+ "name": "project.name",
+ "caption": "Name of the Project (e.g. myapp)"
+ },
+ {
+ "name": "spludo.directory",
+ "caption": "Spludo Directory"
+ }
+ ],
+
+ validateParameter: function(name, value) {
+ return function(cb) {
+ if (name === "project.name") {
+ if (!value.toLowerCase().match(/^[a-z0-9\_\-]+$/)) {
+ sys.puts("Unsupported project name (expected: [a-z0-9\\_\\-]+): " + value);
+ cb(true);
+ } else {
+ var project_directory = SpludoGenerator.working_directory + value + '/';
+
+ try {
+ var file_stat = fs.statSync(project_directory);
+ sys.puts("Folder already exists: " + project_directory);
+ cb(true);
+ } catch (e) {
+ /*
+ * Yay, it fails so it should not exist yet.
+ */
+ SpludoGenerator.target_directory = project_directory;
+ cb(false, value);
+ }
+ }
+ } else if (name === "spludo.directory") {
+ try {
+ /*
+ * Let's check some files which are usually in a spludo installation.
+ */
+ var file_stat = fs.statSync(value);
+ if (!file_stat.isDirectory()) {
+ throw new Error("Spludo directory is not a directory!");
+ }
+ file_stat = fs.statSync(value + 'core');
+ if (!file_stat.isDirectory()) {
+ throw new Error("Spludo's core directory is not a directory!");
+ }
+ fs.statSync(value + 'build/spludo-gen.js');
+ fs.statSync(value + 'core/index.js');
+ cb(false, value);
+ } catch (e) {
+ sys.puts("This is no spludo installation at: " + value);
+ cb(true);
+ }
+ } else {
+ cb(true);
+ }
+ };
+ },
+
+ getParameterDefault: function(name) {
+ return function(cb) {
+ if (name === "spludo.directory") {
+ cb(false, path.dirname(path.dirname(__dirname)) + '/');
+ } else {
+ cb(true, "");
+ }
+ };
+ },
+
+ preExecuteHook: function(values) {
+ return function(cb) {
+ try {
+ fs.mkdirSync(SpludoGenerator.target_directory, 0755);
+ } catch (exception) {
+ sys.puts("Could not create project directory: " + exception.message);
+ process.exit(1);
+ }
+ sys.puts("Directory created: " + SpludoGenerator.target_directory);
+ cb();
+ }
+ }
+});
@@ -0,0 +1,42 @@
+<project name="%%%project.name%%%" default="list" basedir=".">
+
+ <description>
+ This is the %%%project.name%%% application built with spludo.
+ </description>
+
+ <!--
+ this is the build directory, anything which
+ is generated will be available in this directory
+ -->
+ <property name="build" location="build"/>
+
+ <!-- Necessary to use the spludo default tasks -->
+ <property name="spludo.directory" location="%%%spludo.directory%%%"/>
+ <import file="${spludo.directory}/build/build.xml"/>
+
+ <!-- project specific tasks -->
+
+ <target name="clean" description="clean up" depends="core-clean" >
+ <delete dir="${build}"/>
+ </target>
+
+ <target name="test">
+ <exec executable="bash">
+ <arg line="-c &quot;node ${spludo.directory}/build/run_tests.js&quot;" />
+ </exec>
+ </target>
+
+ <target name="test-xml">
+ <mkdir dir="${build}" />
+ <exec executable="bash">
+ <arg line="-c &quot;node ${spludo.directory}/build/run_tests.js xml > ${build}/test_results.xml &quot;" />
+ </exec>
+ </target>
+
+ <target name="lint">
+ <exec executable="bash">
+ <arg line="-c &quot;jsl -conf ${spludo.directory}/build/jsl/jsl.conf -nologo -nofilelisting -process 'static/*.js' -process 'lib/*.js' -process 'controllers/*.js' -process 'views/*.js'&quot;" />
+ </exec>
+ </target>
+</project>
+
@@ -0,0 +1,5 @@
+config.setValues({
+ "logging": {
+ "level": 2
+ }
+});
@@ -0,0 +1,4 @@
+require("%%%spludo.directory%%%core");
+new ConsoleApplication( {
+ "path": process.ARGV[2] || ""
+}).run();
@@ -0,0 +1,2 @@
+require("%%%spludo.directory%%%core");
+new ServerApplication().run();
View
@@ -0,0 +1,186 @@
+/*
+ * This file is part of the Spludo Framework.
+ * Copyright (c) 2009-2010 DracoBlue, http://dracoblue.net/
+ *
+ * Licensed under the terms of MIT License. For the full copyright and license
+ * information, please see the LICENSE file in the root folder.
+ */
+
+var sys = require("sys");
+var fs = require("fs");
+var child_process = require("child_process");
+var path = require("path");
+
+require("./../core/util");
+
+var working_directory = process.cwd() + '/';
+var spludo_directory = path.dirname(__dirname) + '/';
+
+SpludoGenerator = {};
+
+SpludoGenerator.code_templates = {};
+
+SpludoGenerator.target_directory = working_directory;
+SpludoGenerator.working_directory = working_directory;
+
+SpludoGenerator.addCodeTemplate = function(name, options) {
+ options.name = name;
+ SpludoGenerator.code_templates[name] = options;
+};
+
+SpludoGenerator.handleCodeTemplate = function(name) {
+ var self = this;
+
+ var code_template = this.code_templates[name];
+ var parameters = code_template.parameters;
+ var parameters_length = parameters.length;
+
+ var values_chain = [];
+ var values = [];
+
+
+ for (var i = 0; i < parameters_length; i++) {
+ (function(parameter) {
+ var validate_paramter_handler = function(chain_cb) {
+
+ var default_value = "";
+ var value = "";
+
+ chain(function(sub_chain_cb) {
+ code_template.getParameterDefault(parameter.name)(function(err, parameter_default_value) {
+ if (err) {
+ default_value = "";
+ } else {
+ default_value = parameter_default_value;
+ }
+ sub_chain_cb();
+ });
+ }, function(sub_chain_cb) {
+ sys.print(parameter.caption);
+
+ if (default_value !== "") {
+ sys.print(" [" + default_value + "]");
+ }
+
+ sys.print(': ');
+ var st = process.openStdin();
+
+ var data_handler = function(input_value) {
+ st.removeListener("data", data_handler);
+ value = input_value.toString().split("\n")[0];
+ sub_chain_cb();
+ };
+ st.addListener("data", data_handler);
+ }, function() {
+ if (value === "") {
+ value = default_value;
+ }
+
+ code_template.validateParameter(parameter.name, value)(function(err, valid_value) {
+ if (err) {
+ validate_paramter_handler(chain_cb);
+ } else {
+ values.push([parameter.name, valid_value]);
+ chain_cb();
+ }
+ });
+ });
+
+ };
+
+ values_chain.push(validate_paramter_handler);
+ })(parameters[i]);
+ }
+
+ values_chain.push(function() {
+ sys.puts(" ");
+ sys.puts(" Working ... ");
+ sys.puts("");
+
+ if (typeof code_template.preExecuteHook !== "undefined") {
+ code_template.preExecuteHook(values)(function() {
+ self.performCodeTemplate(__dirname + '/code-templates/' + code_template.name + '/', values);
+ });
+ } else {
+ self.performCodeTemplate(__dirname + '/code-templates/' + code_template.name + '/', values);
+ }
+
+ });
+
+ chain.apply(GLOBAL, values_chain);
+};
+
+SpludoGenerator.performCodeTemplate = function(template_directory, values) {
+ var self = this;
+
+ var values_length = values.length;
+
+ var token_replacer = function(input) {
+ var output = input;
+
+ for (var i = 0; i < values_length; i++) {
+ output = output.split("%%%" + values[i][0] + "%%%").join(values[i][1]);
+ }
+
+ return output;
+ };
+
+ var files = [];
+
+ chain(function(chain_cb) {
+ child_process.exec("cd " + template_directory + " && find", function(err, raw_files) {
+ if (err) {
+ sys.puts("Could not list code-template from: " + template_directory);
+ process.exit(1);
+ } else {
+ var raw_files = raw_files.split("\n");
+ var raw_files_length = raw_files.length;
+ for (var i = 0; i < raw_files_length; i++) {
+ var file = raw_files[i].substr(2);
+ if (file !== '') {
+ files.push(file);
+ }
+ }
+ }
+ chain_cb();
+ });
+ }, function() {
+ var files_length = files.length;
+ for (var i = 0; i < files_length; i++) {
+ var file = files[i];
+ var file_stat = fs.statSync(template_directory + file);
+
+ if (file_stat.isDirectory()) {
+ fs.mkdirSync(self.target_directory + token_replacer(file), 0755);
+ sys.puts("Created folder: " + self.target_directory + token_replacer(file));
+ } else {
+ var raw_file_contents = fs.readFileSync(template_directory + file).toString();
+ fs.writeFileSync(self.target_directory + token_replacer(file), token_replacer(raw_file_contents));
+ }
+ sys.puts("Created file: " + self.target_directory + token_replacer(file));
+ }
+
+ sys.puts(" ");
+ sys.puts(" Finished!");
+ sys.puts("");
+
+ process.exit(0);
+ });
+};
+
+require("./code-templates/core-code-templates");
+
+var code_template = process.ARGV[2];
+
+sys.puts(" ");
+sys.puts(" Spludo-Generator - http://spludo.com/ - Copyright 2009-2010 by DracoBlue <http://dracoblue.net>");
+sys.puts(" ");
+sys.puts(" Template: " + code_template);
+sys.puts("");
+
+if (typeof SpludoGenerator.code_templates[code_template] === "undefined") {
+ sys.puts(" Error: Unsupported code template: " + code_template);
+ process.exit(1);
+}
+
+SpludoGenerator.handleCodeTemplate(code_template);
View
@@ -0,0 +1,2 @@
+#!/bin/bash
+node `dirname $0`/build/spludo-gen.js $*

0 comments on commit ee415ac

Please sign in to comment.