1 load(JSBuilderPath + 'src/Template.js');
2 load(JSBuilderPath + 'src/XTemplate.js');
7 * @class Ext.generator.Base
9 * Base class for all Generators
11 Ext.generator.Base = Ext.extend(Object, {
13 * @cfg {Boolean} pretend True to only output what the generator would do (e.g. which files would be created),
14 * without actually modifying anything on the filesystem.
20 constructor: function(config) {
21 Ext.apply(this, config);
24 this.decodeArgs(this.args);
29 * Creates an empty directory at the given location
30 * @param {String} path The directory path
33 var length = arguments.length,
36 for (i = 0; i < length; i++) {
37 dirName = this.basePath + "/" + arguments[i];
38 Logger.log(" Creating dir: " + dirName);
41 Filesystem.mkdir(dirName);
47 * Applies data to an XTemplate, saving its output to the given file name
48 * @param {String} name The name of the template
50 template: function(name, data, filename) {
51 Logger.log(" Creating file: " + filename);
53 // dirty hack to let <tpl> get through without being picked up
58 var name = 'src/generators/' + this.dirName + '/templates/' + name + '.js',
59 stream = new Stream(name, 'rw'),
60 template = new Ext.XTemplate(stream.readText()),
61 contents = template.apply(data),
62 destination = this.basePath + '/' + filename,
63 newFile = new Stream(destination, "w");
65 newFile.writeLine(contents);
66 system.move(destination, filename, true);
71 * Copies a file from the generator's files directory into the app
72 * @param {String} fileName The name of the file to copy
73 * @param {String} destination The destination path (defaults to the fileName)
74 * @param {Boolean} silent True to not log any messages (defaults to false)
76 file: function(fileName, destination, silent) {
77 Logger.log(" Copying " + fileName);
79 destination = this.basePath + '/' + (destination || fileName);
80 fileName = 'src/generators/' + this.dirName + '/files/' + fileName;
82 if (!this.pretend && this.silent !== true) {
83 Filesystem.copy(fileName, destination);
88 * Copies all contents of the given source directory to a destination
89 * @param {String} dirName The name of the directory to copy
90 * @param {String} destination The destination for the source files
92 copyDir: function(dirName, destination) {
93 destination = this.basePath + '/' + (destination || dirName);
96 Filesystem.copy(dirName, destination);
101 * Inserts a script tag to load the given src file inside the given div id
102 * @param {String} path The path to the script to be included
103 * @param {String} id The id of the div to include after
104 * @param {String} htmlFile Optional html file to update (defaults to index.html)
106 insertInclude: function(path, id, htmlFile) {
107 htmlFile = htmlFile || 'index.html';
109 var stream = new Stream(htmlFile, 'rw'),
110 regex = new RegExp('<div id="' + id + '">'),
114 while (line = stream.readLine()) {
117 if (regex.test(line)) {
118 lines.push(' <script type="text/javascript" src="' + path + '"></script>');
122 var destination = htmlFile + "-modified",
123 newFile = new Stream(destination, "w");
125 newFile.writeLine(lines.join("\n"));
126 system.move(destination, htmlFile, true);
131 * Convenience function for displaying a clear message to the user
132 * @param {String} message The message to display
134 headline: function(message) {
136 Logger.log("*********************************************");
138 Logger.log("*********************************************");
142 generate: function() {
148 * @class GeneratorHelper
150 * Generates files and folders based on a template
152 Ext.generator.Factory = Ext.extend(Object, {
156 constructor: function(config) {
157 Ext.apply(this, config);
162 initArguments: function() {},
166 'Arguments in square brackets are optional',
168 'Generating an application:',
169 ' ./generate app AppName [../path/to/app]',
171 'Generating a model:',
172 ' ./generate model User id:int name:string active:boolean',
174 'Generating a controller:',
175 ' ./generate controller users create update destroy',
180 var args = this.args || system.arguments,
181 Gen = Ext.generator.Factory.types[args[0]];
184 new Gen({args: args.slice(1)}).generate();
191 Ext.generator.Factory.types = {};
192 Ext.regGenerator = function(name, constructor) {
193 Ext.generator.Factory.types[name] = constructor;
195 constructor.prototype.dirName = name;
196 constructor.templates = {};
199 Ext.regDispatchable('generate', Ext.generator.Factory);
201 // generate app FB examples/facebook
202 // generate model User id:int name:string email:string
203 // generate controller users index build show new