Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LTB-13] A couple of utilities for executable binaries #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/bin/LICENCE
4 changes: 4 additions & 0 deletions code/bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
loot-bin
=========

_Some fine utilities for your executables._
21 changes: 21 additions & 0 deletions code/bin/lib/Loot/Bin/IO.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- | Utilities for performing IO
module Loot.Bin.IO
( openFileOrStdin
, withFileOrStdin
) where

import Control.Exception.Safe (bracket)
import System.IO (Handle, IOMode (ReadMode), hClose, stdin)


-- | Like 'openFile' but if the filename is @-@ returns 'stdin'.
--
-- Always opens the file in 'ReadMode'.
openFileOrStdin :: MonadIO m => FilePath -> m Handle
openFileOrStdin "-" = pure stdin
openFileOrStdin name = openFile name ReadMode

-- | Like 'withFile', but also like 'openFileOrStdin'.
withFileOrStdin :: (MonadIO m, MonadMask m) => FilePath -> (Handle -> m r) -> m r
withFileOrStdin "-" cont = cont stdin
withFileOrStdin name cont = bracket (openFile name ReadMode) (liftIO . hClose) cont
41 changes: 41 additions & 0 deletions code/bin/lib/Loot/Bin/Paths.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- | Utilities for locating files according to the XDG Base Directory Specification.
--
-- A good way to reuse this module is to create a module called @Paths@ in your
-- project and define in it:
--
-- @
-- appName :: String
-- appName = "\<your app name\>"
--
-- getDataDir :: MonadIO m => m FilePath
-- getDataDir = 'getDataDirFor' appName
--
-- getConfigDir :: MonadIO m => m FilePath
-- getConfigDir = 'getConfigDirFor' appName
--
-- getCacheDir :: MonadIO m => m FilePath
-- getCacheDir = 'getCacheDirFor' appName
-- @
--
-- And then make sure your application places files in the right directories.
module Loot.Bin.Paths
( getDataDirFor
, getConfigDirFor
, getCacheDirFor
) where

import System.Directory (XdgDirectory (XdgCache, XdgConfig, XdgData), getXdgDirectory)
import System.IO (FilePath)


-- | Returns the path to the data directory for your application.
getDataDirFor :: MonadIO m => String -> m FilePath
getDataDirFor = liftIO . getXdgDirectory XdgData

-- | Returns the path to the config directory for your application.
getConfigDirFor :: MonadIO m => String -> m FilePath
getConfigDirFor = liftIO . getXdgDirectory XdgConfig

-- | Returns the path to the cache directory for your application.
getCacheDirFor :: MonadIO m => String -> m FilePath
getCacheDirFor = liftIO . getXdgDirectory XdgCache
10 changes: 10 additions & 0 deletions code/bin/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<<: !include "../base/hpack/lib.yaml"

name: loot-bin

library:
<<: *lib-common

dependencies:
- directory
- safe-exceptions
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ resolver: snapshot.yaml

packages:
- code/base
- code/bin
- code/config
- code/crypto
- code/log
Expand Down