Blame view

js/lib/JSBuilder/src/Project.js 6.38 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
Project = Ext.extend(Object, {
    constructor : function(projectFile, builder) {
        var fileName = projectFile.split(/\\|\//i).pop(),
            dir = projectFile.replace(fileName, ''),
            contents, config;

        if (!Fs.exists(projectFile)) {
            throw 'Project file doesn\'t exist';
        }

        contents = Fs.readFile(projectFile);
        try {
            config = JSON.parse(contents);
        }
        catch (e) {
            throw 'The JSB file is not valid JSON.\n' + e.toString();
        }

        Ext.apply(config, {
            targets: config.targets || config.pkgs || [],
            name: config.projectName,
            license: config.licenseText,
            jsbDir: Fs.getFullPath(dir),
            jsbFile: fileName,
            packages: config.packages || [],
            builds: config.builds || [],
            // If the JSB file contains a deployDir property, append that to the -d deployDir
            deployDir: builder.get('deployDir') + Fs.sep + (config.deployDir || ''),
            resources: config.resources || []
        });

        this.builder = builder;
        this.config = config;

        this.parseTargets();
    },

    parseTargets : function() {
        // Split targets up into packages and builds for backwards compatibility
        this.get('targets').forEach(function(item) {
            if (item.packages) {
                this.get('builds').push(item);
            }
            else {
                this.get('packages').push(item);
            }
        }, this);

        // Convert each package config object into an actual Package instance
        this.set('packages', this.get('packages').map(function(item) {
            return new Package(item, this);
        }, this));

        // Convert each build config object into an actual Build instance
        this.set('builds', this.get('builds').map(function(item) {
            return new Build(item, this);
        }, this));
    },

    getPackageById : function(id) {
        var ret = false;
        this.get('packages').forEach(function(pkg) {
            if (pkg.get('id') == id) {
                ret = pkg;
            }
        });
        return ret;
    },

    /**
     * Creates the directory we are going to deploy to.
     */
    makeDeployDir: function() {
        var project = this.project,
            deployDir = Fs.mkdir(this.get('deployDir'));

        this.set('deployDir', deployDir);

        if (this.get('verbose')) {
            this.log('Created the deploy directory ' + deployDir);
        }
    },

    build : function() {
        this.makeDeployDir();
        this.createPackages();
        this.createBuilds();
        this.copyResources();

        if (this.builder.get('verbose')) {
            Logger.log('');
        }
        Logger.log('Done building!\n');
    },

    createPackages : function() {
        this.get('packages').forEach(function(pkg) {
            pkg.create();
        });
    },

    createBuilds : function() {
        this.get('builds').forEach(function(build) {
            build.create();
        });
    },

    copyResources : function() {
        if (this.builder.get('verbose')) {
            Logger.log('');
        }

        Logger.log('Copy resources...');

        this.get('resources').forEach(function(resource) {
            var filters = resource.filters,
                srcDir = this.get('jsbDir') + Fs.sep + resource.src,
                dest = resource.dest || resource.src;

            dest = dest || '';
            dest = this.get('deployDir') + Fs.sep + dest;

            // TODO: Implement filters
            if (this.builder.get('verbose')) {
                Logger.log('  + ' + resource.src + ' -> ' + dest);
            }

            Fs.copy(srcDir, dest);
        }, this);
    },

    get : function(key) {
        return this.config[key] || false;
    },

    set : function(key, value, ifNotExists) {
        if (ifNotExists && this.get(key) !== false) {
            return;
        }
        this.config[key] = value;
    },

    compressTarget : function(target) {
        if (this.builder.get('nocompress') || !target.get('compress')) {
            return;
        }

        Logger.log('  * Compress and obfuscate ' + target.get('target') + '...');

        var destination = target.get('targetPath'),
            source = destination + '-temp-' + Date.now() * Math.random();
            system.move(destination, source),
            command = this.getCompressor() + '-o ' + destination + ' ' + source;

        Cmd.execute(command);

        Fs.remove(source);
    },

    getCompressor : function() {
        return 'java -jar ' + system.script.replace(Fs.getPath('bin/JSBuilder.js'), '') + Fs.getPath('ycompressor/ycompressor.jar') + ' --type js ';
    },

    getSourceFiles: function() {
        Logger.log('<!--');
        Logger.log('    Source files');
        Logger.log('-->');
        Logger.log('');

        this.get('builds').forEach(function(build) {
            if (build.get('packages')) {
                Logger.log('<!-- build: ' + build.get('name') + ' -->');

                build.get('packages').forEach(function(pkg) {
                    pkg = build.project.getPackageById(pkg);

                    Logger.log('<!-- package: ' + pkg.get('name') + ' -->');

                    pkg.get('files').forEach(function(file) {
                        Logger.log('<script type="text/javascript" src="../../' + file.path + file.name + '"></script>');
                    });

                    Logger.log('');
                });

                Logger.log('');
            };
        });
    },

    getSpecFiles: function() {
        Logger.log('<!--');
        Logger.log('    Spec files');
        Logger.log('-->');
        Logger.log('');

        this.get('builds').forEach(function(build) {
            if (build.get('packages')) {
                Logger.log('<!-- build: ' + build.get('name') + ' -->');

                build.get('packages').forEach(function(pkg) {
                    pkg = build.project.getPackageById(pkg);

                    Logger.log('<!-- package: ' + pkg.get('name') + ' -->');

                    pkg.get('files').forEach(function(file) {
                        Logger.log('<script type="text/javascript" src="spec/' + file.path.replace('src/', '') + file.name + '"></script>');
                    });

                    Logger.log('');
                });

                Logger.log('');
            };
        });
    }
});