280north / cappuccino

Web Application Framework in JavaScript and Objective-J

This URL has Read+Write access

boucher (author)
Mon Nov 02 16:47:07 -0800 2009
commit  93c90274b5376b5c7ef0c9dea0c1ffcec4499ab7
tree    c1e8d95726cd11b873a89fc143ea104a0f436c03
parent  e8b1ff88461f8b4eb303e5435079f00b40159af2
cappuccino / Tools / nib2cib / Converter.j
100644 161 lines (121 sloc) 5.193 kb
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
/*
* Converter.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/CPObject.j>
@import <Foundation/CPData.j>
 
var File = require("file"),
    popen = require("os").popen;
 
 
NibFormatUndetermined = 0,
NibFormatMac = 1,
NibFormatIPhone = 2;
 
ConverterConversionException = @"ConverterConversionException";
 
@implementation Converter : CPObject
{
    NibFormat format @accessors;
    CPString inputPath @accessors;
    CPString outputPath @accessors;
    CPString resourcesPath @accessors;
}
 
- (id)init
{
    self = [super init];
 
    if (self)
        [self setFormat:NibFormatUndetermined];
 
    return self;
}
 
- (void)convert
{
    try
    {
        if ([resourcesPath length] && !File.isReadable(resourcesPath))
            [CPException raise:ConverterConversionException reason:@"Could not read Resources at path \"" + resourcesPath + "\""];
 
        var stringContents = File.read(inputPath, { charset:"UTF-8" }),
            inferredFormat = format;
 
        if (inferredFormat === NibFormatUndetermined)
        {
            // Assume its a Mac file.
            inferredFormat = NibFormatMac;
 
            // Some .xibs are iPhone nibs, check the actual contents in this case.
            if (File.extname(inputPath) !== ".nib" &&
                stringContents.indexOf("<archive type=\"com.apple.InterfaceBuilder3.CocoaTouch.XIB\"") !== -1)
                inferredFormat = NibFormatIPhone;
 
            if (inferredFormat === NibFormatMac)
                CPLog("Auto-detected Cocoa Nib or Xib File");
            else
                CPLog("Auto-detected CocoaTouch Xib File");
        }
 
        var nibData = [self CPCompliantNibDataAtFilePath:inputPath];
 
        if (inferredFormat === NibFormatMac)
            var convertedData = [self convertedDataFromMacData:nibData resourcesPath:resourcesPath];
        else
            [CPException raise:ConverterConversionException reason:@"nib2cib does not understand this nib format."];
 
        if (![outputPath length])
            outputPath = cibExtension(inputPath);
 
        File.write(outputPath, [convertedData string], { charset:"UTF-8" });
    }
    catch(anException)
    {
        print(anException);
    }
}
 
- (CPData)CPCompliantNibDataAtFilePath:(CPString)aFilePath
{
    // Compile xib or nib to make sure we have a non-new format nib.
    var temporaryNibFilePath = File.createTempFile("temp", ".nib", true);
 
    if (popen("/usr/bin/ibtool " + aFilePath + " --compile " + temporaryNibFilePath).wait() === 1)
        throw "Could not compile file at " + aFilePath;
 
    // Convert from binary plist to XML plist
    var temporaryPlistFilePath = File.createTempFile("temp", ".plist", true);
 
    if (popen("/usr/bin/plutil " + " -convert xml1 " + temporaryNibFilePath + " -o " + temporaryPlistFilePath).wait() === 1)
        throw "Could not convert to xml plist for file at " + aFilePath;
 
    if (!File.isReadable(temporaryPlistFilePath))
        [CPException raise:ConverterConversionException reason:@"Unable to convert nib file."];
 
    var plistContents = File.read(temporaryPlistFilePath, { charset:"UTF-8" });
 
    // Minor NS keyed archive to CP keyed archive conversion.
    // Use Java directly because rhino's string.replace is *so slow*. 4 seconds vs. 1 millisecond.
    // plistContents = plistContents.replace(/\<key\>\s*CF\$UID\s*\<\/key\>/g, "<key>CP$UID</key>");
    plistContents = String(java.lang.String(plistContents).replaceAll("\\<key\\>\\s*CF\\$UID\\s*\\<\/key\\>", "<key>CP\\$UID</key>"));
 
    return [CPData dataWithString:plistContents];
}
 
@end
 
File.createTempFile = function(aName, anExtension, shouldDeleteOnExit)
{
    var tempFile = Packages.java.io.File.createTempFile(aName, anExtension),
        tempFilePath = String(tempFile.getAbsolutePath());
 
    if (shouldDeleteOnExit)
        tempFile.deleteOnExit();
 
    return String(tempFilePath);
}
 
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";
}
 
@import "Converter+Mac.j"