summaryrefslogtreecommitdiff
path: root/linters/lint-notes/Main.hs
blob: 02ee3f11d4e75f461abda1e77e18ff9bb4fd925b (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
import qualified Data.Set as S
import System.Process
import System.Environment

import Notes

usage :: IO a
usage = do
    putStrLn $ unlines
        [ "usage:"
        , "    lint-notes <mode>"
        , "    lint-notes <mode> @<response-file>"
        , "    lint-notes <mode> <file>"
        , ""
        , "valid modes:"
        , "  dump             dump all Note definitions and references"
        , "  defs             dump all Note definitions"
        , "  refs             dump all Note references"
        , "  unreferenced     dump all unreferenced Note definitions"
        , "  broken-refs      dump all references to missing Notes"
        ]
    fail "invalid usage"

main :: IO ()
main = do
    args <- getArgs

    let printNoteDefs = putStrLn . unlines . map showNoteDef
        printNoteRefs = putStrLn . unlines . map showNoteRef
        printNoteRefsSugg (bad, sugg) = do
          putStrLn . showNoteRef $ bad
          putStrLn $ "  >" ++ showNoteDef sugg


        parseMode :: String -> Maybe (NoteDb -> IO ())
        parseMode "dump"         = Just $ putStrLn . showNoteDb
        parseMode "unreferenced" = Just $ printNoteDefs . S.toList . unreferencedNotes
        parseMode "defs"         = Just $ printNoteDefs . allNoteDefs
        parseMode "refs"         = Just $ printNoteRefs . allNoteRefs
        parseMode "broken-refs"  = Just $ printNoteRefs . map fst . brokenNoteRefs
        parseMode "broken-refs-suggest" = Just $ mapM_ printNoteRefsSugg . brokenNoteRefs
        parseMode _              = Nothing

    (mode, files) <- case args of
      [mode, "@-"] -> do
        files <- lines <$> getContents
        return (parseMode mode, files)
      [mode, '@':respFile] -> do
        files <- lines <$> readFile respFile
        return (parseMode mode, files)
      [mode] -> do
        let excludeList =
              [ "testsuite/tests/linters/notes.stdout"
              , "linters/lint-notes/test" ]
        files <- lines <$> readProcess "git" ["ls-tree", "--name-only", "-r", "HEAD"] ""
        return (parseMode mode, filter (`notElem` excludeList) files)
      _ -> usage

    case mode of
      Just run -> filesNotes files >>= run
      Nothing -> return ()