Blame view

js/lib/JSBuilder/src/Generator.js 6.06 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
load(JSBuilderPath + 'src/Template.js');
load(JSBuilderPath + 'src/XTemplate.js');

Ext.generator = {};

/**
 * @class Ext.generator.Base
 * @extends Object
 * Base class for all Generators
 */
Ext.generator.Base = Ext.extend(Object, {
    /**
     * @cfg {Boolean} pretend True to only output what the generator would do (e.g. which files would be created),
     * without actually modifying anything on the filesystem.
     */
    pretend: false,
    
    basePath: '.',
    
    constructor: function(config) {
        Ext.apply(this, config);
        
        if (this.args) {
            this.decodeArgs(this.args);
        }
    },
    
    /**
     * Creates an empty directory at the given location
     * @param {String} path The directory path
     */
    mkdir: function() {
        var length = arguments.length,
            dirName, i;
        
        for (i = 0; i < length; i++) {
            dirName = this.basePath + "/" + arguments[i];
            Logger.log("    Creating dir: " + dirName);
            
            if (!this.pretend) {
                Filesystem.mkdir(dirName);
            }
        }
    },
    
    /**
     * Applies data to an XTemplate, saving its output to the given file name
     * @param {String} name The name of the template
     */
    template: function(name, data, filename) {
        Logger.log("    Creating file: " + filename);
        
        // dirty hack to let <tpl> get through without being picked up
        Ext.apply(data, {
            tpl: 'tpl'
        });
        
        var name        = 'src/generators/' + this.dirName + '/templates/' + name + '.js',
            stream      = new Stream(name, 'rw'),
            template    = new Ext.XTemplate(stream.readText()),
            contents    = template.apply(data),
            destination = this.basePath + '/' + filename,
            newFile     = new Stream(destination, "w");
        
        newFile.writeLine(contents);
        system.move(destination, filename, true);
        newFile.close();
    },
    
    /**
     * Copies a file from the generator's files directory into the app
     * @param {String} fileName The name of the file to copy
     * @param {String} destination The destination path (defaults to the fileName)
     * @param {Boolean} silent True to not log any messages (defaults to false)
     */
    file: function(fileName, destination, silent) {
        Logger.log("    Copying " + fileName);
        
        destination = this.basePath + '/' + (destination || fileName);
        fileName = 'src/generators/' + this.dirName + '/files/' + fileName;
        
        if (!this.pretend && this.silent !== true) {
            Filesystem.copy(fileName, destination);
        }
    },
    
    /**
     * Copies all contents of the given source directory to a destination
     * @param {String} dirName The name of the directory to copy
     * @param {String} destination The destination for the source files
     */
    copyDir: function(dirName, destination) {
        destination = this.basePath + '/' + (destination || dirName);
        
        if (!this.pretend) {
            Filesystem.copy(dirName, destination);
        }
    },
    
    /**
     * Inserts a script tag to load the given src file inside the given div id
     * @param {String} path The path to the script to be included
     * @param {String} id The id of the div to include after
     * @param {String} htmlFile Optional html file to update (defaults to index.html)
     */
    insertInclude: function(path, id, htmlFile) {
        htmlFile = htmlFile || 'index.html';
        
        var stream = new Stream(htmlFile, 'rw'),
            regex  = new RegExp('<div id="' + id + '">'),
            lines  = [],
            line;
        
        while (line = stream.readLine()) {
            lines.push(line);
            
            if (regex.test(line)) {
                lines.push('            <script type="text/javascript" src="' + path + '"></script>');
            }
        }
        
        var destination = htmlFile + "-modified",
            newFile     = new Stream(destination, "w");
        
        newFile.writeLine(lines.join("\n"));
        system.move(destination, htmlFile, true);
        newFile.close();
    },
    
    /**
     * Convenience function for displaying a clear message to the user
     * @param {String} message The message to display
     */
    headline: function(message) {
        Logger.log("");
        Logger.log("*********************************************");
        Logger.log(message);
        Logger.log("*********************************************");
        Logger.log("");
    },
    
    generate: function() {
        
    }
});

/**
 * @class GeneratorHelper
 * @extends Cli
 * Generates files and folders based on a template
 */
Ext.generator.Factory = Ext.extend(Object, {
    name: "Generator",
    version: "0.0.1",
    
    constructor: function(config) {
        Ext.apply(this, config);
        
        Cli.call(this);
    },
    
    initArguments: function() {},
    
    usage: [
        'Example usage:',
        'Arguments in square brackets are optional',
        '',
        'Generating an application:',
        '    ./generate app AppName [../path/to/app]',
        '',
        'Generating a model:',
        '    ./generate model User id:int name:string active:boolean',
        '',
        'Generating a controller:',
        '    ./generate controller users create update destroy',
        ''
    ],
    
    run: function() {
        var args = this.args || system.arguments,
            Gen  = Ext.generator.Factory.types[args[0]];
        
        if (Gen) {
            new Gen({args: args.slice(1)}).generate();
        } else {
            this.printUsage();
        }
    }
});

Ext.generator.Factory.types = {};
Ext.regGenerator = function(name, constructor) {
    Ext.generator.Factory.types[name] = constructor;
    
    constructor.prototype.dirName = name;
    constructor.templates = {};
};

Ext.regDispatchable('generate', Ext.generator.Factory);

// generate app FB examples/facebook
// generate model User id:int name:string email:string
// generate controller users index build show new