jtalkc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 so that
  5. # we can find the st and js directories etc.
  6. # Earlier we used this but it does not work on Mac
  7. # JTALK=$(readlink -f `dirname ${0}`/..)
  8. TARGET=`dirname ${0}`/..
  9. pushd . >/dev/null
  10. cd $TARGET
  11. JTALK="`\pwd -P`"
  12. popd >/dev/null
  13. function usage {
  14. cat <<ENDOFHELP
  15. Usage: $0 [-L level] [-l lib1,lib2...] [-i file] [-m class] [-M file]
  16. [-o] [-O|-A] [-d] [-s suffix] [-S suffix] [file1 [file2 ...]] [Program]
  17. Will compile Jtalk files - either separately or into a runnable complete
  18. program. If no .st files are listed only a linking stage is performed.
  19. Files listed will be handled using these rules:
  20. *.js
  21. Files are linked (concatenated) in listed order.
  22. If not found we look in $JTALK/js
  23. *.st
  24. Files are compiled into .js files before concatenated.
  25. If not found we look in $JTALK/st.
  26. NOTE: Each file is currently considered to be a fileout of a single class
  27. category of the same name as the file!
  28. If no Program is specified each given .st file will be compiled into
  29. a .js file. Otherwise a <Program>.js file is linked together based on
  30. the options:
  31. -L level
  32. Add libraries to a predefined level (default is KERNEL) of environment
  33. where level is:
  34. KERNEL - a reasonable level for server side programs or libraries.
  35. PARSER - if you need to parse stuff, includes PetitParser etc.
  36. COMPILER - if you need to compile Jtalk code.
  37. CANVAS - if you need HTML DOM manipulation, typical for client side.
  38. JQUERY - if you want JQuery stuff on top of Canvas.
  39. IDE - if you are extending the IDE with new tools etc.
  40. -l library1,library2
  41. Additionally add listed libraries (no spaces or .js) in listed order.
  42. -i file
  43. Add library initializer <file> instead of default $JTALK/js/init.js
  44. -m class
  45. Add at end a call to #main in class <class>.
  46. -M file
  47. Add at end javascript file <file> acting as main.
  48. -o
  49. Optimize each js file using the Google closure compiler.
  50. Using Closure compiler found at ~/compiler.jar
  51. -O
  52. Optimize final <Program>.js using the Google closure compiler.
  53. Using Closure compiler found at ~/compiler.jar
  54. -A Same as -O but use --compilation_level ADVANCED_OPTIMIZATIONS
  55. -d
  56. Additionally export code for deploy - stripped from source etc.
  57. Uses suffix ".deploy.js" in addition to any explicit given suffic using -s.
  58. -s suffix
  59. Add <suffix> to compiled js files so that File.st is compiled into
  60. File.<suffix>.js.
  61. -S suffix
  62. Use <suffix> for all libraries accessed using -L or -l. This makes it possible
  63. to have multiple flavors of Jtalk and libraries in the same place.
  64. Example invocations:
  65. Just compile Kernel.st to Kernel.js:
  66. jtalkc Kernel.st
  67. Compile Hello.st to Hello.js and create complete program called
  68. Program.js and adding a call to class method #main in class Hello:
  69. jtalkc -N -m Hello Hello.st Program
  70. Compile two .st files into corresponding .js files,
  71. and link with specific myboot.js, myKernel.js, myinit.js
  72. and main.js and create complete program called Program.js:
  73. jtalkc -M main.js myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  74. ENDOFHELP
  75. exit 1;
  76. }
  77. # Check we at least got one argument
  78. if [ -z $1 ] ; then
  79. usage
  80. fi
  81. # Define our predefined library combinations
  82. BOOT="boot"
  83. KERNEL="$BOOT Kernel"
  84. PARSER="$KERNEL Parser"
  85. COMPILER="$PARSER Compiler"
  86. CANVAS="$COMPILER Canvas"
  87. JQUERY="$COMPILER JQuery"
  88. IDE="$JQUERY IDE SUnit Examples"
  89. # Predefined initializer
  90. INITIALIZER="$JTALK/js/init.js"
  91. # Default values
  92. ENV=
  93. INIT=$INITIALIZER
  94. MAIN=
  95. MAINFILE=
  96. BASE=$KERNEL
  97. LOAD=
  98. CLOSUREOPTS=
  99. # Ok, bad coding practice but hey, who would use such a suffix?
  100. SUFFIX=no-silly-suffix
  101. SUFFIXUSED=
  102. DEPLOY=false
  103. NODECOMPILE=nodecompile
  104. # Read options and shift them away
  105. while getopts "L:l:i:m:M:oOAds:S:h?" o; do
  106. case "$o" in
  107. L) BASE=${!OPTARG};; # If OPTARG is "KERNEL" this sets BASE to $KERNEL.
  108. l) LOAD=$OPTARG;;
  109. i) INIT=$OPTARG;;
  110. m) MAIN=$OPTARG;;
  111. M) MAINFILE=$OPTARG;;
  112. o) CLOSURE=true
  113. CLOSUREPARTS=true;;
  114. O) CLOSURE=true
  115. CLOSUREFULL=true;;
  116. A) CLOSURE=true
  117. CLOSUREOPTS="$CLOSUREOPTS --compilation_level ADVANCED_OPTIMIZATIONS"
  118. CLOSUREFULL=true;;
  119. d) DEPLOY=true;;
  120. s) SUFFIX=$OPTARG
  121. SUFFIXUSED=$SUFFIX;;
  122. S) LOADSUFFIX=$OPTARG
  123. SUFFIXUSED=$SUFFIX;;
  124. h) usage;;
  125. [?]) usage;;
  126. esac
  127. done
  128. shift $(($OPTIND - 1))
  129. # Check for Closure compiler and Java
  130. if [ ! -z $CLOSURE ]; then
  131. java > /dev/null
  132. if [ $? -eq 0 ]; then
  133. if [ ! -f ~/compiler.jar ]; then
  134. echo "Can not find Closure compiler at ~/compiler.jar"
  135. exit 1
  136. fi
  137. else
  138. echo "java is not installed and is needed for -O, -A or -o (Closure compiler)."
  139. exit 1
  140. fi
  141. fi
  142. # Function for looking up listed js files
  143. function resolvejs {
  144. FNAME="$1$LOADSUFFIX.js"
  145. if [ -f $FNAME ]; then
  146. RESOLVED="$FNAME"
  147. else
  148. if [ -f $JTALK/js/$FNAME ]; then
  149. RESOLVED="$JTALK/js/$FNAME"
  150. else
  151. echo "Javascript file not found: $FNAME"
  152. exit 1
  153. fi
  154. fi
  155. }
  156. # Resolve listed libraries in $BASE deparated 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 ,
  163. LOAD=${LOAD//,/\ }
  164. for FILE in $LOAD
  165. do
  166. resolvejs $FILE
  167. TOLOAD="$TOLOAD $RESOLVED"
  168. TOADD="$TOADD $RESOLVED"
  169. done
  170. # Resolve COMPILER
  171. for FILE in $COMPILER
  172. do
  173. resolvejs $FILE
  174. TOCOMPILER="$TOCOMPILER $RESOLVED"
  175. done
  176. # Add supplied libraries to load (they are already resolved)
  177. TOCOMPILER="$TOCOMPILER$TOLOAD"
  178. THEREST="init $JTALK/bin/$NODECOMPILE"
  179. # Resolve init and nodecompile
  180. for FILE in $THEREST
  181. do
  182. resolvejs $FILE
  183. TOCOMPILER="$TOCOMPILER $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 jtalkc.XXXXXX`
  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 $CATEGORY$SUFFIXUSED.js"
  203. else
  204. if [ -f $JTALK/st/$1 ]; then
  205. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  206. COMPILED="$COMPILED $CATEGORY$SUFFIXUSED.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 $TOCOMPILER > $TMPDIR/compiler.js
  229. # Compile all collected .st files to .js
  230. echo "Loading libraries$TOOURCOMPILER and compiling ..."
  231. node $TMPDIR/compiler.js $DEPLOY $SUFFIX $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 $CLOSUREOPTS --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 $CLOSUREOPTS --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  298. rm $PROGRAM.js.original
  299. echo "Done."
  300. fi