]>
git.proxmox.com Git - sencha-touch.git/blob - src/examples/audio/.sencha/app/app-build.js
1 importPackage(com
.sencha
.tools
.compressors
.yui
);
2 importPackage(com
.sencha
.tools
.compressors
.closure
);
3 importPackage(com
.sencha
.tools
.external
);
4 importPackage(com
.sencha
.tools
.compiler
.jsb
.statements
);
6 var _logger
= SenchaLogManager
.getLogger("app-build");
8 function runAppBuild(proj
) {
9 var basedir
= proj
.getProperty("framework.config.dir"),
10 appPath
= proj
.getProperty("args.path"),
11 envArg
= proj
.getProperty("args.environment"),
12 ignores
= proj
.getProperty("framework.ignores"),
13 options
= proj
.getProperty("build.options"),
14 cssCompression
= proj
.getProperty("build.compress.css"),
15 config
= readConfig(resolvePath(appPath
, "app.json")),
16 environment
= (envArg
== "native") ? 'package' : envArg
,
18 (proj
.getProperty("args.destination") + '') ||
19 (proj
.getProperty("build.dir") + '') ||
20 (proj
.getProperty("app.build.dir") + ''),
21 operations
= toJS(proj
.getProperty("build.operations")),
22 v2deps
= !!(proj
.getProperty("v2deps") == "true"),
24 sdk
= proj
.getProperty("framework.dir"),
26 (proj
.getProperty("args.archive") + '') ||
27 (config
.archivePath
) ||
29 nativePackaging
= !!(envArg
== 'native'),
30 indexHtmlPath
= config
.indexHtmlPath
|| 'index.html',
31 appUrl
= config
.url
|| resolvePath(src
, indexHtmlPath
),
32 jsAssets
= config
.js
|| [],
33 cssAssets
= config
.css
|| [],
34 appCache
= config
.appCache
,
35 ignore
= config
.ignore
,
37 extras
= config
.extras
|| config
.resources
,
38 appJs
, sdkJs
, sdkIsAll
, assets
, processIndex
;
40 destination
= joinPath(destination
, environment
);
42 if(!PathUtil
.isAbsolute(archive
)) {
43 archive
= resolvePath(appPath
, archive
);
47 operations
= operations
.split('\n');
52 if (appUrl
.indexOf("file:") != 0 && appUrl
.indexOf("http:") != 0) {
53 appUrl
= 'file:///' + StringUtil
.replace(resolvePath(appUrl
), '\\', '/');
56 // check for build dir being immediate child of application dir
57 // native packager can get in to infinite looping when scanning files
58 // under this scenario
59 var canonicalAppPath
= new File(appPath
).getCanonicalPath(),
60 canonicalDestPath
= new File(destination
).getCanonicalPath(),
61 parent
= new File(canonicalDestPath
).getParentFile();
63 if(parent
&& parent
.getCanonicalPath() == canonicalAppPath
) {
64 _logger
.error("Application : {}", canonicalAppPath
);
65 _logger
.error("Destination : {}", canonicalDestPath
);
66 _logger
.error("Destination path cannot reside one level under the Application directory")
67 throw "Destination path cannot reside one level under the Application directory";
71 _logger
.info("Deploying your application to " + destination
);
73 PathUtil
.ensurePathExists(resolvePath(destination
));
76 map(jsAssets
, function (asset
) {
77 if (typeof asset
== 'string') {
78 asset
= { path
:asset
};
93 appJs
= resolvePath(destination
, appJs
);
95 cssAssets
= map(cssAssets
, function (asset
) {
96 if (typeof asset
== 'string') {
97 asset
= { path
:asset
};
103 assets
= filter(concat(jsAssets
, cssAssets
), function (asset
) {
104 return !asset
.shared
|| (environment
!= 'production');
107 _logger
.debug("copying all assets");
108 each(assets
, function (asset
) {
110 asset
.bundle
= false;
111 asset
.update
= false;
112 remoteAssets
.push(asset
);
116 // if not in testing mode, and using the new compiler, and this is
117 // one of the sencha-touch files, don't copy to output directory
118 if( asset
.type
=== 'js' &&
120 file
.indexOf("sencha-touch") != -1) {
121 asset
['x-bootstrap'] = true;
123 // only skip the sdk code in the bundle if the bundle flag
124 // on the sdk asset is explicitly set to false
125 if(('bundle' in asset
) && asset
.bundle
=== false) {
127 sdkIsAll
= sdkJs
.indexOf("-all.js") >= 0;
132 if (asset
['x-bootstrap'] && !asset
.isSdk
) {
136 _logger
.debug("copying file {}", resolvePath(src
, file
));
138 var srcPath
= resolvePath(src
, file
),
139 dstPath
= resolvePath(destination
, stripSpecialDirNames(file
));
141 if(srcPath
!= dstPath
) {
142 PathUtil
.ensurePathExists(dstPath
);
143 copy(srcPath
, dstPath
);
144 _logger
.info("Copied {} to {}", srcPath
, dstPath
);
149 var ignoreFilter
= Filter
.getFileNameFilter(
150 new RegexFilter(ignore
).setInclusive(false));
152 _logger
.debug("copying all extras");
153 each(extras
, function (extra
) {
154 var from = resolvePath(src
, extra
),
155 to
= resolvePath(destination
, extra
);
156 _logger
.debug("Copying from {} to {}", from, to
);
157 if (new File(from).exists()) {
158 PathUtil
.ensurePathExists(to
);
159 copy(from, to
, ignoreFilter
);
160 _logger
.info("Copied {}", from);
162 _logger
.warn("File or folder {} not found", from);
168 processIndex = function () {
169 _logger
.debug("processing page : index.html");
170 jsAssets
= filter(jsAssets
, function(asset
){
171 return !(asset
['x-bootstrap'] && !asset
.isSdk
);
174 var appJson
= jsonEncode({
179 indexHtml
, content
, compressor
, remotes
, microloader
;
181 writeFileContent(new File(destination
, 'app.json'), appJson
);
182 _logger
.info("Generated app.json");
184 indexHtml
= readFileContent(new File(src
, indexHtmlPath
));
186 if (environment
== 'production' && appCache
) {
187 indexHtml
= StringUtil
.replace(
190 '<html manifest="cache.appcache"');
193 compressor
= new ClosureCompressor();
194 microloader
= (environment
== 'production'
198 _logger
.debug("using microloader : {}", microloader
);
199 content
= readFileContent(joinPath(sdk
, "microloader", microloader
));
200 content
= compressor
.compress(content
);
202 '<script type="text/javascript">' +
203 content
+ ';Ext.blink(' +
204 (environment
== 'production' ? jsonEncode({
206 }) : appJson
) + ')' +
210 each(remoteAssets
, function (asset
) {
211 var uri
= asset
.path
;
213 if (asset
.type
=== 'js') {
215 '<script type="text/javascript" src="' +
218 } else if (asset
.type
=== 'css') {
220 '<link rel="stylesheet" type="text/css" href="' +
226 indexHtml
= ('' + indexHtml
).replace(
227 /<script id="microloader"([^<]+)<\/script>/,
230 _logger
.debug("generating new built index.html");
231 writeFileContent(resolvePath(destination
, indexHtmlPath
), indexHtml
);
232 _logger
.info("Embedded microloader into " + indexHtmlPath
);
235 _logger
.info("Resolving your application dependencies (" + appUrl
+ ")");
237 var preprocessor
= new Parser(),
238 jsCompressor
= new YuiJavascriptCompressor(),
239 cssCompressor
= new YuiCssCompressor(),
240 phantomRunner
= new PhantomJsRunner(),
241 processedAssetCount
= 0,
242 assetsCount
, dependencies
, files
, file
,
243 destinationFile
, compressor
,
244 cleanFile
, cleanDestinationFile
;
247 // if v2deps, use the sencha command 2 sytle dependency resolution mechanism
248 // by running the phantomjs dependencies.js script
249 var phantomOut
= phantomRunner
.run([
250 resolvePath(basedir
, "dependencies.js"),
253 exitCode
= phantomOut
.getExitCode(),
254 stdout
= phantomOut
.getOutput(),
255 buffer
= new StringBuilder();
259 _logger
.error("dependencies.js exited with non-zero code : " + exitCode
);
260 _logger
.error(stdout
);
261 throw new ExBuild("failed gathering dependencies").raise();
263 dependencies
= jsonDecode(stdout
);
265 _logger
.info("Found " + dependencies
.length
+ " dependencies. Concatenating all into '" + appJs
+ "'");
267 files
= map(dependencies
, function (dependency
) {
268 return resolvePath(src
, dependency
.path
);
273 each(files
, function (file
) {
274 buffer
.append(FileUtil
.readUnicodeFile(resolvePath(file
))).append('\n');
277 writeFileContent(appJs
, buffer
.toString());
279 // clear the buffer to free memory
282 var sdkTag
= sdkIsAll
? 'framework' : 'core',
285 '--options=' + options
,
291 '-out=' + resolvePath(destination
, sdkFile
)
294 '-options=' + options
,
305 compilerId
= proj
.getProperty("compiler.ref.id"),
306 compiler
= proj
.getReference(compilerId
);
309 _logger
.info("Compiling " + sdkFile
+ " and dependencies");
310 _logger
.debug("running compiler with options : '{}'", sdkJsArgs
.join(' '));
311 compiler
.dispatchArgs(sdkJsArgs
);
312 _logger
.info("Compiling app.js and dependencies");
313 _logger
.debug("running compiler with options : '{}'", appJsArgs
.join(' '));
314 compiler
.dispatchArgs(appJsArgs
);
315 _logger
.info("Completed compilation.");
318 '-options=' + options
,
326 _logger
.debug("running compiler with options : '{}'", appJsArgs
.join(' '));
327 compiler
.dispatchArgs(appJsArgs
);
328 _logger
.info("Completed compilation.");
333 for (var name
in config
.buildOptions
) {
334 if (config
.buildOptions
.hasOwnProperty(name
)) {
335 preprocessor
.setParam(name
, config
.buildOptions
[name
]);
339 assetsCount
= assets
.length
;
341 each(assets
, function (asset
) {
344 destinationFile
= resolvePath(destination
, file
),
345 cleanFile
= stripSpecialDirNames(file
),
346 cleanDestinationFile
= resolvePath(destination
, cleanFile
);
348 // adjust the asset path to use the cleaned filename
349 asset
.path
= cleanFile
;
353 _logger
.debug("Assets => Processed : {} Total : {}",
354 processedAssetCount
, assetsCount
);
356 if (asset
.type
== 'js') {
357 if (!asset
.remote
&& !(asset
['x-bootstrap'] && !asset
.isSdk
)) {
358 _logger
.debug("running preprocessor for file {}", cleanDestinationFile
);
360 cleanDestinationFile
,
361 preprocessor
.parse(readFileContent(cleanDestinationFile
)));
362 _logger
.info('Processed local file ' + asset
.path
);
364 _logger
.info('Processed remote file ' + asset
.path
);
368 if (environment
== 'testing') {
372 if (asset
.remote
|| (asset
['x-bootstrap'] && !asset
.isSdk
)) {
373 ++processedAssetCount
;
375 _logger
.debug("Minifying " + file
);
377 if(asset
.type
== 'js') {
379 cleanDestinationFile
,
380 jsCompressor
.compress(readFileContent(cleanDestinationFile
)));
382 _logger
.info("Minified " + file
);
383 } else if (cssCompression
== "true") {
385 cleanDestinationFile
,
386 cssCompressor
.compress(readFileContent(cleanDestinationFile
)));
388 _logger
.info("Minified " + file
);
391 if (environment
== 'production') {
392 var content
= readFileContent(cleanDestinationFile
),
393 version
= '' + FileUtil
.createChecksum(content
);
394 asset
.version
= version
;
396 _logger
.debug("prepending checksum on {}", cleanDestinationFile
);
398 cleanDestinationFile
,
399 "/*" + version
+ "*/" + content
);
402 _logger
.debug("copying destination to archive");
404 PathUtil
.ensurePathExists(resolvePath(archive
, cleanFile
, version
));
405 copy(cleanDestinationFile
, resolvePath(archive
, cleanFile
, version
));
407 if (asset
.update
== 'delta') {
408 // generate all the deltas to the other archived versions
409 _logger
.debug("generating file deltas");
410 var archivedVersions
= new File(joinPath(archive
, cleanFile
))
413 each(archivedVersions
, function (archivedVersion
) {
414 if(archivedVersion
.isDirectory()) {
418 archivedVersion
= archivedVersion
.name
;
420 if (archivedVersion
== version
) {
424 var deltaFile
= joinPath(
428 archivedVersion
+ '.json');
430 writeFileContent(deltaFile
, '');
432 _logger
.debug("Generating delta from {} to {}",
436 var runner
= new VcDiffRunner(),
441 joinPath(archive
, cleanFile
, archivedVersion
),
443 cleanDestinationFile
,
445 resolvePath(deltaFile
),
448 runnerOut
= runner
.run(args
),
449 exitCode
= runnerOut
.getExitCode(),
450 stdout
= runnerOut
.getOutput();
453 _logger
.error("failed generating diff from {} to {}",
456 _logger
.error(stdout
);
457 throw new ExBuild("failed generating diff from {0} to {1}",
462 // fixup malformed vcdiff content
463 var deltaFilePath
= resolvePath(deltaFile
),
464 content
= FileUtil
.readFile(deltaFilePath
);
465 if(content
.endsWith(",]")) {
466 _logger
.debug("Correcting trailing comma issue in vcdiff output");
467 FileUtil
.writeFile(deltaFilePath
, content
.substring(0, content
.length() - 2) + "]");
473 "Generated delta for: {} from hash: '{}' to hash: '{}'",
474 [cleanFile
, archivedVersion
, version
]);
480 if (++processedAssetCount
== assetsCount
) {
481 _logger
.debug("processed all assets, finalizing build...");
484 if (environment
== 'production' && appCache
) {
485 _logger
.info("Generating appcache");
486 appCache
.cache
= map(appCache
.cache
, function (cache
) {
489 if (!/^(\/|(.*):\/\/)/.test(cache
)) {
491 "Generating checksum for appCache item: {}",
494 checksum
= FileUtil
.createChecksum(
495 readFileData(joinPath(destination
, cache
)));
504 writeAppCache(appCache
, joinPath(destination
, 'cache.appcache'));
507 if (nativePackaging
) {
508 _logger
.info("Generating native package");
509 var packagerConfig
= readConfig(
510 joinPath(src
, 'packager.json'));
512 if (packagerConfig
.platform
.match(/iOS/)) {
514 resolvePath(joinPath(src
, 'resources', 'icons')),
515 resolvePath(destination
),
518 resolvePath(joinPath(src
, 'resources', 'loading')),
519 resolvePath(destination
),
523 // add '' here to coerce to javascript string instead of java string
524 // for json encoding later...
525 packagerConfig
.inputPath
= destination
+ '';
527 var origDestination
= proj
.getProperty("args.destination"),
528 nativePackagePath
= proj
.getProperty("native.build.dir") ||
529 joinPath(origDestination
, "native");
531 packagerConfig
.outputPath
= resolvePath(nativePackagePath
) + '';
533 PathUtil
.ensurePathExists(packagerConfig
.outputPath
);
536 joinPath(src
, 'packager.temp.json'),
537 jsonEncode(packagerConfig
, true));
540 "Packaging your application as a native app to {} ...",
541 packagerConfig
.outputPath
);
543 var stbuildRunner
= new StBuildRunner(),
544 args
= ['package', resolvePath(src
, 'packager.temp.json')],
545 stbuildOut
= stbuildRunner
.run(args
),
546 exitCode
= stbuildOut
.getExitCode(),
547 stdout
= stbuildOut
.getOutput();
550 _logger
.error("failed running native packager");
551 _logger
.error(stdout
);
552 throw new ExBuild("failed running native packager")
555 _logger
.info("Successfully packaged native application");
557 "Package may be run with 'sencha package run -p {}",
558 joinPath(src
, 'packager.temp.json'))
561 _logger
.debug("skipping native packaging");
567 if (environment
== 'testing') {
571 _logger
.info("Successfully deployed your application to " + destination
);
575 function writeAppCache(appCache
, outfile
) {
576 _logger
.debug("generating appcache manifest...");
577 // build the appCache file
578 var builder
= new StringBuilder();
580 builder
.append("CACHE MANIFEST\n");
581 each(appCache
.cache
, function (cache
) {
582 builder
.append("# " + cache
.checksum
+ "\n");
583 builder
.append(cache
.uri
+ "\n");
586 builder
.append("\n\nFALLBACK:\n");
588 each(appCache
.fallback
, function (fallback
) {
589 builder
.append(fallback
+ '\n');
592 builder
.append("\n\nNETWORK:\n");
594 each(appCache
.network
, function (network
) {
595 builder
.append(network
+ '\n');
602 builder
.setLength(0);
607 _logger
.info("building application");