ntalkc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/bash
  2. #
  3. # This is a trivial "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. USAGE="Usage: $0 [-r] file1 file2 ... file3 Program
  7. Will compile a JTalk program into a single javascript file called
  8. <Program>.js by concatenating listed files using these rules:
  9. *.js files are concatenated as is. If not found we look in $JTALK/js
  10. *.st files are compiled into .js files. If not found we look in $JTALK/st.
  11. Each file is currently considered to be a fileout of a single class
  12. category of the same name as the file.
  13. NOTE: boot.js and Kernel.js is always added first and init.js
  14. is always added last. Finally main.js is added if found.
  15. If -r is used we also run node with the resulting Program.js.
  16. Example invocations:
  17. Compile Hello.st to Hello.js and create
  18. complete program called Program.js:
  19. ntalkc Hello.st Program
  20. Compile two different class categories into corresponding .js files,
  21. link with two existing javascript libraries and create complete
  22. program called Program.js:
  23. ntalkc lib1.js lib2.js CategoryOne.st CategoryTwo.st Program
  24. "
  25. if [ "$#" == "0" ]; then
  26. echo "$USAGE"
  27. exit 1
  28. fi
  29. if [ $1 == "-r" ]; then
  30. RUN=true
  31. shift
  32. fi
  33. # Get a unique tempdir and make sure it gets nuked later
  34. TMPDIR=`mktemp -d`
  35. trap "rm -rf $TMPDIR" EXIT
  36. # Collect libraries and Smalltalk files
  37. until [ "$*" = "" ]
  38. do
  39. case $1 in
  40. *.st)
  41. CATEGORY=`basename $1 .st`
  42. if [ -f "$1" ]; then
  43. COMPILE="$COMPILE $1 $CATEGORY"
  44. COMPILED="$COMPILED $CATEGORY.js"
  45. else
  46. if [ -f $JTALK/st/$1 ]; then
  47. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  48. COMPILED="$COMPILED $CATEGORY.js"
  49. else
  50. echo "JTalk file not found: $1"
  51. fi
  52. fi
  53. shift
  54. ;;
  55. *.js)
  56. if [ -f "$1" ]; then
  57. LIBS="$LIBS $1"
  58. else
  59. if [ -f $JTALK/js/$1 ]; then
  60. LIBS="$LIBS $JTALK/js/$1"
  61. else
  62. echo "Javascript file not found: $1"
  63. fi
  64. fi
  65. ;;
  66. esac
  67. # Will end up being the last argument
  68. PROGRAM=$1
  69. shift
  70. done
  71. # Create compiler dynamically
  72. cat $JTALK/js/boot.js $JTALK/js/Kernel.js $JTALK/js/Parser.js $JTALK/js/Compiler.js $JTALK/js/init.js $JTALK/nodejs/nodecompile.js > $TMPDIR/compiler-all.js
  73. # Compile all collected .st files to .js
  74. echo "Compiling$COMPILED ..."
  75. node $TMPDIR/compiler-all.js $COMPILE
  76. # Verify all .js files corresponding .st files were created, otherwise exit
  77. IFS=" "
  78. for FILE in $COMPILED
  79. do
  80. if [ ! -f "$FILE" ]; then
  81. echo "Failed compilation of $FILE, exiting."
  82. exit 1
  83. fi
  84. done
  85. # We must have these first...
  86. echo "Adding boot.js and Kernel.js ..."
  87. BOOT="$JTALK/js/boot.js $JTALK/js/Kernel.js"
  88. # Compose the complete libs.js file from collected .js filenames.
  89. if [ -n "$LIBS" ]; then
  90. echo "Adding libraries $LIBS ..."
  91. cat $LIBS > $TMPDIR/libs.js
  92. LIBS=$TMPDIR/libs.js
  93. fi
  94. # Check for init.js
  95. if [ -f "init.js" ]; then
  96. echo "Adding init.js ..."
  97. INIT="init.js"
  98. else
  99. echo "Adding $JTALK/js/init.js ..."
  100. INIT=$JTALK/js/init.js
  101. fi
  102. # Check for main.js
  103. if [ -f "main.js" ]; then
  104. echo "Adding main.js ..."
  105. MAIN="main.js"
  106. fi
  107. # And finally concatenate Program.js
  108. echo "Writing $PROGRAM.js ..."
  109. cat $BOOT $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  110. echo "Done."
  111. # Optionally run Program and give all args left to it
  112. if [ -n "$RUN" ]; then
  113. echo "Running program"
  114. echo "---------------"
  115. node $PROGRAM.js $@
  116. fi