|
#!/bin/bash |
|
|
|
if [ $# -eq 0 ]; then |
|
echo "haxex - The missing shell for Haxe" |
|
echo "Credits: @jasononeil, @clarkjones, @Matan" |
|
echo "Usage:" |
|
echo "- Hashbang within a Haxe class. e.g. #!/usr/bin/env haxex -lib mcli @" |
|
echo " Format: <interpreter> <haxe-compiler-args> @ <cli-args>" |
|
echo " Everything before @ are arguments passed directly to the haxe compiler." |
|
echo " @ donates where --run will be placed." |
|
echo " Everything after @ will be used as cli arguments into your script." |
|
echo "- bootstrap <path/to/Class>" |
|
echo " Quickly bootstrap a new script with given name and location." |
|
echo " mcli will be used by default." |
|
echo " Parent directories will be created." |
|
echo " +x Execution rights will be granded to file." |
|
echo "Notes:" |
|
echo " Scripts must be in root package. See --cwd below." |
|
echo " Scripts must start with an uppercase. This is Haxe compiler requirement." |
|
echo " Additional haxelibs used must be installed manually before executing the script." |
|
echo " --cwd <shell-cwd> is always passed as the first cli args." |
|
echo " Allows you to know where script is actually being invoked from." |
|
echo " Sys.getCwd() will return the housing directory of the SCRIPT, not the shell exection cwd." |
|
echo " Prevents usage of class packages, but worth it for gaining pwd location." |
|
exit 0 |
|
fi |
|
|
|
if [[ "$1" == "bootstrap" && ! -z "$2" ]]; then |
|
# Path without .hx |
|
path=${2/\.hx/""} |
|
# Path with .hx |
|
target="$path.hx" |
|
|
|
# Exit on error |
|
set -e |
|
|
|
# Ensure any leading directories exist |
|
mkdir -p "$(dirname $path)" |
|
|
|
|
|
if [[ -f "$target" || -d "$target" ]]; then |
|
echo "Aborting! '$2.hx' already exists!" |
|
exit 1 |
|
fi |
|
|
|
echo "Bootstrapping file $target" |
|
cat > "$target" <<'_EOF' |
|
#!/usr/bin/env haxex -lib mcli @ |
|
|
|
/** |
|
Taken from mcli example https://github.com/waneck/mcli |
|
Say hello. |
|
Example inspired by ruby's "executable" lib example |
|
**/ |
|
class HaxeScript extends mcli.CommandLine |
|
{ |
|
/** |
|
Current working directory always passed from haxex |
|
**/ |
|
public var cwd : String; |
|
|
|
/** |
|
Say it in uppercase? |
|
**/ |
|
public var loud : Bool; |
|
|
|
/** |
|
Show this message. |
|
**/ |
|
public function help() |
|
{ |
|
Sys.println(this.showUsage()); |
|
Sys.println(''); |
|
Sys.println('This script is located at: "${Sys.programPath()}"'); |
|
Sys.exit(0); |
|
} |
|
|
|
public function runDefault(?name : String) |
|
{ |
|
if (name == null) |
|
name = "World"; |
|
var msg = 'Hello, $name! From "$cwd"'; |
|
if (loud) |
|
msg = msg.toUpperCase(); |
|
Sys.println(msg); |
|
} |
|
|
|
public static function main() |
|
{ |
|
new mcli.Dispatch(Sys.args()).dispatch(new HaxeScript()); |
|
} |
|
} |
|
_EOF |
|
|
|
# Replace Class name |
|
sed -i 's,HaxeScript,'$(basename "$path")',g' "$target" |
|
|
|
# Add execution rights |
|
chmod a+x "$target" |
|
|
|
exit 0 |
|
fi |
|
|
|
args=$@ |
|
|
|
# Create compiler args |
|
compArgs=${args/@*/" --run"} |
|
|
|
# Create string to match and remove compiler args |
|
compMatch=${compArgs/ --run/"@"} |
|
|
|
# Get cli args by removing compiler args |
|
cliArgs=${args/${compMatch}/""} |
|
|
|
set -- ${cliArgs} |
|
|
|
# Give filepath its own var |
|
filepath=$1 |
|
|
|
# Get filename and directory from filepath |
|
filename=$(basename ${filepath}) |
|
dirpath=$(dirname ${filepath}) |
|
|
|
# Set args to be passed to script |
|
cliArgs=${@:2} |
|
|
|
# Remove ".hx" if it's there |
|
module=${filename/\.hx/""} |
|
|
|
# Grab the current working directory |
|
cwd=$(pwd) |
|
|
|
# Execute |
|
# - step into CWD (avoid packages and allows script execution from any CWD) |
|
# - pass all additional haxe flags |
|
# - specify our module to run |
|
# - pass a special --cwd flag as first CLI flag to le the script know where the original CWD was/is |
|
# - pass all additional args |
|
haxe --cwd "${dirpath}" ${compArgs} ${module} --cwd "${cwd}" ${cliArgs} |