Skip to content

Instantly share code, notes, and snippets.

@jasononeil
Created April 22, 2013 06:18
Show Gist options
  • Select an option

  • Save jasononeil/5432759 to your computer and use it in GitHub Desktop.

Select an option

Save jasononeil/5432759 to your computer and use it in GitHub Desktop.

Revisions

  1. jasononeil created this gist Apr 22, 2013.
    15 changes: 15 additions & 0 deletions HaxeScript.hx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #!/usr/bin/haxex

    // Run with "./HaxeScript.hx some args here"

    class HaxeScript
    {
    public static function main()
    {
    trace('Hi, the following ${Sys.args().length} arguments were passed in:');
    for (a in Sys.args())
    {
    trace (' $a');
    }
    }
    }
    37 changes: 37 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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` and then use that.

    Installation:

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

    Usage:

    #!/usr/bin/haxex
    class MyScript
    {
    public static function main()
    {
    trace('My Script is executing!');
    }
    }

    Running:

    chmod +x MyScript.hx
    ./MyScript.hx

    Pretty neat-o if you ask me.

    What this script actually does:

    1. Reads the first argument as "filename"
    2. Gets rid of the "./" at the start of $filename
    3. Gets rid of the ".hx" at the end of $filename
    4. Turns the "/" dir separators into "." package separators, in case your script is in a subpackage.

    Limitations:

    If your script is in the root package (has no `package` statement at the top of the file), then you need to call it from the directory the script is in. If your script is in a sub-package, you need to call it from the root-level package. Easy rule of thumb: don't use packages, and call from the directory the script is in.
    15 changes: 15 additions & 0 deletions haxex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #!/bin/bash

    # First argument is the filename
    filename=$1

    # Remove "./" if it's there
    filename=${filename/"./"/""}

    # Remove ".hx" if it's there
    filename=${filename/".hx"/""}

    # Change "/" to "."
    filename=${filename/"/"/"."}

    haxe --run $filename ${@:2}