From 8fbd45c24e9ffd83a93c073401d98568883e05fa Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Sat, 6 Jan 2024 11:12:28 -0500 Subject: [PATCH] doc: replace Makefile with Justfile --- CONTRIBUTING.md | 2 +- Justfile | 68 ++++++++++++++++++++++++++++++++++++++++ Makefile | 38 ---------------------- doc/tutorial/protobuf.md | 2 +- nix/default.nix | 9 ++++++ 5 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 Justfile diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 31f6be65b..c11f79cb2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -214,7 +214,7 @@ protobuf definitions present in the [./schemas/monocle folder](./schemas/monocle the api and web client by running the protoc command using the Makefile: ```ShellSession -$ make codegen +$ just codegen ``` ## Create a monocle build diff --git a/Justfile b/Justfile new file mode 100644 index 000000000..44a1ac5b9 --- /dev/null +++ b/Justfile @@ -0,0 +1,68 @@ +# Copyright (C) 2023 Monocle authors +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# doc: https://just.systems/man/en/chapter_1.html + +PINCLUDE := "-I /usr/include ${PROTOC_FLAGS} -I ./schemas/" +# just doesn't support array so we create a space separated string for bash +# see: https://github.com/casey/just/issues/1570 +PBS := `ls schemas/monocle/protob/*.proto | grep -v http.proto | sed 's/schemas.//' | tr '\n' ' '` + +# Update code after changing protobuf schema (./schemas/monocle/protob), ci.dhall or architecture.plantuml +codegen: codegen-ci codegen-doc codegen-stubs codegen-javascript codegen-openapi codegen-haskell + +# Generate CI config from .github/workflows/ci.dhall +[private] +codegen-ci: + set -euxo pipefail + echo "(./.github/workflows/ci.dhall).Nix" | dhall-to-yaml > .github/workflows/nix.yaml + echo "(./.github/workflows/ci.dhall).NixBuild" | dhall-to-yaml > .github/workflows/nix-build.yaml + echo "(./.github/workflows/ci.dhall).Web" | dhall-to-yaml > .github/workflows/web.yaml + echo "(./.github/workflows/ci.dhall).Docker" | dhall-to-yaml > .github/workflows/docker.yaml + echo "(./.github/workflows/ci.dhall).Publish-Master-Image" | dhall-to-yaml > .github/workflows/publish-master.yaml + echo "(./.github/workflows/ci.dhall).Publish-Tag-Image" | dhall-to-yaml > .github/workflows/publish-tag.yaml + +# Generate doc/architecture.png from doc/architecture.plantuml +# just doesn't support timestamp based rule, so we keep make for plantuml +# see: https://github.com/casey/just/issues/867 +[private] +codegen-doc: + set -euxo pipefail + make doc/architecture.png + +# Generate HTTP clients and server +[private] +codegen-stubs: + set -euxo pipefail + mkdir -p srcgen/ + cabal -fcodegen run monocle-codegen ./schemas/monocle/protob/http.proto ./src/Monocle/Client/Api.hs ./src/Monocle/Servant/HTTP.hs ./srcgen/WebApi.res + fourmolu -i ./src/Monocle/Client/Api.hs ./src/Monocle/Servant/HTTP.hs + ./web/node_modules/.bin/bsc -format ./srcgen/WebApi.res > ./web/src/components/WebApi.res + rm -Rf srcgen/ + +# Generate haskell data type from protobuf +[private] +codegen-haskell: + set -euxo pipefail + for pb in {{PBS}}; do \ + compile-proto-file --includeDir /usr/include --includeDir schemas/ --includeDir ${PROTOBUF_SRC} --proto ${pb} --out codegen/; \ + done + find codegen/Monocle -type f -name "*.hs" -exec sed -i {} -e '1i{-# LANGUAGE NoGeneralisedNewtypeDeriving #-}' \; + fourmolu -i codegen/Monocle + +# Generate javascript data type from protobuf +[private] +codegen-javascript: + set -euxo pipefail + rm -f web/src/messages/* + for pb in {{PBS}}; do \ + ocaml-protoc {{PINCLUDE}} -bs -ml_out web/src/messages/ schemas/${pb}; \ + done + python3 ./codegen/rename_bs_module.py ./web/src/messages/ + +# Generate openapi from protobuf +[private] +codegen-openapi: + set -euxo pipefail + protoc {{PINCLUDE}} --openapi_out=./doc/ monocle/protob/http.proto + echo Created doc/openapi.yaml diff --git a/Makefile b/Makefile index 27cec1b46..4715e2647 100644 --- a/Makefile +++ b/Makefile @@ -1,43 +1,5 @@ # Copyright (C) 2021 Monocle authors # SPDX-License-Identifier: AGPL-3.0-or-later -.PHONY: up-stage - - -BASEDIR = monocle/protob -MESSAGES = $(BASEDIR)/search.proto $(BASEDIR)/config.proto $(BASEDIR)/login.proto $(BASEDIR)/metric.proto $(BASEDIR)/auth.proto -CRAWLER = $(BASEDIR)/change.proto $(BASEDIR)/issue.proto $(BASEDIR)/crawler.proto -PINCLUDE = -I /usr/include $(PROTOC_FLAGS) -I ./schemas/ - -codegen: codegen-ci codegen-javascript codegen-stubs codegen-openapi codegen-haskell doc/architecture.png - -codegen-ci: - echo "(./.github/workflows/ci.dhall).Nix" | dhall-to-yaml > .github/workflows/nix.yaml - echo "(./.github/workflows/ci.dhall).NixBuild" | dhall-to-yaml > .github/workflows/nix-build.yaml - echo "(./.github/workflows/ci.dhall).Web" | dhall-to-yaml > .github/workflows/web.yaml - echo "(./.github/workflows/ci.dhall).Docker" | dhall-to-yaml > .github/workflows/docker.yaml - echo "(./.github/workflows/ci.dhall).Publish-Master-Image" | dhall-to-yaml > .github/workflows/publish-master.yaml - echo "(./.github/workflows/ci.dhall).Publish-Tag-Image" | dhall-to-yaml > .github/workflows/publish-tag.yaml doc/architecture.png: doc/architecture.plantuml plantuml ./doc/architecture.plantuml - -codegen-stubs: - mkdir -p srcgen/ - (cabal -fcodegen run monocle-codegen ./schemas/$(BASEDIR)/http.proto ./src/Monocle/Client/Api.hs ./src/Monocle/Servant/HTTP.hs ./srcgen/WebApi.res) - fourmolu -i ./src/Monocle/Client/Api.hs ./src/Monocle/Servant/HTTP.hs - ./web/node_modules/.bin/bsc -format ./srcgen/WebApi.res > ./web/src/components/WebApi.res - rm -Rf srcgen/ - -codegen-haskell: - sh -c 'for pb in $(MESSAGES) $(CRAWLER); do compile-proto-file --includeDir /usr/include --includeDir schemas/ --includeDir ${PROTOBUF_SRC} --proto $${pb} --out codegen/; done' - find codegen/Monocle -type f -name "*.hs" -exec sed -i {} -e '1i{-# LANGUAGE NoGeneralisedNewtypeDeriving #-}' \; - fourmolu -i codegen/Monocle - -codegen-javascript: - rm -f web/src/messages/* - sh -c 'for pb in $(MESSAGES) $(CRAWLER); do ocaml-protoc $(PINCLUDE) -bs -ml_out web/src/messages/ schemas/$${pb}; done' - python3 ./codegen/rename_bs_module.py ./web/src/messages/ - -codegen-openapi: - protoc $(PINCLUDE) --openapi_out=./doc/ $(BASEDIR)/http.proto - @echo Created doc/openapi.yaml diff --git a/doc/tutorial/protobuf.md b/doc/tutorial/protobuf.md index 3a8e16fe9..2707dab4b 100644 --- a/doc/tutorial/protobuf.md +++ b/doc/tutorial/protobuf.md @@ -161,5 +161,5 @@ We would like to avoid doing the above manually, thus Monocle provide a codegen to automate this process in three steps: - 1. Write protobuf definitions in the `schemas/` folder, (and add newly created files to the Makefile `MESSAGES` list) -- 2. Generate the code by running `make codegen-with-container` +- 2. Generate the code by running `just codegen` - 3. Implement the API, e.g. open the `web/src/components/WebApi.res` module to use it. diff --git a/nix/default.nix b/nix/default.nix index ce7bdc0a8..04cd048ca 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -48,6 +48,14 @@ let ]; }; + # pull latest nixpkgs for just [private] support + latestPkgs = import (pkgs.fetchFromGitHub { + owner = "NixOS"; + repo = "nixpkgs"; + rev = "32ea06b23546a0172ac4e2aa733392e02f57503e"; + sha256 = "sha256-NuRRuBO3ijXIu8sD9WaW5m6PHb3COI57UtHDMY+aGTI="; + }) { system = "x86_64-linux"; }; + # create the main package set without options pkgs = nixpkgsSrc { system = "x86_64-linux"; }; pkgsNonFree = nixpkgsSrc { @@ -549,6 +557,7 @@ in rec { packages = p: [ (addExtraDeps p.monocle) p.pretty-simple ]; buildInputs = [ + latestPkgs.just hspkgs.hlint hspkgs.apply-refact hspkgs.ghcid