diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/Main.hs | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/app/Main.hs b/app/Main.hs index 351bcac..179c214 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,10 +2,15 @@ module Main where +import Control.Exception (Exception, catch, throw) import Data.ByteString.Lazy (ByteString) import Data.ByteString.Lazy qualified as LB +import Data.ByteString.Lazy.Char8 qualified as LB8 +import Data.Maybe qualified as Maybe +import Data.String qualified as String import Options.Applicative ((<**>)) import Options.Applicative qualified as O +import System.FilePath as F import System.Process.Typed qualified as P data Options = Options @@ -42,9 +47,55 @@ main :: IO () main = do options <- O.execParser (O.info (commandParser <**> O.helper) O.idm) files <- getFiles - print files + issues <- + fmap Maybe.catMaybes $ + mapM + (\filename -> catch (fmap Just (getIssues filename)) (forgetGetIssuesExceptions)) + files + print issues -getFiles :: IO [ByteString] +data UnknownFileExtension = UnknownFileExtension + { extension :: String + } + deriving (Show) + +instance Exception UnknownFileExtension + +forgetGetIssuesExceptions :: UnknownFileExtension -> IO (Maybe String) +forgetGetIssuesExceptions _ = + pure Nothing + +data Issue = Issue {} + +getIssues :: String -> IO String +getIssues filename = + let extension = F.takeExtension filename + treeGrepperLanguage = + case extension of + ".elm" -> "elm" + ".nix" -> "nix" + ".sh" -> "sh" + _ -> throw (UnknownFileExtension extension) + treeGrepperQuery = + case extension of + ".elm" -> "([(line_comment) (block_comment)])" + ".nix" -> "(comment)" + ".sh" -> "(comment)" + _ -> throw (UnknownFileExtension extension) + in fmap (LB8.unpack . snd) $ + P.readProcessStdout + ( String.fromString + ( "tree-grepper --query '" + ++ treeGrepperLanguage + ++ "' '" + ++ treeGrepperQuery + ++ "' --format json '" + ++ filename + ++ "'" + ) + ) + +getFiles :: IO [String] getFiles = - fmap (LB.split 10 . snd) $ + fmap (lines . LB8.unpack . snd) $ P.readProcessStdout "git ls-files --cached --exclude-standard --other" |