UNPKG

5.36 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 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
61function unquoted(text){
62 return text.replace (/(^")|("$)/g, '')
63}
64
65function detectType(filePath) {
66 var extension = path.extname(filePath).substring(1),
67 filetype = FILETYPE_BY_EXTENSION[unquoted(extension)];
68
69 if (!filetype) {
70 return DEFAULT_FILETYPE;
71 }
72
73 return filetype;
74}
75
76function defaultExtension(fileRef) {
77 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType;
78
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}
86
87function defaultEncoding(fileRef) {
88 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
89 encoding = ENCODING_BY_FILETYPE[unquoted(filetype)];
90
91 if (encoding) {
92 return encoding;
93 }
94}
95
96function detectGroup(fileRef) {
97 var extension = path.extname(fileRef.basename).substring(1),
98 filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
99 groupName = GROUP_BY_FILETYPE[unquoted(filetype)];
100
101 if (extension === 'xcdatamodeld') {
102 return 'Sources';
103 }
104
105 if (!groupName) {
106 return DEFAULT_GROUP;
107 }
108
109 return groupName;
110}
111
112function detectSourcetree(fileRef) {
113
114 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
115 sourcetree = SOURCETREE_BY_FILETYPE[unquoted(filetype)];
116
117 if (fileRef.explicitFileType) {
118 return DEFAULT_PRODUCT_SOURCETREE;
119 }
120
121 if (fileRef.customFramework) {
122 return DEFAULT_SOURCETREE;
123 }
124
125 if (!sourcetree) {
126 return DEFAULT_SOURCETREE;
127 }
128
129 return sourcetree;
130}
131
132function defaultPath(fileRef, filePath) {
133 var filetype = fileRef.lastKnownFileType || fileRef.explicitFileType,
134 defaultPath = PATH_BY_FILETYPE[unquoted(filetype)];
135
136 if (fileRef.customFramework) {
137 return filePath;
138 }
139
140 if (defaultPath) {
141 return path.join(defaultPath, path.basename(filePath));
142 }
143
144 return filePath;
145}
146
147function defaultGroup(fileRef) {
148 var groupName = GROUP_BY_FILETYPE[fileRef.lastKnownFileType];
149
150 if (!groupName) {
151 return DEFAULT_GROUP;
152 }
153
154 return defaultGroup;
155}
156
157function pbxFile(filepath, opt) {
158 var opt = opt || {};
159
160 self = this;
161
162 this.basename = path.basename(filepath);
163 this.lastKnownFileType = opt.lastKnownFileType || detectType(filepath);
164 this.group = detectGroup(self);
165
166 // for custom frameworks
167 if (opt.customFramework == true) {
168 this.customFramework = true;
169 this.dirname = path.dirname(filepath);
170 }
171
172 this.path = defaultPath(this, filepath);
173 this.fileEncoding = this.defaultEncoding = opt.defaultEncoding || defaultEncoding(self);
174
175
176 // When referencing products / build output files
177 if (opt.explicitFileType) {
178 this.explicitFileType = opt.explicitFileType;
179 this.basename = this.basename + '.' + defaultExtension(this);
180 delete this.path;
181 delete this.lastKnownFileType;
182 delete this.group;
183 delete this.defaultEncoding;
184 }
185
186 this.sourceTree = opt.sourceTree || detectSourcetree(self);
187 this.includeInIndex = 0;
188
189 if (opt.weak && opt.weak === true)
190 this.settings = { ATTRIBUTES: ['Weak'] };
191
192 if (opt.compilerFlags) {
193 if (!this.settings)
194 this.settings = {};
195 this.settings.COMPILER_FLAGS = util.format('"%s"', opt.compilerFlags);
196 }
197}
198
199module.exports = pbxFile;