jtalkc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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=`dirname ${0}`/..
  6. function usage {
  7. cat <<ENDOFHELP
  8. Usage: $0 [-N|D] [-K|C] [-o] [-O] [-m class] [-M file] [-i] [-I file] file1 [file2 ...] [Program]
  9. Will compile Jtalk files - either separately or into a runnable complete program.
  10. Files listed will be handled using these rules:
  11. *.js
  12. Files are concatenated in listed order. If not found we look in $JTALK/js
  13. *.st
  14. Files are compiled into .js files before concatenated. If not found we look
  15. in $JTALK/st.
  16. NOTE: Each file is currently considered to be a fileout of a single class
  17. category of the same name as the file!
  18. If no Program is specified each given .st file will be compiled into a .js file.
  19. Otherwise a <Program>.js file is linked together based on the options:
  20. -N or -D
  21. Compile for Node.js or D8 (V8 shell). Implies "-K -i -m Main" so boot.js and Kernel.js
  22. are added first and init.js is added last with a call to Main class>>main.
  23. -K
  24. Add libraries to get minimal JTalk Kernel running.
  25. -C
  26. Add libraries to get minimal JTalk Compiler running.
  27. -o
  28. Optimize each js file using the Google closure compiler. Using ~/compiler.jar
  29. -O
  30. Optimize <Program>.js using the Google closure compiler. Using ~/compiler.jar
  31. -l libray1,library2
  32. Load listed libraries (no spaces) in Compiler before compiling.
  33. -L
  34. Loads all libraries in directory js in Compiler before compiling.
  35. -i
  36. Add library standard initializer $JTALK/js/init.js
  37. -I file
  38. Add library initializer <file>.
  39. -m class
  40. Add call to #main in class <class>.
  41. -M file
  42. Add javascript file <file> at the end acting as main.
  43. Example invocations:
  44. Compile Kernel.st to Kernel.js:
  45. jtalkc Kernel.st
  46. Compile Hello.st to Hello.js and create complete program called
  47. Program.js for Node.js including boot.js, Kernel.js, init.js and
  48. adding a call to class method #main in class Hello:
  49. jtalkc -N -m Hello Hello.st Program
  50. Compile two .st files into corresponding .js files,
  51. and manually link with specific myboot.js, myKernel.js, myinit.js and main.js
  52. and create complete program called Program.js:
  53. jtalkc -M main.js -I myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  54. ENDOFHELP
  55. exit 1;
  56. }
  57. # Check we at least got one argument
  58. if [ -z $1 ] ; then
  59. usage
  60. fi
  61. # Define our predefined library combinations
  62. BOOT="$JTALK/js/boot.js"
  63. KERNEL="$BOOT $JTALK/js/Kernel.js"
  64. COMPILER="$KERNEL $JTALK/js/Parser.js $JTALK/js/Compiler.js"
  65. CANVAS="$COMPILER $JTALK/js/Canvas.js"
  66. # Predefined initializer
  67. INITIALIZER="$JTALK/js/init.js"
  68. # Default values
  69. ENV=
  70. INIT=
  71. MAIN=
  72. MAINFILE=
  73. BASE=$KERNEL
  74. LOAD=
  75. # Read options and shift them away
  76. while getopts "NDKCoOl:LiI:M:m:h?" o; do
  77. case "$o" in
  78. N) ENV=NODE
  79. BASE=$KERNEL
  80. INIT=$INITIALIZER
  81. MAIN=Main;;
  82. D) ENV=D8
  83. BASE=$KERNEL
  84. INIT=$INITIALIZER
  85. MAIN=Main;;
  86. K) BASE=$KERNEL;;
  87. C) BASE=$COMPILER;;
  88. o) CLOSURE=true
  89. CLOSUREPARTS=true;;
  90. O) CLOSURE=true
  91. CLOSUREFULL=true;;
  92. l) LOAD=$OPTARG;;
  93. L) LOAD="$JTALK/*.js";;
  94. I) INIT=$INITIALIZER;;
  95. i) INIT=$OPTARG;;
  96. M) MAINFILE=$OPTARG;;
  97. m) MAIN=$OPTARG;;
  98. h) usage;;
  99. [?]) usage;;
  100. esac
  101. done
  102. shift $(($OPTIND - 1))
  103. # Check for Closure compiler and Java
  104. if [ ! -z $CLOSURE ]; then
  105. java > /dev/null
  106. if [ $? -eq 0 ]; then
  107. if [ ! -f ~/compiler.jar ]; then
  108. echo "Can not find Closure compiler at ~/compiler.jar"
  109. exit 1
  110. fi
  111. else
  112. echo "java is not installed and is needed for -O or -o (Closure compiler)."
  113. exit 1
  114. fi
  115. fi
  116. # Define our Compiler
  117. # We need to add extra listed libraries in $LOAD not already in $CANVAS
  118. OURCOMPILER="$CANVAS $JTALK/js/init.js $JTALK/nodejs/nodecompile.js"
  119. # Combine supplied libraries
  120. LIBS="$BASE $ADDONS"
  121. # Get a unique tempdir and make it get auto removed on exit
  122. TMPDIR=`mktemp -d`
  123. trap "rm -rf $TMPDIR" EXIT
  124. # --------------------------------------------------
  125. # Collect libraries and Smalltalk files looking
  126. # both locally and in $JTALK/js and $JTALK/st
  127. # --------------------------------------------------
  128. PROGRAM=
  129. until [ "$*" = "" ]
  130. do
  131. case $1 in
  132. *.st)
  133. CATEGORY=`basename $1 .st`
  134. if [ -f "$1" ]; then
  135. COMPILE="$COMPILE $1 $CATEGORY"
  136. COMPILED="$COMPILED $CATEGORY.js"
  137. else
  138. if [ -f $JTALK/st/$1 ]; then
  139. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  140. COMPILED="$COMPILED $CATEGORY.js"
  141. else
  142. echo "JTalk file not found: $1"
  143. exit 1
  144. fi
  145. fi
  146. ;;
  147. *.js)
  148. if [ -f "$1" ]; then
  149. LIBS="$LIBS $1"
  150. else
  151. if [ -f $JTALK/js/$1 ]; then
  152. LIBS="$LIBS $JTALK/js/$1"
  153. else
  154. echo "Javascript file not found: $1"
  155. exit 1
  156. fi
  157. fi
  158. ;;
  159. *)
  160. # Will end up being the last non js/st argument
  161. PROGRAM=$1
  162. ;;
  163. esac
  164. shift
  165. done
  166. # --------------------------------------------------
  167. # Actual compilation phase of collected .st files
  168. # --------------------------------------------------
  169. # Create compiler dynamically
  170. cat $OURCOMPILER > $TMPDIR/compiler.js
  171. # Compile all collected .st files to .js
  172. echo "Compiling ..."
  173. node $TMPDIR/compiler.js $COMPILE
  174. # Verify all .js files corresponding to .st files were created, otherwise exit
  175. IFS=" "
  176. for FILE in $COMPILED
  177. do
  178. if [ ! -f "$FILE" ]; then
  179. echo "Failed compilation of $FILE, exiting."
  180. exit 1
  181. fi
  182. done
  183. if [ ! -z $CLOSUREPARTS ]; then
  184. echo "Compiling all js files using Google closure compiler."
  185. ALLJSFILES="$COMPILED $LIBS"
  186. for FILE in $ALLJSFILES
  187. do
  188. mv $FILE $FILE.original
  189. java -jar ~/compiler.jar --js $FILE.original --js_output_file $FILE
  190. rm $FILE.original
  191. done
  192. fi
  193. if [ -z $PROGRAM ]; then
  194. echo "Done."
  195. exit 0
  196. fi
  197. # --------------------------------------------------
  198. # Now we start composing resulting javascript file.
  199. # --------------------------------------------------
  200. # Add collected libraries to libs.js file.
  201. if [ ! -z "$LIBS" ]; then
  202. echo "Adding libraries $LIBS ..."
  203. cat $LIBS > $TMPDIR/libs.js
  204. LIBS=$TMPDIR/libs.js
  205. fi
  206. echo "Adding Jtalk code$COMPILED ..."
  207. # Check for init file
  208. if [ ! -z "$INIT" ]; then
  209. if [ -f "$INIT" ]; then
  210. echo "Adding initializer $INIT ..."
  211. else
  212. echo "Can not find init file $INIT, exiting."
  213. exit 1
  214. fi
  215. fi
  216. # Check for adding main
  217. if [ ! -z "$MAIN" ]; then
  218. echo "Adding call to $MAIN class >> main ..."
  219. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  220. MAIN=$TMPDIR/main.js
  221. fi
  222. # Check for adding main file
  223. if [ ! -z "$MAINFILE" ]; then
  224. if [ -f "$MAINFILE" ]; then
  225. echo "Adding main as $MAINFILE ..."
  226. else
  227. echo "Can not find main file $MAINFILE, exiting."
  228. exit 1
  229. fi
  230. MAIN=$MAINFILE
  231. fi
  232. # And finally concatenate Program.js
  233. echo "Writing $PROGRAM.js ..."
  234. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  235. echo "Done."
  236. if [ ! -z $CLOSUREFULL ]; then
  237. echo "Compiling $PROGRAM.js file using Google closure compiler."
  238. mv $PROGRAM.js $PROGRAM.js.original
  239. java -jar ~/compiler.jar --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  240. rm $PROGRAM.js.original
  241. echo "Done."
  242. fi