Skip to content

Commit

Permalink
Merge pull request #57 from cofree-coffee/gardening/ormolu-formatting
Browse files Browse the repository at this point in the history
Reformat With Ormolu
  • Loading branch information
solomon-b authored Nov 21, 2022
2 parents 19a4f33 + b4efb35 commit 998881c
Show file tree
Hide file tree
Showing 22 changed files with 6,303 additions and 6,198 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/cabal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ on:
- synchronize

jobs:
check-format:
if: "!contains(github.event.pull_request.labels.*.name, 'ignore-server-format-checks')"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Check format
run: |
ORMOLU_VERSION="0.5.0.0"
ORMOLU_URL="https://github.com/tweag/ormolu/releases/download/${ORMOLU_VERSION}/ormolu-Linux.zip"
echo "Downloading from ${ORMOLU_URL}"
curl --fail --location --output ormolu.zip "${ORMOLU_URL}"
unzip ormolu.zip
./ormolu --mode check $(git ls-files '*.hs')
build-test:
runs-on: ubuntu-latest

Expand Down
69 changes: 38 additions & 31 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
{-# LANGUAGE NumDecimals #-}
{-# OPTIONS_GHC -Wno-missing-signatures #-}

module Main where

import CofreeBot
import CofreeBot.Bot.Behaviors.Calculator.Language
import Control.Monad
import Control.Monad.Except ( ExceptT
, runExceptT
)
import Control.Monad.IO.Class ( liftIO )
import GHC.Conc ( threadDelay )
import Network.Matrix.Client
import qualified Options.Applicative as Opt
import OptionsParser
import System.Environment.XDG.BaseDir ( getUserCacheDir )
import System.Process.Typed
import CofreeBot
import CofreeBot.Bot.Behaviors.Calculator.Language
import Control.Monad
import Control.Monad.Except
( ExceptT,
runExceptT,
)
import Control.Monad.IO.Class (liftIO)
import GHC.Conc (threadDelay)
import Network.Matrix.Client
import Options.Applicative qualified as Opt
import OptionsParser
import System.Environment.XDG.BaseDir (getUserCacheDir)
import System.Process.Typed

main :: IO ()
main = do
command <- Opt.execParser parserInfo
command <- Opt.execParser parserInfo
xdgCache <- getUserCacheDir "cofree-bot"

case command of
Expand All @@ -32,15 +34,15 @@ main = do

bot process =
let calcBot =
liftSimpleBot
$ simplifySessionBot printCalcOutput statementP
$ sessionize mempty
$ calculatorBot
helloBot = helloMatrixBot
coinFlipBot' = liftSimpleBot $ simplifyCoinFlipBot coinFlipBot
ghciBot' = liftSimpleBot $ ghciBot process
liftSimpleBot $
simplifySessionBot printCalcOutput statementP $
sessionize mempty $
calculatorBot
helloBot = helloMatrixBot
coinFlipBot' = liftSimpleBot $ simplifyCoinFlipBot coinFlipBot
ghciBot' = liftSimpleBot $ ghciBot process
magic8BallBot' = liftSimpleBot $ simplifyMagic8BallBot magic8BallBot
in calcBot
in calcBot
/.\ coinFlipBot'
/.\ helloBot
/.\ ghciBot'
Expand All @@ -52,8 +54,13 @@ cliMain :: IO ()
cliMain = withProcessWait_ ghciConfig $ \process -> do
void $ threadDelay 1e6
void $ hGetOutput (getStdout process)
void $ loop $ annihilate repl $ flip fixBot mempty $ simplifyMatrixBot $ bot
process
void $
loop $
annihilate repl $
flip fixBot mempty $
simplifyMatrixBot $
bot
process

unsafeCrashInIO :: Show e => ExceptT e IO a -> IO a
unsafeCrashInIO = runExceptT >=> either (fail . show) pure
Expand All @@ -62,10 +69,10 @@ matrixMain :: ClientSession -> String -> IO ()
matrixMain session xdgCache = withProcessWait_ ghciConfig $ \process -> do
void $ threadDelay 1e6
void $ hGetOutput (getStdout process)
unsafeCrashInIO
$ loop
$ annihilate (matrix session xdgCache)
$ batch
$ flip fixBot mempty
$ hoistBot liftIO
$ bot process
unsafeCrashInIO $
loop $
annihilate (matrix session xdgCache) $
batch $
flip fixBot mempty $
hoistBot liftIO $
bot process
137 changes: 81 additions & 56 deletions app/OptionsParser.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module OptionsParser where

import qualified Data.Text as T
import Network.Matrix.Client
import qualified Options.Applicative as Opt
import Data.Text qualified as T
import Network.Matrix.Client
import Options.Applicative qualified as Opt

---------------------
--- Login Command ---
Expand All @@ -18,61 +18,80 @@ parseLogin =
<*> parseInitialDeviceName

parseUsername :: Opt.Parser Username
parseUsername = Username <$> Opt.strOption
( Opt.long "username"
<> Opt.metavar "STRING"
<> Opt.help
"The fully qualified user ID or just local part of the user ID, to log in."
)
parseUsername =
Username
<$> Opt.strOption
( Opt.long "username"
<> Opt.metavar "STRING"
<> Opt.help
"The fully qualified user ID or just local part of the user ID, to log in."
)

parsePassword :: Opt.Parser LoginSecret
parsePassword = Password <$> Opt.strOption
(Opt.long "password" <> Opt.metavar "STRING" <> Opt.help
"The user's password."
)
parsePassword =
Password
<$> Opt.strOption
( Opt.long "password"
<> Opt.metavar "STRING"
<> Opt.help
"The user's password."
)

parseDeviceId :: Opt.Parser (Maybe DeviceId)
parseDeviceId = Opt.optional $ DeviceId <$> Opt.strOption
( Opt.long "device_id"
<> Opt.metavar "STRING"
<> Opt.help
"ID of the client device. If this does not correspond to a known client device, a new device will be created. The server will auto-generate a device_id if this is not specified."
)
parseDeviceId =
Opt.optional $
DeviceId
<$> Opt.strOption
( Opt.long "device_id"
<> Opt.metavar "STRING"
<> Opt.help
"ID of the client device. If this does not correspond to a known client device, a new device will be created. The server will auto-generate a device_id if this is not specified."
)

parseInitialDeviceName :: Opt.Parser (Maybe InitialDeviceDisplayName)
parseInitialDeviceName =
Opt.optional $ InitialDeviceDisplayName <$> Opt.strOption
( Opt.long "initial_device_name"
<> Opt.metavar "STRING"
<> Opt.help
"A display name to assign to the newly-created device. Ignored if device_id corresponds to a known device."
)
Opt.optional $
InitialDeviceDisplayName
<$> Opt.strOption
( Opt.long "initial_device_name"
<> Opt.metavar "STRING"
<> Opt.help
"A display name to assign to the newly-created device. Ignored if device_id corresponds to a known device."
)

---------------------
--- Token Command ---
---------------------

data TokenCredentials = TokenCredentials
{ matrixToken :: MatrixToken
, matrixServer :: MatrixServer
{ matrixToken :: MatrixToken,
matrixServer :: MatrixServer
}

newtype MatrixServer = MatrixServer { getMatrixServer :: T.Text }
newtype MatrixServer = MatrixServer {getMatrixServer :: T.Text}

parseTokenCredentials :: Opt.Parser TokenCredentials
parseTokenCredentials = TokenCredentials <$> parseToken <*> parseServer

parseToken :: Opt.Parser MatrixToken
parseToken = MatrixToken <$> Opt.strOption
(Opt.long "auth_token" <> Opt.metavar "MATRIX_AUTH_TOKEN" <> Opt.help
"Matrix authentication token"
)
parseToken =
MatrixToken
<$> Opt.strOption
( Opt.long "auth_token"
<> Opt.metavar "MATRIX_AUTH_TOKEN"
<> Opt.help
"Matrix authentication token"
)

parseServer :: Opt.Parser MatrixServer
parseServer = fmap MatrixServer $ Opt.strOption
(Opt.long "homeserver" <> Opt.metavar "MATRIX_HOMESERVER" <> Opt.help
"Matrix Homeserver"
)
parseServer =
fmap MatrixServer $
Opt.strOption
( Opt.long "homeserver"
<> Opt.metavar "MATRIX_HOMESERVER"
<> Opt.help
"Matrix Homeserver"
)

-------------------
--- Main Parser ---
Expand All @@ -81,25 +100,31 @@ parseServer = fmap MatrixServer $ Opt.strOption
data Command = LoginCmd LoginCredentials | TokenCmd TokenCredentials | CLI

mainParser :: Opt.Parser Command
mainParser = Opt.subparser
( Opt.command
"gen-token"
(Opt.info (fmap LoginCmd parseLogin)
(Opt.progDesc "Generate a token from a username/password")
)
<> Opt.command
"run"
(Opt.info (fmap TokenCmd parseTokenCredentials)
(Opt.progDesc "Run the bot with an auth token")
)
<> Opt.command
"cli"
(Opt.info (pure CLI) (Opt.progDesc "Run the bot in the CLI"))
)
mainParser =
Opt.subparser
( Opt.command
"gen-token"
( Opt.info
(fmap LoginCmd parseLogin)
(Opt.progDesc "Generate a token from a username/password")
)
<> Opt.command
"run"
( Opt.info
(fmap TokenCmd parseTokenCredentials)
(Opt.progDesc "Run the bot with an auth token")
)
<> Opt.command
"cli"
(Opt.info (pure CLI) (Opt.progDesc "Run the bot in the CLI"))
)

parserInfo :: Opt.ParserInfo Command
parserInfo = Opt.info
(mainParser Opt.<**> Opt.helper)
(Opt.fullDesc <> Opt.progDesc "Print a greeting for TARGET" <> Opt.header
"hello - a test for optparse-applicative"
)
parserInfo =
Opt.info
(mainParser Opt.<**> Opt.helper)
( Opt.fullDesc
<> Opt.progDesc "Print a greeting for TARGET"
<> Opt.header
"hello - a test for optparse-applicative"
)
1 change: 1 addition & 0 deletions cofree-bot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ library
CofreeBot.Bot.Context
CofreeBot.Utils
CofreeBot.Utils.ListT
Parsing

build-depends:
, aeson
Expand Down
15 changes: 14 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
cofree-bot = hfinal.callCabal2nix "cofree-bot" ./. { };
};
};

scripts = import ./nix/scripts.nix {
s = pkgs.writeShellScriptBin;
ormolu = pkgs.ormolu;
};
in
rec {

Expand All @@ -55,8 +60,9 @@
ghcid
haskell.compiler.${compiler}
haskell.packages.${compiler}.haskell-language-server
ormolu
zlib
];
] ++ (builtins.attrValues scripts);
};

packages = flake-utils.lib.flattenTree {
Expand All @@ -72,6 +78,13 @@
src = ./.;
hooks = {
nixpkgs-fmt.enable = true;
ormolu = {
name = "ormolu";
entry = "${pkgs.ormolu}/bin/ormolu --mode inplace $(git ls-files '*.hs')";
files = "\\.l?hs$";
language = "system";
pass_filenames = false;
};
cabal-fmt.enable = true;
};
};
Expand Down
5 changes: 5 additions & 0 deletions nix/scripts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ s, ormolu }:

{
format = s "format" "${ormolu}/bin/ormolu --mode inplace $(git ls-files '*.hs')";
}
19 changes: 10 additions & 9 deletions src/CofreeBot.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module CofreeBot
( module Behaviors
, module Bot
, module Context
, module Utils
) where
( module Behaviors,
module Bot,
module Context,
module Utils,
)
where

import CofreeBot.Bot as Bot
import CofreeBot.Bot.Behaviors as Behaviors
import CofreeBot.Bot.Context as Context
import CofreeBot.Utils as Utils
import CofreeBot.Bot as Bot
import CofreeBot.Bot.Behaviors as Behaviors
import CofreeBot.Bot.Context as Context
import CofreeBot.Utils as Utils
Loading

0 comments on commit 998881c

Please sign in to comment.