Shebangs in scalac code
-
26-02-2021 - |
题
The scala interpreter allows shebangs, but strangely, scalac borks on them. Are there any tricks like these to get around this flaw?
解决方案
#! in scala must be closed with !#. For example:
#!/bin/bash
script_dir=$(cd $(dirname "$0") >/dev/null; pwd -P)
classes_dir=${script_dir}/build/classes
src_dir=${script_dir}/src
mkdir -p ${classes_dir}
scalac -d ${classes_dir} $(find ${src_dir} -name '*.scala')
exec /usr/bin/scala -classpath ${classes_dir} "$0" "$@"
!#
其他提示
Why not strip them transparently before invoking scalac
in your build script?
I don't think this is a question of "obscure shebang syntax", but rather a "language specification" issue.
The Wikipedia article on Shebang does mention:
The contents of the shebang line will be automatically ignored by the interpreter, because the
#
character is a comment marker in many scripting languages.
Some language interpreters that do not use the hash mark to begin comments, such as Scheme, still may ignore the shebang line.
So if scalac hasn't in its specification the directives to ignore the first lines as "script header" like Scheme, we need to wait on your Jira case 5179.