-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Tool.hs
186 lines (158 loc) 路 4.11 KB
/
Tool.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{- |
Module : Iris.Tool
Copyright : (c) 2022 Dmitrii Kovanikov
SPDX-License-Identifier : MPL-2.0
Maintainer : Dmitrii Kovanikov <kovanikov@gmail.com>
Stability : Experimental
Portability : Portable
Utilities to check required tools and their minimal version for a CLI app.
Sometimes, your CLI application
@since 0.0.0.0
-}
module Iris.Tool (
-- * Requiring an executable
need,
Tool (..),
ToolSelector (..),
defaultToolSelector,
-- * Tool requirements check
ToolCheckResult (..),
ToolCheckError (..),
ToolCheckException (..),
checkTool,
) where
import Control.Exception (Exception, throwIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Foldable (traverse_)
import Data.String (IsString (..))
import Data.Text (Text)
import System.Directory (findExecutable)
import System.Process (readProcess)
import qualified Data.Text as Text
{- |
@since 0.0.0.0
-}
data Tool = Tool
{ toolName :: Text
-- ^ @since 0.0.0.0
, toolSelector :: Maybe ToolSelector
-- ^ @since 0.0.0.0
}
{- |
@since 0.0.0.0
-}
instance IsString Tool where
fromString :: String -> Tool
fromString s =
Tool
{ toolName = fromString s
, toolSelector = Nothing
}
{- |
@since 0.0.0.0
-}
data ToolSelector = ToolSelector
{ toolSelectorFunction :: Text -> Bool
-- ^ @since 0.0.0.0
, toolSelectorVersionArg :: Maybe Text
-- ^ @since 0.0.0.0
}
{- |
@since 0.0.0.0
-}
defaultToolSelector :: ToolSelector
defaultToolSelector =
ToolSelector
{ toolSelectorFunction = const True
, toolSelectorVersionArg = Nothing
}
{- |
@since 0.0.0.0
-}
data ToolCheckResult
= -- |
--
-- @since 0.1.0.0
ToolCheckError ToolCheckError
| -- |
--
-- @since 0.0.0.0
ToolOk
deriving stock
( Show
-- ^ @since 0.0.0.0
, Eq
-- ^ @since 0.0.0.0
)
{- |
@since 0.1.0.0
-}
data ToolCheckError
= -- |
--
-- @since 0.1.0.0
ToolNotFound Text
| -- |
--
-- @since 0.1.0.0
ToolWrongVersion Text
deriving stock
( Show
-- ^ @since 0.1.0.0
, Eq
-- ^ @since 0.1.0.0
)
{- |
@since 0.0.0.0
-}
checkTool :: Tool -> IO ToolCheckResult
checkTool Tool{..} =
findExecutable (Text.unpack toolName) >>= \case
Nothing -> pure $ ToolCheckError $ ToolNotFound toolName
Just exe -> case toolSelector of
Nothing -> pure ToolOk
Just ToolSelector{..} -> case toolSelectorVersionArg of
Nothing -> pure ToolOk
Just versionArg -> do
toolVersionOutput <- readProcess exe [Text.unpack versionArg] ""
let version = Text.strip $ Text.pack toolVersionOutput
if toolSelectorFunction version
then pure ToolOk
else pure $ ToolCheckError $ ToolWrongVersion version
{- | An exception thrown by 'need' when there's an error requiring a tool.
@since 0.1.0.0
-}
newtype ToolCheckException = ToolCheckException ToolCheckError
deriving stock
( Show
-- ^ @since 0.1.0.0
)
deriving newtype
( Eq
-- ^ @since 0.1.0.0
)
deriving anyclass
( Exception
-- ^ @since 0.1.0.0
)
{- | Use this function to require specific CLI tools for your CLI application.
The function can be used in the beginning of each command in the following way:
@
app :: App ()
app = Iris.'Iris.Env.asksCliEnv' Iris.'Iris.Env.cliEnvCmd' >>= __\\case__
Download url -> do
Iris.'need' ["curl"]
runDownload url
Evaluate hs -> do
Iris.'need' ["ghc", "cabal"]
runEvaluate hs
@
__Throws:__ 'ToolCheckException' if can't find a tool or if it has wrong version.
@since 0.0.0.0
-}
need :: MonadIO m => [Tool] -> m ()
need = traverse_ $ \tool ->
liftIO $
checkTool tool >>= \case
ToolOk -> pure ()
ToolCheckError toolErr -> throwIO $ ToolCheckException toolErr