jtalkc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/bin/bash
  2. #
  3. # This is a "compiler" for JTalk code. Run without arguments for help.
  4. # Get JTalk root directory from the location of this script.
  5. JTALK=$(readlink -f `dirname ${0}`/..)
  6. function usage {
  7. cat <<ENDOFHELP
  8. Usage: $0 [-N|D|E] [-K|C|J] [-o] [-O] [-g] [-m class] [-M file]
  9. [-i] [-I file] [file1 [file2 ...]] [Program]
  10. Will compile Jtalk files - either separately or into a runnable complete
  11. program. If no files are listed only a linking stage is performed.
  12. Files listed will be handled using these rules:
  13. *.js
  14. Files are concatenated in listed order.
  15. If not found we look in $JTALK/js
  16. *.st
  17. Files are compiled into .js files before concatenated.
  18. If not found we look in $JTALK/st.
  19. NOTE: Each file is currently considered to be a fileout of a single class
  20. category of the same name as the file!
  21. If no Program is specified each given .st file will be compiled into
  22. a .js file. Otherwise a <Program>.js file is linked together based on
  23. the options:
  24. -N or -D or -E
  25. Compilation target. Currently Node.js, D8 (V8 shell) or Enyo (webOS 3.0).
  26. All imply "-K -I" so boot.js and Kernel.js are added first and init.js
  27. is added last.
  28. -K
  29. Add libraries to get minimal Jtalk Kernel running.
  30. -C
  31. Add libraries to get minimal Jtalk Compiler running.
  32. -J
  33. Add libraries to get minimal Jtalk IDE running.
  34. -o
  35. Optimize each js file using the Google closure compiler.
  36. Using Closure at ~/compiler.jar
  37. -O
  38. Optimize final <Program>.js using the Google closure compiler.
  39. Using Closure at ~/compiler.jar
  40. -g
  41. Compile Jtalk code in debug mode - include source and references etc.
  42. -l library1,library2
  43. Load listed libraries (no spaces) into Compiler before compiling.
  44. -L library1,library2
  45. Load listed libraries (no spaces) into Compiler before compiling and also
  46. into Program.js in listed order.
  47. -i
  48. Add library initializer <file>.
  49. -I file
  50. Add library standard initializer $JTALK/js/init.js
  51. -m class
  52. Add call to #main in class <class>.
  53. -M file
  54. Add javascript file <file> at the end acting as main.
  55. Example invocations:
  56. Just compile Kernel.st to Kernel.js:
  57. jtalkc Kernel.st
  58. Compile Hello.st to Hello.js and create complete program called
  59. Program.js for Node.js including boot.js, Kernel.js, init.js and
  60. adding a call to class method #main in class Hello:
  61. jtalkc -N -m Hello Hello.st Program
  62. Compile two .st files into corresponding .js files,
  63. and link with specific myboot.js, myKernel.js, myinit.js
  64. and main.js and create complete program called Program.js:
  65. jtalkc -M main.js -I myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  66. ENDOFHELP
  67. exit 1;
  68. }
  69. # Check we at least got one argument
  70. if [ -z $1 ] ; then
  71. usage
  72. fi
  73. # Define our predefined library combinations
  74. BOOT="boot.js"
  75. KERNEL="$BOOT Kernel.js"
  76. COMPILER="$KERNEL Parser.js Compiler.js"
  77. CANVAS="$COMPILER Canvas.js"
  78. IDE="$CANVAS JQuery.js IDE.js SUnit.js Examples.js"
  79. # Predefined initializer
  80. INITIALIZER="$JTALK/js/init.js"
  81. # Default values
  82. ENV=
  83. INIT=
  84. MAIN=
  85. MAINFILE=
  86. BASE=$KERNEL
  87. LOAD=
  88. LOADANDADD=
  89. DEBUG=false
  90. NODECOMPILE=nodecompile.js
  91. # Read options and shift them away
  92. while getopts "NDEKCJoOgl:L:i:IM:m:h?" o; do
  93. case "$o" in
  94. N) ENV=NODE
  95. BASE=$KERNEL
  96. INIT=$INITIALIZER;;
  97. D) ENV=D8
  98. BASE=$KERNEL
  99. INIT=$INITIALIZER;;
  100. E) ENV=ENYO
  101. BASE=$KERNEL
  102. INIT=$INITIALIZER;;
  103. K) BASE=$KERNEL;;
  104. C) BASE=$COMPILER;;
  105. J) BASE=$IDE;;
  106. o) CLOSURE=true
  107. CLOSUREPARTS=true;;
  108. O) CLOSURE=true
  109. CLOSUREFULL=true;;
  110. g) DEBUG=true;;
  111. l) LOAD=$OPTARG;;
  112. L) LOADANDADD=$OPTARG;;
  113. I) INIT=$INITIALIZER;;
  114. i) INIT=$OPTARG;;
  115. M) MAINFILE=$OPTARG;;
  116. m) MAIN=$OPTARG;;
  117. h) usage;;
  118. [?]) usage;;
  119. esac
  120. done
  121. shift $(($OPTIND - 1))
  122. # Check for Closure compiler and Java
  123. if [ ! -z $CLOSURE ]; then
  124. java > /dev/null
  125. if [ $? -eq 0 ]; then
  126. if [ ! -f ~/compiler.jar ]; then
  127. echo "Can not find Closure compiler at ~/compiler.jar"
  128. exit 1
  129. fi
  130. else
  131. echo "java is not installed and is needed for -O or -o (Closure compiler)."
  132. exit 1
  133. fi
  134. fi
  135. # Function for looking up listed js files
  136. function resolvejs {
  137. if [ -f "$1" ]; then
  138. RESOLVED="$1"
  139. else
  140. if [ -f $JTALK/js/$1 ]; then
  141. RESOLVED="$JTALK/js/$1"
  142. else
  143. echo "Javascript file not found: $1"
  144. exit 1
  145. fi
  146. fi
  147. }
  148. # Resolve listed libraries in $BASE separated by spaces
  149. for FILE in $BASE
  150. do
  151. resolvejs $FILE
  152. TOBASE="$TOBASE $RESOLVED"
  153. done
  154. # Resolve listed libraries in $LOAD separated by spaces
  155. LOAD=${LOAD//,/\ }
  156. for FILE in $LOAD
  157. do
  158. resolvejs $FILE
  159. TOLOAD="$TOLOAD $RESOLVED"
  160. done
  161. # Resolve listed libraries in $LOADANDADD separated by spaces
  162. LOADANDADD=${LOADANDADD//,/\ }
  163. for FILE in $LOADANDADD
  164. do
  165. resolvejs $FILE
  166. TOLOAD="$TOLOAD $RESOLVED"
  167. TOADD="$TOADD $RESOLVED"
  168. done
  169. # Define our Compiler loading supplied libraries
  170. OURCOMPILER="$COMPILER $TOLOAD init.js $JTALK/nodejs/$NODECOMPILE"
  171. # Resolve OURCOMPILER
  172. for FILE in $OURCOMPILER
  173. do
  174. resolvejs $FILE
  175. TOOURCOMPILER="$TOOURCOMPILER $RESOLVED"
  176. done
  177. # Add supplied libraries
  178. LIBS="$TOBASE $TOADD"
  179. # Get a unique tempdir and make it get auto removed on exit
  180. TMPDIR=`mktemp -d`
  181. trap "rm -rf $TMPDIR" EXIT
  182. # --------------------------------------------------
  183. # Collect libraries and Smalltalk files looking
  184. # both locally and in $JTALK/js and $JTALK/st
  185. # --------------------------------------------------
  186. PROGRAM=
  187. until [ "$*" = "" ]
  188. do
  189. case $1 in
  190. *.st)
  191. CATEGORY=`basename $1 .st`
  192. if [ -f "$1" ]; then
  193. COMPILE="$COMPILE $1 $CATEGORY"
  194. COMPILED="$COMPILED $CATEGORY.js"
  195. else
  196. if [ -f $JTALK/st/$1 ]; then
  197. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  198. COMPILED="$COMPILED $CATEGORY.js"
  199. else
  200. echo "JTalk file not found: $1"
  201. exit 1
  202. fi
  203. fi
  204. ;;
  205. *.js)
  206. resolvejs $1
  207. LIBS="$LIBS $RESOLVED"
  208. ;;
  209. *)
  210. # Will end up being the last non js/st argument
  211. PROGRAM=$1
  212. ;;
  213. esac
  214. shift
  215. done
  216. # --------------------------------------------------
  217. # Actual compilation phase of collected .st files
  218. # --------------------------------------------------
  219. # Create compiler dynamically
  220. cat $TOOURCOMPILER > $TMPDIR/compiler.js
  221. # Compile all collected .st files to .js
  222. echo "Loading libraries $TOOURCOMPILER and compiling ..."
  223. node $TMPDIR/compiler.js $DEBUG $COMPILE
  224. # Verify all .js files corresponding to .st files were created, otherwise exit
  225. IFS=" "
  226. for FILE in $COMPILED
  227. do
  228. if [ ! -f "$FILE" ]; then
  229. echo "Failed compilation of $FILE, exiting."
  230. exit 1
  231. fi
  232. done
  233. if [ ! -z $CLOSUREPARTS ]; then
  234. echo "Compiling all js files using Google closure compiler."
  235. ALLJSFILES="$COMPILED $LIBS"
  236. for FILE in $ALLJSFILES
  237. do
  238. mv $FILE $FILE.original
  239. java -jar ~/compiler.jar --js $FILE.original --js_output_file $FILE
  240. rm $FILE.original
  241. done
  242. fi
  243. if [ -z $PROGRAM ]; then
  244. echo "Done."
  245. exit 0
  246. fi
  247. # --------------------------------------------------
  248. # Now we start composing resulting javascript file.
  249. # --------------------------------------------------
  250. # Add collected libraries to libs.js file.
  251. if [ ! -z "$LIBS" ]; then
  252. echo "Adding libraries $LIBS ..."
  253. cat $LIBS > $TMPDIR/libs.js
  254. LIBS=$TMPDIR/libs.js
  255. fi
  256. echo "Adding Jtalk code$COMPILED ..."
  257. # Check for init file
  258. if [ ! -z "$INIT" ]; then
  259. if [ -f "$INIT" ]; then
  260. echo "Adding initializer $INIT ..."
  261. else
  262. echo "Can not find init file $INIT, exiting."
  263. exit 1
  264. fi
  265. fi
  266. # Check for adding main
  267. if [ ! -z "$MAIN" ]; then
  268. echo "Adding call to $MAIN class >> main ..."
  269. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  270. MAIN=$TMPDIR/main.js
  271. fi
  272. # Check for adding main file
  273. if [ ! -z "$MAINFILE" ]; then
  274. if [ -f "$MAINFILE" ]; then
  275. echo "Adding main as $MAINFILE ..."
  276. else
  277. echo "Can not find main file $MAINFILE, exiting."
  278. exit 1
  279. fi
  280. MAIN=$MAINFILE
  281. fi
  282. # And finally concatenate Program.js
  283. echo "Writing $PROGRAM.js ..."
  284. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  285. echo "Done."
  286. if [ ! -z $CLOSUREFULL ]; then
  287. echo "Compiling $PROGRAM.js file using Google closure compiler."
  288. mv $PROGRAM.js $PROGRAM.js.original
  289. java -jar ~/compiler.jar --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  290. rm $PROGRAM.js.original
  291. echo "Done."
  292. fi