diff options
author | Ben Sima <ben@bsima.me> | 2023-10-10 13:15:59 -0400 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2023-10-10 13:15:59 -0400 |
commit | 904de577261e7024373e7a42fd763184764238f9 (patch) | |
tree | 563e4968eab568466ae3e7a1c747dd24a77986c0 /Biz/Bild.hs | |
parent | 6107f8178e26ada67e5d5ec60501e24528b3db56 (diff) |
Don't swallow namespace-parsing errors
Previously, if there was a problem with the inputs and bild failed to
determine the namespace, 'fromPath' would return 'Nothing' and then
'catMaybes' would drop the error-causing input altogether. In the one
time that I had a bad input, this made debugging incredibly difficult.
It's always a bad idea to swallow errors silently, so instead lets just
kill the program if we have bad inputs.
Diffstat (limited to 'Biz/Bild.hs')
-rw-r--r-- | Biz/Bild.hs | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/Biz/Bild.hs b/Biz/Bild.hs index d8cdc6f..d178a83 100644 --- a/Biz/Bild.hs +++ b/Biz/Bild.hs @@ -10,6 +10,7 @@ -- | A specific-purpose build tool. -- -- : out bild +-- : run git -- -- == Design constraints -- @@ -157,7 +158,8 @@ main = Cli.Plan help move test_ pure |> Cli.main Test.group "Biz.Bild" [ test_bildBild, - test_bildExamples + test_bildExamples, + test_isGitIgnored ] test_bildBild :: Test.Tree @@ -196,11 +198,10 @@ move args = IO.hSetBuffering stdout IO.NoBuffering >> Env.getEnv "CODEROOT" +> \root -> Cli.getAllArgs args (Cli.argument "target") - |> filter (not <. Namespace.isCab) |> filterM Dir.doesFileExist + +> filterM (\x -> isGitIgnored x /> don't) +> traverse Dir.makeAbsolute - /> map (Namespace.fromPath root) - /> catMaybes + +> traverse (namespaceFromPathOrDie root) +> foldM analyze mempty /> Map.filter (namespace .> isBuildableNs) +> printOrBuild @@ -223,6 +224,11 @@ move args = Just n -> n printOrBuild :: Analysis -> IO [ExitCode] printOrBuild targets + | Map.null targets = + Log.wipe + >> Log.fail ["bild", "nothing to build"] + >> Log.br + >> exitWith (ExitFailure 1) | args `Cli.has` Cli.longOption "json" = Log.wipe >> putJSON targets >> pure [Exit.ExitSuccess] | otherwise = do @@ -233,6 +239,35 @@ move args = isLoud = args `Cli.has` Cli.longOption "loud" putJSON = Aeson.encode .> ByteString.Lazy.toStrict .> Char8.putStrLn +-- | Don't try to build stuff that isn't part of the git repo. +isGitIgnored :: FilePath -> IO Bool +isGitIgnored path = + Process.readProcessWithExitCode "git" ["check-ignore", path] "" + +> \case + (ExitSuccess, _, _) -> pure True + (ExitFailure _, _, _) -> pure False + +test_isGitIgnored :: Test.Tree +test_isGitIgnored = + Test.group + "isGitIgnored" + [ Test.unit "filters one" <| do + res <- isGitIgnored "_" + res @=? True, + Test.unit "filters many" <| do + traverse isGitIgnored ["Biz/Bild.hs", "__pycache__"] + +> (@=? [False, True]) + ] + +namespaceFromPathOrDie :: FilePath -> FilePath -> IO Namespace +namespaceFromPathOrDie root path = + Namespace.fromPath root path |> \case + Just x -> pure x + Nothing -> + Log.fail ["bild", str path, "could not get namespace"] + >> Log.br + >> exitWith (ExitFailure 1) + nixStore :: String nixStore = "/nix/store/00000000000000000000000000000000-" @@ -367,21 +402,24 @@ instance ToNixFlag Builder where -- | We can't build everything yet... isBuildableNs :: Namespace -> Bool isBuildableNs = \case + (Namespace _ Namespace.Bash) -> False (Namespace _ Namespace.C) -> True (Namespace _ Namespace.Css) -> False (Namespace _ Namespace.Hs) -> True + (Namespace _ Namespace.Html) -> False (Namespace _ Namespace.Json) -> False (Namespace _ Namespace.Keys) -> False (Namespace _ Namespace.Lisp) -> True (Namespace _ Namespace.Md) -> False + (Namespace path Namespace.Nix) + | path `elem` nixTargets -> True + | otherwise -> False (Namespace _ Namespace.None) -> False (Namespace _ Namespace.Py) -> True (Namespace _ Namespace.Sh) -> False (Namespace _ Namespace.Scm) -> True (Namespace _ Namespace.Rs) -> True - (Namespace path Namespace.Nix) - | path `elem` nixTargets -> True - | otherwise -> False + (Namespace _ Namespace.Toml) -> True where nixTargets = [ ["Biz", "Pie"], @@ -461,11 +499,14 @@ analyze hmap ns = case Map.lookup ns hmap of /> Text.lines case ext of -- basically we don't support building these + Namespace.Bash -> pure Nothing Namespace.Css -> pure Nothing Namespace.Json -> pure Nothing Namespace.Keys -> pure Nothing Namespace.Md -> pure Nothing Namespace.None -> pure Nothing + Namespace.Html -> pure Nothing + Namespace.Toml -> pure Nothing Namespace.Py -> contentLines |> Meta.detectAll "#" |