<commit_before>{-# LANGUAGE OverloadedStrings #-}

module Main where

import           Control.Monad
import           Data.List.Split
import           Data.Maybe
import           Data.String
import           Options.Applicative
import           Options.Applicative.Types
import           System.Directory
import           System.Environment
import           System.FilePath

import           Git.Vogue
import           Git.Vogue.Types

import qualified Paths_git_vogue           as Paths

-- | Parse command line options.
optionsParser :: Parser VogueCommand
optionsParser = subparser
    ( command "init" (info pInit
        (progDesc "Initialise git-vogue support in a git repo"))
    <> command "verify" (info pVerify
        (progDesc "Check git-vogue support is all legit"))
    <> command "check" (info pCheck
        (progDesc "Run check plugins on a git repo"))
    <> command "fix" (info pFix
        (progDesc "Run fix plugins on a git repo"))
    )
  where
    pInit = CmdInit <$> option (Just <$> readerAsk)
        (  long "template"
        <> value Nothing
        )
    pVerify = pure CmdVerify
    pCheck = pure CmdRunCheck
    pFix = pure CmdRunFix

-- | Discover all available plugins.
--
-- This function inspects the $PREFIX/libexec/git-vogue directory and the
-- directories listed in the $GIT_VOGUE_PATH environmental variable (if
-- defined) and builds a 'Plugin' for the executables found.
discoverPlugins :: IO [Plugin]
discoverPlugins = do
    -- Use the environmental variable and $libexec/git-vogue/ directories as
    -- the search path.
    path <- fromMaybe "" <$> lookupEnv "GIT_VOGUE_PATH"
    libexec <- (</> "git-vogue") <$> Paths.getLibexecDir
    let directories = splitOn ":" path ++ [libexec]

    -- Find all executables in the directories in path.
    liftM (map fromString)
        (mapM ls directories >>= concatM >>= filterM isExecutable)
  where
    ls :: FilePath -> IO [FilePath]
    ls p = do
        exists <- doesDirectoryExist p
        if exists
            then map (p </>) <$> getDirectoryContents p
            else return []
    isExecutable :: FilePath -> IO Bool
    isExecutable p = executable <$> getPermissions p
    concatM :: Monad m => [[a]] -> m [a]
    concatM = return . concat

-- | Parse the command line and run the command.
main :: IO ()
main = do
  cmd <- execParser opts
  plugins <- discoverPlugins
  runVogue plugins (runCommand cmd)
  where
    opts = info (helper <*> optionsParser)
      ( fullDesc
     <> progDesc "Make your Haskell git repository fashionable"
     <> header "git-vogue - git integration for fashionable Haskell" )<commit_msg>Add debug output for travis<commit_after>{-# LANGUAGE OverloadedStrings #-}

module Main where

import           Control.Monad
import           Data.List.Split
import           Data.Maybe
import           Data.String
import           Options.Applicative
import           Options.Applicative.Types
import           System.Directory
import           System.Environment
import           System.FilePath

import           Git.Vogue
import           Git.Vogue.Types

import qualified Paths_git_vogue           as Paths

-- | Parse command line options.
optionsParser :: Parser VogueCommand
optionsParser = subparser
    ( command "init" (info pInit
        (progDesc "Initialise git-vogue support in a git repo"))
    <> command "verify" (info pVerify
        (progDesc "Check git-vogue support is all legit"))
    <> command "check" (info pCheck
        (progDesc "Run check plugins on a git repo"))
    <> command "fix" (info pFix
        (progDesc "Run fix plugins on a git repo"))
    )
  where
    pInit = CmdInit <$> option (Just <$> readerAsk)
        (  long "template"
        <> value Nothing
        )
    pVerify = pure CmdVerify
    pCheck = pure CmdRunCheck
    pFix = pure CmdRunFix

-- | Discover all available plugins.
--
-- This function inspects the $PREFIX/libexec/git-vogue directory and the
-- directories listed in the $GIT_VOGUE_PATH environmental variable (if
-- defined) and builds a 'Plugin' for the executables found.
discoverPlugins :: IO [Plugin]
discoverPlugins = do
    -- Use the environmental variable and $libexec/git-vogue/ directories as
    -- the search path.
    path <- fromMaybe "" <$> lookupEnv "GIT_VOGUE_PATH"
    libexec <- (</> "git-vogue") <$> Paths.getLibexecDir
    let directories = splitOn ":" path ++ [libexec]

    -- Find all executables in the directories in path.
    liftM (map fromString)
        (mapM ls directories >>= concatM >>= filterM isExecutable)
  where
    ls :: FilePath -> IO [FilePath]
    ls p = do
        putStrLn $ "Trying to get plugins from: " ++ p
        exists <- doesDirectoryExist p
        if exists
            then map (p </>) <$> getDirectoryContents p
            else return []
    isExecutable :: FilePath -> IO Bool
    isExecutable p = executable <$> getPermissions p
    concatM :: Monad m => [[a]] -> m [a]
    concatM = return . concat

-- | Parse the command line and run the command.
main :: IO ()
main = do
  cmd <- execParser opts
  plugins <- discoverPlugins
  runVogue plugins (runCommand cmd)
  where
    opts = info (helper <*> optionsParser)
      ( fullDesc
     <> progDesc "Make your Haskell git repository fashionable"
     <> header "git-vogue - git integration for fashionable Haskell" )
{'fraction_non_alphanumeric': 0.09290953545232274, 'fraction_numerical': 0.0, 'mean_word_length': 2.4963839579224194, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1019958', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1501, 'n_words': 533}