|
1 | 1 | var path = require('path'), |
2 | | - util = require('util'), |
3 | | - M_EXTENSION = /[.]m$/, SOURCE_FILE = 'sourcecode.c.objc', |
4 | | - H_EXTENSION = /[.]h$/, HEADER_FILE = 'sourcecode.c.h', |
5 | | - BUNDLE_EXTENSION = /[.]bundle$/, BUNDLE = '"wrapper.plug-in"', |
6 | | - XIB_EXTENSION = /[.]xib$/, XIB_FILE = 'file.xib', |
7 | | - DYLIB_EXTENSION = /[.]dylib$/, DYLIB = '"compiled.mach-o.dylib"', |
8 | | - FRAMEWORK_EXTENSION = /[.]framework$/, FRAMEWORK = 'wrapper.framework', |
9 | | - ARCHIVE_EXTENSION = /[.]a$/, ARCHIVE = 'archive.ar', |
10 | | - DEFAULT_SOURCE_TREE = '"<group>"', |
11 | | - DEFAULT_FILE_ENCODING = 4; |
12 | | - |
13 | | -function detectLastType(path) { |
14 | | - if (M_EXTENSION.test(path)) |
15 | | - return SOURCE_FILE; |
| 2 | + util = require('util'); |
| 3 | + |
| 4 | +var DEFAULT_SOURCETREE = '"<group>"', |
| 5 | + DEFAULT_PRODUCT_SOURCETREE = 'BUILT_PRODUCTS_DIR', |
| 6 | + DEFAULT_FILEENCODING = 4, |
| 7 | + DEFAULT_GROUP = 'Resources', |
| 8 | + DEFAULT_FILETYPE = 'unknown'; |
| 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 | + dylib: 'compiled.mach-o.dylib', |
| 16 | + framework: 'wrapper.framework', |
| 17 | + h: 'sourcecode.c.h', |
| 18 | + m: 'sourcecode.c.objc', |
| 19 | + markdown: 'text', |
| 20 | + mdimporter: 'wrapper.cfbundle', |
| 21 | + octest: 'wrapper.cfbundle', |
| 22 | + pch: 'sourcecode.c.h', |
| 23 | + plist: 'text.plist.xml', |
| 24 | + sh: 'text.script.sh', |
| 25 | + swift: 'sourcecode.swift', |
| 26 | + xcassets: 'folder.assetcatalog', |
| 27 | + xcconfig: 'text.xcconfig', |
| 28 | + xcdatamodel: 'wrapper.xcdatamodel', |
| 29 | + xcodeproj: 'wrapper.pb-project', |
| 30 | + xctest: 'wrapper.cfbundle', |
| 31 | + xib: 'file.xib' |
| 32 | + }, |
| 33 | + GROUP_BY_FILETYPE = { |
| 34 | + 'archive.ar': 'Frameworks', |
| 35 | + 'compiled.mach-o.dylib': 'Frameworks', |
| 36 | + 'wrapper.framework': 'Frameworks', |
| 37 | + 'sourcecode.c.h': 'Resources', |
| 38 | + 'sourcecode.c.objc': 'Sources', |
| 39 | + 'sourcecode.swift': 'Sources' |
| 40 | + }, |
| 41 | + PATH_BY_FILETYPE = { |
| 42 | + 'compiled.mach-o.dylib': 'usr/lib/', |
| 43 | + 'wrapper.framework': 'System/Library/Frameworks/' |
| 44 | + }, |
| 45 | + SOURCETREE_BY_FILETYPE = { |
| 46 | + 'compiled.mach-o.dylib': 'SDKROOT', |
| 47 | + 'wrapper.framework': 'SDKROOT' |
| 48 | + }, |
| 49 | + ENCODING_BY_FILETYPE = { |
| 50 | + 'sourcecode.c.h': 4, |
| 51 | + 'sourcecode.c.h': 4, |
| 52 | + 'sourcecode.c.objc': 4, |
| 53 | + 'sourcecode.swift': 4, |
| 54 | + 'text': 4, |
| 55 | + 'text.plist.xml': 4, |
| 56 | + 'text.script.sh': 4, |
| 57 | + 'text.xcconfig': 4 |
| 58 | + }; |
| 59 | + |
| 60 | + |
| 61 | +function unquoted(text){ |
| 62 | + return text.replace (/(^")|("$)/g, '') |
| 63 | +} |
16 | 64 |
|
17 | | - if (H_EXTENSION.test(path)) |
18 | | - return HEADER_FILE; |
| 65 | +function detectType(filePath) { |
| 66 | + var extension = path.extname(filePath).substring(1), |
| 67 | + filetype = FILETYPE_BY_EXTENSION[unquoted(extension)]; |
19 | 68 |
|
20 | | - if (BUNDLE_EXTENSION.test(path)) |
21 | | - return BUNDLE; |
| 69 | + if (!filetype) { |
| 70 | + return DEFAULT_FILETYPE; |
| 71 | + } |
22 | 72 |
|
23 | | - if (XIB_EXTENSION.test(path)) |
24 | | - return XIB_FILE; |
| 73 | + return filetype; |
| 74 | +} |
25 | 75 |
|
26 | | - if (FRAMEWORK_EXTENSION.test(path)) |
27 | | - return FRAMEWORK; |
| 76 | +function defaultExtension(fileRef) { |
| 77 | + var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType; |
28 | 78 |
|
29 | | - if (DYLIB_EXTENSION.test(path)) |
30 | | - return DYLIB; |
| 79 | + for(var extension in FILETYPE_BY_EXTENSION) { |
| 80 | + if(FILETYPE_BY_EXTENSION.hasOwnProperty(unquoted(extension)) ) { |
| 81 | + if(FILETYPE_BY_EXTENSION[unquoted(extension)] === filetype ) |
| 82 | + return extension; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
31 | 86 |
|
32 | | - if (ARCHIVE_EXTENSION.test(path)) |
33 | | - return ARCHIVE; |
| 87 | +function defaultEncoding(fileRef) { |
| 88 | + var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType, |
| 89 | + encoding = ENCODING_BY_FILETYPE[unquoted(filetype)]; |
34 | 90 |
|
35 | | - // dunno |
36 | | - return 'unknown'; |
| 91 | + if (encoding) { |
| 92 | + return encoding; |
| 93 | + } |
37 | 94 | } |
38 | 95 |
|
39 | | -function fileEncoding(file) { |
40 | | - if (file.lastType != BUNDLE && !file.customFramework) { |
41 | | - return DEFAULT_FILE_ENCODING; |
| 96 | +function detectGroup(fileRef) { |
| 97 | + var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType, |
| 98 | + groupName = GROUP_BY_FILETYPE[unquoted(filetype)]; |
| 99 | + |
| 100 | + if (!groupName) { |
| 101 | + return DEFAULT_GROUP; |
42 | 102 | } |
| 103 | + |
| 104 | + return groupName; |
43 | 105 | } |
44 | 106 |
|
45 | | -function defaultSourceTree(file) { |
46 | | - if (( file.lastType == DYLIB || file.lastType == FRAMEWORK ) && !file.customFramework) { |
47 | | - return 'SDKROOT'; |
48 | | - } else { |
49 | | - return DEFAULT_SOURCE_TREE; |
| 107 | +function detectSourcetree(fileRef) { |
| 108 | + |
| 109 | + var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType, |
| 110 | + sourcetree = SOURCETREE_BY_FILETYPE[unquoted(filetype)]; |
| 111 | + |
| 112 | + if (fileRef.explicitFileType) { |
| 113 | + return DEFAULT_PRODUCT_SOURCETREE; |
50 | 114 | } |
| 115 | + |
| 116 | + if (fileRef.customFramework) { |
| 117 | + return DEFAULT_SOURCETREE; |
| 118 | + } |
| 119 | + |
| 120 | + if (!sourcetree) { |
| 121 | + return DEFAULT_SOURCETREE; |
| 122 | + } |
| 123 | + |
| 124 | + return sourcetree; |
51 | 125 | } |
52 | 126 |
|
53 | | -function correctPath(file, filepath) { |
54 | | - if (file.lastType == FRAMEWORK && !file.customFramework) { |
55 | | - return 'System/Library/Frameworks/' + filepath; |
56 | | - } else if (file.lastType == DYLIB) { |
57 | | - return 'usr/lib/' + filepath; |
58 | | - } else { |
59 | | - return filepath; |
| 127 | +function defaultPath(fileRef, filePath) { |
| 128 | + var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType, |
| 129 | + defaultPath = PATH_BY_FILETYPE[unquoted(filetype)]; |
| 130 | + |
| 131 | + if (fileRef.customFramework) { |
| 132 | + return filePath; |
| 133 | + } |
| 134 | + |
| 135 | + if (defaultPath) { |
| 136 | + return path.join(defaultPath, path.basename(filePath)); |
60 | 137 | } |
| 138 | + |
| 139 | + return filePath; |
61 | 140 | } |
62 | 141 |
|
63 | | -function correctGroup(file) { |
64 | | - if (file.lastType == SOURCE_FILE) { |
65 | | - return 'Sources'; |
66 | | - } else if (file.lastType == DYLIB || file.lastType == ARCHIVE || file.lastType == FRAMEWORK) { |
67 | | - return 'Frameworks'; |
68 | | - } else { |
69 | | - return 'Resources'; |
| 142 | +function defaultGroup(fileRef) { |
| 143 | + var groupName = GROUP_BY_FILETYPE[fileRef.lastKnownFileType]; |
| 144 | + |
| 145 | + if (!groupName) { |
| 146 | + return DEFAULT_GROUP; |
70 | 147 | } |
| 148 | + |
| 149 | + return defaultGroup; |
71 | 150 | } |
72 | 151 |
|
73 | 152 | function pbxFile(filepath, opt) { |
74 | 153 | var opt = opt || {}; |
| 154 | + |
| 155 | + self = this; |
75 | 156 |
|
76 | | - this.lastType = opt.lastType || detectLastType(filepath); |
| 157 | + this.lastKnownFileType = opt.lastKnownFileType || detectType(filepath); |
| 158 | + this.group = detectGroup(self); |
77 | 159 |
|
78 | 160 | // for custom frameworks |
79 | | - if(opt.customFramework == true) { |
80 | | - this.customFramework = true; |
81 | | - this.dirname = path.dirname(filepath); |
| 161 | + if (opt.customFramework == true) { |
| 162 | + this.customFramework = true; |
| 163 | + this.dirname = path.dirname(filepath); |
82 | 164 | } |
83 | 165 |
|
84 | 166 | this.basename = path.basename(filepath); |
85 | | - this.path = correctPath(this, filepath); |
86 | | - this.group = correctGroup(this); |
| 167 | + this.path = defaultPath(this, filepath); |
| 168 | + this.fileEncoding = this.defaultEncoding = opt.defaultEncoding || defaultEncoding(self); |
| 169 | + |
| 170 | + |
| 171 | + // When referencing products / build output files |
| 172 | + if (opt.explicitFileType) { |
| 173 | + this.explicitFileType = opt.explicitFileType; |
| 174 | + this.basename = this.basename + '.' + defaultExtension(this); |
| 175 | + delete this.path; |
| 176 | + delete this.lastKnownFileType; |
| 177 | + delete this.group; |
| 178 | + delete this.defaultEncoding; |
| 179 | + } |
87 | 180 |
|
88 | | - this.sourceTree = opt.sourceTree || defaultSourceTree(this); |
89 | | - this.fileEncoding = opt.fileEncoding || fileEncoding(this); |
| 181 | + this.sourceTree = opt.sourceTree || detectSourcetree(self); |
| 182 | + this.includeInIndex = 0; |
90 | 183 |
|
91 | | - if (opt.weak && opt.weak === true) |
92 | | - this.settings = { ATTRIBUTES: ['Weak'] }; |
| 184 | + if (opt.weak && opt.weak === true) |
| 185 | + this.settings = { ATTRIBUTES: ['Weak'] }; |
93 | 186 |
|
94 | 187 | if (opt.compilerFlags) { |
95 | 188 | if (!this.settings) |
96 | | - this.settings = {}; |
97 | | - this.settings.COMPILER_FLAGS = util.format('"%s"', opt.compilerFlags); |
| 189 | + this.settings = {}; |
| 190 | + this.settings.COMPILER_FLAGS = util.format('"%s"', opt.compilerFlags); |
98 | 191 | } |
99 | 192 | } |
100 | 193 |
|
|
0 commit comments