packages feed

haskellscript 0.2.2 → 0.2.3

raw patch · 2 files changed

+19/−8 lines, 2 files

Files

haskellscript.cabal view
@@ -1,5 +1,5 @@ name:                   haskellscript-version:                0.2.2+version:                0.2.3 synopsis:               Command line tool for running Haskell scripts with a hashbang. description:   This tool provides the ability to script in a shell with Haskell (including dependencies) the same way that has been possible with bash scripts or Python.
src/HaskellScript.hs view
@@ -6,6 +6,7 @@ import Control.Monad.Trans import Control.Monad.Trans.Either import Crypto.Hash+import Data.Char import Data.Either.Combinators import Data.Monoid import Data.Traversable@@ -15,7 +16,7 @@ import System.FilePath.Posix import System.Process import System.IO hiding (readFile, writeFile)-import Prelude hiding (readFile, writeFile, length, take, drop, FilePath, lines, unlines)+import Prelude hiding (readFile, writeFile, length, take, drop, FilePath, lines, unlines, span, null) import qualified Prelude as P import Data.Text hiding (filter) import qualified Data.Text.IO as TIO@@ -45,19 +46,29 @@     (Right _)     -> return ()     (Left error)  -> putStrLn "Error handling script: " >> print error -isDependencyLine :: Text -> Bool-isDependencyLine line = isPrefixOf "--#" line+parseDependencyLine :: Text -> Maybe Text+parseDependencyLine line = +  let (hyphens, afterHyphens) = span (== '-') line+      (_, afterSpaces)        = span isSpace afterHyphens+      (hashes, afterHashes)   = span (== '#') afterSpaces+      isValid                 = (not $ null hyphens) && (not $ null hashes)+  in  if isValid then Just $ strip afterHashes else Nothing  +parseDependencies :: [Text] -> [Text] -> ([Text], [Text])+parseDependencies [] dependenciesAccumulator              = (dependenciesAccumulator, [])+parseDependencies (first : rest) dependenciesAccumulator  = case (parseDependencyLine first) of+  Just dep  -> parseDependencies rest (dep : dependenciesAccumulator)+  Nothing   -> (dependenciesAccumulator, (first : rest))+ parseScript :: Text -> Either ScriptingError ScriptDetails parseScript script = do   let scriptLines = lines script   afterHashbang <- case scriptLines of-    first : rest  -> if isPrefixOf "#!" first then Right rest else Left $ ScriptParseError "No shebang at start of script."+    first : rest  -> if isPrefixOf "#!" $ strip first then Right rest else Right scriptLines     _             -> Left $ ScriptParseError "Script is empty."-  let (dependenciesLines, remainder) = L.span (\line -> isDependencyLine line || (length $ strip line) == 0) afterHashbang-  let dependencies = L.sort $ fmap (strip . drop 3) $ filter isDependencyLine dependenciesLines-  return $ ScriptDetails dependencies (unlines remainder)+  let (dependencies, remainder) = parseDependencies afterHashbang []+  return $ ScriptDetails (L.sort dependencies) (unlines remainder)   getDependenciesHash :: ScriptDetails -> String