Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 572f44a50d
Fetching contributors…

Cannot retrieve contributors at this time

file 62 lines (47 sloc) 1.904 kb
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

{-| Macros of general use.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -}

{-# LANGUAGE TemplateHaskell
#-}


module Macros where


import MacroMacros

import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Data.List
import Control.Applicative


{-| Just a macro to pull in a file.
-}
pullFile f = lift =<< runIO (readFile f)


{-| Extract the version from the Cabal file and place it here as string.
-}
version = lift =<< do
  runIO $ do
    s <- readFile "system-uuid.cabal"
    case filter version (lines s) of
      v:_ -> return (break_version v)
      [ ] -> error "Could not find version :("
 where
  version line = "version" == take 7 line
  break_version = snd . break (/= ' ') . drop 1 . snd . break (== ':')


{-| Extract the usage from the module we're in and put it here.
-}
usage = lift =<< do
  p <- $(presentFile)
  runIO $ do
    s <- readFile p
    return $ extractUsage s
 where
  fileName mod = map replace mod ++ ".hs"
  replace '.' = '/'
  replace c = c


{-| Pulls the usage out of the comments in a file, digging through the file
to find a comment with no text before the @SYNOPSIS@ or @USAGE@, and then
treating all the text of the comment as the usage statement.
-}
extractUsage s =
  case (begins_with_USAGE |--| terminal_comment) (lines s) of
    lines -> unlines lines
    [ ] -> error "Could not find USAGE :("
 where
  a |--| b = takeWhile (not . b) . dropWhile (not . a)
  begins_with_USAGE line = " USAGE: " == take 8 line
  terminal_comment line = "-}" == drop (length line - 2) line
Something went wrong with that request. Please try again.