bsb-nativeをインストールしておく。
プロジェクト作成
$ bsb -init working-directory-name -theme basic-reason
src/Demo.re
を削除して、src/Main.re
を新規作成する。
src/Main.re
src/Main.re
let message = "Hello, ReasonML!";
let () = print_endline(message);
TDDで開発したいのでディレクトリ構成とbsconfig.jsonをいじる
$ mkdir test
自作のRznUnitを導入する。
RznUnitの lib/RznUnit/RznUnit.re
と lib/RznUnit/RznUnit.rei
をダウンロードして、test/lib/RznUnit
フォルダにコピー。
test/Test.re
test/Test.re
let () = {
open Main;
open RznUnit;
"Test" >@@> [
"Get Main Message" @> () => StringAssert.equal("Hello, ReasonML!", message),
];
};
bsconfig.json
bsconfig.json
{
"name": "project-name",
"version": "0.1.0",
"sources": [
{
"dir" : "src",
"subdirs" : true
},
{
"dir" : "test",
"subdirs" : true,
"type" : "dev"
}
],
"refmt": 3,
"entries": [
{
"backend": "native",
"output-name": "output",
"main-module": "Main"
},
{
"backend": "bytecode",
"output-name": "output",
"main-module": "Test"
}
]
}
backendを、本番でnativeにし、テストでbytecodeに使い分けることで、擬似的にテスト駆動開発できるようにしている。
tasks.jsonをNative用に変更
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "npx bsb -backend native -make-world",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"panel": "dedicated",
"clear": true
},
"problemMatcher": {
"fileLocation": "absolute",
"owner": "ocaml",
"background": {
"activeOnStart": false,
"beginsPattern": ">>>> Start compiling",
"endsPattern": ">>>> Finish compiling"
},
"pattern": [
{
"regexp": "^File \"(.*)\", line (\\d+)(?:, characters (\\d+)-(\\d+))?:$",
"file": 1,
"line": 2,
"column": 3,
"endColumn": 4
},
{
"regexp": "^(?:(?:Parse\\s+)?(Warning|[Ee]rror)(?:\\s+\\d+)?:)?\\s+(.*)$",
"severity": 1,
"message": 2,
"loop": true
}
]
}
},
{
"label": "clean",
"type": "shell",
"command": "npx bsb -clean-world",
"problemMatcher": []
},
{
"label": "run",
"type": "shell",
"command": "npx bsb -backend native -make-world && lib/bs/native/output", // 実行ファイル名は、bsconfig.jsonの"output-name"で指定できる。
"presentation": {
"panel": "dedicated",
"clear": true
},
"problemMatcher": []
},
{
"label": "test",
"type": "shell",
"command": "npx bsb -backend bytecode -make-world && lib/bs/bytecode/output", // 実行ファイル名は、bsconfig.jsonの"output-name"で指定できる。
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"panel": "dedicated",
"clear": true
},
"problemMatcher": []
}
]
}
npmでも実行&テストできるようにpackage.jsonをいじっておく。
package.json
{
"name": "project-name",
"version": "0.1.0",
"scripts": {
"build": "npx bsb -backend native -make-world",
"test": "npx bsb -backend bytecode -make-world && lib/bs/bytecode/output",
"clean": "npx bsb -clean-world"
},
"keywords": [
"BuckleScript",
"Reason"
],
"author": "",
"license": "MIT",
"devDependencies": {
"bs-platform": "^4.0.7",
"bsb-native": "^4.0.1100"
},
"dependencies": {
"npm": "^6.13.1"
}
}
基本的には、"scripts"のところを変更しておく。
ディレクトリ構成
ここまででディレクトリ構成は以下のようになっているはず。
.
├── bsconfig.json
├── package.json
├── src
│ └── Main.re
└── test
├── Test.re
└── lib
└── RznUnit
├── RznUnit.re
└── RznUnit.rei
使用上の注意
初回は npm install
を実行しておく。
後は、Cmd+Shift+T
でテスト、Cmd+Shift+B
でビルドできる。
まとめ(テンプレート)
最終的に出来上がったのがこちら。
zipファイルをダウンロードして、npm install
したら使えます。