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) + ";"
94 parseTarget: function() {
99 // Backwards compatibility with JSB2
100 var target = this.get('target') || this.get('file') || this.getDefaultTarget(),
101 basePath = this.project.get('deployDir') + Fs.sep,
104 target = target.replace(/\//g, Fs.sep);
106 if (target.indexOf('.js') !== -1) {
107 target = target.replace('.js', '');
108 // if (this.get('debug')) {
109 // target += this.project.builder.get('debugSuffix');
114 this.set('target', target);
116 // If the target is a path, then create the needed folders
117 if (target.indexOf(Fs.sep) !== -1) {
118 dir = target.substr(0, target.lastIndexOf(Fs.sep));
119 target = target.replace(dir, '').substr(1);
120 target = Fs.mkdir(basePath + dir) + Fs.sep + target;
123 target = basePath + target;
126 this.set('targetPath', target);
130 writeIncludes: function(file) {
131 var project = this.project,
132 verbose = project.builder.get('verbose'),
133 includes = this.get('files') || this.get('fileIncludes') || [],
134 jsbDir = project.get('jsbDir') + Fs.sep;
136 if (verbose && includes.length) {
137 Logger.log(' - ' + includes.length + ' file(s) included in this target.');
140 // Loop over all file includes, read the contents, and write
141 // it to our target file
142 includes.forEach(function(include) {
143 var path = this.getIncludePath(include),
149 Logger.log(' + ' + path);
153 if (!Fs.exists(jsbDir + path)) {
154 if (Platform.isUnix) {
155 filesStream = new Stream('exec://ls -a ' + jsbDir + path);
156 files = filesStream.readFile().split('\n');
159 files.forEach(function(filePath) {
160 if (!Ext.isEmpty(filePath)) {
161 include = new Stream(filePath);
162 content += include.readFile() + '\n';
169 Logger.log("[ERROR] File '" + jsbDir + path + "' is either not existent or unreadble");
174 content = this.getContent(jsbDir + path);
179 file.writeln(content);
184 getContent: function(file, callNum) {
186 * This function should pretty much never fail since we already know the file exists.
187 * However in Windows it seems to randomly omit files when building because it can't
188 * open the stream, which causes the build to break. Since we know the file is there,
189 * we'll just re-request it until we get it. While stupid, this makes it reliable.
194 callNum = callNum || 0;
196 content = Fs.readFile(file);
198 if (Platform.isWindows && callNum < 5) {
199 return this.getContent(file, callNum + 1);
205 getIncludePath : function(include) {
206 return include.path.replace(/\//g, Fs.sep) + (include.name || include.text || '');
211 return this.config[key] || false;
214 set: function(key, value, ifNotExists) {
215 if (ifNotExists && this.get(key) !== false) {
218 this.config[key] = value;