Skip to content

Commit

Permalink
update to latest melange
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri committed Oct 5, 2023
1 parent 233b25e commit 112e65f
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 501 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ocaml.sandbox": {
"kind": "opam",
"switch": "${workspaceFolder:hello-native-bucklescript}"
}
}
30 changes: 9 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,28 @@ help: ## Print this help message

.PHONY: create-switch
create-switch:
opam switch create . --deps-only
opam switch create . 5.1.0 -y --deps-only

.PHONY: init
init: create-switch install pins ## Configure everything to develop this repository in local

.PHONY: pins
pins: ## Pin development dependencies
opam pin add $(project_name).dev .
init: create-switch install ## Configure everything to develop this repository in local

.PHONY: install
install: ## Install dependencies
yarn install
opam install . --deps-only --with-test
rm -rf node_modules/melange && ln -sfn $$(opam var melange:lib)/runtime node_modules/melange

.PHONY: deps
deps: $(opam_file) ## Alias to update the opam file and install the needed deps

.PHONY: build_js
build_js: ## Build the Melange project
$(DUNE) build @exe_js
.PHONY: build_melange
build_melange: ## Build the Melange project
$(DUNE) build @melange

.PHONY: build_native
build_native: ## Build the native project
$(DUNE) build exes/native.exe

.PHONY: build
build: build_js build_native ## Build both Melange and native projects
build: build_melange build_native ## Build both Melange and native projects

.PHONY: run_js
run_js: ## Run the js project
.PHONY: run_melange
run_js: ## Run the melange project
node _build/default/exes/date/exes/js

.PHONY: run_native
Expand All @@ -64,7 +55,4 @@ format-check: ## Checks if format is correct

.PHONY: watch
watch: ## Watch for the filesystem and rebuild on every change
$(DUNE) build @@default --watch

$(opam_file): $(project_name).opam.template dune-project ## Update the package dependencies when new deps are added to dune-project
opam exec -- dune build @install # Update the $(project_name).opam file
$(DUNE) build --watch
25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This repository contains an example of a library written in
ocaml/reason for both javascript (through bucklescript) and native
(through the ocaml compiler).
OCaml/Reason for both JavaScript (through [Melange](melange.re/)) and native
(through the OCaml compiler).

The `shared` directory contains the core of the library, available in
both modes. `js` and `native` contain the part specific to one
Expand All @@ -21,33 +21,26 @@ step.

## Installation

For bucklescript:
Installing requires [opam](opam.ocaml.org/), OCaml's package manager. Once it is
installed, just needs to run:

```bash
yarn
make init
```

For native:

- install [esy](https://esy.sh/)
- then run `esy install`

It can work with opam, but we use esy in this example as it is easier
to understand for people from the javascript world.

## Compilation

For bucklescript:
For Melange:

```bash
yarn bsb -make-world
dune build @melange
```

For native

```bash
esy dune build @all
dune build exes/native.exe
```

The commands are wrapped in a Makefile and can be called with `make
js` and `make native`.
melange` and `make native`.
3 changes: 1 addition & 2 deletions date/js/date.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ let tomorrow = () => {

let wrap = x => Js.Date.fromString(x);

let unwrap = x =>
(Js.Date.toISOString(x) |> Js.String.split("T"))->Array.get(0);
let unwrap = x => Js.Date.toISOString(x);
21 changes: 15 additions & 6 deletions date/js/dune
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
(library
(name date_js)
(libraries bs_atdgen_codec_runtime)
(modes melange))
(libraries melange-atdgen-codec-runtime tomorrow)
(modes melange)
(preprocess
(pps melange.ppx)))

(copy_files# "../shared_ml/*.{ml,mli}")
(copy_files# "../shared/yesterday.mli")

(copy_files# "../shared_atd/*_bs.{ml,mli}")

(copy_files# "../shared_atd/*_t.{ml,mli}")
(rule
(targets user_t.mli user_t.ml)
(deps ../shared/user.atd)
(action
(run %{bin:atdgen} -t %{deps} -o ../js/user)))

(rule
(target user_bs.ml)
(deps ../shared/user.atd)
(action
(run %{bin:atdgen} -bs %{deps} -o ../js/user)))
16 changes: 12 additions & 4 deletions date/native/dune
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
(library
(name date_native)
(libraries atdgen-runtime yojson unix ISO8601))
(libraries atdgen-runtime tomorrow yojson unix ISO8601))

(copy_files# "../shared_ml/*.{ml,mli}")
(copy_files# "../shared/yesterday.mli")

(copy_files# "../shared_atd/*_j.{ml,mli}")
(rule
(targets user_t.mli user_t.ml)
(deps ../shared/user.atd)
(action
(run %{bin:atdgen} -t %{deps} -o ../native/user)))

(copy_files# "../shared_atd/*_t.{ml,mli}")
(rule
(target user_j.ml)
(deps ../shared/user.atd)
(action
(run %{bin:atdgen} -j -j-std %{deps} -o ../native/user)))
File renamed without changes.
3 changes: 3 additions & 0 deletions date/shared/tomorrow/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name tomorrow)
(modes :standard melange))
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 0 additions & 17 deletions date/shared_atd/dune

This file was deleted.

2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(lang dune 3.6)
(lang dune 3.10)

(using melange 0.1)

Expand Down
6 changes: 3 additions & 3 deletions exes/dune
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(melange.emit
(alias exe_js)
(alias melange)
(target date)
(entries js)
(modules js)
(libraries date_js)
(module_system commonjs))
(module_systems commonjs))

(executable
(name native)
Expand Down
2 changes: 1 addition & 1 deletion exes/js.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Js.log ("yesterday was " ^ Js_date.toISOString (Date_js.Yesterday.yesterday ()))
Js.log ("yesterday was " ^ Js.Date.toISOString (Date_js.Yesterday.yesterday ()))

let user : Date_js.User_t.t =
{ name = "Joe"; added = Date_js.Yesterday.yesterday () }
Expand Down
12 changes: 7 additions & 5 deletions hello-native-melange.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ license: "MIT"
homepage: "https://github.com/ahrefs/hello-native-bucklescript"
bug-reports: "https://github.com/ahrefs/hello-native-bucklescript/issues"
depends: [
"ocaml" {= "4.14.0"}
"dune" {>= "3.0"}
"reason" {= "3.8.1"}
"melange" {= "0.3.1"}
"ocaml" {= "5.1.0"}
"dune" {>= "3.10"}
"reason" {= "3.10.0"}
"melange" {= "2.0.0"}
"melange-atdgen-codec-runtime" {= "dev"}
"atdgen" {= "2.10.0"}
"ISO8601" {= "0.2.6"}
"ocaml-lsp-server" {dev}
Expand All @@ -37,5 +38,6 @@ build: [
dev-repo: "git+https://github.com/ahrefs/hello-native-bucklescript.git"

pin-depends: [
[ "dune.dev" "git+https://github.com/ocaml/dune.git#35418c2" ]
[ "melange-json.dev" "git+https://github.com/melange-community/melange-json.git#ac8a148" ]
[ "melange-atdgen-codec-runtime.dev" "git+https://github.com/ahrefs/melange-atdgen-codec-runtime.git#d5f22fc" ]
]
29 changes: 0 additions & 29 deletions package.json

This file was deleted.

11 changes: 0 additions & 11 deletions patches/@ahrefs+bs-atdgen-codec-runtime+2.2.0.patch

This file was deleted.

34 changes: 0 additions & 34 deletions patches/@glennsl+bs-json+5.0.4.patch

This file was deleted.

Loading

0 comments on commit 112e65f

Please sign in to comment.