1 Target = Ext.extend(Object, {
2 constructor: function(config, project) {
3 this.config = config || {};
4 this.project = project;
10 var project = this.project,
11 builder = project.builder,
12 verbose = builder.get('verbose'),
16 Logger.log('\nCreating the "' + this.get('name') + '" target as "' + this.get('target') + '"');
19 // Open the file stream
20 file = new Stream(this.get('targetPath'), 'w');
23 this.writeIncludes(file);
24 this.onAfterWriteIncludes(file);
26 // Close the target file
32 afterCreate: function() {
33 this.project.compressTarget(this);
35 var filePath = this.get('targetPath');
36 var license = (this.project.get('license')) ? "/*\n" + this.project.get('license') + "\n*/\n" : '';
39 var content = Fs.readFile(filePath);
41 if (content.substring(0, license.length) !== license) {
42 Fs.writeFile(filePath, license + content);
47 onAfterWriteIncludes: function(file) {
48 var namespaceRewrites = this.get('namespaceRewrites'),
49 settings = this.get('settings'),
53 if (namespaceRewrites) {
54 namespaceRewrites.forEach(function(rewrite) {
55 names.push('this.' + rewrite.to + ' || (this.' + rewrite.to + ' = {})');
58 suffix += names.join(', ');
65 onCreate: function(file) {
66 var namespaceRewrites = this.get('namespaceRewrites'),
67 prefix = '(function(',
68 settings = this.get('settings'),
71 if (namespaceRewrites) {
72 namespaceRewrites.forEach(function(rewrite) {
73 names.push(rewrite.from);
76 prefix += names.join(', ');
81 prefix += ["if (typeof Ext === 'undefined') {",
85 "Ext.buildSettings = " + JSON.stringify(settings) + ";",
86 "Ext.isSandboxed = true;"
95 parseTarget: function() {
100 // Backwards compatibility with JSB2
101 var target = this.get('target') || this.get('file') || this.getDefaultTarget(),
102 basePath = this.project.get('deployDir') + Fs.sep,
105 target = target.replace(/\//g, Fs.sep);
107 if (target.indexOf('.js') !== -1) {
108 target = target.replace('.js', '');
109 // if (this.get('debug')) {
110 // target += this.project.builder.get('debugSuffix');
115 this.set('target', target);
117 // If the target is a path, then create the needed folders
118 if (target.indexOf(Fs.sep) !== -1) {
119 dir = target.substr(0, target.lastIndexOf(Fs.sep));
120 target = target.replace(dir, '').substr(1);
121 target = Fs.mkdir(basePath + dir) + Fs.sep + target;
124 target = basePath + target;
127 this.set('targetPath', target);
131 writeIncludes: function(file) {
132 var project = this.project,
133 verbose = project.builder.get('verbose'),
134 includes = this.get('files') || this.get('fileIncludes') || [],
135 jsbDir = project.get('jsbDir') + Fs.sep;
137 if (verbose && includes.length) {
138 Logger.log(' - ' + includes.length + ' file(s) included in this target.');
141 // Loop over all file includes, read the contents, and write
142 // it to our target file
143 includes.forEach(function(include) {
144 var path = this.getIncludePath(include),
150 Logger.log(' + ' + path);
154 if (!Fs.exists(jsbDir + path)) {
155 if (Platform.isUnix) {
156 filesStream = new Stream('exec://ls -a ' + jsbDir + path);
157 files = filesStream.readFile().split('\n');
160 files.forEach(function(filePath) {
161 if (!Ext.isEmpty(filePath)) {
162 include = new Stream(filePath);
163 content += include.readFile() + '\n';
170 Logger.log("[ERROR] File '" + jsbDir + path + "' is either not existent or unreadble");
175 content = this.getContent(jsbDir + path);
180 file.writeln(content);
185 getContent: function(file, callNum) {
187 * This function should pretty much never fail since we already know the file exists.
188 * However in Windows it seems to randomly omit files when building because it can't
189 * open the stream, which causes the build to break. Since we know the file is there,
190 * we'll just re-request it until we get it. While stupid, this makes it reliable.
195 callNum = callNum || 0;
197 content = Fs.readFile(file);
199 if (Platform.isWindows && callNum < 5) {
200 return this.getContent(file, callNum + 1);
206 getIncludePath : function(include) {
207 return include.path.replace(/\//g, Fs.sep) + (include.name || include.text || '');
212 return this.config[key] || false;
215 set: function(key, value, ifNotExists) {
216 if (ifNotExists && this.get(key) !== false) {
219 this.config[key] = value;