PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/java/fanx/tools/Fan.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 339 lines | 267 code | 37 blank | 35 comment | 55 complexity | 211a812c5c473cad53758f163657a49d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 4 Dec 05 Brian Frank Creation
  7. //
  8. package fanx.tools;
  9. import java.io.File;
  10. import fan.sys.*;
  11. import fanx.util.*;
  12. /**
  13. * Fantom runtime command line tool.
  14. */
  15. public class Fan
  16. {
  17. //////////////////////////////////////////////////////////////////////////
  18. // Execute
  19. //////////////////////////////////////////////////////////////////////////
  20. public int execute(String target, String[] args)
  21. throws Exception
  22. {
  23. // args
  24. Sys.bootEnv.setArgs(args);
  25. // check for pods pending installation
  26. checkInstall();
  27. // first try as file name
  28. File file = new File(target);
  29. if (file.exists() && target.toLowerCase().endsWith(".fan") && !file.isDirectory())
  30. {
  31. return executeFile(file, args);
  32. }
  33. else
  34. {
  35. return executeType(target, args);
  36. }
  37. }
  38. private void checkInstall()
  39. {
  40. // During bootstrap, check for pods located in "lib/install" and
  41. // if found copy them to "lib/fan". This gives us a simple way to
  42. // stage installs which don't effect current program until the next
  43. // reboot. This is not really intended to be a long term solution
  44. // because it suffers from limitations: assumes only one Fantom program
  45. // being restarted at a time and doesn't work with Env very well
  46. try
  47. {
  48. File installDir = new File(Sys.homeDir, "lib" + File.separator + "install");
  49. if (!installDir.exists()) return;
  50. File[] files = installDir.listFiles();
  51. for (int i=0; files != null && i<files.length; ++i)
  52. {
  53. File file = files[i];
  54. String name = file.getName();
  55. if (!name.endsWith(".pod")) continue;
  56. System.out.println("INSTALL POD: " + name);
  57. FileUtil.copy(file, new File(Sys.podsDir, name));
  58. file.delete();
  59. }
  60. FileUtil.delete(installDir);
  61. }
  62. catch (Throwable e)
  63. {
  64. System.out.println("ERROR: checkInstall");
  65. e.printStackTrace();
  66. }
  67. }
  68. private int executeFile(File file, String[] args)
  69. throws Exception
  70. {
  71. Pod pod = compileScript(file, args);
  72. if (pod == null) return -1;
  73. List types = pod.types();
  74. Type type = null;
  75. Method main = null;
  76. for (int i=0; i<types.sz(); ++i)
  77. {
  78. type = (Type)types.get(i);
  79. main = type.method("main", false);
  80. if (main != null) break;
  81. }
  82. if (main == null)
  83. {
  84. System.out.println("ERROR: missing main method: " + ((Type)types.get(0)).name() + ".main()");
  85. return -1;
  86. }
  87. return callMain(type, main);
  88. }
  89. static Pod compileScript(File file, String[] args)
  90. {
  91. LocalFile f = (LocalFile)(new LocalFile(file).normalize());
  92. Map options = new Map(Sys.StrType, Sys.ObjType);
  93. for (int i=0; args != null && i<args.length; ++i)
  94. if (args[i].equals("-fcodeDump")) options.add("fcodeDump", Boolean.TRUE);
  95. try
  96. {
  97. // use Fantom reflection to run compiler::Main.compileScript(File)
  98. return Env.cur().compileScript(f, options).pod();
  99. }
  100. catch (Err e)
  101. {
  102. System.out.println("ERROR: cannot compile script");
  103. if (!e.getClass().getName().startsWith("fan.compiler"))
  104. e.trace();
  105. return null;
  106. }
  107. catch (Exception e)
  108. {
  109. System.out.println("ERROR: cannot compile script");
  110. e.printStackTrace();
  111. return null;
  112. }
  113. }
  114. private int executeType(String target, String[] args)
  115. throws Exception
  116. {
  117. if (target.indexOf("::") < 0) target += "::Main.main";
  118. else if (target.indexOf('.') < 0) target += ".main";
  119. try
  120. {
  121. int dot = target.lastIndexOf('.');
  122. Type type = Type.find(target.substring(0, dot), true);
  123. Method main = type.method(target.substring(dot+1), true);
  124. return callMain(type, main);
  125. }
  126. catch (Throwable e)
  127. {
  128. System.out.println("ERROR: " + e);
  129. return -1;
  130. }
  131. }
  132. static int callMain(Type t, Method m)
  133. {
  134. // check parameter type and build main arguments
  135. List args;
  136. List params = m.params();
  137. if (params.sz() == 0)
  138. {
  139. args = null;
  140. }
  141. else if (((Param)params.get(0)).type().is(Sys.StrType.toListOf()) &&
  142. (params.sz() == 1 || ((Param)params.get(1)).hasDefault()))
  143. {
  144. args = new List(Sys.ObjType, new Object[] { Env.cur().args() });
  145. }
  146. else
  147. {
  148. System.out.println("ERROR: Invalid parameters for main: " + m.signature());
  149. return -1;
  150. }
  151. // invoke
  152. try
  153. {
  154. if (m.isStatic())
  155. return toResult(m.callList(args));
  156. else
  157. return toResult(m.callOn(t.make(), args));
  158. }
  159. catch (Err ex)
  160. {
  161. ex.trace();
  162. return -1;
  163. }
  164. finally
  165. {
  166. cleanup();
  167. }
  168. }
  169. static int toResult(Object obj)
  170. {
  171. if (obj instanceof Long) return ((Long)obj).intValue();
  172. return 0;
  173. }
  174. static void cleanup()
  175. {
  176. try
  177. {
  178. Env.cur().out().flush();
  179. Env.cur().err().flush();
  180. }
  181. catch (Throwable e) {}
  182. }
  183. //////////////////////////////////////////////////////////////////////////
  184. // Version
  185. //////////////////////////////////////////////////////////////////////////
  186. static void version(String progName)
  187. {
  188. println(progName);
  189. println("Copyright (c) 2006-2013, Brian Frank and Andy Frank");
  190. println("Licensed under the Academic Free License version 3.0");
  191. println("");
  192. println("Java Runtime:");
  193. println(" java.version: " + System.getProperty("java.version"));
  194. println(" java.vm.name: " + System.getProperty("java.vm.name"));
  195. println(" java.vm.vendor: " + System.getProperty("java.vm.vendor"));
  196. println(" java.vm.version: " + System.getProperty("java.vm.version"));
  197. println(" java.home: " + System.getProperty("java.home"));
  198. println(" fan.platform: " + Env.cur().platform());
  199. println(" fan.version: " + Sys.sysPod.version());
  200. println(" fan.env: " + Env.cur());
  201. println(" fan.home: " + Env.cur().homeDir().osPath());
  202. String[] path = Env.cur().toDebugPath();
  203. if (path != null)
  204. {
  205. println("");
  206. println("Env Path:");
  207. for (int i=0; i<path.length; ++i)
  208. println(" " + path[i]);
  209. println("");
  210. }
  211. }
  212. static void pods(String progName)
  213. {
  214. version(progName);
  215. long t1 = System.nanoTime();
  216. List pods = Pod.list();
  217. long t2 = System.nanoTime();
  218. println("");
  219. println("Fantom Pods [" + (t2-t1)/1000000L + "ms]:");
  220. println(" Pod Version");
  221. println(" --- -------");
  222. for (int i=0; i<pods.sz(); ++i)
  223. {
  224. Pod pod = (Pod)pods.get(i);
  225. println(" " +
  226. FanStr.justl(pod.name(), 18L) + " " +
  227. FanStr.justl(pod.version().toString(), 8));
  228. }
  229. }
  230. //////////////////////////////////////////////////////////////////////////
  231. // Run
  232. //////////////////////////////////////////////////////////////////////////
  233. public int run(String[] args)
  234. {
  235. try
  236. {
  237. if (args.length == 0) { help(); return -1; }
  238. // process args
  239. for (int i=0; i<args.length; ++i)
  240. {
  241. String a = args[i].intern();
  242. if (a.length() == 0) continue;
  243. if (a == "-help" || a == "-h" || a == "-?")
  244. {
  245. help();
  246. return 2;
  247. }
  248. else if (a == "-version")
  249. {
  250. version("Fantom Launcher");
  251. return 3;
  252. }
  253. else if (a == "-pods")
  254. {
  255. pods("Fantom Launcher");
  256. return 4;
  257. }
  258. else if (a.charAt(0) == '-')
  259. {
  260. System.out.println("WARNING: Unknown option " + a);
  261. }
  262. else
  263. {
  264. String target = a;
  265. String[] targetArgs = new String[args.length-i-1];
  266. System.arraycopy(args, i+1, targetArgs, 0, targetArgs.length);
  267. return execute(target, targetArgs);
  268. }
  269. }
  270. help();
  271. return 2;
  272. }
  273. catch (Throwable e)
  274. {
  275. e.printStackTrace();
  276. return 1;
  277. }
  278. }
  279. void help()
  280. {
  281. println("Fantom Launcher");
  282. println("Usage:");
  283. println(" fan [options] <pod>::<type>.<method> [args]*");
  284. println(" fan [options] <filename> [args]*");
  285. println("Options:");
  286. println(" -help, -h, -? print usage help");
  287. println(" -version print version information");
  288. println(" -pods list installed pods");
  289. }
  290. public static void println(String s)
  291. {
  292. System.out.println(s);
  293. }
  294. //////////////////////////////////////////////////////////////////////////
  295. // Main
  296. //////////////////////////////////////////////////////////////////////////
  297. public static void main(final String[] args)
  298. throws Exception
  299. {
  300. System.exit(new Fan().run(args));
  301. }
  302. }