summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimonas Kazlauskas <git@kazlauskas.me>2014-06-13 12:52:08 +0300
committerSimonas Kazlauskas <git@kazlauskas.me>2014-06-13 19:11:30 +0300
commit721bdc2950a6d65e023985a33696892a364b4984 (patch)
tree3bf49ee507ab19df24914aa069ee5a91bb219c5a /src
parent801e07714aa39d1638ebb9b6ce97eb738d00231e (diff)
downloadhakyll-721bdc2950a6d65e023985a33696892a364b4984.tar.gz
Generate a cabal file for the initialised site
Fixes #267
Diffstat (limited to 'src')
-rw-r--r--src/Hakyll/Init.hs60
1 files changed, 52 insertions, 8 deletions
diff --git a/src/Hakyll/Init.hs b/src/Hakyll/Init.hs
index d50c6f6..51ba14b 100644
--- a/src/Hakyll/Init.hs
+++ b/src/Hakyll/Init.hs
@@ -5,11 +5,15 @@ module Main
--------------------------------------------------------------------------------
+import Control.Arrow (first)
import Control.Monad (forM_)
-import System.Directory (copyFile)
+import Data.Char (isAlphaNum, isNumber)
+import Data.List (intercalate)
+import Data.Version (Version(..))
+import System.Directory (copyFile, canonicalizePath)
import System.Environment (getArgs, getProgName)
import System.Exit (exitFailure)
-import System.FilePath ((</>))
+import System.FilePath ((</>), splitDirectories)
--------------------------------------------------------------------------------
@@ -26,12 +30,52 @@ main = do
files <- getRecursiveContents (const $ return False) srcDir
case args of
- [dstDir] -> forM_ files $ \file -> do
- let dst = dstDir </> file
- src = srcDir </> file
- putStrLn $ "Creating " ++ dst
- makeDirectories dst
- copyFile src dst
+ [dstDir] -> do
+ forM_ files $ \file -> do
+ let dst = dstDir </> file
+ src = srcDir </> file
+ putStrLn $ "Creating " ++ dst
+ makeDirectories dst
+ copyFile src dst
+ -- canonicalizePath is safe because the destination
+ -- directory should exist at this point
+ canonicalizePath dstDir >>= createCabal
_ -> do
putStrLn $ "Usage: " ++ progName ++ " <directory>"
exitFailure
+
+
+createCabal :: FilePath -> IO ()
+createCabal dstDir = do
+ putStrLn $ "Creating " ++ name ++ ".cabal"
+ writeFile (dstDir </> name ++ ".cabal") $ unlines [
+ "name: " ++ name
+ , "version: 0.1.0.0"
+ , "build-type: Simple"
+ , "cabal-version: >= 1.10"
+ , ""
+ , "executable site"
+ , " main-is: site.hs"
+ , " build-depends: base == 4.*"
+ , " , hakyll == " ++ version' ++ ".*"
+ , " ghc-options: -threaded"
+ , " default-language: Haskell2010"
+ ]
+ where
+ -- Major hakyll version
+ version' = intercalate "." . take 2 . map show $ versionBranch version
+ -- last is safe here as the path is canonicalised and "/" is just
+ -- a very rare but possible corner case
+ name = case last (splitDirectories dstDir) of
+ "/" -> fallbackName
+ x -> repair (fallbackName ++) id x
+ -- Package name repair code comes from
+ -- cabal-install.Distribution.Client.Init.Heuristics
+ repair invalid valid x = case dropWhile (not . isAlphaNum) x of
+ "" -> repairComponent ""
+ x' -> let (c, r) = first repairComponent $ break (not . isAlphaNum) x'
+ in c ++ repairRest r
+ where repairComponent c | all isNumber c = invalid c
+ | otherwise = valid c
+ repairRest = repair id ('-' :)
+ fallbackName = "site"