@@ -8,12 +8,13 @@ module Main
88import Control.Arrow (first)
99import Control.Monad (forM_)
1010import Data.Char (isAlphaNum, isNumber)
11+import Data.List (foldl')
1112import Data.List (intercalate)
12−import Data.Version (Version(..))
13−import System.Directory (copyFile, canonicalizePath)
13+import Data.Version (Version (..))
14+import System.Directory (canonicalizePath, copyFile)
1415import System.Environment (getArgs, getProgName)
1516import System.Exit (exitFailure)
16−import System.FilePath ((</>), splitDirectories)
17+import System.FilePath (splitDirectories, (</>))
1718
1819
1920--------------------------------------------------------------------------------
@@ -37,38 +38,25 @@ main = do
3738 putStrLn $ "Creating " ++ dst
3839 makeDirectories dst
3940 copyFile src dst
40− -- canonicalizePath is safe because the destination
41− -- directory should exist at this point
42− canonicalizePath dstDir >>= createCabal
41+
42+ name <- makeName dstDir
43+ let cabalPath = dstDir </> name ++ ".cabal"
44+ putStrLn $ "Creating " ++ cabalPath
45+ createCabal cabalPath name
4346 _ -> do
4447 putStrLn $ "Usage: " ++ progName ++ " <directory>"
4548 exitFailure
4649
47−
48−createCabal :: FilePath -> IO ()
49−createCabal dstDir = do
50− putStrLn $ "Creating " ++ name ++ ".cabal"
51− writeFile (dstDir </> name ++ ".cabal") $ unlines [
52− "name: " ++ name
53− , "version: 0.1.0.0"
54− , "build-type: Simple"
55− , "cabal-version: >= 1.10"
56− , ""
57− , "executable site"
58− , " main-is: site.hs"
59− , " build-depends: base == 4.*"
60− , " , hakyll == " ++ version' ++ ".*"
61− , " ghc-options: -threaded"
62− , " default-language: Haskell2010"
63− ]
50+-- | Figure out a good cabal package name from the given (existing) directory
51+-- name
52+makeName :: FilePath -> IO String
53+makeName dstDir = do
54+ canonical <- canonicalizePath dstDir
55+ return $ case safeLast (splitDirectories canonical) of
56+ Nothing -> fallbackName
57+ Just "/" -> fallbackName
58+ Just x -> repair (fallbackName ++) id x
6459 where
65− -- Major hakyll version
66− version' = intercalate "." . take 2 . map show $ versionBranch version
67− -- last is safe here as the path is canonicalised and "/" is just
68− -- a very rare but possible corner case
69− name = case last (splitDirectories dstDir) of
70− "/" -> fallbackName
71− x -> repair (fallbackName ++) id x
7260 -- Package name repair code comes from
7361 -- cabal-install.Distribution.Client.Init.Heuristics
7462 repair invalid valid x = case dropWhile (not . isAlphaNum) x of
@@ -79,3 +67,24 @@ createCabal dstDir = do
7967 | otherwise = valid c
8068 repairRest = repair id ('-' :)
8169 fallbackName = "site"
70+
71+ safeLast = foldl' (\_ x -> Just x) Nothing
72+
73+createCabal :: FilePath -> String -> IO ()
74+createCabal path name = do
75+ writeFile (path ++ ".cabal") $ unlines [
76+ "name: " ++ name
77+ , "version: 0.1.0.0"
78+ , "build-type: Simple"
79+ , "cabal-version: >= 1.10"
80+ , ""
81+ , "executable site"
82+ , " main-is: site.hs"
83+ , " build-depends: base == 4.*"
84+ , " , hakyll == " ++ version' ++ ".*"
85+ , " ghc-options: -threaded"
86+ , " default-language: Haskell2010"
87+ ]
88+ where
89+ -- Major hakyll version
90+ version' = intercalate "." . take 2 . map show $ versionBranch version