Skip to content

Commit

Permalink
feat(android): log build warnings if res files have invalid names
Browse files Browse the repository at this point in the history
- Restored old behavior (before Titanium 9.0.0) where we used to rename invalid "res" files into valid names.
  * No longer post-fixing MD5 hash to res files anymore (which was the old behavior).
- TIMOB-27718: Now log a build warning for every "res" file name that is invalid.
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Jan 23, 2020
1 parent 9695253 commit e7df669
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
30 changes: 26 additions & 4 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2606,7 +2606,10 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {
let isDrawable = false;

if (m && m.length >= 4 && m[3]) {
const destFilename = m[3].toLowerCase();
const destFilename = m[3];
const destLowerCaseFilename = destFilename.toLowerCase();
const extMatch = destLowerCaseFilename.match(drawableExtRegExp);
const origExt = extMatch && extMatch[1] || '';

destDir = path.join(
_t.buildAppMainResDir,
Expand All @@ -2615,11 +2618,30 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {

if (splashScreenRegExp.test(filename)) {
// we have a splash screen image
const extMatch = destFilename.match(drawableExtRegExp);
const origExt = extMatch && extMatch[1] || '';
to = path.join(destDir, 'background' + origExt);
} else {
to = path.join(destDir, filename);
// We have a drawable image file. (Rename it if it contains invalid characters.)
let warningMessages = [];
if (destFilename.includes('/') || destFilename.includes('\\')) {
warningMessages.push(__('- Files cannot be put into subdirectories.'));
}
let destFilteredFilename = destLowerCaseFilename.replace(drawableExtRegExp, '');
destFilteredFilename = destFilteredFilename.replace(/[^a-z0-9_]/g, '_') + origExt;
if (destFilteredFilename !== destFilename) {
warningMessages.push(__('- Names must contain only lowercase a-z, 0-9, or underscore.'));
}
if (/^\d/.test(destFilteredFilename)) {
warningMessages.push(__('- Names cannot start with a number.'));
destFilteredFilename = '_' + destFilteredFilename;
}
if (warningMessages.length > 0) {
_t.logger.warn(__(`Invalid "res" file: ${path.relative(_t.projectDir, from)}`));
for (const nextMessage of warningMessages) {
_t.logger.warn(nextMessage);
}
_t.logger.warn(__(`- Titanium will rename to: ${destFilteredFilename}`));
}
to = path.join(destDir, destFilteredFilename);
}
isDrawable = true;
} else if (m = relPath.match(relSplashScreenRegExp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,17 +905,33 @@ private static String getResourceKeyForImage(String url)
}

// This is a "res" drawable references. Remove file extension since Android strips them off in built app.
// Note: Periods are not legal characters in resource names. So, it's okay to strip off the 1st one found.
int index = path.indexOf('.');
final String NINE_PATCH_EXTENSION = ".9.png";
String pathWithoutExtension = path;
int index = -1;
if (path.toLowerCase().endsWith(NINE_PATCH_EXTENSION)) {
index = path.length() - NINE_PATCH_EXTENSION.length();
} else {
index = path.lastIndexOf('.');
if ((index >= 0) && (path.indexOf('/', index) >= 0)) {
index = -1;
}
}
if (index > 0) {
pathWithoutExtension = path.substring(0, index);
} else if (index == 0) {
pathWithoutExtension = null;
}

// If we've extracted a valid "res" path, then store it for fast access later.
// Handle extracted "res" file path.
if ((pathWithoutExtension != null) && !pathWithoutExtension.isEmpty()) {
// If "res" file path is invalid, then make it valid.
// Must be all lower-case, can only contain letters/number, and cannot start with a number.
pathWithoutExtension = pathWithoutExtension.toLowerCase().replaceAll("[^a-z0-9_]", "_");
if (Character.isDigit(pathWithoutExtension.charAt(0))) {
pathWithoutExtension = "_" + pathWithoutExtension;
}

// Add "res" path to dictionary for fast access later.
TiUIHelper.resourceImageKeys.put(url, pathWithoutExtension);
}

Expand Down

0 comments on commit e7df669

Please sign in to comment.