Skip to content

Commit 9c06ac0

Browse files
ralf-at-androidAndroid (Google) Code Review
authored andcommitted
Merge "Merge "AAPT: support a new --ignore-assets flag."" into jb-dev
2 parents ee3bb64 + 6c255a3 commit 9c06ac0

File tree

3 files changed

+99
-45
lines changed

3 files changed

+99
-45
lines changed

tools/aapt/AaptAssets.cpp

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,55 +56,92 @@ static bool validateFileName(const char* fileName)
5656
return true;
5757
}
5858

59+
// The default to use if no other ignore pattern is defined.
60+
const char * const gDefaultIgnoreAssets =
61+
"!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~";
62+
// The ignore pattern that can be passed via --ignore-assets in Main.cpp
63+
const char * gUserIgnoreAssets = NULL;
64+
5965
static bool isHidden(const char *root, const char *path)
6066
{
61-
const char *ext = NULL;
62-
const char *type = NULL;
63-
64-
// Skip all hidden files.
65-
if (path[0] == '.') {
66-
// Skip ., .. and .svn but don't chatter about it.
67-
if (strcmp(path, ".") == 0
68-
|| strcmp(path, "..") == 0
69-
|| strcmp(path, ".svn") == 0) {
70-
return true;
71-
}
72-
type = "hidden";
73-
} else if (path[0] == '_') {
74-
// skip directories starting with _ (don't chatter about it)
75-
String8 subdirName(root);
76-
subdirName.appendPath(path);
77-
if (getFileType(subdirName.string()) == kFileTypeDirectory) {
78-
return true;
79-
}
80-
} else if (strcmp(path, "CVS") == 0) {
81-
// Skip CVS but don't chatter about it.
82-
return true;
83-
} else if (strcasecmp(path, "thumbs.db") == 0
84-
|| strcasecmp(path, "picasa.ini") == 0) {
85-
// Skip suspected image indexes files.
86-
type = "index";
87-
} else if (path[strlen(path)-1] == '~') {
88-
// Skip suspected emacs backup files.
89-
type = "backup";
90-
} else if ((ext = strrchr(path, '.')) != NULL && strcmp(ext, ".scc") == 0) {
91-
// Skip VisualSourceSafe files and don't chatter about it
67+
// Patterns syntax:
68+
// - Delimiter is :
69+
// - Entry can start with the flag ! to avoid printing a warning
70+
// about the file being ignored.
71+
// - Entry can have the flag "<dir>" to match only directories
72+
// or <file> to match only files. Default is to match both.
73+
// - Entry can be a simplified glob "<prefix>*" or "*<suffix>"
74+
// where prefix/suffix must have at least 1 character (so that
75+
// we don't match a '*' catch-all pattern.)
76+
// - The special filenames "." and ".." are always ignored.
77+
// - Otherwise the full string is matched.
78+
// - match is not case-sensitive.
79+
80+
if (strcmp(path, ".") == 0 || strcmp(path, "..") == 0) {
9281
return true;
93-
} else {
94-
// Let everything else through.
95-
return false;
9682
}
9783

98-
/* If we get this far, "type" should be set and the file
99-
* should be skipped.
100-
*/
101-
String8 subdirName(root);
102-
subdirName.appendPath(path);
103-
fprintf(stderr, " (skipping %s %s '%s')\n", type,
104-
getFileType(subdirName.string())==kFileTypeDirectory ? "dir":"file",
105-
subdirName.string());
84+
const char *delim = ":";
85+
const char *p = gUserIgnoreAssets;
86+
if (!p || !p[0]) {
87+
p = getenv("ANDROID_AAPT_IGNORE");
88+
}
89+
if (!p || !p[0]) {
90+
p = gDefaultIgnoreAssets;
91+
}
92+
char *patterns = strdup(p);
10693

107-
return true;
94+
bool ignore = false;
95+
bool chatty = true;
96+
char *matchedPattern = NULL;
97+
98+
String8 fullPath(root);
99+
fullPath.appendPath(path);
100+
FileType type = getFileType(fullPath);
101+
102+
int plen = strlen(path);
103+
104+
// Note: we don't have strtok_r under mingw.
105+
for(char *token = strtok(patterns, delim);
106+
!ignore && token != NULL;
107+
token = strtok(NULL, delim)) {
108+
chatty = token[0] != '!';
109+
if (!chatty) token++; // skip !
110+
if (strncasecmp(token, "<dir>" , 5) == 0) {
111+
if (type != kFileTypeDirectory) continue;
112+
token += 5;
113+
}
114+
if (strncasecmp(token, "<file>", 6) == 0) {
115+
if (type != kFileTypeRegular) continue;
116+
token += 6;
117+
}
118+
119+
matchedPattern = token;
120+
int n = strlen(token);
121+
122+
if (token[0] == '*') {
123+
// Match *suffix
124+
token++;
125+
if (n <= plen) {
126+
ignore = strncasecmp(token, path + plen - n, n) == 0;
127+
}
128+
} else if (n > 1 && token[n - 1] == '*') {
129+
// Match prefix*
130+
ignore = strncasecmp(token, path, n - 1) == 0;
131+
} else {
132+
ignore = strcasecmp(token, path) == 0;
133+
}
134+
}
135+
136+
if (ignore && chatty) {
137+
fprintf(stderr, " (skipping %s '%s' due to ANDROID_AAPT_IGNORE pattern '%s')\n",
138+
type == kFileTypeDirectory ? "dir" : "file",
139+
path,
140+
matchedPattern ? matchedPattern : "");
141+
}
142+
143+
free(patterns);
144+
return ignore;
108145
}
109146

110147
// =========================================================================

tools/aapt/AaptAssets.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
using namespace android;
2424

25+
26+
extern const char * const gDefaultIgnoreAssets;
27+
extern const char * gUserIgnoreAssets;
28+
2529
bool valid_symbol_name(const String8& str);
2630

2731
class AaptAssets;

tools/aapt/Main.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ void usage(void)
178178
" --non-constant-id\n"
179179
" Make the resources ID non constant. This is required to make an R java class\n"
180180
" that does not contain the final value but is used to make reusable compiled\n"
181-
" libraries that need to access resources.\n");
181+
" libraries that need to access resources.\n"
182+
" --ignore-assets\n"
183+
" Assets to be ignored. Default pattern is:\n"
184+
" %s\n",
185+
gDefaultIgnoreAssets);
182186
}
183187

184188
/*
@@ -556,7 +560,16 @@ int main(int argc, char* const argv[])
556560
bundle.setNonConstantId(true);
557561
} else if (strcmp(cp, "-no-crunch") == 0) {
558562
bundle.setUseCrunchCache(true);
559-
}else {
563+
} else if (strcmp(cp, "-ignore-assets") == 0) {
564+
argc--;
565+
argv++;
566+
if (!argc) {
567+
fprintf(stderr, "ERROR: No argument supplied for '--ignore-assets' option\n");
568+
wantUsage = true;
569+
goto bail;
570+
}
571+
gUserIgnoreAssets = argv[0];
572+
} else {
560573
fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp);
561574
wantUsage = true;
562575
goto bail;

0 commit comments

Comments
 (0)