-
Notifications
You must be signed in to change notification settings - Fork 0
/
hs-ports.hs
41 lines (34 loc) · 1.08 KB
/
hs-ports.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Prelude as P
import Data.List
import Data.Maybe
import System.Directory
import System.Environment
import System.FilePath.Posix
import qualified Data.Text as T
import qualified Data.Text.IO as T
portsDir = "/usr/ports"
hackageMk = "lang/ghc/bsd.hackage.mk"
hsPorts portsdir = do
contents <- fmap (P.map T.strip . T.lines) $ T.readFile (portsdir </> hackageMk)
let ports = P.filter (pid `T.isInfixOf`) contents
return $ P.map (T.unpack . getDirectory) ports
where
getDirectory = (!! 1) . T.words . snd . T.breakOn pid
pid = T.pack "_port="
printHsPorts portsdir =
hsPorts portsdir >>= putStrLn . unlines . sort
getPortsDir = do
env <- getEnvironment
return $ maybe portsDir id (lookup "PORTSDIR" env)
bailOut s = putStrLn $ "ERROR: " ++ s
-- A main function for running from a shell.
main = do
args <- getArgs
case args of
["ports"] -> do
pdir <- getPortsDir
mkThere <- doesFileExist (pdir </> hackageMk)
if mkThere
then printHsPorts pdir
else bailOut "No bsd.hackage.mk found."
_ -> bailOut "Missing or invalid query."