forked from cappuccino/cappuccino
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.j
250 lines (187 loc) · 7.25 KB
/
main.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* main.j
* nib2cib
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPCib.j>
@import "NSFoundation.j"
@import "NSAppKit.j"
@import "Nib2CibKeyedUnarchiver.j"
importPackage(java.io);
importClass(java.io.File);
CPLogRegister(CPLogPrint);
function exec(command)
{
var p = Packages.java.lang.Runtime.getRuntime().exec(jsArrayToJavaArray(command));
var result = p.waitFor();
var reader = new Packages.java.io.BufferedReader(new Packages.java.io.InputStreamReader(p.getInputStream()));
while (s = reader.readLine())
print(s);
var reader = new Packages.java.io.BufferedReader(new Packages.java.io.InputStreamReader(p.getErrorStream()));
while (s = reader.readLine())
print(s);
return result;
}
function printUsage()
{
java.lang.System.out.println("usage: steam INPUT_FILE [OUTPUT_FILE]");
java.lang.System.exit(1);
}
function cibExtension(aPath)
{
var start = aPath.length - 1;
while (aPath.charAt(start) === '/')
start--;
aPath = aPath.substr(0, start + 1);
var dotIndex = aPath.lastIndexOf('.');
if (dotIndex == -1)
return aPath + ".cib";
var slashIndex = aPath.lastIndexOf('/');
if (slashIndex > dotIndex)
return aPath + ".cib";
return aPath.substr(0, dotIndex) + ".cib";
}
function convert(inputFileName, outputFileName, resourcesPath)
{
var resourcesFile = nil;
if (resourcesPath)
{
resourcesFile = new File(resourcesPath).getCanonicalFile();
if (!resourcesFile.canRead())
{
print("Could not find Resources at " + resourcesFile);
return;
}
}
// Make sure we can read the file
if (!(new Packages.java.io.File(inputFileName)).canRead())
{
print("Could not read file at " + inputFileName);
return;
}
// Compile xib or nib to make sure we have a non-new format nib.
var temporaryNibFile = Packages.java.io.File.createTempFile("temp", ".nib"),
temporaryNibFilePath = temporaryNibFile.getAbsolutePath();
temporaryNibFile.deleteOnExit();
if (exec(["/usr/bin/ibtool", inputFileName, "--compile", temporaryNibFilePath]))
{
print("Could not compile file at " + inputFileName);
return;
}
// Convert from binary plist to XML plist
var temporaryPlistFile = Packages.java.io.File.createTempFile("temp", ".plist"),
temporaryPlistFilePath = temporaryPlistFile.getAbsolutePath();
temporaryPlistFile.deleteOnExit();
if (exec(["/usr/bin/plutil", "-convert", "xml1", temporaryNibFilePath, "-o", temporaryPlistFilePath]))
{
print("Could not convert to xml plist for file at " + inputFileName);
return;
}
var data = [CPURLConnection sendSynchronousRequest:[CPURLRequest requestWithURL:temporaryPlistFilePath] returningResponse:nil error:nil];
// Minor NSKeyedArchive to CPKeyedArchive conversion.
[data setString:[data string].replace(/\<key\>\s*CF\$UID\s*\<\/key\>/g, "<key>CP$UID</key>")];
// Unarchive the NS data
var unarchiver = [[Nib2CibKeyedUnarchiver alloc] initForReadingWithData:data resourcesFile:resourcesFile],
objectData = [unarchiver decodeObjectForKey:@"IB.objectdata"],
data = [CPData data],
archiver = [[CPKeyedArchiver alloc] initForWritingWithMutableData:data];
// Re-archive the CP data.
[archiver encodeObject:objectData forKey:@"CPCibObjectDataKey"];
[archiver finishEncoding];
var writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName), "UTF-8"));
writer.write([data string]);
writer.close();
}
function readPlist(/*File*/ aFile)
{
var reader = new BufferedReader(new FileReader(aFile)),
fileContents = "";
// Get contents of the file
while (reader.ready())
fileContents += reader.readLine() + '\n';
reader.close();
var data = new objj_data();
data.string = fileContents;
return new CPPropertyListCreateFromData(data);
}
function importFiles(filePaths, aCallback)
{
if (filePaths.length === 0)
aCallback();
else
objj_import(new File(filePaths.shift()).getCanonicalPath(), YES, function() { importFiles(filePaths, aCallback) });
}
function loadFrameworks(frameworkPaths, aCallback)
{
if (frameworkPaths.length === 0)
return aCallback();
var frameworkPath = frameworkPaths.shift(),
infoPlist = new File(frameworkPath + "/Info.plist");
if (!infoPlist.exists())
{
java.lang.System.out.println("'" + frameworkPath + "' is not a framework or could not be found.");
java.lang.System.exit(1);
}
var infoDictionary = readPlist(new File(frameworkPath + "/Info.plist"));
if ([infoDictionary objectForKey:@"CPBundlePackageType"] !== "FMWK")
{
java.lang.System.out.println("'" + frameworkPath + "' is not a framework .");
java.lang.System.exit(1);
}
var files = [infoDictionary objectForKey:@"CPBundleReplacedFiles"],
index = 0,
count = files.length;
for (; index < count; ++index)
files[index] = frameworkPath + '/' + files[index];
importFiles(files, function() { loadFrameworks(frameworkPaths, aCallback) });
}
function main()
{
var count = arguments.length;
if (count < 1)
printUsage();
var index = 0,
inputFileName = nil,
outputFileName = nil,
resourcesPath = nil,
frameworkPaths = [];
for (; index < count; ++index)
{
switch(arguments[index])
{
case "-help":
case "--help": printUsage();
case "-F": frameworkPaths.push(arguments[++index]);
break;
case "-R": resourcesPath = arguments[++index];
break;
default: if (inputFileName && inputFileName.length > 0)
outputFileName = arguments[index];
else
inputFileName = arguments[index];
}
}
if (!outputFileName || outputFileName.length < 1)
outputFileName = cibExtension(inputFileName);
if (frameworkPaths.length)
loadFrameworks(frameworkPaths, function() { convert(inputFileName, outputFileName, resourcesPath); });
else
convert(inputFileName, outputFileName, resourcesPath);
}