@@ -2,6 +2,7 @@ module Bin.AppM where
22
33import Prelude
44
5+ import Bin.CLI (Verbosity (..))
56import Control.Monad.Except (runExceptT )
67import Control.Monad.Reader (class MonadAsk , class MonadReader , ReaderT , ask , runReaderT )
78import Control.Monad.Reader as ReaderT
@@ -10,13 +11,12 @@ import Data.Newtype (class Newtype)
1011import Effect.Aff (Aff )
1112import Effect.Aff.Class (class MonadAff , liftAff )
1213import Effect.Class (class MonadEffect )
14+ import Effect.Class.Console as Console
1315import Lib.Foreign.Octokit (GitHubError , Octokit )
1416import Lib.Git (GitM (..))
1517import Lib.GitHub (GitHubM (..))
16- import Lib.Nix.Manifest (CombinedManifest , GitHubBinaryManifest , NamedManifest , NPMRegistryManifest )
18+ import Lib.Nix.Manifest (ManifestCodec , NamedManifest )
1719import Lib.Nix.Manifest as Nix.Manifest
18- import Lib.Nix.Manifest as Tool
19- import Lib.Tool (Tool (..))
2020import Lib.Utils as Utils
2121import Node.Path (FilePath )
2222import Node.Path as Path
@@ -31,6 +31,7 @@ type Env =
3131 , tmpDir :: FilePath
3232 , gitBranch :: String
3333 , manifestDir :: FilePath
34+ , verbosity :: Verbosity
3435 }
3536
3637newtype AppM a = AppM (ReaderT Env Aff a )
@@ -48,82 +49,50 @@ derive newtype instance MonadReader Env AppM
4849
4950instance MonadApp AppM where
5051 runGitHubM (GitHubM run) = do
51- { octokit } <- ask
52- liftAff $ runReaderT (runExceptT run) octokit
52+ { octokit, verbosity } <- ask
53+ let debugFn msg = when (verbosity >= Verbose ) $ Console .log $ " [DEBUG] " <> msg
54+ liftAff $ runReaderT (runExceptT run) { octokit, debug: debugFn }
5355
5456 runGitM (GitM run) = do
55- { tmpDir, gitBranch } <- ask
56- liftAff $ runReaderT (runExceptT run) { cwd: tmpDir, branch: gitBranch }
57+ { tmpDir, gitBranch, verbosity } <- ask
58+ let debugFn msg = when (verbosity >= Verbose ) $ Console .log $ " [DEBUG] " <> msg
59+ liftAff $ runReaderT (runExceptT run) { cwd: tmpDir, branch: gitBranch, debug: debugFn }
5760
5861runAppM :: forall a . Env -> AppM a -> Aff a
5962runAppM env (AppM run) = runReaderT run env
6063
61- getNamedManifestPath :: AppM FilePath
62- getNamedManifestPath = do
63- { manifestDir } <- ask
64- pure $ Path .concat [ manifestDir, Nix.Manifest .namedPath ]
65-
64+ -- | Read the named manifest (tool channel -> version mappings)
6665readNamedManifest :: AppM NamedManifest
6766readNamedManifest = do
68- path <- getNamedManifestPath
69- Utils .readJsonFile path Nix.Manifest .namedManifestCodec
67+ { manifestDir } <- ask
68+ Utils .readJsonFile ( Path .concat [ manifestDir, Nix.Manifest .namedPath ]) Nix.Manifest .namedManifestCodec
7069
70+ -- | Write the named manifest
7171writeNamedManifest :: NamedManifest -> AppM Unit
7272writeNamedManifest manifest = do
73- path <- getNamedManifestPath
74- Utils .writeJsonFile path Nix.Manifest .namedManifestCodec manifest
75-
76- getToolManifestPath :: Tool -> AppM FilePath
77- getToolManifestPath tool = do
7873 { manifestDir } <- ask
79- pure $ Path .concat [ manifestDir, Tool .filename tool ]
80-
81- readPursManifest :: AppM GitHubBinaryManifest
82- readPursManifest = do
83- path <- getToolManifestPath Purs
84- Utils .readJsonFile path Nix.Manifest .githubBinaryManifestCodec
85-
86- writePursManifest :: GitHubBinaryManifest -> AppM Unit
87- writePursManifest manifest = do
88- path <- getToolManifestPath Purs
89- Utils .writeJsonFile path Nix.Manifest .githubBinaryManifestCodec manifest
90-
91- readSpagoManifest :: AppM CombinedManifest
92- readSpagoManifest = do
93- path <- getToolManifestPath Spago
94- Utils .readJsonFile path Nix.Manifest .combinedManifestCodec
74+ Utils .writeJsonFile (Path .concat [ manifestDir, Nix.Manifest .namedPath ]) Nix.Manifest .namedManifestCodec manifest
9575
96- writeSpagoManifest :: CombinedManifest -> AppM Unit
97- writeSpagoManifest manifest = do
98- path <- getToolManifestPath Spago
99- Utils .writeJsonFile path Nix.Manifest .combinedManifestCodec manifest
100-
101- readPursTidyManifest :: AppM NPMRegistryManifest
102- readPursTidyManifest = do
103- path <- getToolManifestPath PursTidy
104- Utils .readJsonFile path Nix.Manifest .npmRegistryManifestCodec
105-
106- writePursTidyManifest :: NPMRegistryManifest -> AppM Unit
107- writePursTidyManifest manifest = do
108- path <- getToolManifestPath PursTidy
109- Utils .writeJsonFile path Nix.Manifest .npmRegistryManifestCodec manifest
110-
111- readPursBackendEsManifest :: AppM NPMRegistryManifest
112- readPursBackendEsManifest = do
113- path <- getToolManifestPath PursBackendEs
114- Utils .readJsonFile path Nix.Manifest .npmRegistryManifestCodec
115-
116- writePursBackendEsManifest :: NPMRegistryManifest -> AppM Unit
117- writePursBackendEsManifest manifest = do
118- path <- getToolManifestPath PursBackendEs
119- Utils .writeJsonFile path Nix.Manifest .npmRegistryManifestCodec manifest
120-
121- readPursLanguageServerManifest :: AppM NPMRegistryManifest
122- readPursLanguageServerManifest = do
123- path <- getToolManifestPath PursLanguageServer
124- Utils .readJsonFile path Nix.Manifest .npmRegistryManifestCodec
76+ -- | Read a tool's manifest using its codec
77+ readManifest :: forall a . ManifestCodec a -> AppM a
78+ readManifest { codec, tool } = do
79+ { manifestDir } <- ask
80+ Utils .readJsonFile (Path .concat [ manifestDir, Nix.Manifest .filename tool ]) codec
12581
126- writePursLanguageServerManifest :: NPMRegistryManifest -> AppM Unit
127- writePursLanguageServerManifest manifest = do
128- path <- getToolManifestPath PursLanguageServer
129- Utils .writeJsonFile path Nix.Manifest .npmRegistryManifestCodec manifest
82+ -- | Write a tool's manifest using its codec
83+ writeManifest :: forall a . ManifestCodec a -> a -> AppM Unit
84+ writeManifest { codec, tool } manifest = do
85+ { manifestDir } <- ask
86+ Utils .writeJsonFile (Path .concat [ manifestDir, Nix.Manifest .filename tool ]) codec manifest
87+
88+ -- | Log a message (always shown unless Quiet)
89+ log :: String -> AppM Unit
90+ log msg = do
91+ { verbosity } <- ask
92+ when (verbosity > Quiet ) $ Console .log $ " [INFO] " <> msg
93+
94+ -- | Log a message only in Verbose mode
95+ debug :: String -> AppM Unit
96+ debug msg = do
97+ { verbosity } <- ask
98+ when (verbosity >= Verbose ) $ Console .log $ " [DEBUG] " <> msg
0 commit comments