• 7391a1f5797bd1cfdfb64b461a552080?s=80&d=mm

    Changes from fraya

    fraya - 10 months ago (Feb 08, 2017, 9:54 PM)
    Fix titles
  • Changes pending approval
      Looks like something's not quite right here.. We've been notified of the issue, and will get back to you soon.
      2_SettingUpTheEnvironment
      # 2 - Setting Up The Environment







      ### Learning Objectives :

      - Learn how to obtain the latest version of Fantom
      - Learn what comes with Fantom
      - Learn how to set up your environment for Fantom programming
      - Learn how to obtain an IDE or Text Editor for Fantom
      - Learn how to compile and run a Fantom application





      ### Introduction

      In this chapter we will be setting up our computer for Fantom programming. This is the first step in your journey to Fantom programming and the most important one. As of now the latest stable release of Fantom is Fantom 1.0.669. Fantom's versioning scheme is `major.minor.build.patch`. For recent instructions on how to set up Fantom on your computer you can visit their website ( http://fantom.org/doc/docTools/Setup.html ). I will show you how to obtain the latest release no matter what realease it is.




      ### Installing Fantom

      To install the latest release of Fantom visit this link ( https://bitbucket.org/fantom/fan-1.0/downloads ) to download the latest .zip file of Fantom.

      **Windows**

      1. Extract the contents of the .zip file to a directory. Preferably your C:\ directory. You can test the install
      2. Add **{ directory were fantom is }\bin** to your Path environment variable. ( Control Panel -> System -> Advance System Settings -> Environment Variables -> Path )

      You can run `fan -version` in the command prompt to see which runtime fantom is using. By deafault it is the current Java VM on your system.

      Fantom launcher will attempt to locate the current Java VM via the registry key *SOFTWARE\\JavaSoft\\Java Runtime Environment.CurrentVersion*. Fantom currently requires Java version 1.5 or greater to run. Java 1.6 or greater is required to compile from source.

      The Fantom launcher will attempt to use the latest version of .NET installed.
      Fantom currently requires .NET version 2.0 or greater.

      **Unix/Linux**

      1. Unpack .zip file to a directory
      2. Add the bin folder of the unpacked fantom folder to your PATH,
      `PATH=$PATH:{ Directory of unpacked Fantom folder }/bin`

      Fantom allows the first line of a source file to start with "#!" to run as a Unix shell script:

      #! /usr/bin/env fan
      class Script { static Void main() { echo("hi") } }

      chmod +x myscript.fan
      ./myscript.fan

      **Setting up SWT**

      Fantom's FWT library depends on the SWT Library to work. To install SWT on your system.


      1. Verify your platform identifier using `fan -version` command
      2. Download your platform's "swt.jar" from [eclipse.org](http://www.eclipse.org/swt/ "Eclipse Website")
      3. Put "swt.jar" into *{ path to java folder }/ext/{platform}/swt.jar*




      ### What's Included

      Now that you have Fantom setup on your system lets take a look at what comes with your install.

      **Fantom's Source Code**
      *fantom/src*
      Everything from the compiler to the standard libraries are included. Fantom is open-source under the [Academic Free License version 3.0](http://opensource.org/licenses/AFL-3.0)

      **Fantom Laucher ( fan )**
      *fantom/bin*
      This is a tool that is used to run Fantom code. You can invoke it by running it or using the `fan` command. For more information on the Fantom launcher visit [Fantom's Website](http://fantom.org/doc/docTools/Fan.html).

      **Fantom Interactive Shell ( fansh )**
      *fantom/bin*
      This is tool for programming Fantom in an interactive environment. You can use this to experiment with Fantom code or just explore fantom code. You can invoke it by running it or running the `fansh` command.

      **Fantom Unit Test Framwork ( fant ) **
      *fantom/bin*
      This tool is for unit testing in Fantom. It's very similar to common test frameworks such as JUnit. The key concepts for testing:

      - Write test classes which subclass Test
      - Write test methods which start with "test"
      - Assert test conditions via the "verifyXXX" methods
      - Run the tests the fant command line tool
      You can invoke it by running it or running the `fant` command.

      **Fantom Disassembler ( fanp ) **
      *fantom/bin*
      This tool is for disassembling compile fcode. This is a great tool for people who want to hack the compiler or want to see the runtime emit code. You can invoke it by running it or running the `fanp` command.

      **Fantom Build Engine**

      Fantom comes with its standard build engine to promote consistency and make sharing code easy. Build scripts are normal Fantom scripts which subclass from BuildScript. Characteristics of build scripts:

      - BuildScript base class handles common functions like command line parsing, environment setup, and logging
      - Subclasses define targets which are things the build script can do such as "compile", "clean", or "test"
      - Targets are implemented as normal Fantom methods which can execute any procedural code needed using the APIs installed
      - The build pod provides libraries of tasks which are designed to provide common chunks of functionality which can be composed to define targets
      The build pod provides a library of predefined BuildScript classes to handle common scripts - one you will use all the time is BuildPod used to build Fantom pods.




      ### Getting a Text editor or IDE

      IDE or Integrated Development Environments are great tools for learning a programming langauge. They make learning a programming langauge easier by given you code hints and also make compilation time faster. They help with managing projects and managing your dependencies. They also help reduce errors in your code and provide tools for debugging and testing your code. It also helps with code formatting and refactoring. Below is a list of Text Editors and IDEs that have good support for Fantom. Fantom also has a more updated list on their [site](http://fantom.org/doc/docTools/IDEs.html).

      IDEs :
      - [Flux](http://fantom.org/doc/docTools/Flux.html) Basic Text Editor that comes with Fantom.
      - [F4](http://www.xored.com/products/f4/) eclipsed base IDE by Xored
      - [FantomIDE](http://fantomide.colar.net/home#.UztrhlfLJsI) Netbeans based IDE by tcolar
      - [Camembert](http://www.status302.com/camembert#.UztsF1fLJsI) lightweight IDE written in Fantom by tcolar

      Text Editors ( *fantom/amd/tools* )
      - Emacs
      - jEdit
      - Vim
      - Textpad




      ### Testing the setup and Running an example

      To test that our setup is correct we will run a simple Fantom example. If you are using an IDE make a new project named *Test*. If you are using a plain text editor then make a new file named *Test.fan*. make sure the extension is .fan. Insert the following code into the file :

      class MainClass {
      Void main() {
      echo( "Hello, World!" )
      }
      }

      If you are using an IDE it should have a run & compile option. just run that option and the output should be as it is below. If you used a text editor you will have to run the script using the fantom launcher command line tool. go to your platforms command line interface ( Command Prompt or Terminal ). Type the following commands. Make sure you are in the directory where you saved *Test.fan*.

      > fan Test.fan
      Hello, World!


      Dont forget to add .fan to the end of your files you run.




      ### Closing

      You finally have your environment set up. We figure out how to get the latest version of Fantom. We learned how to set up our environment. We also looked at what came with the Fantom distribution. We learned the different IDEs and Tools that we can use to help with our development. Finally we ran a simple Fantom program. In the next chapter we will take an in-depth look at our sample program. We will examine some of Fantom syntax. We will also talk about similarities Fantom has to other languages. After that chapter we will be really getting into Fantom and talk about statements, expressions, and variables.







      ## Exercises

      *1. Change `echo( "Hello, World!" )` to just `echo`*

      *2. Change `echo( "Hello, World!" )` to `echo( "Goodbye, World" )`*

      *3. Try to make the fantom compiler throw an error. ( hint : remove keyword class, or remove the `(` and `)` after main )*