Skip to content

Commit 64ea6c0

Browse files
committed
refactor: extract constants in separate file
1 parent 054facf commit 64ea6c0

24 files changed

+202
-194
lines changed

lib/constants.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var DEFAULT_SOURCETREE = '"<group>"',
2+
DEFAULT_PRODUCT_SOURCETREE = 'BUILT_PRODUCTS_DIR',
3+
DEFAULT_FILEENCODING = 4,
4+
DEFAULT_GROUP = 'Resources',
5+
DEFAULT_FILETYPE = 'unknown',
6+
HEADER_FILE_TYPE_SUFFIX = ".h",
7+
ENTITLEMENTS_FILE_TYPE_SUFFIX = ".entitlements",
8+
SOURCE_CODE_FILE_TYPE_PREFIX = "sourcecode.";
9+
10+
var FILETYPE_BY_EXTENSION = {
11+
a: 'archive.ar',
12+
app: 'wrapper.application',
13+
appex: 'wrapper.app-extension',
14+
bundle: 'wrapper.plug-in',
15+
c: 'sourcecode.c.c',
16+
cc: 'sourcecode.cpp.cpp',
17+
cpp: 'sourcecode.cpp.cpp',
18+
cxx: 'sourcecode.cpp.cpp',
19+
'c++': 'sourcecode.cpp.cpp',
20+
dylib: 'compiled.mach-o.dylib',
21+
framework: 'wrapper.framework',
22+
h: 'sourcecode.c.h',
23+
hh: 'sourcecode.cpp.h',
24+
hpp: 'sourcecode.cpp.h',
25+
hxx: 'sourcecode.cpp.h',
26+
'h++': 'sourcecode.cpp.h',
27+
m: 'sourcecode.c.objc',
28+
mm: 'sourcecode.cpp.objcpp',
29+
markdown: 'text',
30+
mdimporter: 'wrapper.cfbundle',
31+
octest: 'wrapper.cfbundle',
32+
pch: 'sourcecode.c.h',
33+
plist: 'text.plist.xml',
34+
entitlements: 'text.plist.entitlements',
35+
png: "image.png",
36+
sh: 'text.script.sh',
37+
swift: 'sourcecode.swift',
38+
tbd: 'sourcecode.text-based-dylib-definition',
39+
xcassets: 'folder.assetcatalog',
40+
xcconfig: 'text.xcconfig',
41+
xcdatamodel: 'wrapper.xcdatamodel',
42+
xcodeproj: 'wrapper.pb-project',
43+
xctest: 'wrapper.cfbundle',
44+
xib: 'file.xib',
45+
strings: 'text.plist.strings'
46+
},
47+
GROUP_BY_FILETYPE = {
48+
'archive.ar': 'Frameworks',
49+
'compiled.mach-o.dylib': 'Frameworks',
50+
'sourcecode.text-based-dylib-definition': 'Frameworks',
51+
'wrapper.framework': 'Frameworks',
52+
'embedded.framework': 'Embed Frameworks',
53+
'sourcecode.c.h': 'Resources',
54+
'sourcecode.c.c': 'Sources',
55+
'sourcecode.c.objc': 'Sources',
56+
'sourcecode.swift': 'Sources',
57+
'sourcecode.cpp.cpp': 'Sources',
58+
'sourcecode.cpp.objcpp': 'Sources'
59+
},
60+
PATH_BY_FILETYPE = {
61+
'compiled.mach-o.dylib': 'usr/lib/',
62+
'sourcecode.text-based-dylib-definition': 'usr/lib/',
63+
'wrapper.framework': 'System/Library/Frameworks/'
64+
},
65+
SOURCETREE_BY_FILETYPE = {
66+
'compiled.mach-o.dylib': 'SDKROOT',
67+
'sourcecode.text-based-dylib-definition': 'SDKROOT',
68+
'wrapper.framework': 'SDKROOT'
69+
},
70+
ENCODING_BY_FILETYPE = {
71+
'sourcecode.c.h': 4,
72+
'sourcecode.c.h': 4,
73+
'sourcecode.cpp.h': 4,
74+
'sourcecode.c.c': 4,
75+
'sourcecode.c.objc': 4,
76+
'sourcecode.cpp.cpp': 4,
77+
'sourcecode.cpp.objcpp': 4,
78+
'sourcecode.swift': 4,
79+
'text': 4,
80+
'text.plist.xml': 4,
81+
'text.script.sh': 4,
82+
'text.xcconfig': 4,
83+
'text.plist.strings': 4
84+
};
85+
86+
function isHeaderFileType(fileType) {
87+
return fileType.endsWith(HEADER_FILE_TYPE_SUFFIX);
88+
}
89+
90+
function isSourceFileType(fileType) {
91+
return fileType.startsWith(SOURCE_CODE_FILE_TYPE_PREFIX) && !isHeaderFileType(fileType);
92+
}
93+
94+
function isAssetFileType(fileType) {
95+
return fileType === FILETYPE_BY_EXTENSION.xcassets;
96+
}
97+
98+
function isResource(group) {
99+
return group === "Resources";
100+
}
101+
102+
function isEntitlementFileType(fileType) {
103+
return fileType.endsWith(ENTITLEMENTS_FILE_TYPE_SUFFIX);
104+
}
105+
106+
function isPlistFileType(fileType) {
107+
return fileType === FILETYPE_BY_EXTENSION.plist;
108+
}
109+
110+
function unquoted(text) {
111+
return text == null ? '' : text.replace (/(^")|("$)/g, '')
112+
}
113+
114+
module.exports = {
115+
DEFAULT_SOURCETREE,
116+
DEFAULT_PRODUCT_SOURCETREE,
117+
DEFAULT_FILEENCODING,
118+
DEFAULT_GROUP,
119+
DEFAULT_FILETYPE,
120+
HEADER_FILE_TYPE_SUFFIX,
121+
ENTITLEMENTS_FILE_TYPE_SUFFIX,
122+
SOURCE_CODE_FILE_TYPE_PREFIX,
123+
FILETYPE_BY_EXTENSION,
124+
GROUP_BY_FILETYPE,
125+
PATH_BY_FILETYPE,
126+
SOURCETREE_BY_FILETYPE,
127+
ENCODING_BY_FILETYPE,
128+
isHeaderFileType,
129+
isSourceFileType,
130+
isAssetFileType,
131+
isResource,
132+
isEntitlementFileType,
133+
isPlistFileType,
134+
unquoted
135+
}

lib/pbxFile.js

+17-145
Original file line numberDiff line numberDiff line change
@@ -16,134 +16,24 @@
1616
*/
1717

1818
var path = require('path'),
19-
util = require('util');
20-
21-
var DEFAULT_SOURCETREE = '"<group>"',
22-
DEFAULT_PRODUCT_SOURCETREE = 'BUILT_PRODUCTS_DIR',
23-
DEFAULT_FILEENCODING = 4,
24-
DEFAULT_GROUP = 'Resources',
25-
DEFAULT_FILETYPE = 'unknown',
26-
HEADER_FILE_TYPE_SUFFIX = ".h",
27-
ENTITLEMENTS_FILE_TYPE_SUFFIX = ".entitlements",
28-
SOURCE_CODE_FILE_TYPE_PREFIX = "sourcecode.";
29-
30-
var FILETYPE_BY_EXTENSION = {
31-
a: 'archive.ar',
32-
app: 'wrapper.application',
33-
appex: 'wrapper.app-extension',
34-
bundle: 'wrapper.plug-in',
35-
c: 'sourcecode.c.c',
36-
cc: 'sourcecode.cpp.cpp',
37-
cpp: 'sourcecode.cpp.cpp',
38-
cxx: 'sourcecode.cpp.cpp',
39-
'c++': 'sourcecode.cpp.cpp',
40-
dylib: 'compiled.mach-o.dylib',
41-
framework: 'wrapper.framework',
42-
h: 'sourcecode.c.h',
43-
hh: 'sourcecode.cpp.h',
44-
hpp: 'sourcecode.cpp.h',
45-
hxx: 'sourcecode.cpp.h',
46-
'h++': 'sourcecode.cpp.h',
47-
m: 'sourcecode.c.objc',
48-
mm: 'sourcecode.cpp.objcpp',
49-
markdown: 'text',
50-
mdimporter: 'wrapper.cfbundle',
51-
octest: 'wrapper.cfbundle',
52-
pch: 'sourcecode.c.h',
53-
plist: 'text.plist.xml',
54-
entitlements: 'text.plist.entitlements',
55-
png: "image.png",
56-
sh: 'text.script.sh',
57-
swift: 'sourcecode.swift',
58-
tbd: 'sourcecode.text-based-dylib-definition',
59-
xcassets: 'folder.assetcatalog',
60-
xcconfig: 'text.xcconfig',
61-
xcdatamodel: 'wrapper.xcdatamodel',
62-
xcodeproj: 'wrapper.pb-project',
63-
xctest: 'wrapper.cfbundle',
64-
xib: 'file.xib',
65-
strings: 'text.plist.strings'
66-
},
67-
GROUP_BY_FILETYPE = {
68-
'archive.ar': 'Frameworks',
69-
'compiled.mach-o.dylib': 'Frameworks',
70-
'sourcecode.text-based-dylib-definition': 'Frameworks',
71-
'wrapper.framework': 'Frameworks',
72-
'embedded.framework': 'Embed Frameworks',
73-
'sourcecode.c.h': 'Resources',
74-
'sourcecode.c.c': 'Sources',
75-
'sourcecode.c.objc': 'Sources',
76-
'sourcecode.swift': 'Sources',
77-
'sourcecode.cpp.cpp': 'Sources',
78-
'sourcecode.cpp.objcpp': 'Sources'
79-
},
80-
PATH_BY_FILETYPE = {
81-
'compiled.mach-o.dylib': 'usr/lib/',
82-
'sourcecode.text-based-dylib-definition': 'usr/lib/',
83-
'wrapper.framework': 'System/Library/Frameworks/'
84-
},
85-
SOURCETREE_BY_FILETYPE = {
86-
'compiled.mach-o.dylib': 'SDKROOT',
87-
'sourcecode.text-based-dylib-definition': 'SDKROOT',
88-
'wrapper.framework': 'SDKROOT'
89-
},
90-
ENCODING_BY_FILETYPE = {
91-
'sourcecode.c.h': 4,
92-
'sourcecode.c.h': 4,
93-
'sourcecode.cpp.h': 4,
94-
'sourcecode.c.c': 4,
95-
'sourcecode.c.objc': 4,
96-
'sourcecode.cpp.cpp': 4,
97-
'sourcecode.cpp.objcpp': 4,
98-
'sourcecode.swift': 4,
99-
'text': 4,
100-
'text.plist.xml': 4,
101-
'text.script.sh': 4,
102-
'text.xcconfig': 4,
103-
'text.plist.strings': 4
104-
};
105-
106-
function isHeaderFileType(fileType) {
107-
return fileType.endsWith(HEADER_FILE_TYPE_SUFFIX);
108-
}
109-
110-
function isSourceFileType(fileType) {
111-
return fileType.startsWith(SOURCE_CODE_FILE_TYPE_PREFIX) && !isHeaderFileType(fileType);
112-
}
113-
114-
function isAssetFileType(fileType) {
115-
return fileType === FILETYPE_BY_EXTENSION.xcassets;
116-
}
117-
118-
function isResource(group) {
119-
return group === "Resources";
120-
}
121-
122-
function isEntitlement(fileType) {
123-
return fileType.endsWith(ENTITLEMENTS_FILE_TYPE_SUFFIX);
124-
}
125-
126-
function isPlist(fileType) {
127-
return fileType === FILETYPE_BY_EXTENSION.plist;
128-
}
129-
130-
function unquoted(text){
131-
return text == null ? '' : text.replace (/(^")|("$)/g, '')
132-
}
19+
util = require('util'),
20+
constants = require('./constants'),
21+
unquoted = constants.unquoted,
22+
FILETYPE_BY_EXTENSION = constants.FILETYPE_BY_EXTENSION;
13323

13424
function detectType(filePath) {
13525
var extension = path.extname(filePath).substring(1),
13626
filetype = FILETYPE_BY_EXTENSION[unquoted(extension)];
13727

13828
if (!filetype) {
139-
return DEFAULT_FILETYPE;
29+
return constants.DEFAULT_FILETYPE;
14030
}
14131

14232
return filetype;
14333
}
14434

14535
function defaultExtension(fileRef) {
146-
var filetype = fileRef.lastKnownFileType && fileRef.lastKnownFileType != DEFAULT_FILETYPE ?
36+
var filetype = fileRef.lastKnownFileType && fileRef.lastKnownFileType != constants.DEFAULT_FILETYPE ?
14737
fileRef.lastKnownFileType : fileRef.explicitFileType;
14838

14939
for(var extension in FILETYPE_BY_EXTENSION) {
@@ -156,7 +46,7 @@ function defaultExtension(fileRef) {
15646

15747
function defaultEncoding(fileRef) {
15848
var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
159-
encoding = ENCODING_BY_FILETYPE[unquoted(filetype)];
49+
encoding = constants.ENCODING_BY_FILETYPE[unquoted(filetype)];
16050

16151
if (encoding) {
16252
return encoding;
@@ -166,18 +56,18 @@ function defaultEncoding(fileRef) {
16656
function detectGroup(fileRef, opt) {
16757
var extension = path.extname(fileRef.basename).substring(1),
16858
filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
169-
groupName = GROUP_BY_FILETYPE[unquoted(filetype)];
59+
groupName = constants.GROUP_BY_FILETYPE[unquoted(filetype)];
17060

17161
if (extension === 'xcdatamodeld') {
17262
return 'Sources';
17363
}
17464

17565
if (opt.customFramework && opt.embed) {
176-
return GROUP_BY_FILETYPE['embedded.framework'];
66+
return constants.GROUP_BY_FILETYPE['embedded.framework'];
17767
}
17868

17969
if (!groupName) {
180-
return DEFAULT_GROUP;
70+
return constants.DEFAULT_GROUP;
18171
}
18272

18373
return groupName;
@@ -186,26 +76,26 @@ function detectGroup(fileRef, opt) {
18676
function detectSourcetree(fileRef) {
18777

18878
var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
189-
sourcetree = SOURCETREE_BY_FILETYPE[unquoted(filetype)];
79+
sourcetree = constants.SOURCETREE_BY_FILETYPE[unquoted(filetype)];
19080

19181
if (fileRef.explicitFileType) {
192-
return DEFAULT_PRODUCT_SOURCETREE;
82+
return constants.DEFAULT_PRODUCT_SOURCETREE;
19383
}
19484

19585
if (fileRef.customFramework) {
196-
return DEFAULT_SOURCETREE;
86+
return constants.DEFAULT_SOURCETREE;
19787
}
19888

19989
if (!sourcetree) {
200-
return DEFAULT_SOURCETREE;
90+
return constants.DEFAULT_SOURCETREE;
20191
}
20292

20393
return sourcetree;
20494
}
20595

20696
function defaultPath(fileRef, filePath) {
20797
var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
208-
defaultPath = PATH_BY_FILETYPE[unquoted(filetype)];
98+
defaultPath = constants.PATH_BY_FILETYPE[unquoted(filetype)];
20999

210100
if (fileRef.customFramework) {
211101
return filePath;
@@ -218,18 +108,8 @@ function defaultPath(fileRef, filePath) {
218108
return filePath;
219109
}
220110

221-
function defaultGroup(fileRef) {
222-
var groupName = GROUP_BY_FILETYPE[fileRef.lastKnownFileType];
223-
224-
if (!groupName) {
225-
return DEFAULT_GROUP;
226-
}
227-
228-
return defaultGroup;
229-
}
230-
231111
function pbxFile(filepath, opt) {
232-
var opt = opt || {};
112+
opt = opt || {};
233113

234114
this.basename = opt.basename || path.basename(filepath);
235115
this.lastKnownFileType = opt.lastKnownFileType || detectType(filepath);
@@ -275,12 +155,4 @@ function pbxFile(filepath, opt) {
275155
}
276156
}
277157

278-
module.exports = {
279-
pbxFile: pbxFile,
280-
isSourceFileType,
281-
isHeaderFileType,
282-
isResource,
283-
isEntitlement,
284-
isAssetFileType,
285-
isPlist
286-
}
158+
module.exports = pbxFile;

0 commit comments

Comments
 (0)