#2105 A Quick Start To Fantom Development

SlimerDude Sat 9 Mar 2013

ALIEN-AID: Visit A Guide To Fantom For Beginners for an updated version of this content.

Following is something I wrote for some colleagues last year - just thought I'd share it.


For those Newbies who can't see the wood for the trees.

A Quick Start To Fantom Development

This doc hopes to explain (concisely) how you might set up and use a typical Fantom dev environment. For the info presented here is scattered throughout the Fantom docs and is not too apparent from first glance.

To avoid complication, there are no IDEs in this section, we'll just assume plain text editors.

Install Fantom

Installing Fantom is no different to installing ant or maven.

  • download Fantom from fantom.org
  • unzip into somewhere convenient, we'll use /apps/fantom/
  • create the environment variable FAN_HOME = /apps/fantom
  • update the Path environment variable to include %FAN_HOME%/bin (windows notation) or $FAN_HOME/bin (*nix notation)

Run a Fantom Script

Every Fantom source file can be run like a script with the static Void main(Str[] args) method being invoked.

Lets make a file called Wotever.fan which looks like:

class Wotever {
    static Void main(Str[] args := [,]) {
        echo("Fantom. Wotever.")
    }
}

We can now run it with the following fan command:

C:\>fan Wotever.fan
Fantom. Wotever.

That maybe fine for a simple single file, but what of a Fantom project with multiple source files? Well...

Create and Build a Fantom Application

The typical dir structure for a simple application project looks like:

/myFantomProj/
  - fan/
    - Wotever.fan
  - build.fan

All the source files go in the fan dir (with the exception of the special build.fan). Lets make a Hello World; edit Wotever.fan to be:

class Wotever {
    static Void main(Str[] args := [,]) {
        echo("Fantom. Wotever.")
    }
}

To run this you first compile it into a .pod file (think .jar file). This is the job of build.fan:

class Build : build::BuildPod {
    new make() {
        podName = "example"
        summary = "Wotever Example"
        srcDirs = [`fan/`]
        depends = ["sys 1.0"]
        version = Version([1,0,0])
    }
}

The above build.fan compiles our example.pod, and like .jar files, pods are just .zip files. (Note it's the BuildPod superclass that does all the hard work, all we do is configure it.)

srcDirs are URIs, hence the back quotes. All dirs are relative to build.fan and must end with a forward slash. Also dirs are not recursive, so if we had public and internal source dirs, srcDirs would look like:

srcDirs = [`fan/`, `fan/public/`, `fan/internal/`]

The syntax for dependant pods requires both the pod name and the version, see Depend for full details.

Lets run build.fan as a script:

C:\myFantomProj>fan build.fan

compile [Wotever]
  Compile [Wotever]
    FindSourceFiles [1 files]
    WritePod [file:/C:/apps/fantom/lib/fan/Wotever.pod]
BUILD SUCCESS [89ms]!

Pods automatically get saved into /apps/fantom/lib/fan/ along with all the other Fantom system pods. So example.pod is now part of the Fantom install.

Run Your Application

The .pod is run in the same way as any (system) Fantom application:

> fan example::Wotever
Fantom. Wotever.

Note the pod::class notation.

Note also we didn't have to specify a class path or similar. Everything is taken from the Fantom install, even our example.pod. This simplifies execution quite a bit.

Let's make it even easier to launch our application, lets make a Main.fan like so:

/myFantomProj/
  - fan/
    - Main.fan
    - Wotever.fan
  - build.fan
class Main{
    Void main() {
        echo("My main() man.")
        Wotever.main()
    }
}

Now we can run our app by just specifying the pod:

> fan example
My main() man.
Fantom. Wotever.

Note how if no class is specified, Fantom looks for one called Main. Also, if no method is specified, Fantom looks for one called main. If the method is an instance method, Fantom creates an instance via a no-args constructor.

e.g. to call the main method directly we could type:

> fan example::Wotever.main

Have fun.

Login or Signup to reply.