diff options
-rw-r--r-- | anissue.cabal | 1 | ||||
-rw-r--r-- | app/Main.hs | 57 | ||||
-rw-r--r-- | default.nix | 7 |
3 files changed, 61 insertions, 4 deletions
diff --git a/anissue.cabal b/anissue.cabal index 4bae665..c528361 100644 --- a/anissue.cabal +++ b/anissue.cabal @@ -73,6 +73,7 @@ executable anissue -- Other library packages from which modules are imported. build-depends: base ^>=4.16.4.0, bytestring, + filepath, optparse-applicative, typed-process 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" diff --git a/default.nix b/default.nix index 08799f6..722d91c 100644 --- a/default.nix +++ b/default.nix @@ -1,4 +1,7 @@ -{ pkgs ? import <nixpkgs> { } }: +{ pkgs ? import <nixpkgs> { + overlays = [ (import ./pkgs) ]; + } +}: let @@ -17,6 +20,8 @@ rec { buildInputs = [ haskellPackages.cabal-install haskellPackages.ormolu + pkgs.tree-grepper + pkgs.git ]; shellHook = '' HISTFILE=${pkgs.lib.escapeShellArg ./.}/.history; export HISTFILE |