aboutsummaryrefslogtreecommitdiffstats
path: root/app/Main.hs
blob: f48d824ca764129fa197f52ed32c02ab45dde574 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PartialTypeSignatures #-}

-- TODO Tag improvements (OR-filtering).
--
-- Currently it is not possible to filter for an issue satisfying one filter or another. We could add the following syntax allowing it:
--
-- `--filter '(@assigned aforemny) OR (@due 2023-10-4)'`
-- @topic tags

-- TODO Tag improvements (globbing).
--
-- I would like to filter `--filter '@assigned *@posteo.de'`.
-- @topic tags

-- TODO Tag improvements (priorities)
--
-- I would like anissue to support priorities when filtering. Let's for a first implementation say that priorities are represented by an integer-values `@priority` tag on an issue. The `--filter` can be extended for filtering integer-valued tags, and the following syntax:
--
-- - `--filter '@priority >1'`, `--filter '@priority <1`
-- - `--filter '@priority >=1'`, `--filter '@priority <=1`
--
-- At some later point, we can configure `high`, `medium`, `low` to mean
-- `1,2,3`, `4,5,6`, `7,8,9` respectively.
--
-- Issues having a `@priority` tag whose value is not an integer should be regarded not matching the filter.
-- @topic tags

-- TODO Tag improvements (dates).
--
-- I would like anissue to support due dates when filtering. Let's for a first implementation add the following filter syntax:
--
-- `--filter '@due 2023-10-04'` for all issues that are marked `@due 2023-10-04` or with an earlier `@due` date.
--
-- Issues having a `@due` tag whose value does not follow that date format precisely should be regarded not matching the filter.
-- @topic tags

-- TODO Add support for ammendments
--
-- The user can ammend more information to an issue which is located at
-- a different place by referencing the issue's id.  Example:
--
-- ```bash
-- #!/usr/bin/env bash
--
-- set -efu
--
-- ls -al
-- # TODO Original issue
-- #
-- # @id original-issue
--
-- ls
-- # @original-issue more information on the issue
-- ```

-- TODO Add support for other keywords
--
-- Additionally to TODO, also FIXME should start an issue.  There might
-- be more interesting keywords.

-- TODO Generate and show hash for each issue
--
-- The show hash should be generated by slugifying the issues title. Shell completion will help with the initially long issue ids. Internally, the slugified title should be added as an `@id` tag, unless an `@id` tag is present on the issue.

module Main where

import Control.Exception (Exception, catch, throw, throwIO)
import Data.Aeson qualified as A
import Data.ByteString.Lazy qualified as L
import Data.ByteString.Lazy.Char8 qualified as L8
import Data.List (intercalate)
import Data.Maybe (catMaybes)
import Data.Maybe qualified as Maybe
import Data.String qualified as String
import Data.Text qualified as T
import Issue (Issue (..))
import Issue qualified as I
import Issue.Filter (Filter)
import Issue.Filter qualified as I
import Issue.Tag qualified as I
import Options.Applicative ((<**>))
import Options.Applicative qualified as O
import Prettyprinter qualified as P
import Prettyprinter.Render.Terminal qualified as P
import System.Exit (ExitCode (ExitFailure), exitWith)
import System.FilePath qualified as F
import System.IO (hPutStrLn, stderr)
import System.Process.Typed qualified as P
import Text.Printf
import TreeGrepper.Match qualified as G
import TreeGrepper.Result qualified as G

data Options = Options
  { optCommand :: Command
  }
  deriving (Show)

data Command
  = List
      { files :: [String],
        filters :: [Filter]
      }
  | Show
      { files :: [String],
        filters :: [Filter]
      }
  deriving (Show)

commandParser :: O.Parser Command
commandParser =
  O.hsubparser
    ( O.command "list" (O.info listCommandParser (O.progDesc "List all issues"))
        <> O.command "show" (O.info showCommandParser (O.progDesc "Show details of all issues"))
    )

optionsParser :: O.Parser Options
optionsParser = Options <$> commandParser

listCommandParser :: O.Parser Command
listCommandParser = List <$> filesArg <*> I.filterOption

showCommandParser :: O.Parser Command
showCommandParser = Show <$> filesArg <*> I.filterOption

filesArg :: O.Parser [String]
filesArg = O.many (O.strArgument (O.metavar "FILE" <> O.action "file"))

main :: IO ()
main = do
  options <- O.execParser (O.info (commandParser <**> O.helper) O.idm)
  let files
        | opts@(List {}) <- options = opts.files
        | opts@(Show {}) <- options = opts.files
  filePaths <- getFiles files
  let filters
        | opts@(List {}) <- options = opts.filters
        | opts@(Show {}) <- options = opts.filters
  issues <-
    filter (I.applyFilter filters)
      . concat
      <$> catch
        ( fmap Maybe.catMaybes $
            mapM
              (\filename -> catch (fmap Just (getIssues filename)) (forgetGetIssuesExceptions))
              filePaths
        )
        ( \(InvalidTreeGrepperResult e) ->
            do
              hPutStrLn stderr e
              exitWith (ExitFailure 1)
        )
  case options of
    List {} -> listMatches issues
    Show {} -> showMatches issues

showMatches :: [Issue] -> IO ()
showMatches issues = do
  putDoc . P.vsep $
    map
      ( \issue ->
          P.vsep
            ( concat
                [ [P.annotate P.bold (P.pretty issue.title)],
                  maybe [] ((: []) . P.pretty) issue.description,
                  map
                    ( \(I.Tag k v) ->
                        P.annotate (P.colorDull P.Yellow) $
                          P.pretty ("@" `T.append` k `T.append` " " `T.append` v)
                    )
                    issue.tags
                ]
            )
      )
      issues

listMatches :: [Issue] -> IO ()
listMatches issues =
  putDoc . P.vsep $
    map
      ( \issue ->
          P.hsep
            ( concat
                [ [P.annotate P.bold (P.pretty issue.title)],
                  map
                    ( \(I.Tag k v) ->
                        P.annotate (P.colorDull P.Yellow) $
                          P.pretty ("@" `T.append` k `T.append` ":" `T.append` v)
                    )
                    issue.tags
                ]
            )
      )
      issues

putDoc :: P.Doc P.AnsiStyle -> IO ()
putDoc doc = do
  isTty <- (== 1) <$> c_isatty 1
  P.putDoc . (if isTty then id else P.unAnnotate) $ doc

foreign import ccall "unistd.h isatty" c_isatty :: Int -> IO Int

data UnknownFileExtension = UnknownFileExtension
  { extension :: String
  }
  deriving (Show)

instance Exception UnknownFileExtension

data InvalidTreeGrepperResult = InvalidTreeGrepperResult
  { error :: String
  }
  deriving (Show)

instance Exception InvalidTreeGrepperResult

forgetGetIssuesExceptions :: UnknownFileExtension -> IO (Maybe a)
forgetGetIssuesExceptions _ = pure Nothing

getIssues :: FilePath -> IO [Issue]
getIssues filename =
  let extension = F.takeExtension filename
      treeGrepperLanguage =
        -- TODO Add support for all tree-grepper supported files
        --
        -- tree-grepper supported files can be listed through `tree-grepper
        -- --languages`.
        case extension of
          ".elm" -> "elm"
          ".hs" -> "haskell"
          ".nix" -> "nix"
          ".sh" -> "sh"
          _ -> throw (UnknownFileExtension extension)
      treeGrepperQuery =
        case extension of
          ".elm" -> "([(line_comment) (block_comment)])"
          ".hs" -> "(comment)"
          ".nix" -> "(comment)"
          ".sh" -> "(comment)"
          _ -> throw (UnknownFileExtension extension)
      decode raw =
        case A.eitherDecode raw of
          Left e -> throw (InvalidTreeGrepperResult e)
          Right treeGrepperResult -> treeGrepperResult
   in catMaybes
        . map (uncurry I.fromMatch)
        . concatMap (\result -> map ((,) result) result.matches)
        . map fixTreeGrepper
        . decode
        <$> sh
          ( String.fromString
              ( printf
                  "tree-grepper --query %s %s --format json %s"
                  (quote treeGrepperLanguage)
                  (quote treeGrepperQuery)
                  (quote filename)
              )
          )

data ProcessException = ProcessException String ExitCode L.ByteString
  deriving (Show)

instance Exception ProcessException

sh :: P.ProcessConfig stdin stdoutIgnored stderr -> IO L.ByteString
sh proc = do
  (exitCode, out, err) <- P.readProcess proc
  if exitCode == P.ExitSuccess
    then pure out
    else throwIO $ ProcessException (show proc) exitCode err

fixTreeGrepper :: G.Result -> G.Result
fixTreeGrepper treeGrepperResult =
  treeGrepperResult {G.matches = G.merge treeGrepperResult.matches}

getFiles :: [String] -> IO [FilePath]
getFiles files =
  lines . L8.unpack
    <$> sh
      ( String.fromString
          ( (printf "git ls-files --cached --exclude-standard --other%s")
              ( case files of
                  [] -> ""
                  _ -> " -- " ++ intercalate " " (map quote files)
              )
          )
      )
  where

quote :: String -> String
quote s = "'" ++ escape s ++ "'"
  where
    escape [] = []
    escape ('\'' : cs) = '\\' : '\'' : escape cs
    escape (c : cs) = c : escape cs