UNPKG

6 kBJavaScriptView Raw
1var path = require('path'),
2 util = require('util');
3
4var DEFAULT_SOURCETREE = '"<group>"',
5 DEFAULT_PRODUCT_SOURCETREE = 'BUILT_PRODUCTS_DIR',
6 DEFAULT_FILEENCODING = 4,
7 DEFAULT_GROUP = 'Resources',
8 DEFAULT_FILETYPE = 'unknown';
9
10var 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 tbd: 'sourcecode.text-based-dylib-definition',
27 xcassets: 'folder.assetcatalog',
28 xcconfig: 'text.xcconfig',
29 xcdatamodel: 'wrapper.xcdatamodel',
30 xcodeproj: 'wrapper.pb-project',
31 xctest: 'wrapper.cfbundle',
32 xib: 'file.xib'
33 },
34 GROUP_BY_FILETYPE = {
35 'archive.ar': 'Frameworks',
36 'compiled.mach-o.dylib': 'Frameworks',
37 'sourcecode.text-based-dylib-definition': 'Frameworks',
38 'wrapper.framework': 'Frameworks',
39 'embedded.framework': 'Embed Frameworks',
40 'sourcecode.c.h': 'Resources',
41 'sourcecode.c.objc': 'Sources',
42 'sourcecode.swift': 'Sources'
43 },
44 PATH_BY_FILETYPE = {
45 'compiled.mach-o.dylib': 'usr/lib/',
46 'sourcecode.text-based-dylib-definition': 'usr/lib/',
47 'wrapper.framework': 'System/Library/Frameworks/'
48 },
49 SOURCETREE_BY_FILETYPE = {
50 'compiled.mach-o.dylib': 'SDKROOT',
51 'sourcecode.text-based-dylib-definition': 'SDKROOT',
52 'wrapper.framework': 'SDKROOT'
53 },
54 ENCODING_BY_FILETYPE = {
55 'sourcecode.c.h': 4,
56 'sourcecode.c.h': 4,
57 'sourcecode.c.objc': 4,
58 'sourcecode.swift': 4,
59 'text': 4,
60 'text.plist.xml': 4,
61 'text.script.sh': 4,
62 'text.xcconfig': 4
63 };
64
65
66function unquoted(text){
67 return text.replace (/(^")|("$)/g, '')
68}
69
70function detectType(filePath) {
71 var extension = path.extname(filePath).substring(1),
72 filetype = FILETYPE_BY_EXTENSION[unquoted(extension)];
73
74 if (!filetype) {
75 return DEFAULT_FILETYPE;
76 }
77
78 return filetype;
79}
80
81function defaultExtension(fileRef) {
82 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType;
83
84 for(var extension in FILETYPE_BY_EXTENSION) {
85 if(FILETYPE_BY_EXTENSION.hasOwnProperty(unquoted(extension)) ) {
86 if(FILETYPE_BY_EXTENSION[unquoted(extension)] === filetype )
87 return extension;
88 }
89 }
90}
91
92function defaultEncoding(fileRef) {
93 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
94 encoding = ENCODING_BY_FILETYPE[unquoted(filetype)];
95
96 if (encoding) {
97 return encoding;
98 }
99}
100
101function detectGroup(fileRef, opt) {
102 var extension = path.extname(fileRef.basename).substring(1),
103 filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
104 groupName = GROUP_BY_FILETYPE[unquoted(filetype)];
105
106 if (extension === 'xcdatamodeld') {
107 return 'Sources';
108 }
109
110 if (opt.customFramework && opt.embed) {
111 return GROUP_BY_FILETYPE['embedded.framework'];
112 }
113
114 if (!groupName) {
115 return DEFAULT_GROUP;
116 }
117
118 return groupName;
119}
120
121function detectSourcetree(fileRef) {
122
123 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
124 sourcetree = SOURCETREE_BY_FILETYPE[unquoted(filetype)];
125
126 if (fileRef.explicitFileType) {
127 return DEFAULT_PRODUCT_SOURCETREE;
128 }
129
130 if (fileRef.customFramework) {
131 return DEFAULT_SOURCETREE;
132 }
133
134 if (!sourcetree) {
135 return DEFAULT_SOURCETREE;
136 }
137
138 return sourcetree;
139}
140
141function defaultPath(fileRef, filePath) {
142 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
143 defaultPath = PATH_BY_FILETYPE[unquoted(filetype)];
144
145 if (fileRef.customFramework) {
146 return filePath;
147 }
148
149 if (defaultPath) {
150 return path.join(defaultPath, path.basename(filePath));
151 }
152
153 return filePath;
154}
155
156function defaultGroup(fileRef) {
157 var groupName = GROUP_BY_FILETYPE[fileRef.lastKnownFileType];
158
159 if (!groupName) {
160 return DEFAULT_GROUP;
161 }
162
163 return defaultGroup;
164}
165
166function pbxFile(filepath, opt) {
167 var opt = opt || {};
168
169 this.basename = path.basename(filepath);
170 this.lastKnownFileType = opt.lastKnownFileType || detectType(filepath);
171 this.group = detectGroup(this, opt);
172
173 // for custom frameworks
174 if (opt.customFramework == true) {
175 this.customFramework = true;
176 this.dirname = path.dirname(filepath).replace(/\\/g, '/');
177 }
178
179 this.path = defaultPath(this, filepath).replace(/\\/g, '/');
180 this.fileEncoding = this.defaultEncoding = opt.defaultEncoding || defaultEncoding(this);
181
182 // When referencing products / build output files
183 if (opt.explicitFileType) {
184 this.explicitFileType = opt.explicitFileType;
185 this.basename = this.basename + '.' + defaultExtension(this);
186 delete this.path;
187 delete this.lastKnownFileType;
188 delete this.group;
189 delete this.defaultEncoding;
190 }
191
192 this.sourceTree = opt.sourceTree || detectSourcetree(this);
193 this.includeInIndex = 0;
194
195 if (opt.weak && opt.weak === true)
196 this.settings = { ATTRIBUTES: ['Weak'] };
197
198 if (opt.compilerFlags) {
199 if (!this.settings)
200 this.settings = {};
201 this.settings.COMPILER_FLAGS = util.format('"%s"', opt.compilerFlags);
202 }
203
204 if (opt.embed && opt.sign) {
205 if (!this.settings)
206 this.settings = {};
207 if (!this.settings.ATTRIBUTES)
208 this.settings.ATTRIBUTES = [];
209 this.settings.ATTRIBUTES.push('CodeSignOnCopy');
210 }
211}
212
213module.exports = pbxFile;