From 6a6aa57afe94f8ffee3a648ca66aa16099714b94 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 25 Jul 2020 14:49:11 -0700 Subject: bild: port to haskell --- .envrc | 7 ------- Alpha.hs | 1 + Biz/Bild.hs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Hero/Part.hs | 0 bild | 25 ------------------------- nix/build.nix | 44 +++++++++++++++++++++++++++++++++++++------- 6 files changed, 92 insertions(+), 39 deletions(-) create mode 100644 Biz/Bild.hs create mode 100644 Hero/Part.hs delete mode 100755 bild diff --git a/.envrc b/.envrc index dca006f..1d0ec77 100644 --- a/.envrc +++ b/.envrc @@ -1,16 +1,9 @@ PATH_add $PWD export BIZ_ROOT=$PWD -export HERO_PORT=3000 -export HERO_NODE=$BIZ_ROOT/_bild/Hero.Node/static -export HERO_KEEP=$BIZ_ROOT/_keep -export HERO_SKEY=$BIZ_ROOT/_skey - export GUILE_LOAD_PATH=$PWD export EDITOR=vim if type lorri &>/dev/null then eval "$(lorri direnv)" -else - use nix fi diff --git a/Alpha.hs b/Alpha.hs index e96089f..8e77a5e 100644 --- a/Alpha.hs +++ b/Alpha.hs @@ -20,6 +20,7 @@ module Alpha ( -- * Re-export Protolude module X, + String, -- * Applying (<|), diff --git a/Biz/Bild.hs b/Biz/Bild.hs new file mode 100644 index 0000000..565792e --- /dev/null +++ b/Biz/Bild.hs @@ -0,0 +1,54 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NoImplicitPrelude #-} + +-- | A general purpose build tool. +-- +-- - with a nix build, results are linked in _bild/nix/ +-- - for a dev build, results are stored in _bild/dev/ +module Biz.Bild where + +import Alpha +import qualified System.Directory as Dir +import qualified System.Environment as Env +import qualified System.Exit as Exit +import System.FilePath (()) +import qualified System.Process as Process + +main :: IO () +main = Env.getArgs /> head >>= \case + Nothing -> do + basename <- Env.getProgName + Exit.die <| "usage: " <> basename <> " " + Just target -> nixBuild target + +{- +TODO: +- parse target syntax +- write dev builder for ghc/ghcjs +-} + +type Target = String + +nixBuild :: Target -> IO () +nixBuild target = do + root <- Env.getEnv "BIZ_ROOT" + cwd <- Dir.getCurrentDirectory + let qualifiedTarget = reps root "" cwd <> target + Process.callProcess + "nix-build" + [ "-o", + root "_bild/nix" qualifiedTarget, + root "default.nix", + "--attr", + qualifiedTarget + ] + +-- | Replace 'a' in 's' with 'b'. +reps :: String -> String -> String -> String +reps a b s@(x : xs) = + if isPrefixOf a s + then-- then, write 'b' and replace jumping 'a' substring + b ++ reps a b (drop (length a) s) + else-- then, write 'x' char and try to replace tail string + x : reps a b xs +reps _ _ [] = [] diff --git a/Hero/Part.hs b/Hero/Part.hs new file mode 100644 index 0000000..e69de29 diff --git a/bild b/bild deleted file mode 100755 index 7ef5009..0000000 --- a/bild +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash -set -exo pipefail -prefix=$(echo $PWD | sed -e "s|^$BIZ_ROOT/*||g" -e "s|/|.|g") -if [[ "$prefix" == "" ]] -then - # TODO: make this accept any number of build targets - target="$1" -else - target="$prefix.$1" -fi -log=$(mktemp) -nix-build \ - -o "$BIZ_ROOT/_bild/$target" \ - $BIZ_ROOT/default.nix \ - --attr "$target" \ - --show-trace \ - 2>&1 \ - | tee $log -if [[ ${PIPESTATUS[0]} != 0 ]] -then - o=$(gawk "match(\$0, /'(.*)'\sfailed/, a) {print a[1]}" $log \ - | head -n 1) - nix log $o -fi - diff --git a/nix/build.nix b/nix/build.nix index 7335159..6a12cf5 100644 --- a/nix/build.nix +++ b/nix/build.nix @@ -107,21 +107,51 @@ in { nixpkgs.python37Packages.pylint nixpkgs.wemux ]; + EXAMPLE = "hi"; shellHook = '' - echo "bizdev" | ${nixpkgs.figlet}/bin/figlet | ${nixpkgs.lolcat}/bin/lolcat - echo "(be sure to run 'nix-shell' to get the build functions)" - echo "-------------------------------------------------------" - echo "" + function help() { + echo "" + echo "bizdev" | ${nixpkgs.figlet}/bin/figlet | ${nixpkgs.lolcat}/bin/lolcat + echo "" + echo " bild compile code" + echo " deps manage dependencies with niv" + echo " ghci start ghci with correct options" + echo " help show this message" + echo " hero compile and start a dev server for herocomics.app" + echo " lint auto-lint all changed files" + echo " ./push TODO: convert to haskell" + echo " ./ship TODO: convert to haskell" + } - function repl() { - ghci -i$BIZ_ROOT -ghci-script "$BIZ_ROOT/.ghci" + function bild() { + runghc Biz.Bild $@ } function deps() { niv --sources-file $BIZ_ROOT/nix/sources.json $@ } - alias lint=$BIZ_ROOT/Biz/lint.py + function ghci() { + ghci -i$BIZ_ROOT -ghci-script "$BIZ_ROOT/.ghci" + } + + function hero() { + out="_bild/nix" + export HERO_PORT=3000 + export HERO_NODE=$BIZ_ROOT/$out/Hero.Node/static + export HERO_KEEP=$BIZ_ROOT/_keep + export HERO_SKEY=$BIZ_ROOT/_skey + b="runghc Biz.Bild" + rg --files \ + | entr -rcs \ + "$b Hero.Host && $b Hero.Node && $out/Hero.Host/bin/mmc" + } + + function lint() { + alias lint=$BIZ_ROOT/Biz/lint.py + } + + help ''; }; -- cgit v1.2.3