Skip to content

Instantly share code, notes, and snippets.

@Matan
Forked from clarkjones/HaxeScript.hx
Last active February 5, 2022 12:27
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Matan/85273fc592950f9aae8d143857013c21 to your computer and use it in GitHub Desktop.
Save Matan/85273fc592950f9aae8d143857013c21 to your computer and use it in GitHub Desktop.
haxex, a short shell script that can be used so you have Haxe shell scripting

Note: This is a modified version of the haxex script created by @jasononeil and @clarkjones

Modifications from orginal

  • Haxe compiler args: Use libs, etc. See haxe --help
  • Help: Execute script without arguments to print help.
  • PWD support: Allow execution from any directory. E.g. bin/MyScript.hx without requiring a package.
  • Bootstrap: Instantly create a new script. See help message.

This is a tiny shell script that you can use to make scripting with Haxe very straight forward.

Using a straight #!/usr/bin/haxe doesn't work currently, as the Haxe compiler will have to be a little more clever about realising it is a shell script and that we wish to execute it. Maybe this will be native some day, for now, you can install the script below into /usr/bin/haxex or /usr/local/bin/haxex and then use that.

Installation:

  1. sudo nano /usr/bin/haxex, paste the 'haxex' file in there
  2. sudo chmod +x /usr/bin/haxex

Help (see Notes section for limitions): haxex (execute without args)

Bootstrap:

haxex bootstrap bin/MyFirstScript.hx
haxelib install mcli
bin/MyFirstScript.hx

Simple Example:

#!/usr/bin/env haxex -debug @
class MyScript 
{
public static function main() 
{
	trace('My Script is executing!');
	#if debug
		trace('with debug flag!');
	#end
}
}

Running:

chmod +x MyScript.hx
./MyScript.hx

For the included HaxeScript:

 haxelib install mcli
 chmod +x HaxeScript.hx
./HaxeScript.hx [yourname] --loud
#!/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());
}
}
#!/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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment