dumpPackageDescription :: PackageDescription -> FilePath -> IO Sexp
dumpPackageDescription pkgDesc cabalFile = do
    (cabalDirs, autogenDir)  <- getBuildDirectories Cabal pkgDesc cabalDir
    (stackDirs, autogenDir') <- getBuildDirectories Stack pkgDesc cabalDir
    let buildDirs   = cabalDirs ++ stackDirs
        autogenDirs = [autogenDir, autogenDir']
    return $
        SList
            [ cons (sym "build-directories") (ordNub (map normalise buildDirs))
            , cons (sym "source-directories") sourceDirs
            , cons (sym "extensions") exts
            , cons (sym "languages") langs
            , cons (sym "dependencies") deps
            , cons (sym "other-options") (cppOpts ++ ghcOpts)
            , cons (sym "autogen-directories") (map normalise autogenDirs)
            ]
  where
    cabalDir :: FilePath
    cabalDir = dropFileName cabalFile
    buildInfo :: [BuildInfo]
    buildInfo = allBuildInfo pkgDesc
    sourceDirs :: [FilePath]
    sourceDirs = ordNub (map normalise (getSourceDirectories buildInfo cabalDir))
    exts :: [Extension]
    exts = nub (concatMap usedExtensions buildInfo)
    langs :: [Language]
    langs = nub (concatMap allLanguages buildInfo)
    thisPackage :: PackageName
    thisPackage = pkgName (package pkgDesc)
    deps :: [Dependency]
    deps =
        nub (filter (\(Dependency name _) -> name /= thisPackage) (buildDepends pkgDesc))
    -- The "cpp-options" configuration field.
    cppOpts :: [String]
    cppOpts =
        ordNub (filter isAllowedOption (concatMap cppOptions buildInfo))
    -- The "ghc-options" configuration field.
    ghcOpts :: [String]
    ghcOpts =
        ordNub (filter isAllowedOption (concatMap (hcOptions GHC) buildInfo))
dumpPackageDescription :: PackageDescription -> FilePath -> IO Sexp
dumpPackageDescription pkgDesc cabalFile = do
    (cabalDirs, autogenDir)  <- getBuildDirectories Cabal pkgDesc cabalDir
    (stackDirs, autogenDir') <- getBuildDirectories Stack pkgDesc cabalDir
    let buildDirs   = cabalDirs ++ stackDirs
        autogenDirs = [autogenDir, autogenDir']
    return $
        SList
            [ cons (sym "build-directories") (ordNub (map normalise buildDirs))
            , cons (sym "source-directories") sourceDirs
            , cons (sym "extensions") exts
            , cons (sym "languages") langs
            , cons (sym "dependencies") deps
            , cons (sym "other-options") (cppOpts ++ ghcOpts)
            , cons (sym "autogen-directories") (map normalise autogenDirs)
            ]
  where
    cabalDir :: FilePath
    cabalDir = dropFileName cabalFile
    buildInfo :: [BuildInfo]
    buildInfo = allBuildInfo pkgDesc
    sourceDirs :: [FilePath]
    sourceDirs = ordNub (map normalise (getSourceDirectories buildInfo cabalDir))
    exts :: [Extension]
    exts = nub (concatMap usedExtensions buildInfo)
    langs :: [Language]
    langs = nub (concatMap allLanguages buildInfo)
    thisPackage :: PackageName
    thisPackage = pkgName (package pkgDesc)
    deps :: [Dependency]
    deps =
        nub (filter (\(Dependency name _) -> name /= thisPackage) (buildDepends pkgDesc))
    -- The "cpp-options" configuration field.
    cppOpts :: [String]
    cppOpts =
        ordNub (filter isAllowedOption (concatMap cppOptions buildInfo))
    -- The "ghc-options" configuration field.
    ghcOpts :: [String]
    ghcOpts =
        ordNub (filter isAllowedOption (concatMap (hcOptions GHC) buildInfo))
dumpPackageDescription pkgDesc cabalFile = do
    (cabalDirs, autogenDir)  <- getBuildDirectories Cabal pkgDesc cabalDir
    (stackDirs, autogenDir') <- getBuildDirectories Stack pkgDesc cabalDir
    let buildDirs   = cabalDirs ++ stackDirs
        autogenDirs = [autogenDir, autogenDir']
    return $
        SList
            [ cons (sym "build-directories") (ordNub (map normalise buildDirs))
            , cons (sym "source-directories") sourceDirs
            , cons (sym "extensions") exts
            , cons (sym "languages") langs
            , cons (sym "dependencies") deps
            , cons (sym "other-options") (cppOpts ++ ghcOpts)
            , cons (sym "autogen-directories") (map normalise autogenDirs)
            ]
  where
    cabalDir :: FilePath
    cabalDir = dropFileName cabalFile
    buildInfo :: [BuildInfo]
    buildInfo = allBuildInfo pkgDesc
    sourceDirs :: [FilePath]
    sourceDirs = ordNub (map normalise (getSourceDirectories buildInfo cabalDir))
    exts :: [Extension]
    exts = nub (concatMap usedExtensions buildInfo)
    langs :: [Language]
    langs = nub (concatMap allLanguages buildInfo)
    thisPackage :: PackageName
    thisPackage = pkgName (package pkgDesc)
    deps :: [Dependency]
    deps =
        nub (filter (\(Dependency name _) -> name /= thisPackage) (buildDepends pkgDesc))
    -- The "cpp-options" configuration field.
    cppOpts :: [String]
    cppOpts =
        ordNub (filter isAllowedOption (concatMap cppOptions buildInfo))
    -- The "ghc-options" configuration field.
    ghcOpts :: [String]
    ghcOpts =
        ordNub (filter isAllowedOption (concatMap (hcOptions GHC) buildInfo))