Created
May 8, 2012 18:47
-
-
Save andreif/2638425 to your computer and use it in GitHub Desktop.
bridgesupport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "hpricot" | |
require "fileutils" | |
module BridgeSupport | |
class Parser | |
def initialize(path_from, path_to) | |
@doc = open(path_from) {|f| Hpricot(f).search('signatures').first} | |
if @doc and @doc.children | |
FileUtils.mkdir_p File.dirname(path_to) | |
o = open(path_to, 'w') | |
o.write(@doc.children.collect do |el| | |
next if el.is_a?(Hpricot::Text) | |
case el.name | |
when 'class' then ClassDef.new(el).write('%s/%s.rb' % [File.dirname(path_to),el.attributes['name']]) | |
when 'function' then FunctionDef.new(el).to_s | |
when 'enum' then EnumDef.new(el).to_s | |
when 'constant' then ConstantDef.new(el).to_s | |
when 'struct' then StructDef.new(el).to_s | |
else BaseDef.new(el).to_s | |
end | |
end.compact.join("\n\n")) | |
o.close | |
end | |
end | |
end | |
class BaseDef | |
def initialize(el) | |
@el = el | |
end | |
def to_s | |
'# ' + @el.to_s.gsub("\n","\n# ") | |
end | |
def attr(key) | |
@el.attributes[key] | |
end | |
def indent(str, n=2) | |
n = ' '*n if n.is_a? Fixnum | |
str = str.join("\n\n") if str.is_a? Array | |
n + str.gsub("\n", "\n"+n) | |
end | |
def result(s, default=nil) | |
case s | |
when "NSString*" then "NSString.new" | |
when 'void' then "nil" | |
when 'int' then '0' | |
when '_Bool', 'BOOL' then 'true' | |
else default or s.gsub(/\*+$/,'') | |
end | |
end | |
end | |
class StructDef < BaseDef | |
def name | |
attr('name') | |
end | |
def to_s | |
"class #{name}\n%s\n\n%s\nend" % [indent(fields.join("\n")), indent(super)] | |
end | |
def fields | |
@el.search('field').collect do |c| | |
attr = c.attributes | |
'def %s; %s end # %s' % [attr['name'], result(attr['declared_type']), c.to_s] | |
end | |
end | |
end | |
class ClassDef < BaseDef | |
def to_s | |
"class %s\n%s\n\n%s\nend" % [attr('name'), indent(methods_def), indent(super)] | |
end | |
def parent | |
" < NSObject" | |
end | |
def methods_def | |
@el.children.collect do |c| | |
if c.name == 'method' | |
BridgeSupport::MethodDef.new(c).to_s | |
end | |
end.compact | |
end | |
def write(path) | |
FileUtils.mkdir_p(File.dirname(path)) | |
open(path,'w') {|f| f.write(to_s)} | |
end | |
end | |
class FunctionDef < BaseDef | |
def retval | |
@el.search('retval').first.attributes | |
end | |
def retval_s | |
"%s [ %s ]" % [retval['declared_type'], retval['type']] | |
end | |
def result | |
super(retval['declared_type']) | |
end | |
def to_s | |
r = result.empty? ? "" : " %s\n" % result | |
"def #{name}(%s) #=> #{retval_s}\n#{r}%s\nend" % [args.join(', '), indent(super)] | |
end | |
def name | |
attr('name') | |
end | |
def args | |
@el.children.collect do |c| | |
c.attributes['name'] if c.name == 'arg' | |
end.compact | |
end | |
end | |
class MethodDef < FunctionDef | |
def to_s | |
arg = args.join(', ') | |
r = result.empty? ? "" : "; %s" % result | |
"def self.#{name}(#{arg})#{r} end\n" + super | |
end | |
def messages | |
attr('selector')[0..-1].split(':') | |
end | |
def name | |
messages.first | |
end | |
def args | |
msg = messages | |
arg = @el.children.collect do |c| | |
c.attributes['name'] if c.name == 'arg' | |
end.compact | |
arg.collect.with_index do |a,i| | |
if i > 0 and msg and msg[i] | |
"%s:%s" % [msg[i], a] | |
else | |
a | |
end | |
end | |
end | |
end | |
class EnumDef < BaseDef | |
def to_s | |
"%s = %s\n%s" % [attr('name'), attr('value'), super] | |
end | |
end | |
class ConstantDef < BaseDef | |
def to_s | |
"%s = %s\n%s" % [attr('name'), result(attr('declared_type')), super] | |
end | |
end | |
end | |
home_dir = '/Users/andrei/DropboxAndrei/iOS' | |
source_dirs = [ | |
#'/Library/RubyMotion/data/5.1/BridgeSupport', | |
"#{home_dir}/cocos2d-iphone", | |
#"#{home_dir}/Examples/cocosmotion/vendor/cocos2d-iphone", | |
] | |
destination_dir = "#{home_dir}/BridgeSupport" | |
source_dirs.each do |source_dir| | |
p source_dir | |
Dir[source_dir + '/*.bridgesupport'].each do |f| | |
p f | |
BridgeSupport::Parser.new(f, destination_dir + '/%s/framework.rb' % File.basename(f,'.bridgesupport')) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment