ruboto 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +35 -11
- data/assets/rakelib/ruboto.rake +2 -0
- data/assets/samples/sample_activity.rb +1 -0
- data/assets/samples/sample_class.rb +2 -0
- data/assets/samples/sample_class_test.rb +1 -0
- data/assets/src/InheritingActivity.java +1 -1
- data/assets/src/InheritingBroadcastReceiver.java +2 -1
- data/assets/src/InheritingClass.java +15 -7
- data/assets/src/InheritingService.java +1 -2
- data/assets/src/RubotoActivity.java +20 -110
- data/assets/src/RubotoBroadcastReceiver.java +16 -62
- data/assets/src/RubotoService.java +13 -89
- data/assets/src/org/ruboto/EntryPointActivity.java +1 -1
- data/assets/src/org/ruboto/JRubyAdapter.java +23 -23
- data/assets/src/org/ruboto/RubotoComponent.java +8 -0
- data/assets/src/org/ruboto/Script.java +10 -5
- data/assets/src/org/ruboto/ScriptInfo.java +53 -0
- data/assets/src/org/ruboto/ScriptLoader.java +143 -0
- data/assets/src/org/ruboto/test/ActivityTest.java +15 -8
- data/assets/src/org/ruboto/test/InstrumentationTestRunner.java +11 -6
- data/assets/src/ruboto/activity.rb +43 -17
- data/assets/src/ruboto/base.rb +7 -1
- data/assets/src/ruboto/generate.rb +6 -2
- data/assets/src/ruboto/legacy.rb +1 -1
- data/assets/src/ruboto/menu.rb +4 -4
- data/assets/src/ruboto/widget.rb +4 -11
- data/lib/ruboto/commands/base.rb +21 -7
- data/lib/ruboto/util/build.rb +6 -3
- data/lib/ruboto/util/code_formatting.rb +2 -2
- data/lib/ruboto/util/update.rb +58 -58
- data/lib/ruboto/util/xml_element.rb +14 -11
- data/lib/ruboto/version.rb +2 -1
- data/test/activity/generate_activity.rb +35 -0
- data/test/activity/generate_activity_test.rb +17 -0
- data/test/activity/image_button_activity.rb +1 -0
- data/test/activity/image_button_and_button_activity.rb +1 -0
- data/test/activity/margins_activity.rb +1 -0
- data/test/activity/navigation_activity.rb +20 -5
- data/test/activity/navigation_activity_test.rb +43 -4
- data/test/activity/option_menu_activity.rb +1 -0
- data/test/activity/psych_activity.rb +2 -1
- data/test/activity/stack_activity.rb +1 -0
- data/test/activity/stack_activity_test.rb +11 -5
- data/test/block_def_activity/margins_activity.rb +2 -1
- data/test/block_def_activity/psych_activity.rb +1 -1
- data/test/block_def_activity/stack_activity_test.rb +1 -1
- data/test/handle_activity/psych_activity.rb +1 -1
- data/test/handle_activity/stack_activity_test.rb +17 -9
- data/test/minimal_app_test.rb +2 -2
- data/test/rake_test.rb +1 -1
- data/test/ruboto_gen_test.rb +162 -34
- data/test/service_test.rb +1 -2
- data/test/sqldroid_test.rb +87 -0
- data/test/test_helper.rb +19 -12
- data/test/update_test_methods.rb +29 -44
- data/test/view_constants_test.rb +1 -2
- metadata +12 -4
@@ -1,23 +1,16 @@
|
|
1
1
|
package THE_PACKAGE;
|
2
2
|
|
3
3
|
import org.ruboto.Script;
|
4
|
+
import org.ruboto.ScriptLoader;
|
4
5
|
import java.io.IOException;
|
5
6
|
|
6
7
|
public class THE_RUBOTO_CLASS THE_ACTION THE_ANDROID_CLASS {
|
7
8
|
THE_CONSTANTS
|
8
9
|
|
9
|
-
private
|
10
|
-
private String scriptName;
|
11
|
-
private Object rubyInstance;
|
12
|
-
private Object[] callbackProcs = new Object[CONSTANTS_COUNT];
|
13
|
-
public Object[] args;
|
10
|
+
private final ScriptInfo scriptInfo = new ScriptInfo(CONSTANTS_COUNT);
|
14
11
|
|
15
|
-
public
|
16
|
-
|
17
|
-
}
|
18
|
-
|
19
|
-
public void setScriptName(String name){
|
20
|
-
scriptName = name;
|
12
|
+
public ScriptInfo getScriptInfo() {
|
13
|
+
return scriptInfo;
|
21
14
|
}
|
22
15
|
|
23
16
|
/****************************************************************************************
|
@@ -25,96 +18,27 @@ THE_CONSTANTS
|
|
25
18
|
* Service Lifecycle: onCreate
|
26
19
|
*/
|
27
20
|
|
21
|
+
// FIXME(uwe): Only used for block based primary activities. Remove if we remove support for such.
|
22
|
+
public void onCreateSuper() {
|
23
|
+
super.onCreate();
|
24
|
+
}
|
25
|
+
|
28
26
|
@Override
|
29
27
|
public void onCreate() {
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
if (e.getClassName().equals("java.lang.reflect.Method") && e.getMethodName().equals("invokeNative")) {
|
34
|
-
return;
|
35
|
-
}
|
36
|
-
if (e.getClassName().equals("android.app.ActivityThread") && e.getMethodName().equals("handleCreateService")) {
|
37
|
-
break;
|
38
|
-
}
|
28
|
+
if (ScriptLoader.isCalledFromJRuby()) {
|
29
|
+
super.onCreate();
|
30
|
+
return;
|
39
31
|
}
|
40
|
-
|
41
32
|
System.out.println("RubotoService.onCreate()");
|
42
|
-
args = new Object[0];
|
43
|
-
|
44
|
-
super.onCreate();
|
45
33
|
|
46
34
|
if (JRubyAdapter.setUpJRuby(this)) {
|
47
|
-
rubyInstance = this;
|
48
|
-
|
49
35
|
// TODO(uwe): Only needed for non-class-based definitions
|
50
36
|
// Can be removed if we stop supporting non-class-based definitions
|
51
37
|
JRubyAdapter.defineGlobalVariable("$context", this);
|
52
38
|
JRubyAdapter.defineGlobalVariable("$service", this);
|
53
39
|
// TODO end
|
54
40
|
|
55
|
-
|
56
|
-
if (scriptName != null) {
|
57
|
-
String rubyClassName = Script.toCamelCase(scriptName);
|
58
|
-
System.out.println("Looking for Ruby class: " + rubyClassName);
|
59
|
-
Object rubyClass = null;
|
60
|
-
String script = new Script(scriptName).getContents();
|
61
|
-
if (script.matches("(?s).*class " + rubyClassName + ".*")) {
|
62
|
-
if (!rubyClassName.equals(getClass().getSimpleName())) {
|
63
|
-
System.out.println("Script defines methods on meta class");
|
64
|
-
// FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7
|
65
|
-
if (JRubyAdapter.isJRubyPreOneSeven() || JRubyAdapter.isRubyOneEight()) {
|
66
|
-
JRubyAdapter.put("$java_instance", this);
|
67
|
-
JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("class << $java_instance; self; end"));
|
68
|
-
} else if (JRubyAdapter.isJRubyOneSeven() && JRubyAdapter.isRubyOneNine()) {
|
69
|
-
JRubyAdapter.put(rubyClassName, JRubyAdapter.runRubyMethod(this, "singleton_class"));
|
70
|
-
} else {
|
71
|
-
throw new RuntimeException("Unknown JRuby/Ruby version: " + JRubyAdapter.get("JRUBY_VERSION") + "/" + JRubyAdapter.get("RUBY_VERSION"));
|
72
|
-
}
|
73
|
-
}
|
74
|
-
} else {
|
75
|
-
rubyClass = JRubyAdapter.get(rubyClassName);
|
76
|
-
}
|
77
|
-
if (rubyClass == null) {
|
78
|
-
System.out.println("Loading script: " + scriptName);
|
79
|
-
if (script.matches("(?s).*class " + rubyClassName + ".*")) {
|
80
|
-
System.out.println("Script contains class definition");
|
81
|
-
if (rubyClassName.equals(getClass().getSimpleName())) {
|
82
|
-
System.out.println("Script has separate Java class");
|
83
|
-
JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("Java::" + getClass().getName()));
|
84
|
-
}
|
85
|
-
// System.out.println("Set class: " + JRubyAdapter.get(rubyClassName));
|
86
|
-
}
|
87
|
-
JRubyAdapter.setScriptFilename(scriptName);
|
88
|
-
JRubyAdapter.runScriptlet(script);
|
89
|
-
rubyClass = JRubyAdapter.get(rubyClassName);
|
90
|
-
}
|
91
|
-
if (rubyClass != null) {
|
92
|
-
System.out.println("Call on_create on: " + this);
|
93
|
-
// FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7
|
94
|
-
if (JRubyAdapter.isJRubyPreOneSeven()) {
|
95
|
-
JRubyAdapter.put("$ruby_instance", this);
|
96
|
-
JRubyAdapter.runScriptlet("$ruby_instance.on_create");
|
97
|
-
} else if (JRubyAdapter.isJRubyOneSeven()) {
|
98
|
-
JRubyAdapter.runRubyMethod(this, "on_create");
|
99
|
-
} else {
|
100
|
-
throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION"));
|
101
|
-
}
|
102
|
-
}
|
103
|
-
} else {
|
104
|
-
// FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7
|
105
|
-
if (JRubyAdapter.isJRubyPreOneSeven()) {
|
106
|
-
JRubyAdapter.runScriptlet("$service.initialize_ruboto");
|
107
|
-
JRubyAdapter.runScriptlet("$service.on_create");
|
108
|
-
} else if (JRubyAdapter.isJRubyOneSeven()) {
|
109
|
-
JRubyAdapter.runRubyMethod(this, "initialize_ruboto");
|
110
|
-
JRubyAdapter.runRubyMethod(this, "on_create", args[0]);
|
111
|
-
} else {
|
112
|
-
throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION"));
|
113
|
-
}
|
114
|
-
}
|
115
|
-
} catch(IOException e) {
|
116
|
-
e.printStackTrace();
|
117
|
-
}
|
41
|
+
ScriptLoader.loadScript(this);
|
118
42
|
} else {
|
119
43
|
// FIXME(uwe): What to do if the Ruboto Core platform cannot be found?
|
120
44
|
}
|
@@ -150,7 +150,7 @@ public class EntryPointActivity extends org.ruboto.RubotoActivity {
|
|
150
150
|
if(appStarted) return;
|
151
151
|
appStarted = true;
|
152
152
|
Log.i("Starting activity");
|
153
|
-
loadScript();
|
153
|
+
ScriptLoader.loadScript(this, args[0]);
|
154
154
|
onStart();
|
155
155
|
super.onResume();
|
156
156
|
hideProgress();
|
@@ -49,7 +49,7 @@ public class JRubyAdapter {
|
|
49
49
|
*/
|
50
50
|
@SuppressWarnings("unchecked")
|
51
51
|
@Deprecated public static <T> T callMethod(Object receiver, String methodName, Object[] args, Class<T> returnType) {
|
52
|
-
return runRubyMethod(returnType, receiver, methodName, args);
|
52
|
+
return (T) runRubyMethod(returnType, receiver, methodName, args);
|
53
53
|
}
|
54
54
|
|
55
55
|
/**
|
@@ -57,7 +57,7 @@ public class JRubyAdapter {
|
|
57
57
|
*/
|
58
58
|
@SuppressWarnings("unchecked")
|
59
59
|
@Deprecated public static <T> T callMethod(Object receiver, String methodName, Object arg, Class<T> returnType) {
|
60
|
-
return runRubyMethod(returnType, receiver, methodName, arg);
|
60
|
+
return (T) runRubyMethod(returnType, receiver, methodName, arg);
|
61
61
|
}
|
62
62
|
|
63
63
|
/**
|
@@ -65,7 +65,7 @@ public class JRubyAdapter {
|
|
65
65
|
*/
|
66
66
|
@SuppressWarnings("unchecked")
|
67
67
|
@Deprecated public static <T> T callMethod(Object receiver, String methodName, Class<T> returnType) {
|
68
|
-
return runRubyMethod(returnType, receiver, methodName);
|
68
|
+
return (T) runRubyMethod(returnType, receiver, methodName);
|
69
69
|
}
|
70
70
|
|
71
71
|
/**
|
@@ -106,7 +106,7 @@ public class JRubyAdapter {
|
|
106
106
|
* @deprecated As of Ruboto 0.7.0, replaced by {@link #runScriptlet(String code)}
|
107
107
|
*/
|
108
108
|
@Deprecated public static String execute(String code) {
|
109
|
-
return runRubyMethod(String.class, exec(code), "inspect");
|
109
|
+
return (String) runRubyMethod(String.class, exec(code), "inspect");
|
110
110
|
}
|
111
111
|
|
112
112
|
public static Object get(String name) {
|
@@ -127,7 +127,7 @@ public class JRubyAdapter {
|
|
127
127
|
}
|
128
128
|
|
129
129
|
public static String getScriptFilename() {
|
130
|
-
return callScriptingContainerMethod(String.class, "getScriptFilename");
|
130
|
+
return (String) callScriptingContainerMethod(String.class, "getScriptFilename");
|
131
131
|
}
|
132
132
|
|
133
133
|
public static Object runRubyMethod(Object receiver, String methodName, Object... args) {
|
@@ -163,9 +163,15 @@ public class JRubyAdapter {
|
|
163
163
|
@SuppressWarnings("unchecked")
|
164
164
|
public static <T> T runRubyMethod(Class<T> returnType, Object receiver, String methodName, Object... args) {
|
165
165
|
try {
|
166
|
+
// FIXME(uwe): Simplify when we stop supporting JRuby < 1.7.0
|
166
167
|
if (isJRubyPreOneSeven()) {
|
167
|
-
|
168
|
-
|
168
|
+
if (args.length == 0) {
|
169
|
+
Method m = ruby.getClass().getMethod("callMethod", Object.class, String.class, Class.class);
|
170
|
+
return (T) m.invoke(ruby, receiver, methodName, returnType);
|
171
|
+
} else {
|
172
|
+
Method m = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class, Class.class);
|
173
|
+
return (T) m.invoke(ruby, receiver, methodName, args, returnType);
|
174
|
+
}
|
169
175
|
} else {
|
170
176
|
Method m = ruby.getClass().getMethod("runRubyMethod", Class.class, Object.class, String.class, Object[].class);
|
171
177
|
return (T) m.invoke(ruby, returnType, receiver, methodName, args);
|
@@ -218,7 +224,7 @@ public class JRubyAdapter {
|
|
218
224
|
}
|
219
225
|
}
|
220
226
|
|
221
|
-
public static
|
227
|
+
public static boolean setUpJRuby(Context appContext) {
|
222
228
|
return setUpJRuby(appContext, output == null ? System.out : output);
|
223
229
|
}
|
224
230
|
|
@@ -232,19 +238,21 @@ public class JRubyAdapter {
|
|
232
238
|
// END Ruboto HeapAlloc
|
233
239
|
setDebugBuild(appContext);
|
234
240
|
Log.d("Setting up JRuby runtime (" + (isDebugBuild ? "DEBUG" : "RELEASE") + ")");
|
241
|
+
System.setProperty("jruby.compile.mode", "OFF"); // OFF OFFIR
|
235
242
|
System.setProperty("jruby.bytecode.version", "1.6");
|
236
243
|
System.setProperty("jruby.interfaces.useProxy", "true");
|
237
244
|
System.setProperty("jruby.management.enabled", "false");
|
238
245
|
System.setProperty("jruby.objectspace.enabled", "false");
|
239
246
|
System.setProperty("jruby.thread.pooling", "true");
|
240
247
|
System.setProperty("jruby.native.enabled", "false");
|
241
|
-
// System.setProperty("jruby.compat.version", "RUBY1_8"); // RUBY1_9 is the default
|
248
|
+
// System.setProperty("jruby.compat.version", "RUBY1_8"); // RUBY1_9 is the default in JRuby 1.7
|
249
|
+
System.setProperty("jruby.ir.passes", "LocalOptimizationPass,DeadCodeElimination");
|
250
|
+
System.setProperty("jruby.backtrace.style", "normal"); // normal raw full mri
|
242
251
|
|
243
|
-
// Uncomment these to debug Ruby source loading
|
252
|
+
// Uncomment these to debug/profile Ruby source loading
|
244
253
|
// System.setProperty("jruby.debug.loadService", "true");
|
245
254
|
// System.setProperty("jruby.debug.loadService.timing", "true");
|
246
255
|
|
247
|
-
|
248
256
|
ClassLoader classLoader;
|
249
257
|
Class<?> scriptingContainerClass;
|
250
258
|
String apkName = null;
|
@@ -263,10 +271,10 @@ public class JRubyAdapter {
|
|
263
271
|
apkName = pkgInfo.applicationInfo.sourceDir;
|
264
272
|
RUBOTO_CORE_VERSION_NAME = pkgInfo.versionName;
|
265
273
|
} catch (PackageManager.NameNotFoundException e2) {
|
266
|
-
out.println("JRuby not found in local APK:");
|
267
|
-
e1.printStackTrace(
|
268
|
-
out.println("JRuby not found in platform APK:");
|
269
|
-
e2.printStackTrace(
|
274
|
+
System.out.println("JRuby not found in local APK:");
|
275
|
+
e1.printStackTrace();
|
276
|
+
System.out.println("JRuby not found in platform APK:");
|
277
|
+
e2.printStackTrace();
|
270
278
|
return false;
|
271
279
|
}
|
272
280
|
|
@@ -291,14 +299,6 @@ public class JRubyAdapter {
|
|
291
299
|
.newInstance(Enum.valueOf(scopeClass, localContextScope),
|
292
300
|
Enum.valueOf(behaviorClass, localVariableBehavior));
|
293
301
|
|
294
|
-
Class compileModeClass = Class.forName("org.jruby.RubyInstanceConfig$CompileMode", true, classLoader);
|
295
|
-
callScriptingContainerMethod(Void.class, "setCompileMode", Enum.valueOf(compileModeClass, "OFF"));
|
296
|
-
|
297
|
-
// Class traceTypeClass = Class.forName("org.jruby.runtime.backtrace.TraceType", true, classLoader);
|
298
|
-
// Method traceTypeForMethod = traceTypeClass.getMethod("traceTypeFor", String.class);
|
299
|
-
// Object traceTypeRaw = traceTypeForMethod.invoke(null, "raw");
|
300
|
-
// callScriptingContainerMethod(Void.class, "setTraceType", traceTypeRaw);
|
301
|
-
|
302
302
|
// FIXME(uwe): Write tutorial on profiling.
|
303
303
|
// container.getProvider().getRubyInstanceConfig().setProfilingMode(mode);
|
304
304
|
|
@@ -31,6 +31,7 @@ public class Script {
|
|
31
31
|
public static String toCamelCase(String s) {
|
32
32
|
String[] parts = s.replace(".rb", "").split("_");
|
33
33
|
for (int i = 0 ; i < parts.length ; i++) {
|
34
|
+
if (parts[i].length() == 0) continue;
|
34
35
|
parts[i] = parts[i].substring(0,1).toUpperCase() + parts[i].substring(1);
|
35
36
|
}
|
36
37
|
return java.util.Arrays.toString(parts).replace(", ", "").replaceAll("[\\[\\]]", "");
|
@@ -44,7 +45,7 @@ public class Script {
|
|
44
45
|
"(?<=[A-Za-z])(?=[^A-Za-z])"
|
45
46
|
),
|
46
47
|
"_"
|
47
|
-
).toLowerCase();
|
48
|
+
).replace("__", "_").toLowerCase();
|
48
49
|
}
|
49
50
|
|
50
51
|
// Private static methods
|
@@ -109,7 +110,7 @@ public class Script {
|
|
109
110
|
boolean exists() {
|
110
111
|
for (String dir : scriptsDir) {
|
111
112
|
System.out.println("Checking file: " + dir + "/" + name);
|
112
|
-
if (new File(
|
113
|
+
if (new File(dir + "/" + name).exists()) {
|
113
114
|
return true;
|
114
115
|
}
|
115
116
|
}
|
@@ -132,9 +133,13 @@ public class Script {
|
|
132
133
|
InputStream is = null;
|
133
134
|
BufferedReader buffer = null;
|
134
135
|
try {
|
135
|
-
|
136
|
-
|
137
|
-
|
136
|
+
for (String dir : scriptsDir) {
|
137
|
+
System.out.println("Checking file: " + dir + "/" + name);
|
138
|
+
if (new File(dir + "/" + name).exists()) {
|
139
|
+
is = new java.io.FileInputStream(dir + "/" + name);
|
140
|
+
}
|
141
|
+
}
|
142
|
+
if (is == null) {
|
138
143
|
is = getClass().getClassLoader().getResourceAsStream(name);
|
139
144
|
}
|
140
145
|
buffer = new BufferedReader(new java.io.InputStreamReader(is), 8192);
|
@@ -0,0 +1,53 @@
|
|
1
|
+
package org.ruboto;
|
2
|
+
|
3
|
+
public class ScriptInfo {
|
4
|
+
private String rubyClassName;
|
5
|
+
private String scriptName;
|
6
|
+
private Object rubyInstance;
|
7
|
+
|
8
|
+
// FIXME(uwe): Only used for legacy handle_xxx callbacks. Remove when we stop supporting these.
|
9
|
+
private final Object[] callbackProcs;
|
10
|
+
|
11
|
+
public ScriptInfo(int callbackSize) {
|
12
|
+
callbackProcs = new Object[callbackSize];
|
13
|
+
}
|
14
|
+
|
15
|
+
public Object[] getCallbackProcs() {
|
16
|
+
return callbackProcs;
|
17
|
+
}
|
18
|
+
|
19
|
+
public void setCallbackProc(int id, Object obj) {
|
20
|
+
callbackProcs[id] = obj;
|
21
|
+
}
|
22
|
+
|
23
|
+
public String getRubyClassName() {
|
24
|
+
if (rubyClassName == null && scriptName != null) {
|
25
|
+
return Script.toCamelCase(scriptName);
|
26
|
+
}
|
27
|
+
return rubyClassName;
|
28
|
+
}
|
29
|
+
|
30
|
+
public void setRubyClassName(String name) {
|
31
|
+
rubyClassName = name;
|
32
|
+
}
|
33
|
+
|
34
|
+
public Object getRubyInstance() {
|
35
|
+
return rubyInstance;
|
36
|
+
}
|
37
|
+
|
38
|
+
public void setRubyInstance(Object instance) {
|
39
|
+
rubyInstance = instance;
|
40
|
+
}
|
41
|
+
|
42
|
+
public String getScriptName() {
|
43
|
+
if (scriptName == null && rubyClassName != null) {
|
44
|
+
return Script.toSnakeCase(rubyClassName) + ".rb";
|
45
|
+
}
|
46
|
+
return scriptName;
|
47
|
+
}
|
48
|
+
|
49
|
+
public void setScriptName(String name) {
|
50
|
+
scriptName = name;
|
51
|
+
}
|
52
|
+
|
53
|
+
}
|
@@ -0,0 +1,143 @@
|
|
1
|
+
package org.ruboto;
|
2
|
+
|
3
|
+
import java.io.IOException;
|
4
|
+
|
5
|
+
import android.app.ProgressDialog;
|
6
|
+
import android.content.Context;
|
7
|
+
|
8
|
+
public class ScriptLoader {
|
9
|
+
/**
|
10
|
+
Return true if we are called from JRuby.
|
11
|
+
*/
|
12
|
+
public static boolean isCalledFromJRuby() {
|
13
|
+
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
14
|
+
int maxLookBack = Math.min(10, stackTraceElements.length);
|
15
|
+
for(int i = 0; i < maxLookBack ; i++){
|
16
|
+
if (stackTraceElements[i].getClassName().startsWith("org.jruby.javasupport.JavaMethod")) {
|
17
|
+
return true;
|
18
|
+
}
|
19
|
+
}
|
20
|
+
return false;
|
21
|
+
}
|
22
|
+
|
23
|
+
public static void loadScript(final RubotoComponent component, Object... args) {
|
24
|
+
try {
|
25
|
+
if (component.getScriptInfo().getScriptName() != null) {
|
26
|
+
System.out.println("Looking for Ruby class: " + component.getScriptInfo().getRubyClassName());
|
27
|
+
Object rubyClass = JRubyAdapter.get(component.getScriptInfo().getRubyClassName());
|
28
|
+
System.out.println("Found: " + rubyClass);
|
29
|
+
Script rubyScript = new Script(component.getScriptInfo().getScriptName());
|
30
|
+
Object rubyInstance;
|
31
|
+
if (rubyScript.exists()) {
|
32
|
+
rubyInstance = component;
|
33
|
+
final String script = rubyScript.getContents();
|
34
|
+
if (script.matches("(?s).*class " + component.getScriptInfo().getRubyClassName() + ".*")) {
|
35
|
+
if (!component.getScriptInfo().getRubyClassName().equals(component.getClass().getSimpleName())) {
|
36
|
+
System.out.println("Script defines methods on meta class");
|
37
|
+
// FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7
|
38
|
+
if (JRubyAdapter.isJRubyPreOneSeven() || JRubyAdapter.isRubyOneEight()) {
|
39
|
+
JRubyAdapter.put("$java_instance", component);
|
40
|
+
JRubyAdapter.put(component.getScriptInfo().getRubyClassName(), JRubyAdapter.runScriptlet("class << $java_instance; self; end"));
|
41
|
+
} else if (JRubyAdapter.isJRubyOneSeven() && JRubyAdapter.isRubyOneNine()) {
|
42
|
+
JRubyAdapter.runScriptlet("Java::" + component.getClass().getName() + ".__persistent__ = true");
|
43
|
+
JRubyAdapter.put(component.getScriptInfo().getRubyClassName(), JRubyAdapter.runRubyMethod(component, "singleton_class"));
|
44
|
+
} else {
|
45
|
+
throw new RuntimeException("Unknown JRuby/Ruby version: " + JRubyAdapter.get("JRUBY_VERSION") + "/" + JRubyAdapter.get("RUBY_VERSION"));
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
if (rubyClass == null) {
|
50
|
+
System.out.println("Loading script: " + component.getScriptInfo().getScriptName());
|
51
|
+
if (script.matches("(?s).*class " + component.getScriptInfo().getRubyClassName() + ".*")) {
|
52
|
+
System.out.println("Script contains class definition");
|
53
|
+
if (component.getScriptInfo().getRubyClassName().equals(component.getClass().getSimpleName())) {
|
54
|
+
System.out.println("Script has separate Java class");
|
55
|
+
// FIXME(uwe): Simplify when we stop support for JRuby < 1.7.0
|
56
|
+
if (!JRubyAdapter.isJRubyPreOneSeven()) {
|
57
|
+
JRubyAdapter.runScriptlet("Java::" + component.getClass().getName() + ".__persistent__ = true");
|
58
|
+
}
|
59
|
+
JRubyAdapter.put(component.getScriptInfo().getRubyClassName(), JRubyAdapter.runScriptlet("Java::" + component.getClass().getName()));
|
60
|
+
}
|
61
|
+
System.out.println("Set class: " + JRubyAdapter.get(component.getScriptInfo().getRubyClassName()));
|
62
|
+
Thread t = new Thread(new Runnable(){
|
63
|
+
public void run() {
|
64
|
+
JRubyAdapter.setScriptFilename(component.getScriptInfo().getScriptName());
|
65
|
+
JRubyAdapter.runScriptlet(script);
|
66
|
+
}
|
67
|
+
});
|
68
|
+
try {
|
69
|
+
t.start();
|
70
|
+
t.join();
|
71
|
+
} catch(InterruptedException ie) {
|
72
|
+
Thread.currentThread().interrupt();
|
73
|
+
throw new RuntimeException("Interrupted loading script.", ie);
|
74
|
+
}
|
75
|
+
rubyClass = JRubyAdapter.get(component.getScriptInfo().getRubyClassName());
|
76
|
+
} else {
|
77
|
+
// FIXME(uwe): Only needed for initial block-based activity definition
|
78
|
+
System.out.println("Script contains block based activity definition");
|
79
|
+
if (!JRubyAdapter.isJRubyPreOneSeven()) {
|
80
|
+
JRubyAdapter.runScriptlet("Java::" + component.getClass().getName() + ".__persistent__ = true");
|
81
|
+
}
|
82
|
+
JRubyAdapter.runScriptlet("$activity.instance_variable_set '@ruboto_java_class', '" + component.getScriptInfo().getRubyClassName() + "'");
|
83
|
+
JRubyAdapter.runScriptlet("puts %Q{$activity: #$activity}");
|
84
|
+
JRubyAdapter.setScriptFilename(component.getScriptInfo().getScriptName());
|
85
|
+
JRubyAdapter.runScriptlet(script);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
} else if (rubyClass != null) {
|
89
|
+
// We have a predefined Ruby class without corresponding Ruby source file.
|
90
|
+
System.out.println("Create separate Ruby instance for class: " + rubyClass);
|
91
|
+
rubyInstance = JRubyAdapter.runRubyMethod(rubyClass, "new");
|
92
|
+
JRubyAdapter.runRubyMethod(rubyInstance, "instance_variable_set", "@ruboto_java_instance", component);
|
93
|
+
} else {
|
94
|
+
// Neither script file nor predefined class
|
95
|
+
throw new RuntimeException("Either script or predefined class must be present.");
|
96
|
+
}
|
97
|
+
if (rubyClass != null) {
|
98
|
+
if (component instanceof android.content.Context) {
|
99
|
+
callOnCreate(rubyInstance, args);
|
100
|
+
}
|
101
|
+
} else {
|
102
|
+
// FIXME(uwe): Remove when we stop supporting block based main activities.
|
103
|
+
component.onCreateSuper();
|
104
|
+
}
|
105
|
+
component.getScriptInfo().setRubyInstance(rubyInstance);
|
106
|
+
} else { // if (configBundle != null) {
|
107
|
+
// FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7
|
108
|
+
if (JRubyAdapter.isJRubyPreOneSeven()) {
|
109
|
+
JRubyAdapter.runScriptlet("$activity.initialize_ruboto");
|
110
|
+
} else if (JRubyAdapter.isJRubyOneSeven()) {
|
111
|
+
JRubyAdapter.runRubyMethod(component, "initialize_ruboto");
|
112
|
+
} else {
|
113
|
+
throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION"));
|
114
|
+
}
|
115
|
+
if (component instanceof android.content.Context) {
|
116
|
+
callOnCreate(component, args);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
} catch(IOException e){
|
120
|
+
e.printStackTrace();
|
121
|
+
if (component instanceof android.content.Context) {
|
122
|
+
ProgressDialog.show((android.content.Context) component, "Script failed", "Something bad happened", true, true);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
private static final void callOnCreate(Object rubyInstance, Object[] args) {
|
128
|
+
System.out.println("Call on_create on: " + rubyInstance + ", " + JRubyAdapter.get("JRUBY_VERSION"));
|
129
|
+
// FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7
|
130
|
+
if (JRubyAdapter.isJRubyPreOneSeven()) {
|
131
|
+
if (args.length > 0) {
|
132
|
+
JRubyAdapter.put("$bundle", args[0]);
|
133
|
+
}
|
134
|
+
JRubyAdapter.put("$ruby_instance", rubyInstance);
|
135
|
+
JRubyAdapter.runScriptlet("$ruby_instance.on_create(" + (args.length > 0 ? "$bundle" : "") + ")");
|
136
|
+
} else if (JRubyAdapter.isJRubyOneSeven()) {
|
137
|
+
JRubyAdapter.runRubyMethod(rubyInstance, "on_create", (Object[]) args);
|
138
|
+
} else {
|
139
|
+
throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION"));
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|