Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@asufana
Last active July 6, 2016 15:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asufana/b64f280fe5e442a45253 to your computer and use it in GitHub Desktop.
Save asufana/b64f280fe5e442a45253 to your computer and use it in GitHub Desktop.
OSXでのF#(FSharp)環境整備

OSX での F# (FSharp) 環境整備

環境情報

  • Mac OSX El Capitan 10.11.1
  • mono 4.2.1

mono インストール

$ brew install mono
$ mono --version
Mono JIT compiler version 4.2.1 (explicit/6dd2d0d Fri Nov  6 12:25:19 EST 2015)
mono 設定

http://jessekallhoff.com/2015/06/24/aspnet-vnext-on-osx-kqueue-filesystemwatcher-has-reached-the-maximum-number-of-files-to-watch/

$ export MONO_MANAGED_WATCHER=false

nuget スクリプト作成

F# パッケージ取得ツールである nuget.exe の呼び出しスクリプトを作成する

nuget.exe ダウンロード
$ wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
$ sudo mv nuget.exe /usr/local/bin
nuget スクリプト作成
$ touch nuget
$ vim nuget
#!/bin/sh
script_dir="$(cd "$(dirname "${BASH_SOURCE:-${(%):-%N}}")"; pwd)"
mono --runtime=v4.0 ${script_dir}/nuget.exe $*

$ chmod 777 nuget
$ sudo mv nuget /usr/local/bin
nugetinstall スクリプト作成
$ touch nugetinstall
$ vim nugetinstall
#!/bin/sh
nuget install $* -OutputDirectory packages -ExcludeVersion

$ chmod 777 nugetinstall
$ sudo mv nuget /usr/local/bin
動作確認
$ nugetinstall FAKE
...
Successfully installed 'FAKE 4.11.3' to packages

$ tree
.
└── packages
    └── FAKE
        ├── FAKE.nupkg
        ├── docs
        │   ├── RELEASE_NOTES.html
		...

Paket スクリプト作成

F# パッケージ管理ツールである paket.exe の呼び出しスクリプトを作成する

$ touch paket
$ vin paket
#!/bin/sh
paket_path=./packages/Paket/tools/paket.exe
if [ ! -e $paket_path ]; then
  echo "Paket installing..."
  nugetinstall Paket
fi
mono $paket_path $*

$ chmod 777 paket
$ sudo mv paket /usr/local/bin
動作確認
$ paket init

//依存設定
$ vim paket.dependencies
source https://nuget.org/api/v2
nuget Suave

$ paket install
$ tree .
.
├── packages
│   ├── FSharp.Core
│   │   ├── FSharp.Core.4.0.0.1.nupkg
...
│   ├── Paket
│   │   ├── Paket.nupkg
...
│   └── Suave
│       ├── Suave.0.33.0.nupkg
...

FAKE スクリプト作成

F# ビルドツールである fake.exe の呼び出しスクリプトを作成する

$ touch fake
$ vim fake
#!/bin/sh
fake_path=./packages/FAKE/tools/FAKE.exe
if [ ! -e $fake_path ]; then
  echo "FAKE installing..."
  nugetinstall FAKE
fi
mono $fake_path $*

$ chmod 777 fake
$ sudo mv fake /usr/local/bin
fakerun スクリプト作成

Paketでライブラリ取得後に、Fake起動するスクリプトを作成

$ touch fakerun
$ vim fakerun
#!/bin/sh
if [ ! -e paket.lock ]; then
  paket install
else
  paket restore
fi
fake --printdetails --fsiargs $*

$ chmod 777 fakerun
$ sudo mv fakerun /usr/local/bin

Paket/FAKE を利用した Suave.io アプリ

依存ライブラリの設定と取得

$ paket init
$ vim paket.dependencies
source https://www.nuget.org/api/v2
nuget FAKE
nuget Suave
nuget FSharp.Compiler.Service

$ paket install

Webアプリの最小限コード

$ touch suave.fsx
$ vim suave.fsx
#r "packages/Suave/lib/net40/Suave.dll"
#r "packages/FAKE/tools/FakeLib.dll"
open Fake
open System
open System.IO
open Suave
open Suave.Http
open Suave.Web
open Suave.Types
open Suave.Http.Successful

Target "run" (fun _ ->
    startWebServer defaultConfig (OK "Hello")
)
RunTargetOrDefault "run"

実行する

$ fakerun suave.fsx
[I] 2015-12-23T04:32:09.6107750Z: listener started in 43.286 ms with binding 127.0.0.1:8083 [Suave.Tcp.tcpIpServer]

http://localhost:8083/ にアクセス!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment