Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ais.ifidsadminutility;
- import android.os.Environment;
- import android.util.Log;
- import clojure.lang.IFn;
- import clojure.lang.RT;
- import clojure.java.api.Clojure;
- import clojure.lang.Compiler;
- import java.io.File;
- import java.io.IOException;
- public class ScriptManager {
- private static ScriptManager instance = null;
- public static ScriptManager getInstance() {
- if(instance == null) {
- instance = new ScriptManager();
- }
- return instance;
- }
- public void runTest() {
- // Load the Clojure script -- as a side effect this initializes the runtime.
- //IFn read_string = Clojure.var("clojure.core", "read-string");
- //IFn eval = Clojure.var("clojure.core", "eval");
- //String str = "(ns user) (defn foo [a b] (str a \" \" b))";
- RT.init();
- try {
- FileUtilities.writeAssetToFile("scripts/main.clj", "main.clj", this.getScriptFolder());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Log.e("ScriptManager", "Failed to load copy to external storage: ");
- Log.e("ScriptManager", e.getMessage());
- e.printStackTrace();
- }
- //IFn map = Clojure.var("clojure.core", "map");
- //IFn inc = Clojure.var("clojure.core", "inc");
- //Object resMap = map.invoke(inc, Clojure.read("[1 2 3]"));
- //Log.v("ScriptManager", resMap.toString());
- //Grabbing the load-file clojure function
- IFn load_file = Clojure.var("clojure.core", "load-file");
- IFn load_string = Clojure.var("clojure.core", "load-string");
- IFn slurp = Clojure.var("clojure.core", "slurp");
- if (load_file == null) {
- Log.e("ScriptManager", "load_file is null");
- }
- String scriptPath = new File(this.getScriptFolder(), "main.clj").getAbsolutePath();
- //try and read print the output of our file
- String text = (String) slurp.invoke(scriptPath);
- Log.v("ScriptManager", text);
- try {
- load_string.invoke(text);
- }
- catch (Exception e) {
- Log.e("ScriptManager", "Failed to load-string init file");
- Log.e("ScriptManager", e.getMessage());
- return;
- }
- try {
- load_file.invoke(scriptPath);
- }
- catch (Exception e) {
- Log.e("ScriptManager", "Failed to load-file init file");
- Log.e("ScriptManager", e.getMessage());
- return;
- }
- // Get a reference to the foo function.
- IFn foo = Clojure.var("user", "foo");
- // Call it!
- Object result = foo.invoke("Hi there");
- Log.v("ScriptManager", result.toString());
- }
- public String getScriptFolder() {
- return new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/iFIDS.com/scripts/").getAbsolutePath();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement