Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-16491] Fixed icons with Windows Hybrid apps. #5539

Merged
merged 2 commits into from
Mar 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 53 additions & 75 deletions mobileweb/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ MobileWebBuilder.prototype.validate = function validate(logger, config, cli) {
this.enableLogging = true;
}

if (!cli.tiapp.icon || !['Resources', 'Resources/android'].some(function (p) {
return fs.existsSync(cli.argv['project-dir'], p, cli.tiapp.icon);
})) {
cli.tiapp.icon = 'appicon.png';
}

// TODO: validate modules here
};

Expand Down Expand Up @@ -333,6 +339,9 @@ MobileWebBuilder.prototype.initialize = function initialize(next) {
this.moduleSearchPaths = this.moduleSearchPaths.concat(this.config.paths.modules);
}

this.ignoreDirs = new RegExp(this.config.get('cli.ignoreDirs'));
this.ignoreFiles = new RegExp(this.config.get('cli.ignoreFiles'));

this.projectDependencies = [];
this.modulesToLoad = [];
this.tiModulesToLoad = [];
Expand Down Expand Up @@ -477,26 +486,33 @@ MobileWebBuilder.prototype.createBuildDirs = function createBuildDirs(next) {
};

MobileWebBuilder.prototype.copyFiles = function copyFiles(next) {
this.logger.info(__('Copying project files'));
var logger = this.logger;

afs.copyDirSyncRecursive(this.mobilewebThemeDir, path.join(this.buildDir, 'themes'), { preserve: true, logger: this.logger.debug });
afs.copyDirSyncRecursive(this.mobilewebTitaniumDir, path.join(this.buildDir, 'titanium'), { preserve: true, logger: this.logger.debug });
afs.copyDirSyncRecursive(this.projectResDir, this.buildDir, { preserve: true, logger: this.logger.debug, rootIgnore: ti.filterPlatforms('mobileweb') });
logger.info(__('Copying project files'));

var mobilewebDir = path.join(this.projectResDir, 'mobileweb');
var copyOpts = {
preserve: true,
ignoreDirs: this.ignoreDirs,
ignoreFiles: this.ignoreFiles,
callback: function (src, dest, contents) {
logger.debug(__('Copying %s => %s', src.cyan, dest.cyan));
return contents;
}
};

afs.copyDirSyncRecursive(this.mobilewebTitaniumDir, path.join(this.buildDir, 'titanium'), copyOpts);
afs.copyDirSyncRecursive(this.mobilewebThemeDir, path.join(this.buildDir, 'themes'), copyOpts);

copyOpts.rootIgnore = ti.availablePlatformsNames;
afs.copyDirSyncRecursive(this.projectResDir, this.buildDir, copyOpts);

var mobilewebDir = path.join(this.projectResDir, 'mobileweb');
if (fs.existsSync(mobilewebDir)) {
afs.copyDirSyncRecursive(mobilewebDir, this.buildDir, { preserve: true, logger: this.logger.debug, rootIgnore: ['apple_startup_images', 'splash'] });
['Default.jpg', 'Default-Portrait.jpg', 'Default-Landscape.jpg'].forEach(function (file) {
file = path.join(mobilewebDir, 'apple_startup_images', file);
if (fs.existsSync(file)) {
afs.copyFileSync(file, this.buildDir, { logger: this.logger.debug });
afs.copyFileSync(file, path.join(this.buildDir, 'apple_startup_images'), { logger: this.logger.debug });
}
}, this);
copyOpts.rootIgnore = ['apple_startup_images', 'splash'];
afs.copyDirSyncRecursive(mobilewebDir, this.buildDir, copyOpts);
}

next();
this.cli.emit('build.mobileweb.copyFiles', this, next);
};

MobileWebBuilder.prototype.findProjectDependencies = function findProjectDependencies(next) {
Expand Down Expand Up @@ -531,42 +547,7 @@ MobileWebBuilder.prototype.findProjectDependencies = function findProjectDepende
};

MobileWebBuilder.prototype.createIcons = function createIcons(next) {
this.logger.info(__('Creating favicon and Apple touch icons'));

var file = path.join(this.projectResDir, this.tiapp.icon);
if (!/\.(png|jpg|gif)$/.test(file) || !fs.existsSync(file)) {
file = path.join(this.projectResDir, 'mobileweb', 'appicon.png');
}

if (!fs.existsSync(file)) {
return next();
}

afs.copyFileSync(file, this.buildDir, { logger: this.logger.debug });

var params = [
{ file: path.join(this.buildDir, 'favicon.ico'), width: 16, height: 16 },
{ file: path.join(this.buildDir, 'apple-touch-icon-precomposed.png'), width: 57, height: 57 },
{ file: path.join(this.buildDir, 'apple-touch-icon-57x57-precomposed.png'), width: 57, height: 57 },
{ file: path.join(this.buildDir, 'apple-touch-icon-72x72-precomposed.png'), width: 72, height: 72 },
{ file: path.join(this.buildDir, 'apple-touch-icon-114x114-precomposed.png'), width: 114, height: 114 },
{ file: path.join(this.buildDir, 'appicon144.png'), width: 144, height: 144 }
];

appc.image.resize(file, params, function (err, stdout, stderr) {
if (err) {
this.logger.error(__('Failed to create icons'));
stdout && stdout.toString().split('\n').forEach(function (line) {
line && this.logger.error(line.replace(/^\[ERROR\]/i, '').trim());
}, this);
stderr && stderr.toString().split('\n').forEach(function (line) {
line && this.logger.error(line.replace(/^\[ERROR\]/i, '').trim());
}, this);
this.logger.log('');
process.exit(1);
}
next();
}.bind(this), this.logger);
this.cli.emit('build.mobileweb.createIcons', this, next);
};

MobileWebBuilder.prototype.findModulesToCache = function findModulesToCache(next) {
Expand Down Expand Up @@ -751,7 +732,7 @@ MobileWebBuilder.prototype.assembleTitaniumJS = function assembleTitaniumJS(next

// 2) read in the config.js and fill in the template
function (tiJS, next) {
this.cli.createHook('build.mobileweb.processConfigTemplate', this, function (template, options, callback) {
this.cli.createHook('build.mobileweb.assembleConfigTemplate', this, function (template, options, callback) {
callback(null, tiJS + ejs.render(template, options) + '\n\n');
})(
fs.readFileSync(path.join(this.platformPath, 'src', 'config.js')).toString(),
Expand Down Expand Up @@ -789,13 +770,11 @@ MobileWebBuilder.prototype.assembleTitaniumJS = function assembleTitaniumJS(next
);
}.bind(this),

// 3) copy in the wp8 shim if building for windows phone 8
// 3) copy platform specific functionality
function (tiJS, next) {
if (this.target == 'wp8') {
next(null, tiJS + fs.readFileSync(path.join(this.platformPath, 'src', 'wp8.js')).toString() + '\n');
} else {
next(null, tiJS);
}
this.cli.createHook('build.mobileweb.assemblePlatformImplementation', this, function (contents, callback) {
callback(null, contents);
})(tiJS, next);
}.bind(this),

// 4) copy in instrumentation if it's enabled
Expand Down Expand Up @@ -1154,24 +1133,23 @@ MobileWebBuilder.prototype.createIndexHtml = function createIndexHtml(next) {
}

// write the index.html
fs.writeFile(
path.join(this.buildDir, 'index.html'),
ejs.render(
fs.readFileSync(path.join(this.platformPath, 'src', 'index.html')).toString(),
{
target: this.target,
tiHeader: ejs.render(fs.readFileSync(path.join(this.templatesDir, 'header.html.ejs')).toString()),
projectName: this.tiapp.name || '',
appDescription: this.tiapp.description || '',
appPublisher: this.tiapp.publisher || '',
tiGenerator: 'Appcelerator Titanium Mobile ' + ti.manifest.version,
tiStatusbarStyle: statusBarStyle,
tiCss: fs.readFileSync(path.join(this.buildDir, 'titanium.css')).toString(),
splashScreen: this.splashHtml,
tiJs: fs.readFileSync(path.join(this.buildDir, 'titanium.js')).toString(),
prefetch: this.prefetch
}
),
this.cli.createHook('build.mobileweb.createIndexHtml', this, function (template, options, callback) {
fs.writeFile(path.join(this.buildDir, 'index.html'), ejs.render(template, options), callback);
})(
fs.readFileSync(path.join(this.platformPath, 'src', 'index.html')).toString(),
{
target: this.target,
tiHeader: ejs.render(fs.readFileSync(path.join(this.templatesDir, 'header.html.ejs')).toString()),
projectName: this.tiapp.name || '',
appDescription: this.tiapp.description || '',
appPublisher: this.tiapp.publisher || '',
tiGenerator: 'Appcelerator Titanium Mobile ' + ti.manifest.version,
tiStatusbarStyle: statusBarStyle,
tiCss: fs.readFileSync(path.join(this.buildDir, 'titanium.css')).toString(),
splashScreen: this.splashHtml,
tiJs: fs.readFileSync(path.join(this.buildDir, 'titanium.js')).toString(),
prefetch: this.prefetch
},
next
);
};
Expand Down
113 changes: 107 additions & 6 deletions mobileweb/cli/hooks/package_web.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,118 @@
*/

var appc = require('node-appc'),
async = require('async'),
ejs = require('ejs'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
uuid = require('node-uuid'),
windows = require('titanium-sdk/lib/windows'),
wrench = require('wrench'),
__ = appc.i18n(__dirname).__;

exports.cliVersion = '>=3.X';

exports.init = function (logger, config, cli) {
var appleTouchImages = {
'appleTouchDefault': 'Default',
'appleTouchPortrait': 'Default-Portrait',
'appleTouchLandscape': 'Default-Landscape'
};

cli.on('build.mobileweb.copyFiles', function (builder, callback) {
if (builder.target != 'web') return callback();

// copy the splash images over
var mobilewebDir = path.join(builder.projectResDir, 'mobileweb');
if (fs.existsSync(mobilewebDir)) {
Object.keys(appleTouchImages).forEach(function (type) {
var filename = appleTouchImages[type] + '.png',
file = path.join(mobilewebDir, 'apple_startup_images', filename);
if (!fs.existsSync(file)) {
filename = appleTouchImages[type] + '.jpg';
file = path.join(mobilewebDir, 'apple_startup_images', filename);
}
if (fs.existsSync(file)) {
appleTouchImages[type] = filename;
appc.fs.copyFileSync(file, builder.buildDir, { logger: logger.debug });
} else {
appleTouchImages[type] = null;
}
}, this);
}
callback();
});

cli.on('build.mobileweb.createIcons', function (builder, callback) {
if (builder.target != 'web') return callback();

logger.info(__('Creating favicon and Apple touch icons'));

var buildDir = builder.buildDir,
iconFilename = /\.(png|jpg|gif)$/.test(builder.tiapp.icon) ? builder.tiapp.icon : 'appicon.png',
file = path.join(builder.projectResDir, 'mobileweb', iconFilename),
resizeImages = [];

if (!fs.existsSync(file)) {
// try in the root
file = path.join(builder.projectResDir, iconFilename);
}

// if they don't have a appicon, copy it from the sdk
if (!fs.existsSync(file)) {
file = path.join(builder.platformPath, 'templates', 'app', 'default', 'Resources', 'mobileweb', 'appicon.png');
}

// copy the appicon.png
appc.fs.copyFileSync(file, buildDir, { logger: logger.debug });

function copyIcon(filename, width, height) {
var file = path.join(builder.projectResDir, 'mobileweb', filename);
if (!fs.existsSync(file)) {
file = path.join(builder.projectResDir, filename);
}
if (fs.existsSync(file)) {
appc.fs.copyFileSync(file, buildDir, { logger: logger.debug });
} else {
resizeImages.push({
file: path.join(buildDir, filename).replace(/\.ico$/, '.png'),
width: width,
height: height
});
}
}

copyIcon('favicon.png', 16, 16);
copyIcon('apple-touch-icon-precomposed.png', 57, 57);
copyIcon('apple-touch-icon-57x57-precomposed.png', 57, 57);
copyIcon('apple-touch-icon-72x72-precomposed.png', 72, 72);
copyIcon('apple-touch-icon-114x114-precomposed.png', 114, 114);
copyIcon('appicon144.png', 144, 144);

// if there are no images to resize, just return
if (!resizeImages.length) return callback();

appc.image.resize(file, resizeImages, function (err, stdout, stderr) {
if (err) {
logger.error(__('Failed to create icons'));
stdout && stdout.toString().split('\n').forEach(function (line) {
line && logger.error(line.replace(/^\[ERROR\]/i, '').trim());
});
stderr && stderr.toString().split('\n').forEach(function (line) {
line && logger.error(line.replace(/^\[ERROR\]/i, '').trim());
});
logger.log('');
process.exit(1);
}

// rename the favicon
fs.renameSync(path.join(buildDir, 'favicon.png'), path.join(buildDir, 'favicon.ico'));

callback();
}, logger);
});

cli.on('build.mobileweb.createIndexHtml', {
pre: function (data, next) {
if (this.target == 'web') {
appc.util.mix(data.args[1], appleTouchImages);
}
next();
}
});
};