Skip to content

Commit

Permalink
Merge pull request typelead#75 from jneira/own-config
Browse files Browse the repository at this point in the history
 Add .etlas file as possible configuration source
  • Loading branch information
rahulmutt authored Oct 20, 2018
2 parents cac3f90 + 25f63db commit 7695eed
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 135 deletions.
52 changes: 28 additions & 24 deletions etlas-cabal/Distribution/PackageDescription/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,36 +1779,40 @@ checkCabalFileBOM ops = do
-- but generalized over monads.
findPackageDesc :: Monad m => CheckPackageContentOps m
-> m (Either PackageCheck FilePath) -- ^<pkgname>.cabal
findPackageDesc ops
= do let dir = "."
dhallFile = dir </> "etlas.dhall"

existDhallFile <- doesFileExist ops dhallFile

if existDhallFile then return (Right dhallFile)
else do

files <- getDirectoryContents ops dir
-- to make sure we do not mistake a ~/.cabal/ dir for a <pkgname>.cabal
-- file we filter to exclude dirs and null base file names:
cabalFiles <- filterM (doesFileExist ops)
[ dir </> file
| file <- files
, let (name, ext) = splitExtension file
, not (null name) && ext == ".cabal" ]
case cabalFiles of
[] -> return (Left $ PackageBuildImpossible noDesc)
[cabalFile] -> return (Right cabalFile)
findPackageDesc ops = do
let dir = "."
files <- getDirectoryContents ops dir
let findConfigFiles pred = do
cfgFiles <- filterM (doesFileExist ops)
[ dir </> file | file <- files , pred file ]
return cfgFiles

findFirstConfigFile [] = return $ Left
$ PackageBuildImpossible noDesc
findFirstConfigFile (pred:preds) = do
cfgFiles <- findConfigFiles pred
case cfgFiles of
[] -> findFirstConfigFile preds
[cfgFile] -> return (Right cfgFile)
multiple -> return (Left $ PackageBuildImpossible
$ multiDesc multiple)
$ multiDesc multiple)

findFirstConfigFile [(== "etlas.dhall")
,hasNotNullNameAndExtension ".etlas"
,hasNotNullNameAndExtension ".cabal"
]
where
hasNotNullNameAndExtension extCfg file =
let (name, ext) = splitExtension file
in not (null name) && ext == extCfg

noDesc :: String
noDesc = "No cabal file found.\n"
++ "Please create a package description file <pkgname>.cabal"
noDesc = "No config file found.\n"
++ "Please create a package description file "
++ "etlas.dhall, <pkgname>.etlas or <pkgname>.cabal"

multiDesc :: [String] -> String
multiDesc l = "Multiple cabal files found while checking.\n"
multiDesc l = "Multiple config files found while checking.\n"
++ "Please use only one of: "
++ intercalate ", " l

Expand Down
2 changes: 1 addition & 1 deletion etlas-cabal/Distribution/PackageDescription/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Distribution.PackageDescription.Utils (
-- Exception and logging utils

userBug :: String -> a
userBug msg = error $ msg ++ ". This is a bug in your .cabal file."
userBug msg = error $ msg ++ ". This is a bug in your .etlas or .cabal file."

cabalBug :: String -> a
cabalBug msg = error $ msg ++ ". This is possibly a bug in Cabal.\n"
Expand Down
2 changes: 1 addition & 1 deletion etlas-cabal/Distribution/Simple/BuildTarget.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ checkBuildTargets verbosity pkg_descr lbi targets = do

formatReason cn DisabledComponent =
"Cannot process the " ++ cn ++ " because the component is marked "
++ "as disabled in the .cabal file."
++ "as disabled in the config file."
formatReason cn DisabledAllTests =
"Cannot process the " ++ cn ++ " because test suites are not "
++ "enabled. Run configure with the flag --enable-tests"
Expand Down
53 changes: 30 additions & 23 deletions etlas-cabal/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,33 +1475,40 @@ defaultPackageDesc _verbosity = tryFindPackageDesc currentDir
-- an @etlas.dhall@ file or @.cabal@ files.
findPackageDesc :: FilePath -- ^Where to look
-> NoCallStackIO (Either String FilePath) -- ^<pkgname>.cabal
findPackageDesc dir
= do let dhallFile = dir </> "etlas.dhall"

existDhallFile <- doesFileExist dhallFile

if existDhallFile then return (Right dhallFile)
else do
files <- getDirectoryContents dir
-- to make sure we do not mistake a ~/.cabal/ dir for a <pkgname>.cabal
-- file we filter to exclude dirs and null base file names:
cabalFiles <- filterM doesFileExist
[ dir </> file
| file <- files
, let (name, ext) = splitExtension file
, not (null name) && ext == ".cabal" ]
case cabalFiles of
[] -> return (Left noDesc)
[cabalFile] -> return (Right cabalFile)
multiple -> return (Left $ multiDesc multiple)

findPackageDesc dir = do
files <- getDirectoryContents dir
-- to make sure we do not mistake a ~/.<ext>/ dir for a <pkgname>.<ext>
-- file we filter to exclude dirs and null base file names:
let findConfigFiles pred = do
cfgFiles <- filterM doesFileExist
[ dir </> file | file <- files , pred file ]
return cfgFiles

findFirstConfigFile [] = return $ Left noDesc
findFirstConfigFile (pred:preds) = do
cfgFiles <- findConfigFiles pred
case cfgFiles of
[] -> findFirstConfigFile preds
[cfgFile] -> return (Right cfgFile)
multiple -> return (Left $ multiDesc multiple)

findFirstConfigFile [(== "etlas.dhall")
,hasNotNullNameAndExtension ".etlas"
,hasNotNullNameAndExtension ".cabal"
]

where
hasNotNullNameAndExtension extCfg file =
let (name, ext) = splitExtension file
in not (null name) && ext == extCfg

noDesc :: String
noDesc = "No cabal file found.\n"
++ "Please create a package description file <pkgname>.cabal"
noDesc = "No config file found.\n"
++ "Please create a package description file "
++ "etlas.dhall, <pkgname>.etlas or <pkgname>.cabal"

multiDesc :: [String] -> String
multiDesc l = "Multiple cabal files found.\n"
multiDesc l = "Multiple config files found.\n"
++ "Please use only one of: "
++ intercalate ", " l

Expand Down
4 changes: 2 additions & 2 deletions etlas/Distribution/Client/CmdErrorMessages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ renderTargetProblemCommon verb (TargetComponentNotProjectLocal pkgid cname _) =
renderTargetProblemCommon verb (TargetComponentNotBuildable pkgid cname _) =
"Cannot " ++ verb ++ " the " ++ showComponentName cname ++ " because it is "
++ "marked as 'buildable: False' within the '" ++ display (packageName pkgid)
++ ".cabal' file (at least for the current configuration). If you believe it "
++ ".cabal/.etlas' or file (at least for the current configuration). If you believe it "
++ "should be buildable then check the .cabal file to see if the buildable "
++ "property is conditional on flags. Alternatively you may simply have to "
++ "edit the .cabal file to declare it as buildable and fix any resulting "
Expand Down Expand Up @@ -347,7 +347,7 @@ renderTargetProblemNoTargets :: String -> TargetSelector -> String
renderTargetProblemNoTargets verb targetSelector =
"Cannot " ++ verb ++ " " ++ renderTargetSelector targetSelector
++ " because " ++ reason targetSelector ++ ". "
++ "Check the .cabal "
++ "Check the .cabal or .etlas "
++ plural (targetSelectorPluralPkgs targetSelector)
"file for the package and make sure that it properly declares "
"files for the packages and make sure that they properly declare "
Expand Down
7 changes: 4 additions & 3 deletions etlas/Distribution/Client/IndexUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ tarEntriesList = go 0
extractPkg :: Verbosity -> Tar.Entry -> BlockNo -> Maybe (IO (Maybe PackageEntry))
extractPkg verbosity entry blockNo = case Tar.entryContent entry of
Tar.NormalFile content _
| takeExtension fileName == ".cabal" || takeFileName fileName == "etlas.dhall"
| takeExtension fileName `elem` [".etlas",".cabal"]
|| takeFileName fileName == "etlas.dhall"
-> case splitDirectories (normalise fileName) of
[pkgname,vers,_] -> case simpleParse vers of
Just ver -> Just $ do
Expand All @@ -541,7 +542,7 @@ extractPkg verbosity entry blockNo = case Tar.entryContent entry of
parsed' <- parsed
case parsed' of
Just d -> return d
Nothing -> error $ "Couldn't read cabal file "
Nothing -> error $ "Couldn't read etlas or cabal file "
++ show fileName
parsed = if takeExtension fileName == ".dhall"
then fmap Just $ PackageDesc.Parse.parseGenericPackageDescriptionFromDhall fileName
Expand Down Expand Up @@ -822,7 +823,7 @@ packageListFromCache verbosity mkPkg idxFile hnd Cache{..} mode patchesDir

getPackageDesc :: Either FilePath BlockNo -> IO (FilePath, ByteString)
getPackageDesc (Left relPath) = do
let path = indexDir </> relPath
let path = indexDir </> relPath
content <- BS.readFile path
return (path, content)
getPackageDesc (Right blockNo) = getEntryInfo blockNo
Expand Down
19 changes: 12 additions & 7 deletions etlas/Distribution/Client/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ initCabal verbosity packageDBs repoCtxt binariesPath comp progdb initFlags = do
writeChangeLog initFlags'
createSourceDirectories initFlags'
createMainHs initFlags'
success <- writeCabalFile initFlags'
success <- writeEtlasFile initFlags'

when success $ generateWarnings initFlags'

Expand Down Expand Up @@ -684,13 +684,18 @@ writeChangeLog flags = when (any (== defaultChangeLog) $ maybe [] id (extraSrc f
pver = maybe "" display $ flagToMaybe $ version flags


--writeCabalFile :: InitFlags -> IO Bool
--writeCabalFile = writeConfigFile ".cabal"

writeCabalFile :: InitFlags -> IO Bool
writeCabalFile flags@(InitFlags{packageName = NoFlag}) = do
writeEtlasFile :: InitFlags -> IO Bool
writeEtlasFile = writeConfigFile ".etlas"

writeConfigFile :: String -> InitFlags -> IO Bool
writeConfigFile _ flags@(InitFlags{packageName = NoFlag}) = do
message flags "Error: no package name provided."
return False
writeCabalFile flags@(InitFlags{packageName = Flag p}) = do
let cabalFileName = display p ++ ".cabal"
writeConfigFile ext flags@(InitFlags{packageName = Flag p}) = do
let cabalFileName = display p ++ ext
message flags $ "Generating " ++ cabalFileName ++ "..."
writeFileSafe flags cabalFileName (generateCabalFile cabalFileName flags)
return True
Expand Down Expand Up @@ -943,9 +948,9 @@ generateWarnings :: InitFlags -> IO ()
generateWarnings flags = do
message flags ""
when (synopsis flags `elem` [NoFlag, Flag ""])
(message flags "Warning: no synopsis given. You should edit the .cabal file and add one.")
(message flags "Warning: no synopsis given. You should edit the .etlas or .cabal file and add one.")

message flags "You may want to edit the .cabal file and add a Description field."
message flags "You may want to edit the .etlas or .cabal file and add a Description field."

-- | Possibly generate a message to stdout, taking into account the
-- --quiet flag.
Expand Down
20 changes: 12 additions & 8 deletions etlas/Distribution/Client/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,23 +1361,27 @@ installLocalTarballPackage
-> Bool
-> Bool
-> IO BuildOutcome
installLocalTarballPackage verbosity pkgid
tarballPath distPref installPkg patchesDir isGit isBinary = do
installLocalTarballPackage verbosity pkgid tarballPath distPref installPkg
patchesDir isGit isBinary = do
tmp <- getTemporaryDirectory
withTempDirectory verbosity tmp "etlas-tmp" $ \tmpDirPath ->
onFailure UnpackFailed $ do
let relUnpackedPath = if isGit then ""
else display pkgid ++ (if isBinary then "-bin" else "")
absUnpackedPath = tmpDirPath </> relUnpackedPath
descFilePath = absUnpackedPath
</> display (packageName pkgid) <.> "cabal"
descFilePath ext = absUnpackedPath
</> display (packageName pkgid) <.> ext
dhallFile = absUnpackedPath </> "etlas.dhall"
info verbosity $ (if isGit then "Copying " else "Extracting ")
++ tarballPath
++ " to " ++ tmpDirPath ++ "..."
patchedExtractTarGzFile verbosity False tmpDirPath relUnpackedPath tarballPath patchesDir isGit isBinary
exists <- doesFileExist descFilePath
when (not exists) $
die' verbosity $ "Package .cabal file not found: " ++ show descFilePath
patchedExtractTarGzFile verbosity False tmpDirPath relUnpackedPath
tarballPath patchesDir isGit isBinary
cfgFiles <- filterM doesFileExist
[descFilePath "etlas", descFilePath "cabal", dhallFile]
when (null cfgFiles) $
die' verbosity $ "Package .etlas or .cabal file not found: "
++ show (descFilePath "")
maybeRenameDistDir absUnpackedPath
installPkg (Just absUnpackedPath)

Expand Down
13 changes: 8 additions & 5 deletions etlas/Distribution/Client/ProjectBuilding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,10 @@ unpackPackageTarball verbosity patchesDir tarball parentdir pkgid _pkgTextOverri

-- Sanity check
--
exists <- doesFileExist cabalFile
when (not exists) $
die' verbosity $ "Package .cabal file not found in the tarball: " ++ cabalFile
existsAny <- forM [dhallFile,etlasFile,cabalFile] doesFileExist
when (not $ or existsAny) $
die' verbosity $ "Package etlas.dhall, .etlas or .cabal file not found in the tarball: "
++ cabalFile

-- TODO: We ignore revisions because that messes with patches. If for some reason
-- someone needs a revision, they can submit the revision as an updated
Expand All @@ -856,8 +857,10 @@ unpackPackageTarball verbosity patchesDir tarball parentdir pkgid _pkgTextOverri
-- writeFileAtomic cabalFile pkgtxt

where
cabalFile = parentdir </> pkgsubdir
</> display pkgname <.> "cabal"
cabalFile = configFile $ display pkgname <.> "cabal"
etlasFile = configFile $ display pkgname <.> "etlas"
dhallFile = configFile $ "etlas.dhall"
configFile file = parentdir </> pkgsubdir </> file
pkgsubdir = display pkgid
pkgname = packageName pkgid

Expand Down
Loading

0 comments on commit 7695eed

Please sign in to comment.