/
rb_nibtool
executable file
·194 lines (162 loc) · 5.08 KB
/
rb_nibtool
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
#!/usr/bin/env ruby
# This script is called by Interface Builder. Do not touch it unless you
# really know what you're doing.
#
# This file is covered by the Ruby license.
#
# Copyright (C) 2008-2010, Apple Inc
require 'optparse'
require 'ripper/filter'
framework 'Foundation'
class NibPlistGenerator < Ripper::Filter
DOES_NOTHING =
"This option does nothing, it's there for backward compatibility"
def self.new(argv)
file = nil
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"
opts.on('-p', '--plist', DOES_NOTHING) {}
opts.on('-s', '--sorted-plist', DOES_NOTHING) {}
opts.on('-f', '--file PATH', 'Path to Ruby file') do |_file|
file = case _file
when '' then nil
when '-' then '/dev/stdin'
else _file
end
end
opts.on('-h', '--help', 'Print this message') { opt_error opts }
begin
opts.parse!(argv)
rescue OptionParser::InvalidOption => e
puts e
opt_error opts
end
opt_error opts if file.nil?
end
o = super(File.read(file)); o.parse(nil); o
end
def dump(io)
h = {}
@ib_classes ||= []
h['IBClasses'] = @ib_classes.map do |k|
{ 'CLASS' => k.name,
'LANGUAGE' => 'Ruby',
'OUTLETS' => k.outlets,
'ACTIONS' => k.actions,
'SUPERCLASS' => k.super }
end
unless d = NSPropertyListSerialization.dataFromPropertyList(h,
format:NSPropertyListXMLFormat_v1_0, errorDescription:nil)
$stderr.puts "Cannot serialize #{h} as a plist"
exit 1
end
io.write NSString.alloc.initWithData(d, encoding:NSUTF8StringEncoding)
end
private
def self.opt_error(opts); puts opts; exit 1; end
class IBClass
attr_accessor :name, :super, :outlets, :actions
def initialize(name)
@name = name
@super = 'NSObject'
@outlets = []
@actions = []
end
end
def on_default(event, token, data)
@token_n ||= 0
return token if event == :on_sp
p @token_n, event, token, data, '---' if $DEBUG
if event == :on_kw
if token == 'class'
@def_class = true
elsif token == 'def'
@def_method = true
end
elsif event == :on_const
if @def_class
@def_class = nil
@waiting_for_superclass = @token_n
@ib_classes ||= []
if k = @ib_classes.find { |x| x.name == token }
@current_class = k
else
@ib_classes << @current_class = IBClass.new(token)
end
elsif @waiting_for_superclass
raise "current_class undefined" unless @current_class
@current_class.super = token
end
elsif event == :on_ident
if token == 'attr_accessor' or token == 'attr_writer' \
or token == 'ib_outlet' or token == 'ib_outlets' \
or token == 'ns_outlet' or token == 'ns_outlets'
@waiting_for_outlet = @token_n
elsif token == 'ib_action'
@waiting_for_action = @token_n
elsif @waiting_for_outlet
raise "current_class undefined" unless @current_class
ary = @current_class.outlets
ary << token unless ary.include?(token)
@waiting_for_outlet += 1 # maybe another outlet is coming...
elsif @waiting_for_action
raise "current_class undefined" unless @current_class
ary = @current_class.actions
ary << token unless ary.include?(token)
elsif @def_method
@def_method = nil
@current_method = token
@waiting_for_method_end = @token_n
@on_def_method_paren = 0
end
end
if @waiting_for_superclass and @token_n != @waiting_for_superclass
if event == :on_op and token == '<'
@waiting_for_superclass += 1
else
@waiting_for_superclass = nil
end
end
if @waiting_for_outlet and @token_n != @waiting_for_outlet
if (event == :on_symbeg and token == ':') \
or (event == :on_comma and token == ',')
@waiting_for_outlet += 1
else
@waiting_for_outlet = nil
end
end
if @waiting_for_action and @token_n != @waiting_for_action
if (event == :on_symbeg and token == ':')
@waiting_for_action += 1
else
@waiting_for_action = nil
end
end
if @waiting_for_method_end and @token_n != @waiting_for_method_end
if event == :on_lparen
@on_def_method_paren += 1
@waiting_for_method_end += 1
elsif event == :on_rparen
@on_def_method_paren -= 1
@waiting_for_method_end += 1
elsif event == :on_ident and token == 'sender'
@waiting_for_method_end += 1
@has_sender_arg = true
elsif @on_def_method_paren == 0 \
and @has_sender_arg \
and (event == :on_nl or event == :on_ignored_nl \
or event == :on_semicolon or event == :on_comment)
ary = @current_class.actions
ary << @current_method unless ary.include?(@current_method)
@waiting_for_method_end = nil
@has_sender_arg = nil
else
@waiting_for_method_end = nil
end
end
@token_n += 1
token
end
end
gen = NibPlistGenerator.new(ARGV)
gen.dump(STDOUT)