Blame view

js/lib/JSBuilder/src/Target.js 6.31 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
Target = Ext.extend(Object, {
    constructor: function(config, project) {
        this.config = config || {};
        this.project = project;
    },

    create: function() {
        this.parseTarget();

        var project = this.project,
            builder = project.builder,
            verbose = builder.get('verbose'),
            file;

        if (verbose) {
            Logger.log('\nCreating the "' + this.get('name') + '" target as "' + this.get('target') + '"');
        }

        // Open the file stream
        file = new Stream(this.get('targetPath'), 'w');

        this.onCreate(file);
        this.writeIncludes(file);
        this.onAfterWriteIncludes(file);

        // Close the target file
        file.close();

        this.afterCreate();
    },

    afterCreate: function() {
        this.project.compressTarget(this);

        var filePath = this.get('targetPath');
        var license = (this.project.get('license')) ? "/*\n" + this.project.get('license') + "\n*/\n" : '';

        if (license) {
            var content = Fs.readFile(filePath);

            if (content.substring(0, license.length) !== license) {
                Fs.writeFile(filePath, license + content);
            }
        }
    },

    onAfterWriteIncludes: function(file) {
        var namespaceRewrites = this.get('namespaceRewrites'),
            settings = this.get('settings'),
            suffix = '})(',
            names = [];

        if (namespaceRewrites) {
            namespaceRewrites.forEach(function(rewrite) {
                names.push('this.' + rewrite.to + ' || (this.' + rewrite.to + ' = {})');
            });

            suffix += names.join(', ');
            suffix += ');';

            file.writeln(suffix);
        }
    },

    onCreate: function(file) {
        var namespaceRewrites = this.get('namespaceRewrites'),
            prefix = '(function(',
            settings = this.get('settings'),
            names = [];

        if (namespaceRewrites) {
            namespaceRewrites.forEach(function(rewrite) {
                names.push(rewrite.from);
            });

            prefix += names.join(', ');
            prefix += '){';

            if (settings) {
                prefix += "\n";
                prefix +=  ["if (typeof Ext === 'undefined') {",
                                "this.Ext = {};",
                            "}",
                            "",
                            "Ext.buildSettings = " + JSON.stringify(settings) + ";"
                           ].join("\n");
            }


            file.writeln(prefix);
        }
    },

    parseTarget: function() {
        if (this.parsed) {
            return;
        }

        // Backwards compatibility with JSB2
        var target = this.get('target') || this.get('file') || this.getDefaultTarget(),
            basePath = this.project.get('deployDir') + Fs.sep,
            dir;

        target = target.replace(/\//g, Fs.sep);

        if (target.indexOf('.js') !== -1) {
            target = target.replace('.js', '');
//            if (this.get('debug')) {
//                target += this.project.builder.get('debugSuffix');
//            }
            target += '.js';
        }

        this.set('target', target);

        // If the target is a path, then create the needed folders
        if (target.indexOf(Fs.sep) !== -1) {
            dir = target.substr(0, target.lastIndexOf(Fs.sep));
            target = target.replace(dir, '').substr(1);
            target = Fs.mkdir(basePath + dir) + Fs.sep + target;
        }
        else {
            target = basePath + target;
        }

        this.set('targetPath', target);
        this.parsed = true;
    },

    writeIncludes: function(file) {
        var project = this.project,
            verbose = project.builder.get('verbose'),
            includes = this.get('files') || this.get('fileIncludes') || [],
            jsbDir = project.get('jsbDir') + Fs.sep;

        if (verbose && includes.length) {
            Logger.log('  - ' + includes.length + ' file(s) included in this target.');
        }

        // Loop over all file includes, read the contents, and write
        // it to our target file
        includes.forEach(function(include) {
            var path = this.getIncludePath(include),
                content = '',
                filesStream, files;

            if (verbose) {
                Logger.log('    + ' + path);
            }


            if (!Fs.exists(jsbDir + path)) {
                if (Platform.isUnix) {
                    filesStream = new Stream('exec://ls -a ' + jsbDir + path);
                    files = filesStream.readFile().split('\n');
                    filesStream.close();
                    files.forEach(function(filePath) {
                        if (!Ext.isEmpty(filePath)) {
                            include = new Stream(filePath);
                            content += include.readFile() + '\n';
                            include.close();
                        }
                    });
                }
            }
            else {
                content = this.getContent(jsbDir + path);
            }



            file.writeln(content);
        }, this);
    },


    getContent: function(file, callNum) {
        /**
         * This function should pretty much never fail since we already know the file exists.
         * However in Windows it seems to randomly omit files when building because it can't
         * open the stream, which causes the build to break. Since we know the file is there,
         * we'll just re-request it until we get it. While stupid, this makes it reliable.
         */

        var content = '',
            stream;

        callNum = callNum || 0;
        try {
            stream = new Stream(file);
            content = stream.readFile();
            stream.close();
        } catch (e) {
            if (Platform.isWindows && callNum < 5) {
                return this.getContent(file, callNum + 1);
            }
        }
        return content;
    },

    getIncludePath : function(include) {
        return include.path.replace(/\//g, Fs.sep) + (include.name || include.text || '');
    },


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

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