forked from ruboto/ruboto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
144 lines (125 loc) · 5.29 KB
/
Rakefile
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
task :default => :gem
task :gem do
`gem build ruboto-core.gemspec`
end
task :release do
`gem push #{Dir['ruboto-core-*.gem'][-1]}`
end
require 'erb'
def unprefixed_class(class_name)
/\.([^\.]+)\z/.match(class_name)[1]
end
# active_support/inflector.rb
def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
def transform_return_type(type)
if type.include?(".")
return type
elsif type == "int"
return "Integer"
else
return type.capitalize
end
end
task :generate_java_classes do
all_callbacks = eval(IO.read("lib/java_class_gen/interfaces.txt"))
@starting_methods = {"BroadcastReceiver" => "onReceive"}
@starting_methods.default = "onCreate"
all_callbacks.each do |full_class, method_hash|
@class = unprefixed_class full_class
@callbacks = method_hash
@full_class = full_class
@first_method = @starting_methods[@class]
##############################################################################################
#
# This code resolves any issues with the generated callbacks.
#
# 1) Remove callbacks that are hard coded in RubotoActivity.erb:
#
if @class == "Activity"
@callbacks["android.view.View$OnCreateContextMenuListener"].delete("onCreateContextMenu")
#
# 2) Remove callbacks that are causing a problem
#
@callbacks["android.app.Activity"].delete("onRetainNonConfigurationChildInstances")
#
# 3) Override the callback constant for a few key callbacks
#
@callbacks["android.app.Activity"]["onMenuItemSelected"]["constant"] = "CB_CREATE_OPTIONS_MENU"
@callbacks["android.app.Activity"]["onContextItemSelected"]["constant"] = "CB_CREATE_CONTEXT_MENU"
#
# 4) Create a unique name for callbacks that have duplicate names
#
@callbacks["android.content.DialogInterface$OnClickListener"]["onClick"]["ruby_method"] = "on_dialog_click"
@callbacks["android.content.DialogInterface$OnClickListener"]["onClick"]["constant"] = "CB_DIALOG_CLICK"
@callbacks["android.content.DialogInterface$OnKeyListener"]["onKey"]["ruby_method"] = "on_dialog_key"
@callbacks["android.content.DialogInterface$OnKeyListener"]["onKey"]["constant"] = "CB_DIALOG_KEY"
@callbacks["android.content.DialogInterface$OnMultiChoiceClickListener"]["onClick"]["ruby_method"] = "on_dialog_multi_choice_click"
@callbacks["android.content.DialogInterface$OnMultiChoiceClickListener"]["onClick"]["constant"] = "CB_DIALOG_MULTI_CHOICE_CLICK"
#
# 5) Report any duplicate name callbacks not handled
#
callbacks = {}
@callbacks.each do |interface,i_info|
i_info.each do |method,v|
if callbacks[method] and not v["ruby_method"]
puts "#{method} in #{interface} and #{callbacks[method]}"
else
callbacks[v["ruby_method"] || method] = interface
end
end
end
#
# 6) Create a few new special case callbacks
#
@callbacks["none"] = {
"onDraw" => {"args" => ["android.view.View", "android.graphics.Canvas"]},
"onSizeChanged" => {"args" => ["android.view.View", "int", "int", "int", "int"]}
}
end
#
##############################################################################################
#
# This code takes the callbacks hash (read out of the interfaces.txt file) and prepares
# it for use in the code below.
#
@implements = []
@constants = []
@callbacks.each do |interface,i_info|
i_info.each do |method,v|
v["interface"] = interface.gsub("$", ".")
v["interface"] = "Activity" if v["interface"] == "android.app.Activity"
v["method"] = method
v["return_type"] = (v["return_type"] || "void").gsub("$", ".")
v["interface_method"] = v["interface_method"] || v["method"]
v["ruby_method"] = v["ruby_method"] || v["method"].gsub(/[A-Z]/) {|i| "_#{i.downcase}"}
@implements << v["interface"] if v["interface"] != full_class and
v["interface"] != @class and
v["interface"] != "none" and
not @implements.include?(v["interface"])
unless v["constant"]
constant = v["method"].gsub(/[A-Z]/) {|i| "_#{i}"}.upcase
constant = constant[3..-1] if constant[0..2] == "ON_"
v["constant"] = "CB_#{constant}"
end
@constants << v["constant"] unless @constants.include?(v["constant"]) || v["method"] == @first_method
v["args"] = (v["args"] || [])
v["args_with_types"], v["args_alone"] = [], []
v["args"].each_with_index {|arg_type, i| v["args_with_types"] << "#{arg_type.gsub("$", ".")} arg#{i}"; v["args_alone"] << "arg#{i}"}
v["args_with_types"] = v["args_with_types"].join(", ")
end
end
##############################################################################################
File.open("assets/src/Inheriting#{@class}.java", "w") do |file|
file.write ERB.new(IO.read("lib/java_class_gen/InheritingClass.java.erb"), 0, "%").result
end
File.open("assets/src/org/ruboto/Ruboto#{@class}.java", "w") do |file|
file.write ERB.new(IO.read("lib/java_class_gen/RubotoClass.java.erb"), 0, "%").result
end
end
end