1
0

jtalkc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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] [-p prefix] [-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. -p prefix
  43. Add <prefix> to compiled js files so that File.st is compiled into
  44. <prefix>File.st.
  45. -l library1,library2
  46. Load listed libraries (no spaces) into Compiler before compiling.
  47. -L library1,library2
  48. Load listed libraries (no spaces) into Compiler before compiling and also
  49. into Program.js in listed order.
  50. -i
  51. Add library initializer <file>.
  52. -I file
  53. Add library standard initializer $JTALK/js/init.js
  54. -m class
  55. Add call to #main in class <class>.
  56. -M file
  57. Add javascript file <file> at the end acting as main.
  58. Example invocations:
  59. Just compile Kernel.st to Kernel.js:
  60. jtalkc Kernel.st
  61. Compile Hello.st to Hello.js and create complete program called
  62. Program.js for Node.js including boot.js, Kernel.js, init.js and
  63. adding a call to class method #main in class Hello:
  64. jtalkc -N -m Hello Hello.st Program
  65. Compile two .st files into corresponding .js files,
  66. and link with specific myboot.js, myKernel.js, myinit.js
  67. and main.js and create complete program called Program.js:
  68. jtalkc -M main.js -I myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  69. ENDOFHELP
  70. exit 1;
  71. }
  72. # Check we at least got one argument
  73. if [ -z $1 ] ; then
  74. usage
  75. fi
  76. # Define our predefined library combinations
  77. BOOT="boot.js"
  78. KERNEL="$BOOT Kernel.js"
  79. COMPILER="$KERNEL Parser.js Compiler.js"
  80. CANVAS="$COMPILER Canvas.js"
  81. IDE="$CANVAS JQuery.js IDE.js SUnit.js Examples.js"
  82. # Predefined initializer
  83. INITIALIZER="$JTALK/js/init.js"
  84. # Default values
  85. ENV=
  86. INIT=
  87. MAIN=
  88. MAINFILE=
  89. BASE=
  90. LOAD=
  91. LOADANDADD=
  92. # Ok, bad coding practice but hey, who would use such a prefix?
  93. PREFIX=no-silly-prefix
  94. PREFIXUSED=
  95. DEBUG=false
  96. NODECOMPILE=nodecompile.js
  97. # Read options and shift them away
  98. while getopts "NDEKCJoOgp:l:L:i:IM:m:h?" o; do
  99. case "$o" in
  100. N) ENV=NODE
  101. BASE=$KERNEL
  102. INIT=$INITIALIZER;;
  103. D) ENV=D8
  104. BASE=$KERNEL
  105. INIT=$INITIALIZER;;
  106. E) ENV=ENYO
  107. BASE=$KERNEL
  108. INIT=$INITIALIZER;;
  109. K) BASE=$KERNEL;;
  110. C) BASE=$COMPILER;;
  111. J) BASE=$IDE;;
  112. o) CLOSURE=true
  113. CLOSUREPARTS=true;;
  114. O) CLOSURE=true
  115. CLOSUREFULL=true;;
  116. g) DEBUG=true;;
  117. p) PREFIX=$OPTARG
  118. PREFIXUSED=$PREFIX;;
  119. l) LOAD=$OPTARG;;
  120. L) LOADANDADD=$OPTARG;;
  121. I) INIT=$INITIALIZER;;
  122. i) INIT=$OPTARG;;
  123. M) MAINFILE=$OPTARG;;
  124. m) MAIN=$OPTARG;;
  125. h) usage;;
  126. [?]) usage;;
  127. esac
  128. done
  129. shift $(($OPTIND - 1))
  130. # Check for Closure compiler and Java
  131. if [ ! -z $CLOSURE ]; then
  132. java > /dev/null
  133. if [ $? -eq 0 ]; then
  134. if [ ! -f ~/compiler.jar ]; then
  135. echo "Can not find Closure compiler at ~/compiler.jar"
  136. exit 1
  137. fi
  138. else
  139. echo "java is not installed and is needed for -O or -o (Closure compiler)."
  140. exit 1
  141. fi
  142. fi
  143. # Function for looking up listed js files
  144. function resolvejs {
  145. if [ -f "$1" ]; then
  146. RESOLVED="$1"
  147. else
  148. if [ -f $JTALK/js/$1 ]; then
  149. RESOLVED="$JTALK/js/$1"
  150. else
  151. echo "Javascript file not found: $1"
  152. exit 1
  153. fi
  154. fi
  155. }
  156. # Resolve listed libraries in $BASE separated by spaces
  157. for FILE in $BASE
  158. do
  159. resolvejs $FILE
  160. TOBASE="$TOBASE $RESOLVED"
  161. done
  162. # Resolve listed libraries in $LOAD separated by spaces
  163. LOAD=${LOAD//,/\ }
  164. for FILE in $LOAD
  165. do
  166. resolvejs $FILE
  167. TOLOAD="$TOLOAD $RESOLVED"
  168. done
  169. # Resolve listed libraries in $LOADANDADD separated by spaces
  170. LOADANDADD=${LOADANDADD//,/\ }
  171. for FILE in $LOADANDADD
  172. do
  173. resolvejs $FILE
  174. TOLOAD="$TOLOAD $RESOLVED"
  175. TOADD="$TOADD $RESOLVED"
  176. done
  177. # Define our Compiler loading supplied libraries
  178. OURCOMPILER="$COMPILER $TOLOAD init.js $JTALK/nodejs/$NODECOMPILE"
  179. # Resolve OURCOMPILER
  180. for FILE in $OURCOMPILER
  181. do
  182. resolvejs $FILE
  183. TOOURCOMPILER="$TOOURCOMPILER $RESOLVED"
  184. done
  185. # Add supplied libraries
  186. LIBS="$TOBASE $TOADD"
  187. # Get a unique tempdir and make it get auto removed on exit
  188. TMPDIR=`mktemp -d`
  189. trap "rm -rf $TMPDIR" EXIT
  190. # --------------------------------------------------
  191. # Collect libraries and Smalltalk files looking
  192. # both locally and in $JTALK/js and $JTALK/st
  193. # --------------------------------------------------
  194. PROGRAM=
  195. until [ "$*" = "" ]
  196. do
  197. case $1 in
  198. *.st)
  199. CATEGORY=`basename $1 .st`
  200. if [ -f "$1" ]; then
  201. COMPILE="$COMPILE $1 $CATEGORY"
  202. COMPILED="$COMPILED $PREFIXUSED$CATEGORY.js"
  203. else
  204. if [ -f $JTALK/st/$1 ]; then
  205. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  206. COMPILED="$COMPILED $PREFIXUSED$CATEGORY.js"
  207. else
  208. echo "JTalk file not found: $1"
  209. exit 1
  210. fi
  211. fi
  212. ;;
  213. *.js)
  214. resolvejs $1
  215. LIBS="$LIBS $RESOLVED"
  216. ;;
  217. *)
  218. # Will end up being the last non js/st argument
  219. PROGRAM=$1
  220. ;;
  221. esac
  222. shift
  223. done
  224. # --------------------------------------------------
  225. # Actual compilation phase of collected .st files
  226. # --------------------------------------------------
  227. # Create compiler dynamically
  228. cat $TOOURCOMPILER > $TMPDIR/compiler.js
  229. # Compile all collected .st files to .js
  230. echo "Loading libraries $TOOURCOMPILER and compiling ..."
  231. node $TMPDIR/compiler.js $DEBUG $PREFIX $COMPILE
  232. # Verify all .js files corresponding to .st files were created, otherwise exit
  233. IFS=" "
  234. for FILE in $COMPILED
  235. do
  236. if [ ! -f "$FILE" ]; then
  237. echo "Failed compilation of $FILE, exiting."
  238. exit 1
  239. fi
  240. done
  241. if [ ! -z $CLOSUREPARTS ]; then
  242. echo "Compiling all js files using Google closure compiler."
  243. ALLJSFILES="$COMPILED $LIBS"
  244. for FILE in $ALLJSFILES
  245. do
  246. mv $FILE $FILE.original
  247. java -jar ~/compiler.jar --js $FILE.original --js_output_file $FILE
  248. rm $FILE.original
  249. done
  250. fi
  251. if [ -z $PROGRAM ]; then
  252. echo "Done."
  253. exit 0
  254. fi
  255. # --------------------------------------------------
  256. # Now we start composing resulting javascript file.
  257. # --------------------------------------------------
  258. # Add collected libraries to libs.js file.
  259. if [ ! -z "$LIBS" ]; then
  260. echo "Adding libraries $LIBS ..."
  261. cat $LIBS > $TMPDIR/libs.js
  262. LIBS=$TMPDIR/libs.js
  263. fi
  264. echo "Adding Jtalk code$COMPILED ..."
  265. # Check for init file
  266. if [ ! -z "$INIT" ]; then
  267. if [ -f "$INIT" ]; then
  268. echo "Adding initializer $INIT ..."
  269. else
  270. echo "Can not find init file $INIT, exiting."
  271. exit 1
  272. fi
  273. fi
  274. # Check for adding main
  275. if [ ! -z "$MAIN" ]; then
  276. echo "Adding call to $MAIN class >> main ..."
  277. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  278. MAIN=$TMPDIR/main.js
  279. fi
  280. # Check for adding main file
  281. if [ ! -z "$MAINFILE" ]; then
  282. if [ -f "$MAINFILE" ]; then
  283. echo "Adding main as $MAINFILE ..."
  284. else
  285. echo "Can not find main file $MAINFILE, exiting."
  286. exit 1
  287. fi
  288. MAIN=$MAINFILE
  289. fi
  290. # And finally concatenate Program.js
  291. echo "Writing $PROGRAM.js ..."
  292. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  293. echo "Done."
  294. if [ ! -z $CLOSUREFULL ]; then
  295. echo "Compiling $PROGRAM.js file using Google closure compiler."
  296. mv $PROGRAM.js $PROGRAM.js.original
  297. java -jar ~/compiler.jar --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  298. rm $PROGRAM.js.original
  299. echo "Done."
  300. fi