{-# LANGUAGE NoImplicitPrelude     #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE NoFieldSelectors      #-}
{-# LANGUAGE OverloadedRecordDot   #-}
{-# LANGUAGE OverloadedStrings     #-}

-- Types and functions related to Stack's @script@ command.

module Stack.Script
  ( ScriptOpts (..)
  , ScriptExecute (..)
  , ShouldRun (..)
  , scriptCmd
  ) where

import           Data.ByteString.Builder ( toLazyByteString )
import qualified Data.ByteString.Char8 as S8
import qualified Data.Conduit.List as CL
import           Data.List.Split ( splitWhen )
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import           Distribution.Compiler ( CompilerFlavor (..) )
import           Distribution.ModuleName ( ModuleName )
import qualified Distribution.PackageDescription as PD
import qualified Distribution.Types.CondTree as C
import           Distribution.Types.ModuleReexport ( moduleReexportName )
import           Distribution.Types.PackageName ( mkPackageName )
import           Distribution.Types.VersionRange ( withinRange )
import           Distribution.System ( Platform (..) )
import qualified Pantry.SHA256 as SHA256
import           Path
                   ( (</>), filename, fromAbsDir, fromAbsFile, fromRelFile
                   , parent, parseRelDir, replaceExtension, splitExtension
                   )
import           Path.IO ( getModificationTime, resolveFile' )
import qualified RIO.Directory as Dir
import           RIO.Process ( exec, proc, readProcessStdout_, withWorkingDir )
import qualified RIO.Text as T
import           Stack.Build ( build )
import           Stack.Build.Installed ( getInstalled, toInstallMap )
import           Stack.Constants ( osIsWindows, relDirScripts )
import           Stack.Prelude
import           Stack.Runners
                   ( ShouldReexec (..), withConfig, withDefaultEnvConfig )
import           Stack.Setup ( withNewLocalBuildTargets )
import           Stack.SourceMap ( getCompilerInfo, immutableLocSha )
import           Stack.Types.Compiler ( ActualCompiler (..) )
import           Stack.Types.CompilerPaths
                   ( CompilerPaths (..), GhcPkgExe (..), HasCompiler (..) )
import           Stack.Types.Config ( Config (..), HasConfig (..), stackRootL )
import           Stack.Types.ConfigMonoid ( ConfigMonoid (..) )
import qualified Stack.Types.ConfigMonoid as ConfigMonoid ( ConfigMonoid (..) )
import           Stack.Types.DumpPackage ( DumpPackage (..) )
import           Stack.Types.EnvConfig
                   ( EnvConfig (..), HasEnvConfig (..), actualCompilerVersionL
                   , appropriateGhcColorFlag
                   )
import           Stack.Types.EnvSettings ( defaultEnvSettings )
import           Stack.Types.GlobalOpts ( GlobalOpts (..) )
import           Stack.Types.Platform ( HasPlatform (..) )
import           Stack.Types.Runner ( Runner, globalOptsL )
import           Stack.Types.SourceMap
                   ( CommonPackage (..), DepPackage (..), SourceMap (..) )
import           Stack.Types.StackYamlLoc ( StackYamlLoc (..) )
import           System.FilePath ( splitDrive )

-- | Type representing exceptions thrown by functions exported by the

-- "Stack.Script" module.

data ScriptException
  = MutableDependenciesForScript [PackageName]
  | AmbiguousModuleName ModuleName [PackageName]
  | ArgumentsWithNoRunInvalid
  | NoRunWithoutCompilationInvalid
  | FailedToParseScriptFileAsDirBug (Path Rel File)
  | FailedToParseFileAsDirBug (Path Abs Dir)
  deriving (Int -> ScriptException -> ShowS
[ScriptException] -> ShowS
ScriptException -> FilePath
(Int -> ScriptException -> ShowS)
-> (ScriptException -> FilePath)
-> ([ScriptException] -> ShowS)
-> Show ScriptException
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptException -> ShowS
showsPrec :: Int -> ScriptException -> ShowS
$cshow :: ScriptException -> FilePath
show :: ScriptException -> FilePath
$cshowList :: [ScriptException] -> ShowS
showList :: [ScriptException] -> ShowS
Show, Typeable)

instance Exception ScriptException where
  displayException :: ScriptException -> FilePath
displayException (MutableDependenciesForScript [PackageName]
names) = [FilePath] -> FilePath
unlines
    ([FilePath] -> FilePath) -> [FilePath] -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
"Error: [S-4994]"
    FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: FilePath
"No mutable packages are allowed in the 'script' command. Mutable \
      \packages found:"
    FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: (PackageName -> FilePath) -> [PackageName] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (\PackageName
name -> FilePath
"- " FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ PackageName -> FilePath
packageNameString PackageName
name) [PackageName]
names
  displayException (AmbiguousModuleName ModuleName
mname [PackageName]
pkgs) = [FilePath] -> FilePath
unlines
    ([FilePath] -> FilePath) -> [FilePath] -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
"Error: [S-1691]"
    FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: (  FilePath
"Module "
      FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ ModuleName -> FilePath
moduleNameString ModuleName
mname
      FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ FilePath
" appears in multiple packages: "
      )
    FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: [ [FilePath] -> FilePath
unwords ([FilePath] -> FilePath) -> [FilePath] -> FilePath
forall a b. (a -> b) -> a -> b
$ (PackageName -> FilePath) -> [PackageName] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map PackageName -> FilePath
packageNameString [PackageName]
pkgs ]
  displayException ScriptException
ArgumentsWithNoRunInvalid =
    FilePath
"Error: [S-5067]\n"
    FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ FilePath
"'--no-run' incompatible with arguments."
  displayException ScriptException
NoRunWithoutCompilationInvalid =
    FilePath
"Error: [S-9469]\n"
    FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ FilePath
"'--no-run' requires either '--compile' or '--optimize'."
  displayException (FailedToParseScriptFileAsDirBug Path Rel File
fp) = FilePath -> ShowS
bugReport FilePath
"[S-5055]" ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
       FilePath
"Failed to parse script file name as directory:\n"
    FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> Path Rel File -> FilePath
fromRelFile Path Rel File
fp FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
"\n"
  displayException (FailedToParseFileAsDirBug Path Abs Dir
p) = FilePath -> ShowS
bugReport FilePath
"[S-9464]" ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
       FilePath
"Failed to parse path to script file as directory:\n"
    FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> Path Abs Dir -> FilePath
fromAbsDir Path Abs Dir
p FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
"\n"

-- | Type representing choices of interpreting, compiling (without optimisation)

-- and compiling (with optimisation).

data ScriptExecute
  = SEInterpret
  | SECompile
    -- ^ Without optimisation.

  | SEOptimize
    -- ^ Compile with optimisation.

  deriving Int -> ScriptExecute -> ShowS
[ScriptExecute] -> ShowS
ScriptExecute -> FilePath
(Int -> ScriptExecute -> ShowS)
-> (ScriptExecute -> FilePath)
-> ([ScriptExecute] -> ShowS)
-> Show ScriptExecute
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptExecute -> ShowS
showsPrec :: Int -> ScriptExecute -> ShowS
$cshow :: ScriptExecute -> FilePath
show :: ScriptExecute -> FilePath
$cshowList :: [ScriptExecute] -> ShowS
showList :: [ScriptExecute] -> ShowS
Show

-- | Type representing choices of whether to run or not.

data ShouldRun
  = YesRun
    -- ^ Run.

  | NoRun
    -- ^ Do not run.

  deriving Int -> ShouldRun -> ShowS
[ShouldRun] -> ShowS
ShouldRun -> FilePath
(Int -> ShouldRun -> ShowS)
-> (ShouldRun -> FilePath)
-> ([ShouldRun] -> ShowS)
-> Show ShouldRun
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ShouldRun -> ShowS
showsPrec :: Int -> ShouldRun -> ShowS
$cshow :: ShouldRun -> FilePath
show :: ShouldRun -> FilePath
$cshowList :: [ShouldRun] -> ShowS
showList :: [ShouldRun] -> ShowS
Show

-- | Type representing command line options for the @stack script@ command.

data ScriptOpts = ScriptOpts
  { ScriptOpts -> [FilePath]
packages :: ![String]
  , ScriptOpts -> FilePath
file :: !FilePath
  , ScriptOpts -> [FilePath]
args :: ![String]
  , ScriptOpts -> ScriptExecute
compile :: !ScriptExecute
  , ScriptOpts -> Bool
useRoot :: !Bool
  , ScriptOpts -> [FilePath]
ghcOptions :: ![String]
  , ScriptOpts -> [PackageIdentifierRevision]
scriptExtraDeps :: ![PackageIdentifierRevision]
  , ScriptOpts -> ShouldRun
shouldRun :: !ShouldRun
  }
  deriving Int -> ScriptOpts -> ShowS
[ScriptOpts] -> ShowS
ScriptOpts -> FilePath
(Int -> ScriptOpts -> ShowS)
-> (ScriptOpts -> FilePath)
-> ([ScriptOpts] -> ShowS)
-> Show ScriptOpts
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptOpts -> ShowS
showsPrec :: Int -> ScriptOpts -> ShowS
$cshow :: ScriptOpts -> FilePath
show :: ScriptOpts -> FilePath
$cshowList :: [ScriptOpts] -> ShowS
showList :: [ScriptOpts] -> ShowS
Show

-- | Run a Stack Script

scriptCmd :: ScriptOpts -> RIO Runner ()
scriptCmd :: ScriptOpts -> RIO Runner ()
scriptCmd ScriptOpts
opts = do
  -- Some warnings in case the user somehow tries to set a stack.yaml location.

  -- Note that in this functions we use logError instead of logWarn because,

  -- when using the interpreter mode, only error messages are shown. See:

  -- https://github.com/commercialhaskell/stack/issues/3007

  Getting StackYamlLoc Runner StackYamlLoc -> RIO Runner StackYamlLoc
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view ((GlobalOpts -> Const StackYamlLoc GlobalOpts)
-> Runner -> Const StackYamlLoc Runner
forall env. HasRunner env => Lens' env GlobalOpts
Lens' Runner GlobalOpts
globalOptsL ((GlobalOpts -> Const StackYamlLoc GlobalOpts)
 -> Runner -> Const StackYamlLoc Runner)
-> ((StackYamlLoc -> Const StackYamlLoc StackYamlLoc)
    -> GlobalOpts -> Const StackYamlLoc GlobalOpts)
-> Getting StackYamlLoc Runner StackYamlLoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GlobalOpts -> StackYamlLoc)
-> SimpleGetter GlobalOpts StackYamlLoc
forall s a. (s -> a) -> SimpleGetter s a
to (.stackYaml)) RIO Runner StackYamlLoc
-> (StackYamlLoc -> RIO Runner ()) -> RIO Runner ()
forall a b. RIO Runner a -> (a -> RIO Runner b) -> RIO Runner b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    SYLOverride Path Abs File
fp -> Utf8Builder -> RIO Runner ()
forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logError (Utf8Builder -> RIO Runner ()) -> Utf8Builder -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$
         Utf8Builder
"Ignoring override stack.yaml file for script command: "
      Utf8Builder -> Utf8Builder -> Utf8Builder
forall a. Semigroup a => a -> a -> a
<> FilePath -> Utf8Builder
forall a. IsString a => FilePath -> a
fromString (Path Abs File -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path Abs File
fp)
    StackYamlLoc
SYLGlobalProject -> Utf8Builder -> RIO Runner ()
forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logError Utf8Builder
"Ignoring SYLGlobalProject for script command"
    StackYamlLoc
SYLDefault -> () -> RIO Runner ()
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    SYLNoProject [PackageIdentifierRevision]
_ -> Bool -> RIO Runner () -> RIO Runner ()
forall a. HasCallStack => Bool -> a -> a
assert Bool
False (() -> RIO Runner ()
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

  Path Abs File
file <- FilePath -> RIO Runner (Path Abs File)
forall (m :: * -> *). MonadIO m => FilePath -> m (Path Abs File)
resolveFile' ScriptOpts
opts.file
  let scriptFile :: Path Rel File
scriptFile = Path Abs File -> Path Rel File
forall b. Path b File -> Path Rel File
filename Path Abs File
file

  Bool
isNoRunCompile <- FirstFalse -> Bool
fromFirstFalse (FirstFalse -> Bool)
-> (ConfigMonoid -> FirstFalse) -> ConfigMonoid -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (.noRunCompile) (ConfigMonoid -> Bool)
-> RIO Runner ConfigMonoid -> RIO Runner Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                           Getting ConfigMonoid Runner ConfigMonoid -> RIO Runner ConfigMonoid
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view ((GlobalOpts -> Const ConfigMonoid GlobalOpts)
-> Runner -> Const ConfigMonoid Runner
forall env. HasRunner env => Lens' env GlobalOpts
Lens' Runner GlobalOpts
globalOptsL ((GlobalOpts -> Const ConfigMonoid GlobalOpts)
 -> Runner -> Const ConfigMonoid Runner)
-> ((ConfigMonoid -> Const ConfigMonoid ConfigMonoid)
    -> GlobalOpts -> Const ConfigMonoid GlobalOpts)
-> Getting ConfigMonoid Runner ConfigMonoid
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GlobalOpts -> ConfigMonoid)
-> SimpleGetter GlobalOpts ConfigMonoid
forall s a. (s -> a) -> SimpleGetter s a
to (.configMonoid))

  let scriptDir :: Path Abs Dir
scriptDir = Path Abs File -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs File
file
      modifyGO :: GlobalOpts -> GlobalOpts
modifyGO GlobalOpts
go = GlobalOpts
go
        { configMonoid = go.configMonoid
            { ConfigMonoid.installGHC = FirstTrue $ Just True
            }
        , stackYaml = SYLNoProject opts.scriptExtraDeps
        }
      (ShouldRun
shouldRun, ScriptExecute
shouldCompile) = if Bool
isNoRunCompile
        then (ShouldRun
NoRun, ScriptExecute
SECompile)
        else (ScriptOpts
opts.shouldRun, ScriptOpts
opts.compile)

  Path Abs Dir
root <- ShouldReexec
-> RIO Config (Path Abs Dir) -> RIO Runner (Path Abs Dir)
forall a. ShouldReexec -> RIO Config a -> RIO Runner a
withConfig ShouldReexec
NoReexec (RIO Config (Path Abs Dir) -> RIO Runner (Path Abs Dir))
-> RIO Config (Path Abs Dir) -> RIO Runner (Path Abs Dir)
forall a b. (a -> b) -> a -> b
$ Getting (Path Abs Dir) Config (Path Abs Dir)
-> RIO Config (Path Abs Dir)
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Path Abs Dir) Config (Path Abs Dir)
forall s. HasConfig s => Lens' s (Path Abs Dir)
Lens' Config (Path Abs Dir)
stackRootL
  Path Abs Dir
outputDir <- if ScriptOpts
opts.useRoot
    then do
      Path Rel Dir
scriptFileAsDir <- RIO Runner (Path Rel Dir)
-> (Path Rel Dir -> RIO Runner (Path Rel Dir))
-> Maybe (Path Rel Dir)
-> RIO Runner (Path Rel Dir)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        (ScriptException -> RIO Runner (Path Rel Dir)
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (ScriptException -> RIO Runner (Path Rel Dir))
-> ScriptException -> RIO Runner (Path Rel Dir)
forall a b. (a -> b) -> a -> b
$ Path Rel File -> ScriptException
FailedToParseScriptFileAsDirBug Path Rel File
scriptFile)
        Path Rel Dir -> RIO Runner (Path Rel Dir)
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (FilePath -> Maybe (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel Dir)
parseRelDir (FilePath -> Maybe (Path Rel Dir))
-> FilePath -> Maybe (Path Rel Dir)
forall a b. (a -> b) -> a -> b
$ Path Rel File -> FilePath
fromRelFile Path Rel File
scriptFile)
      let fileAsDir :: Path Abs Dir
fileAsDir = Path Abs Dir
scriptDir Path Abs Dir -> Path Rel Dir -> Path Abs Dir
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel Dir
scriptFileAsDir
          -- We drop the information about the drive. On Windows, in principle,

          -- the drive could distinguish between two otherwise identical

          -- fileAsDir (eg C:\MyScript.hs\ D:\MyScript.hs\). In pactice, we

          -- tolerate that possibility as being unlikely.

          (FilePath
_, FilePath
escaped) = FilePath -> (FilePath, FilePath)
splitDrive (Path Abs Dir -> FilePath
fromAbsDir Path Abs Dir
fileAsDir)
      Path Rel Dir
escapedRelDir <- RIO Runner (Path Rel Dir)
-> (Path Rel Dir -> RIO Runner (Path Rel Dir))
-> Maybe (Path Rel Dir)
-> RIO Runner (Path Rel Dir)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        (ScriptException -> RIO Runner (Path Rel Dir)
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (ScriptException -> RIO Runner (Path Rel Dir))
-> ScriptException -> RIO Runner (Path Rel Dir)
forall a b. (a -> b) -> a -> b
$ Path Abs Dir -> ScriptException
FailedToParseFileAsDirBug Path Abs Dir
fileAsDir)
        Path Rel Dir -> RIO Runner (Path Rel Dir)
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (FilePath -> Maybe (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel Dir)
parseRelDir FilePath
escaped)
      Path Abs Dir -> RIO Runner (Path Abs Dir)
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Path Abs Dir -> RIO Runner (Path Abs Dir))
-> Path Abs Dir -> RIO Runner (Path Abs Dir)
forall a b. (a -> b) -> a -> b
$ Path Abs Dir
root Path Abs Dir -> Path Rel Dir -> Path Abs Dir
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel Dir
relDirScripts Path Rel Dir -> Path Rel Dir -> Path Rel Dir
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel Dir
escapedRelDir
    else Path Abs Dir -> RIO Runner (Path Abs Dir)
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Path Abs Dir
scriptDir

  -- path does not necessarily end with an extension.

  let dropExtension :: Path b File -> f (Path b File)
dropExtension Path b File
path = Path b File -> f (Path b File)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Path b File -> f (Path b File)) -> Path b File -> f (Path b File)
forall a b. (a -> b) -> a -> b
$ Path b File
-> ((Path b File, FilePath) -> Path b File)
-> Maybe (Path b File, FilePath)
-> Path b File
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Path b File
path (Path b File, FilePath) -> Path b File
forall a b. (a, b) -> a
fst (Maybe (Path b File, FilePath) -> Path b File)
-> Maybe (Path b File, FilePath) -> Path b File
forall a b. (a -> b) -> a -> b
$ Path b File -> Maybe (Path b File, FilePath)
forall (m :: * -> *) b.
MonadThrow m =>
Path b File -> m (Path b File, FilePath)
splitExtension Path b File
path

  Path Abs File
exe <- if Bool
osIsWindows
    then FilePath -> Path Abs File -> RIO Runner (Path Abs File)
forall (m :: * -> *) b.
MonadThrow m =>
FilePath -> Path b File -> m (Path b File)
replaceExtension FilePath
".exe" (Path Abs Dir
outputDir Path Abs Dir -> Path Rel File -> Path Abs File
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel File
scriptFile)
    else Path Abs File -> RIO Runner (Path Abs File)
forall {f :: * -> *} {b}.
Applicative f =>
Path b File -> f (Path b File)
dropExtension (Path Abs Dir
outputDir Path Abs Dir -> Path Rel File -> Path Abs File
forall b t. Path b Dir -> Path Rel t -> Path b t
</> Path Rel File
scriptFile)

  case ShouldRun
shouldRun of
    ShouldRun
YesRun -> () -> RIO Runner ()
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    ShouldRun
NoRun -> do
      Bool -> RIO Runner () -> RIO Runner ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FilePath] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ScriptOpts
opts.args) (RIO Runner () -> RIO Runner ()) -> RIO Runner () -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$ ScriptException -> RIO Runner ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO ScriptException
ArgumentsWithNoRunInvalid
      case ScriptExecute
shouldCompile of
        ScriptExecute
SEInterpret -> ScriptException -> RIO Runner ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO ScriptException
NoRunWithoutCompilationInvalid
        ScriptExecute
SECompile -> () -> RIO Runner ()
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
        ScriptExecute
SEOptimize -> () -> RIO Runner ()
forall a. a -> RIO Runner a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

  -- Optimization: if we're compiling, and the executable is newer than the

  -- source file, run it immediately.

  (Runner -> Runner) -> RIO Runner () -> RIO Runner ()
forall a. (Runner -> Runner) -> RIO Runner a -> RIO Runner a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (ASetter Runner Runner GlobalOpts GlobalOpts
-> (GlobalOpts -> GlobalOpts) -> Runner -> Runner
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter Runner Runner GlobalOpts GlobalOpts
forall env. HasRunner env => Lens' env GlobalOpts
Lens' Runner GlobalOpts
globalOptsL GlobalOpts -> GlobalOpts
modifyGO) (RIO Runner () -> RIO Runner ()) -> RIO Runner () -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$
    case ScriptExecute
shouldCompile of
      ScriptExecute
SEInterpret -> ShouldRun
-> ScriptExecute -> Path Abs File -> Path Abs File -> RIO Runner ()
forall {t}.
ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
longWay ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs File
file Path Abs File
exe
      ScriptExecute
SECompile -> ShouldRun
-> ScriptExecute -> Path Abs File -> Path Abs File -> RIO Runner ()
forall {t}.
ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
shortCut ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs File
file Path Abs File
exe
      ScriptExecute
SEOptimize -> ShouldRun
-> ScriptExecute -> Path Abs File -> Path Abs File -> RIO Runner ()
forall {t}.
ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
shortCut ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs File
file Path Abs File
exe

 where
  runCompiled :: ShouldRun -> Path Abs File -> RIO env ()
runCompiled ShouldRun
shouldRun Path Abs File
exe = do
    case ShouldRun
shouldRun of
      ShouldRun
YesRun -> FilePath -> [FilePath] -> RIO env ()
forall env b.
(HasProcessContext env, HasLogFunc env) =>
FilePath -> [FilePath] -> RIO env b
exec (Path Abs File -> FilePath
fromAbsFile Path Abs File
exe) ScriptOpts
opts.args
      ShouldRun
NoRun -> [StyleDoc] -> RIO env ()
forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
[StyleDoc] -> m ()
prettyInfoL
        [ FilePath -> StyleDoc
flow FilePath
"Compilation finished, executable available at"
        , Style -> StyleDoc -> StyleDoc
style Style
File (FilePath -> StyleDoc
forall a. IsString a => FilePath -> a
fromString (Path Abs File -> FilePath
fromAbsFile Path Abs File
exe)) StyleDoc -> StyleDoc -> StyleDoc
forall a. Semigroup a => a -> a -> a
<> StyleDoc
"."
        ]

  shortCut :: ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
shortCut ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs t
file Path Abs File
exe =
    (IOException -> RIO Runner ()) -> RIO Runner () -> RIO Runner ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(IOException -> m a) -> m a -> m a
handleIO (RIO Runner () -> IOException -> RIO Runner ()
forall a b. a -> b -> a
const (RIO Runner () -> IOException -> RIO Runner ())
-> RIO Runner () -> IOException -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$ ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
forall {t}.
ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
longWay ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs t
file Path Abs File
exe) (RIO Runner () -> RIO Runner ()) -> RIO Runner () -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$ do
      UTCTime
srcMod <- Path Abs t -> RIO Runner UTCTime
forall (m :: * -> *) b t. MonadIO m => Path b t -> m UTCTime
getModificationTime Path Abs t
file
      UTCTime
exeMod <- FilePath -> RIO Runner UTCTime
forall (m :: * -> *). MonadIO m => FilePath -> m UTCTime
Dir.getModificationTime (Path Abs File -> FilePath
fromAbsFile Path Abs File
exe)
      if UTCTime
srcMod UTCTime -> UTCTime -> Bool
forall a. Ord a => a -> a -> Bool
< UTCTime
exeMod
        then ShouldRun -> Path Abs File -> RIO Runner ()
forall {env}.
(HasTerm env, HasProcessContext env) =>
ShouldRun -> Path Abs File -> RIO env ()
runCompiled ShouldRun
shouldRun Path Abs File
exe
        else ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
forall {t}.
ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
longWay ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs t
file Path Abs File
exe

  longWay :: ShouldRun
-> ScriptExecute -> Path Abs t -> Path Abs File -> RIO Runner ()
longWay ShouldRun
shouldRun ScriptExecute
shouldCompile Path Abs t
file Path Abs File
exe =
    ShouldReexec -> RIO Config () -> RIO Runner ()
forall a. ShouldReexec -> RIO Config a -> RIO Runner a
withConfig ShouldReexec
YesReexec (RIO Config () -> RIO Runner ()) -> RIO Config () -> RIO Runner ()
forall a b. (a -> b) -> a -> b
$
    RIO EnvConfig () -> RIO Config ()
forall a. RIO EnvConfig a -> RIO Config a
withDefaultEnvConfig (RIO EnvConfig () -> RIO Config ())
-> RIO EnvConfig () -> RIO Config ()
forall a b. (a -> b) -> a -> b
$ do
      Config
config <- Getting Config EnvConfig Config -> RIO EnvConfig Config
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Config EnvConfig Config
forall env. HasConfig env => Lens' env Config
Lens' EnvConfig Config
configL
      ProcessContext
menv <- IO ProcessContext -> RIO EnvConfig ProcessContext
forall a. IO a -> RIO EnvConfig a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ProcessContext -> RIO EnvConfig ProcessContext)
-> IO ProcessContext -> RIO EnvConfig ProcessContext
forall a b. (a -> b) -> a -> b
$ Config
config.processContextSettings EnvSettings
defaultEnvSettings
      ProcessContext -> RIO EnvConfig () -> RIO EnvConfig ()
forall env a.
HasProcessContext env =>
ProcessContext -> RIO env a -> RIO env a
withProcessContext ProcessContext
menv (RIO EnvConfig () -> RIO EnvConfig ())
-> RIO EnvConfig () -> RIO EnvConfig ()
forall a b. (a -> b) -> a -> b
$ do
        Maybe FilePath
colorFlag <- RIO EnvConfig (Maybe FilePath)
forall env.
(HasEnvConfig env, HasRunner env) =>
RIO env (Maybe FilePath)
appropriateGhcColorFlag

        Set PackageName
targetsSet <-
          case ScriptOpts
opts.packages of
            [] -> FilePath -> RIO EnvConfig (Set PackageName)
getPackagesFromImports ScriptOpts
opts.file -- Using the import parser

            [FilePath]
packages -> do
              let targets :: [FilePath]
targets = (FilePath -> [FilePath]) -> [FilePath] -> [FilePath]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap FilePath -> [FilePath]
wordsComma [FilePath]
packages
              [PackageName]
targets' <- (FilePath -> RIO EnvConfig PackageName)
-> [FilePath] -> RIO EnvConfig [PackageName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM FilePath -> RIO EnvConfig PackageName
forall (m :: * -> *). MonadThrow m => FilePath -> m PackageName
parsePackageNameThrowing [FilePath]
targets
              Set PackageName -> RIO EnvConfig (Set PackageName)
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set PackageName -> RIO EnvConfig (Set PackageName))
-> Set PackageName -> RIO EnvConfig (Set PackageName)
forall a b. (a -> b) -> a -> b
$ [PackageName] -> Set PackageName
forall a. Ord a => [a] -> Set a
Set.fromList [PackageName]
targets'

        Bool -> RIO EnvConfig () -> RIO EnvConfig ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set PackageName -> Bool
forall a. Set a -> Bool
Set.null Set PackageName
targetsSet) (RIO EnvConfig () -> RIO EnvConfig ())
-> RIO EnvConfig () -> RIO EnvConfig ()
forall a b. (a -> b) -> a -> b
$ do
          -- Optimization: use the relatively cheap ghc-pkg list --simple-output

          -- to check which packages are installed already. If all needed

          -- packages are available, we can skip the (rather expensive) build

          -- call below.

          GhcPkgExe Path Abs File
pkg <- Getting GhcPkgExe EnvConfig GhcPkgExe -> RIO EnvConfig GhcPkgExe
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting GhcPkgExe EnvConfig GhcPkgExe -> RIO EnvConfig GhcPkgExe)
-> Getting GhcPkgExe EnvConfig GhcPkgExe -> RIO EnvConfig GhcPkgExe
forall a b. (a -> b) -> a -> b
$ Getting GhcPkgExe EnvConfig CompilerPaths
forall env. HasCompiler env => SimpleGetter env CompilerPaths
SimpleGetter EnvConfig CompilerPaths
compilerPathsL Getting GhcPkgExe EnvConfig CompilerPaths
-> ((GhcPkgExe -> Const GhcPkgExe GhcPkgExe)
    -> CompilerPaths -> Const GhcPkgExe CompilerPaths)
-> Getting GhcPkgExe EnvConfig GhcPkgExe
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CompilerPaths -> GhcPkgExe)
-> SimpleGetter CompilerPaths GhcPkgExe
forall s a. (s -> a) -> SimpleGetter s a
to (.pkg)
          -- https://github.com/haskell/process/issues/251

          [ByteString]
bss <- ((), [ByteString]) -> [ByteString]
forall a b. (a, b) -> b
snd (((), [ByteString]) -> [ByteString])
-> RIO EnvConfig ((), [ByteString]) -> RIO EnvConfig [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath
-> [FilePath]
-> ConduitM ByteString Void (RIO EnvConfig) ()
-> ConduitM ByteString Void (RIO EnvConfig) [ByteString]
-> RIO EnvConfig ((), [ByteString])
forall e o env.
(HasProcessContext env, HasLogFunc env, HasCallStack) =>
FilePath
-> [FilePath]
-> ConduitM ByteString Void (RIO env) e
-> ConduitM ByteString Void (RIO env) o
-> RIO env (e, o)
sinkProcessStderrStdout (Path Abs File -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path Abs File
pkg)
              [FilePath
"list", FilePath
"--simple-output"] ConduitM ByteString Void (RIO EnvConfig) ()
forall (m :: * -> *) i o. Monad m => ConduitT i o m ()
CL.sinkNull ConduitM ByteString Void (RIO EnvConfig) [ByteString]
forall (m :: * -> *) a o. Monad m => ConduitT a o m [a]
CL.consume -- FIXME use the package info from envConfigPackages, or is that crazy?

          let installed :: Set FilePath
installed = [FilePath] -> Set FilePath
forall a. Ord a => [a] -> Set a
Set.fromList
                        ([FilePath] -> Set FilePath) -> [FilePath] -> Set FilePath
forall a b. (a -> b) -> a -> b
$ ShowS -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map ShowS
toPackageName
                        ([FilePath] -> [FilePath]) -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> a -> b
$ FilePath -> [FilePath]
words
                        (FilePath -> [FilePath]) -> FilePath -> [FilePath]
forall a b. (a -> b) -> a -> b
$ ByteString -> FilePath
S8.unpack
                        (ByteString -> FilePath) -> ByteString -> FilePath
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
S8.concat [ByteString]
bss
          if Set FilePath -> Bool
forall a. Set a -> Bool
Set.null (Set FilePath -> Bool) -> Set FilePath -> Bool
forall a b. (a -> b) -> a -> b
$ Set FilePath -> Set FilePath -> Set FilePath
forall a. Ord a => Set a -> Set a -> Set a
Set.difference ((PackageName -> FilePath) -> Set PackageName -> Set FilePath
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map PackageName -> FilePath
packageNameString Set PackageName
targetsSet) Set FilePath
installed
            then Utf8Builder -> RIO EnvConfig ()
forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logDebug Utf8Builder
"All packages already installed"
            else do
              Utf8Builder -> RIO EnvConfig ()
forall (m :: * -> *) env.
(MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack) =>
Utf8Builder -> m ()
logDebug Utf8Builder
"Missing packages, performing installation"
              let targets :: [Text]
targets =
                    (PackageName -> Text) -> [PackageName] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath -> Text
T.pack (FilePath -> Text)
-> (PackageName -> FilePath) -> PackageName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> FilePath
packageNameString) ([PackageName] -> [Text]) -> [PackageName] -> [Text]
forall a b. (a -> b) -> a -> b
$ Set PackageName -> [PackageName]
forall a. Set a -> [a]
Set.toList Set PackageName
targetsSet
              [Text] -> RIO EnvConfig () -> RIO EnvConfig ()
forall env a. HasEnvConfig env => [Text] -> RIO env a -> RIO env a
withNewLocalBuildTargets [Text]
targets (RIO EnvConfig () -> RIO EnvConfig ())
-> RIO EnvConfig () -> RIO EnvConfig ()
forall a b. (a -> b) -> a -> b
$ Maybe (Set (Path Abs File) -> IO ()) -> RIO EnvConfig ()
forall env.
HasEnvConfig env =>
Maybe (Set (Path Abs File) -> IO ()) -> RIO env ()
build Maybe (Set (Path Abs File) -> IO ())
forall a. Maybe a
Nothing

        let ghcArgs :: [FilePath]
ghcArgs = [[FilePath]] -> [FilePath]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
              [ [FilePath
"-i", FilePath
"-i" FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ Path Abs Dir -> FilePath
fromAbsDir (Path Abs t -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs t
file)]
              , [FilePath
"-hide-all-packages"]
              , Maybe FilePath -> [FilePath]
forall a. Maybe a -> [a]
maybeToList Maybe FilePath
colorFlag
              , ShowS -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath
"-package" ++)
                  ([FilePath] -> [FilePath]) -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> a -> b
$ Set FilePath -> [FilePath]
forall a. Set a -> [a]
Set.toList
                  (Set FilePath -> [FilePath]) -> Set FilePath -> [FilePath]
forall a b. (a -> b) -> a -> b
$ FilePath -> Set FilePath -> Set FilePath
forall a. Ord a => a -> Set a -> Set a
Set.insert FilePath
"base"
                  (Set FilePath -> Set FilePath) -> Set FilePath -> Set FilePath
forall a b. (a -> b) -> a -> b
$ (PackageName -> FilePath) -> Set PackageName -> Set FilePath
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map PackageName -> FilePath
packageNameString Set PackageName
targetsSet
              , case ScriptExecute
shouldCompile of
                  ScriptExecute
SEInterpret -> []
                  ScriptExecute
SECompile -> []
                  ScriptExecute
SEOptimize -> [FilePath
"-O2"]
              , ScriptOpts
opts.ghcOptions
              , if ScriptOpts
opts.useRoot
                  then
                    [ FilePath
"-outputdir=" FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ Path Abs Dir -> FilePath
fromAbsDir (Path Abs File -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs File
exe)
                    , FilePath
"-o", Path Abs File -> FilePath
fromAbsFile Path Abs File
exe
                    ]
                  else []
              ]
        case ScriptExecute
shouldCompile of
          ScriptExecute
SEInterpret -> do
            Path Abs File
interpret <- Getting (Path Abs File) EnvConfig (Path Abs File)
-> RIO EnvConfig (Path Abs File)
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting (Path Abs File) EnvConfig (Path Abs File)
 -> RIO EnvConfig (Path Abs File))
-> Getting (Path Abs File) EnvConfig (Path Abs File)
-> RIO EnvConfig (Path Abs File)
forall a b. (a -> b) -> a -> b
$ Getting (Path Abs File) EnvConfig CompilerPaths
forall env. HasCompiler env => SimpleGetter env CompilerPaths
SimpleGetter EnvConfig CompilerPaths
compilerPathsL Getting (Path Abs File) EnvConfig CompilerPaths
-> ((Path Abs File -> Const (Path Abs File) (Path Abs File))
    -> CompilerPaths -> Const (Path Abs File) CompilerPaths)
-> Getting (Path Abs File) EnvConfig (Path Abs File)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CompilerPaths -> Path Abs File)
-> SimpleGetter CompilerPaths (Path Abs File)
forall s a. (s -> a) -> SimpleGetter s a
to (.interpreter)
            FilePath -> [FilePath] -> RIO EnvConfig ()
forall env b.
(HasProcessContext env, HasLogFunc env) =>
FilePath -> [FilePath] -> RIO env b
exec (Path Abs File -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path Abs File
interpret)
                ([FilePath]
ghcArgs [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ Path Abs t -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path Abs t
file FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: ScriptOpts
opts.args)
          ScriptExecute
_ -> do
            -- Use readProcessStdout_ so that (1) if GHC does send any output

            -- to stdout, we capture it and stop it from being sent to our

            -- stdout, which could break scripts, and (2) if there's an

            -- exception, the standard output we did capture will be reported

            -- to the user.

            IO () -> RIO EnvConfig ()
forall a. IO a -> RIO EnvConfig a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO EnvConfig ()) -> IO () -> RIO EnvConfig ()
forall a b. (a -> b) -> a -> b
$ Bool -> FilePath -> IO ()
forall (m :: * -> *). MonadIO m => Bool -> FilePath -> m ()
Dir.createDirectoryIfMissing Bool
True (Path Abs Dir -> FilePath
fromAbsDir (Path Abs File -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs File
exe))
            FilePath
compilerExeName <-
              Getting FilePath EnvConfig FilePath -> RIO EnvConfig FilePath
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting FilePath EnvConfig FilePath -> RIO EnvConfig FilePath)
-> Getting FilePath EnvConfig FilePath -> RIO EnvConfig FilePath
forall a b. (a -> b) -> a -> b
$ Getting FilePath EnvConfig CompilerPaths
forall env. HasCompiler env => SimpleGetter env CompilerPaths
SimpleGetter EnvConfig CompilerPaths
compilerPathsL Getting FilePath EnvConfig CompilerPaths
-> ((FilePath -> Const FilePath FilePath)
    -> CompilerPaths -> Const FilePath CompilerPaths)
-> Getting FilePath EnvConfig FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CompilerPaths -> Path Abs File)
-> SimpleGetter CompilerPaths (Path Abs File)
forall s a. (s -> a) -> SimpleGetter s a
to (.compiler) Getting FilePath CompilerPaths (Path Abs File)
-> ((FilePath -> Const FilePath FilePath)
    -> Path Abs File -> Const FilePath (Path Abs File))
-> (FilePath -> Const FilePath FilePath)
-> CompilerPaths
-> Const FilePath CompilerPaths
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path Abs File -> FilePath)
-> SimpleGetter (Path Abs File) FilePath
forall s a. (s -> a) -> SimpleGetter s a
to Path Abs File -> FilePath
forall b t. Path b t -> FilePath
toFilePath
            FilePath -> RIO EnvConfig () -> RIO EnvConfig ()
forall env (m :: * -> *) a.
(HasProcessContext env, MonadReader env m, MonadIO m) =>
FilePath -> m a -> m a
withWorkingDir (Path Abs Dir -> FilePath
fromAbsDir (Path Abs t -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent Path Abs t
file)) (RIO EnvConfig () -> RIO EnvConfig ())
-> RIO EnvConfig () -> RIO EnvConfig ()
forall a b. (a -> b) -> a -> b
$ FilePath
-> [FilePath]
-> (ProcessConfig () () () -> RIO EnvConfig ())
-> RIO EnvConfig ()
forall env (m :: * -> *) a.
(HasProcessContext env, HasLogFunc env, MonadReader env m,
 MonadIO m, HasCallStack) =>
FilePath -> [FilePath] -> (ProcessConfig () () () -> m a) -> m a
proc
              FilePath
compilerExeName
              ([FilePath]
ghcArgs [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [Path Abs t -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path Abs t
file])
              (RIO EnvConfig ByteString -> RIO EnvConfig ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (RIO EnvConfig ByteString -> RIO EnvConfig ())
-> (ProcessConfig () () () -> RIO EnvConfig ByteString)
-> ProcessConfig () () ()
-> RIO EnvConfig ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProcessConfig () () () -> RIO EnvConfig ByteString
forall (m :: * -> *) stdin stdoutIgnored stderr.
MonadIO m =>
ProcessConfig stdin stdoutIgnored stderr -> m ByteString
readProcessStdout_)
            ShouldRun -> Path Abs File -> RIO EnvConfig ()
forall {env}.
(HasTerm env, HasProcessContext env) =>
ShouldRun -> Path Abs File -> RIO env ()
runCompiled ShouldRun
shouldRun Path Abs File
exe

  toPackageName :: ShowS
toPackageName = ShowS
forall a. [a] -> [a]
reverse ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Int -> [a] -> [a]
drop Int
1 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'-') ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
forall a. [a] -> [a]
reverse

  -- Like words, but splits on both commas and spaces

  wordsComma :: FilePath -> [FilePath]
wordsComma = (Char -> Bool) -> FilePath -> [FilePath]
forall a. (a -> Bool) -> [a] -> [[a]]
splitWhen (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
',')

getPackagesFromImports ::
     FilePath -- ^ script filename

  -> RIO EnvConfig (Set PackageName)
getPackagesFromImports :: FilePath -> RIO EnvConfig (Set PackageName)
getPackagesFromImports FilePath
scriptFP = do
  (Set PackageName
pns, Set ModuleName
mns) <- IO (Set PackageName, Set ModuleName)
-> RIO EnvConfig (Set PackageName, Set ModuleName)
forall a. IO a -> RIO EnvConfig a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Set PackageName, Set ModuleName)
 -> RIO EnvConfig (Set PackageName, Set ModuleName))
-> IO (Set PackageName, Set ModuleName)
-> RIO EnvConfig (Set PackageName, Set ModuleName)
forall a b. (a -> b) -> a -> b
$ ByteString -> (Set PackageName, Set ModuleName)
parseImports (ByteString -> (Set PackageName, Set ModuleName))
-> IO ByteString -> IO (Set PackageName, Set ModuleName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO ByteString
S8.readFile FilePath
scriptFP
  if Set ModuleName -> Bool
forall a. Set a -> Bool
Set.null Set ModuleName
mns
    then Set PackageName -> RIO EnvConfig (Set PackageName)
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set PackageName
pns
    else Set PackageName -> Set PackageName -> Set PackageName
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set PackageName
pns (Set PackageName -> Set PackageName)
-> RIO EnvConfig (Set PackageName)
-> RIO EnvConfig (Set PackageName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set ModuleName -> RIO EnvConfig (Set PackageName)
getPackagesFromModuleNames Set ModuleName
mns

getPackagesFromModuleNames ::
     Set ModuleName
  -> RIO EnvConfig (Set PackageName)
getPackagesFromModuleNames :: Set ModuleName -> RIO EnvConfig (Set PackageName)
getPackagesFromModuleNames Set ModuleName
mns = do
  SnapshotCacheHash
hash <- RIO EnvConfig SnapshotCacheHash
hashSnapshot
  SnapshotCacheHash
-> RIO EnvConfig (Map PackageName (Set ModuleName))
-> ((ModuleName -> RIO EnvConfig [PackageName])
    -> RIO EnvConfig (Set PackageName))
-> RIO EnvConfig (Set PackageName)
forall env a.
(HasPantryConfig env, HasLogFunc env) =>
SnapshotCacheHash
-> RIO env (Map PackageName (Set ModuleName))
-> ((ModuleName -> RIO env [PackageName]) -> RIO env a)
-> RIO env a
withSnapshotCache SnapshotCacheHash
hash RIO EnvConfig (Map PackageName (Set ModuleName))
mapSnapshotPackageModules (((ModuleName -> RIO EnvConfig [PackageName])
  -> RIO EnvConfig (Set PackageName))
 -> RIO EnvConfig (Set PackageName))
-> ((ModuleName -> RIO EnvConfig [PackageName])
    -> RIO EnvConfig (Set PackageName))
-> RIO EnvConfig (Set PackageName)
forall a b. (a -> b) -> a -> b
$ \ModuleName -> RIO EnvConfig [PackageName]
getModulePackages -> do
    [Set PackageName]
pns <- [ModuleName]
-> (ModuleName -> RIO EnvConfig (Set PackageName))
-> RIO EnvConfig [Set PackageName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (Set ModuleName -> [ModuleName]
forall a. Set a -> [a]
Set.toList Set ModuleName
mns) ((ModuleName -> RIO EnvConfig (Set PackageName))
 -> RIO EnvConfig [Set PackageName])
-> (ModuleName -> RIO EnvConfig (Set PackageName))
-> RIO EnvConfig [Set PackageName]
forall a b. (a -> b) -> a -> b
$ \ModuleName
mn -> do
      [PackageName]
pkgs <- ModuleName -> RIO EnvConfig [PackageName]
getModulePackages ModuleName
mn
      case [PackageName]
pkgs of
        [] -> Set PackageName -> RIO EnvConfig (Set PackageName)
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set PackageName
forall a. Set a
Set.empty
        [PackageName
pn] -> Set PackageName -> RIO EnvConfig (Set PackageName)
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set PackageName -> RIO EnvConfig (Set PackageName))
-> Set PackageName -> RIO EnvConfig (Set PackageName)
forall a b. (a -> b) -> a -> b
$ PackageName -> Set PackageName
forall a. a -> Set a
Set.singleton PackageName
pn
        [PackageName]
_ -> ScriptException -> RIO EnvConfig (Set PackageName)
forall e a. (HasCallStack, Exception e) => e -> RIO EnvConfig a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM (ScriptException -> RIO EnvConfig (Set PackageName))
-> ScriptException -> RIO EnvConfig (Set PackageName)
forall a b. (a -> b) -> a -> b
$ ModuleName -> [PackageName] -> ScriptException
AmbiguousModuleName ModuleName
mn [PackageName]
pkgs
    Set PackageName -> RIO EnvConfig (Set PackageName)
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set PackageName -> RIO EnvConfig (Set PackageName))
-> Set PackageName -> RIO EnvConfig (Set PackageName)
forall a b. (a -> b) -> a -> b
$ [Set PackageName] -> Set PackageName
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions [Set PackageName]
pns Set PackageName -> Set PackageName -> Set PackageName
forall a. Ord a => Set a -> Set a -> Set a
`Set.difference` Set PackageName
blacklist

hashSnapshot :: RIO EnvConfig SnapshotCacheHash
hashSnapshot :: RIO EnvConfig SnapshotCacheHash
hashSnapshot = do
  SourceMap
sourceMap <- Getting SourceMap EnvConfig SourceMap -> RIO EnvConfig SourceMap
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting SourceMap EnvConfig SourceMap -> RIO EnvConfig SourceMap)
-> Getting SourceMap EnvConfig SourceMap -> RIO EnvConfig SourceMap
forall a b. (a -> b) -> a -> b
$ (EnvConfig -> Const SourceMap EnvConfig)
-> EnvConfig -> Const SourceMap EnvConfig
forall env. HasEnvConfig env => Lens' env EnvConfig
Lens' EnvConfig EnvConfig
envConfigL ((EnvConfig -> Const SourceMap EnvConfig)
 -> EnvConfig -> Const SourceMap EnvConfig)
-> Getting SourceMap EnvConfig SourceMap
-> Getting SourceMap EnvConfig SourceMap
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EnvConfig -> SourceMap) -> SimpleGetter EnvConfig SourceMap
forall s a. (s -> a) -> SimpleGetter s a
to (.sourceMap)
  Builder
compilerInfo <- RIO EnvConfig Builder
forall env. (HasConfig env, HasCompiler env) => RIO env Builder
getCompilerInfo
  let eitherPliHash :: (a, r) -> Either a Builder
eitherPliHash (a
pn, r
dep)
        | PLImmutable PackageLocationImmutable
pli <- r
dep.location = Builder -> Either a Builder
forall a b. b -> Either a b
Right (Builder -> Either a Builder) -> Builder -> Either a Builder
forall a b. (a -> b) -> a -> b
$ PackageLocationImmutable -> Builder
immutableLocSha PackageLocationImmutable
pli
        | Bool
otherwise = a -> Either a Builder
forall a b. a -> Either a b
Left a
pn
      deps :: [(PackageName, DepPackage)]
deps = Map PackageName DepPackage -> [(PackageName, DepPackage)]
forall k a. Map k a -> [(k, a)]
Map.toList SourceMap
sourceMap.deps
  case [Either PackageName Builder] -> ([PackageName], [Builder])
forall a b. [Either a b] -> ([a], [b])
partitionEithers (((PackageName, DepPackage) -> Either PackageName Builder)
-> [(PackageName, DepPackage)] -> [Either PackageName Builder]
forall a b. (a -> b) -> [a] -> [b]
map (PackageName, DepPackage) -> Either PackageName Builder
forall {r} {a}.
HasField "location" r PackageLocation =>
(a, r) -> Either a Builder
eitherPliHash [(PackageName, DepPackage)]
deps) of
    ([], [Builder]
pliHashes) -> do
      let hashedContent :: Builder
hashedContent = [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ Builder
compilerInfo Builder -> [Builder] -> [Builder]
forall a. a -> [a] -> [a]
: [Builder]
pliHashes
      SnapshotCacheHash -> RIO EnvConfig SnapshotCacheHash
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (SnapshotCacheHash -> RIO EnvConfig SnapshotCacheHash)
-> SnapshotCacheHash -> RIO EnvConfig SnapshotCacheHash
forall a b. (a -> b) -> a -> b
$ SHA256 -> SnapshotCacheHash
SnapshotCacheHash (ByteString -> SHA256
SHA256.hashLazyBytes
        (ByteString -> SHA256) -> ByteString -> SHA256
forall a b. (a -> b) -> a -> b
$ Builder -> ByteString
toLazyByteString Builder
hashedContent)
    ([PackageName]
mutables, [Builder]
_) -> ScriptException -> RIO EnvConfig SnapshotCacheHash
forall e a. (HasCallStack, Exception e) => e -> RIO EnvConfig a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM (ScriptException -> RIO EnvConfig SnapshotCacheHash)
-> ScriptException -> RIO EnvConfig SnapshotCacheHash
forall a b. (a -> b) -> a -> b
$ [PackageName] -> ScriptException
MutableDependenciesForScript [PackageName]
mutables

mapSnapshotPackageModules :: RIO EnvConfig (Map PackageName (Set ModuleName))
mapSnapshotPackageModules :: RIO EnvConfig (Map PackageName (Set ModuleName))
mapSnapshotPackageModules = do
  SourceMap
sourceMap <- Getting SourceMap EnvConfig SourceMap -> RIO EnvConfig SourceMap
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting SourceMap EnvConfig SourceMap -> RIO EnvConfig SourceMap)
-> Getting SourceMap EnvConfig SourceMap -> RIO EnvConfig SourceMap
forall a b. (a -> b) -> a -> b
$ (EnvConfig -> Const SourceMap EnvConfig)
-> EnvConfig -> Const SourceMap EnvConfig
forall env. HasEnvConfig env => Lens' env EnvConfig
Lens' EnvConfig EnvConfig
envConfigL ((EnvConfig -> Const SourceMap EnvConfig)
 -> EnvConfig -> Const SourceMap EnvConfig)
-> Getting SourceMap EnvConfig SourceMap
-> Getting SourceMap EnvConfig SourceMap
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EnvConfig -> SourceMap) -> SimpleGetter EnvConfig SourceMap
forall s a. (s -> a) -> SimpleGetter s a
to (.sourceMap)
  InstallMap
installMap <- SourceMap -> RIO EnvConfig InstallMap
forall (m :: * -> *). MonadIO m => SourceMap -> m InstallMap
toInstallMap SourceMap
sourceMap
  (InstalledMap
_installedMap, [DumpPackage]
globalDumpPkgs, [DumpPackage]
snapshotDumpPkgs, [DumpPackage]
_localDumpPkgs) <-
    InstallMap
-> RIO
     EnvConfig
     (InstalledMap, [DumpPackage], [DumpPackage], [DumpPackage])
forall env.
HasEnvConfig env =>
InstallMap
-> RIO
     env (InstalledMap, [DumpPackage], [DumpPackage], [DumpPackage])
getInstalled InstallMap
installMap
  let globals :: Map PackageName (Set ModuleName)
globals = Map PackageName GlobalPackage
-> [DumpPackage] -> Map PackageName (Set ModuleName)
forall a.
Map PackageName a
-> [DumpPackage] -> Map PackageName (Set ModuleName)
dumpedPackageModules SourceMap
sourceMap.globalPkgs [DumpPackage]
globalDumpPkgs
      notHidden :: Map k DepPackage -> Map k DepPackage
notHidden = (DepPackage -> Bool) -> Map k DepPackage -> Map k DepPackage
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (DepPackage -> Bool) -> DepPackage -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (.hidden))
      notHiddenDeps :: Map PackageName DepPackage
notHiddenDeps = Map PackageName DepPackage -> Map PackageName DepPackage
forall {k}. Map k DepPackage -> Map k DepPackage
notHidden SourceMap
sourceMap.deps
      installedDeps :: Map PackageName (Set ModuleName)
installedDeps = Map PackageName DepPackage
-> [DumpPackage] -> Map PackageName (Set ModuleName)
forall a.
Map PackageName a
-> [DumpPackage] -> Map PackageName (Set ModuleName)
dumpedPackageModules Map PackageName DepPackage
notHiddenDeps [DumpPackage]
snapshotDumpPkgs
      dumpPkgs :: Set PackageName
dumpPkgs =
        [PackageName] -> Set PackageName
forall a. Ord a => [a] -> Set a
Set.fromList ([PackageName] -> Set PackageName)
-> [PackageName] -> Set PackageName
forall a b. (a -> b) -> a -> b
$ (DumpPackage -> PackageName) -> [DumpPackage] -> [PackageName]
forall a b. (a -> b) -> [a] -> [b]
map (PackageIdentifier -> PackageName
pkgName (PackageIdentifier -> PackageName)
-> (DumpPackage -> PackageIdentifier) -> DumpPackage -> PackageName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (.packageIdent)) [DumpPackage]
snapshotDumpPkgs
      notInstalledDeps :: Map PackageName DepPackage
notInstalledDeps = Map PackageName DepPackage
-> Set PackageName -> Map PackageName DepPackage
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.withoutKeys Map PackageName DepPackage
notHiddenDeps Set PackageName
dumpPkgs
  Map PackageName (Set ModuleName)
otherDeps <- Map PackageName DepPackage
-> (DepPackage -> RIO EnvConfig (Set ModuleName))
-> RIO EnvConfig (Map PackageName (Set ModuleName))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Map PackageName DepPackage
notInstalledDeps ((DepPackage -> RIO EnvConfig (Set ModuleName))
 -> RIO EnvConfig (Map PackageName (Set ModuleName)))
-> (DepPackage -> RIO EnvConfig (Set ModuleName))
-> RIO EnvConfig (Map PackageName (Set ModuleName))
forall a b. (a -> b) -> a -> b
$ \DepPackage
dep -> do
    GenericPackageDescription
gpd <- IO GenericPackageDescription
-> RIO EnvConfig GenericPackageDescription
forall a. IO a -> RIO EnvConfig a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO DepPackage
dep.depCommon.gpd
    [ModuleName] -> Set ModuleName
forall a. Ord a => [a] -> Set a
Set.fromList ([ModuleName] -> Set ModuleName)
-> RIO EnvConfig [ModuleName] -> RIO EnvConfig (Set ModuleName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericPackageDescription -> RIO EnvConfig [ModuleName]
allExposedModules GenericPackageDescription
gpd
  -- source map construction process should guarantee unique package names in

  -- these maps

  Map PackageName (Set ModuleName)
-> RIO EnvConfig (Map PackageName (Set ModuleName))
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map PackageName (Set ModuleName)
 -> RIO EnvConfig (Map PackageName (Set ModuleName)))
-> Map PackageName (Set ModuleName)
-> RIO EnvConfig (Map PackageName (Set ModuleName))
forall a b. (a -> b) -> a -> b
$ Map PackageName (Set ModuleName)
globals Map PackageName (Set ModuleName)
-> Map PackageName (Set ModuleName)
-> Map PackageName (Set ModuleName)
forall a. Semigroup a => a -> a -> a
<> Map PackageName (Set ModuleName)
installedDeps Map PackageName (Set ModuleName)
-> Map PackageName (Set ModuleName)
-> Map PackageName (Set ModuleName)
forall a. Semigroup a => a -> a -> a
<> Map PackageName (Set ModuleName)
otherDeps

dumpedPackageModules ::
     Map PackageName a
  -> [DumpPackage]
  -> Map PackageName (Set ModuleName)
dumpedPackageModules :: forall a.
Map PackageName a
-> [DumpPackage] -> Map PackageName (Set ModuleName)
dumpedPackageModules Map PackageName a
pkgs [DumpPackage]
dumpPkgs =
  let pnames :: Set PackageName
pnames = Map PackageName a -> Set PackageName
forall k a. Map k a -> Set k
Map.keysSet Map PackageName a
pkgs Set PackageName -> Set PackageName -> Set PackageName
forall a. Ord a => Set a -> Set a -> Set a
`Set.difference` Set PackageName
blacklist
  in  [(PackageName, Set ModuleName)] -> Map PackageName (Set ModuleName)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
        [ (PackageName
pn, DumpPackage
dp.exposedModules)
        | DumpPackage
dp <- [DumpPackage]
dumpPkgs
        , let PackageIdentifier PackageName
pn Version
_ = DumpPackage
dp.packageIdent
        , PackageName
pn PackageName -> Set PackageName -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set PackageName
pnames
        ]

allExposedModules :: PD.GenericPackageDescription -> RIO EnvConfig [ModuleName]
allExposedModules :: GenericPackageDescription -> RIO EnvConfig [ModuleName]
allExposedModules GenericPackageDescription
gpd = do
  Platform Arch
curArch OS
curOs <- Getting Platform EnvConfig Platform -> RIO EnvConfig Platform
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Platform EnvConfig Platform
forall env. HasPlatform env => Lens' env Platform
Lens' EnvConfig Platform
platformL
  ActualCompiler
curCompiler <- Getting ActualCompiler EnvConfig ActualCompiler
-> RIO EnvConfig ActualCompiler
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting ActualCompiler EnvConfig ActualCompiler
forall env. HasSourceMap env => SimpleGetter env ActualCompiler
SimpleGetter EnvConfig ActualCompiler
actualCompilerVersionL
  let checkCond :: ConfVar -> Either ConfVar Bool
checkCond (PD.OS OS
os) = Bool -> Either ConfVar Bool
forall a. a -> Either ConfVar a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Either ConfVar Bool) -> Bool -> Either ConfVar Bool
forall a b. (a -> b) -> a -> b
$ OS
os OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
curOs
      checkCond (PD.Arch Arch
arch) = Bool -> Either ConfVar Bool
forall a. a -> Either ConfVar a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Either ConfVar Bool) -> Bool -> Either ConfVar Bool
forall a b. (a -> b) -> a -> b
$ Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== Arch
curArch
      checkCond (PD.Impl CompilerFlavor
compiler VersionRange
range) = case ActualCompiler
curCompiler of
        ACGhc Version
version ->
          Bool -> Either ConfVar Bool
forall a. a -> Either ConfVar a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Either ConfVar Bool) -> Bool -> Either ConfVar Bool
forall a b. (a -> b) -> a -> b
$ CompilerFlavor
compiler CompilerFlavor -> CompilerFlavor -> Bool
forall a. Eq a => a -> a -> Bool
== CompilerFlavor
GHC Bool -> Bool -> Bool
&& Version
version Version -> VersionRange -> Bool
`withinRange` VersionRange
range
        ACGhcGit {} ->
          Bool -> Either ConfVar Bool
forall a. a -> Either ConfVar a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Either ConfVar Bool) -> Bool -> Either ConfVar Bool
forall a b. (a -> b) -> a -> b
$ CompilerFlavor
compiler CompilerFlavor -> CompilerFlavor -> Bool
forall a. Eq a => a -> a -> Bool
== CompilerFlavor
GHC
      -- currently we don't do flag checking here

      checkCond ConfVar
other = ConfVar -> Either ConfVar Bool
forall a b. a -> Either a b
Left ConfVar
other
      mlibrary :: Maybe Library
mlibrary = ([Dependency], Library) -> Library
forall a b. (a, b) -> b
snd (([Dependency], Library) -> Library)
-> (CondTree ConfVar [Dependency] Library
    -> ([Dependency], Library))
-> CondTree ConfVar [Dependency] Library
-> Library
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ConfVar -> Either ConfVar Bool)
-> CondTree ConfVar [Dependency] Library -> ([Dependency], Library)
forall a d v.
(Semigroup a, Semigroup d) =>
(v -> Either v Bool) -> CondTree v d a -> (d, a)
C.simplifyCondTree ConfVar -> Either ConfVar Bool
checkCond (CondTree ConfVar [Dependency] Library -> Library)
-> Maybe (CondTree ConfVar [Dependency] Library) -> Maybe Library
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericPackageDescription
-> Maybe (CondTree ConfVar [Dependency] Library)
PD.condLibrary GenericPackageDescription
gpd
  [ModuleName] -> RIO EnvConfig [ModuleName]
forall a. a -> RIO EnvConfig a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ModuleName] -> RIO EnvConfig [ModuleName])
-> [ModuleName] -> RIO EnvConfig [ModuleName]
forall a b. (a -> b) -> a -> b
$ case Maybe Library
mlibrary  of
    Just Library
lib -> Library -> [ModuleName]
PD.exposedModules Library
lib [ModuleName] -> [ModuleName] -> [ModuleName]
forall a. [a] -> [a] -> [a]
++
                (ModuleReexport -> ModuleName) -> [ModuleReexport] -> [ModuleName]
forall a b. (a -> b) -> [a] -> [b]
map ModuleReexport -> ModuleName
moduleReexportName (Library -> [ModuleReexport]
PD.reexportedModules Library
lib)
    Maybe Library
Nothing  -> [ModuleName]
forall a. Monoid a => a
mempty

-- | The Stackage project introduced the concept of hidden packages, to deal

-- with conflicting module names. However, this is a relatively recent addition

-- (at time of writing). See:

-- http://www.snoyman.com/blog/2017/01/conflicting-module-names. To kick this

-- thing off a bit better, we're included a blacklist of packages that should

-- never be auto-parsed in.

blacklist :: Set PackageName
blacklist :: Set PackageName
blacklist = [PackageName] -> Set PackageName
forall a. Ord a => [a] -> Set a
Set.fromList
  [ FilePath -> PackageName
mkPackageName FilePath
"Glob"
  , FilePath -> PackageName
mkPackageName FilePath
"HTF"
  , FilePath -> PackageName
mkPackageName FilePath
"async-dejafu"
  , FilePath -> PackageName
mkPackageName FilePath
"binary-ieee754"
  , FilePath -> PackageName
mkPackageName FilePath
"cipher-aes"
  , FilePath -> PackageName
mkPackageName FilePath
"cipher-blowfish"
  , FilePath -> PackageName
mkPackageName FilePath
"cipher-camellia"
  , FilePath -> PackageName
mkPackageName FilePath
"cipher-des"
  , FilePath -> PackageName
mkPackageName FilePath
"cipher-rc4"
  , FilePath -> PackageName
mkPackageName FilePath
"control-monad-free"
  , FilePath -> PackageName
mkPackageName FilePath
"courier"
  , FilePath -> PackageName
mkPackageName FilePath
"crypto-api"
  , FilePath -> PackageName
mkPackageName FilePath
"crypto-cipher-types"
  , FilePath -> PackageName
mkPackageName FilePath
"crypto-numbers"
  , FilePath -> PackageName
mkPackageName FilePath
"crypto-pubkey"
  , FilePath -> PackageName
mkPackageName FilePath
"crypto-random"
  , FilePath -> PackageName
mkPackageName FilePath
"cryptohash"
  , FilePath -> PackageName
mkPackageName FilePath
"cryptohash-conduit"
  , FilePath -> PackageName
mkPackageName FilePath
"cryptohash-md5"
  , FilePath -> PackageName
mkPackageName FilePath
"cryptohash-sha1"
  , FilePath -> PackageName
mkPackageName FilePath
"cryptohash-sha256"
  , FilePath -> PackageName
mkPackageName FilePath
"fay-base"
  , FilePath -> PackageName
mkPackageName FilePath
"gl"
  , FilePath -> PackageName
mkPackageName FilePath
"gtk3"
  , FilePath -> PackageName
mkPackageName FilePath
"hashmap"
  , FilePath -> PackageName
mkPackageName FilePath
"hledger-web"
  , FilePath -> PackageName
mkPackageName FilePath
"hxt-unicode"
  , FilePath -> PackageName
mkPackageName FilePath
"kawhi"
  , FilePath -> PackageName
mkPackageName FilePath
"language-c"
  , FilePath -> PackageName
mkPackageName FilePath
"log"
  , FilePath -> PackageName
mkPackageName FilePath
"monad-extras"
  , FilePath -> PackageName
mkPackageName FilePath
"monads-tf"
  , FilePath -> PackageName
mkPackageName FilePath
"nanospec"
  , FilePath -> PackageName
mkPackageName FilePath
"newtype-generics"
  , FilePath -> PackageName
mkPackageName FilePath
"objective"
  , FilePath -> PackageName
mkPackageName FilePath
"plot-gtk3"
  , FilePath -> PackageName
mkPackageName FilePath
"prompt"
  , FilePath -> PackageName
mkPackageName FilePath
"regex-compat-tdfa"
  , FilePath -> PackageName
mkPackageName FilePath
"regex-pcre-builtin"
  , FilePath -> PackageName
mkPackageName FilePath
"rerebase"
  , FilePath -> PackageName
mkPackageName FilePath
"svg-tree"
  , FilePath -> PackageName
mkPackageName FilePath
"zip"
  ]

parseImports :: ByteString -> (Set PackageName, Set ModuleName)
parseImports :: ByteString -> (Set PackageName, Set ModuleName)
parseImports =
  [(Set PackageName, Set ModuleName)]
-> (Set PackageName, Set ModuleName)
forall a. Monoid a => [a] -> a
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold ([(Set PackageName, Set ModuleName)]
 -> (Set PackageName, Set ModuleName))
-> (ByteString -> [(Set PackageName, Set ModuleName)])
-> ByteString
-> (Set PackageName, Set ModuleName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Maybe (Set PackageName, Set ModuleName))
-> [ByteString] -> [(Set PackageName, Set ModuleName)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (ByteString -> Maybe (Set PackageName, Set ModuleName)
forall {a}.
IsString a =>
ByteString -> Maybe (Set PackageName, Set a)
parseLine (ByteString -> Maybe (Set PackageName, Set ModuleName))
-> (ByteString -> ByteString)
-> ByteString
-> Maybe (Set PackageName, Set ModuleName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
stripCR') ([ByteString] -> [(Set PackageName, Set ModuleName)])
-> (ByteString -> [ByteString])
-> ByteString
-> [(Set PackageName, Set ModuleName)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
S8.lines
 where
  -- Remove any carriage pure character present at the end, to support

  -- Windows-style line endings (CRLF)

  stripCR' :: ByteString -> ByteString
stripCR' ByteString
bs
    | ByteString -> Bool
S8.null ByteString
bs = ByteString
bs
    | ByteString -> Char
S8.last ByteString
bs Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\r' = HasCallStack => ByteString -> ByteString
ByteString -> ByteString
S8.init ByteString
bs
    | Bool
otherwise = ByteString
bs

  stripPrefix :: ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
x ByteString
y
    | ByteString
x ByteString -> ByteString -> Bool
`S8.isPrefixOf` ByteString
y = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
S8.drop (ByteString -> Int
S8.length ByteString
x) ByteString
y
    | Bool
otherwise = Maybe ByteString
forall a. Maybe a
Nothing

  parseLine :: ByteString -> Maybe (Set PackageName, Set a)
parseLine ByteString
bs0 = do
    ByteString
bs1 <- ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
"import " ByteString
bs0
    let bs2 :: ByteString
bs2 = (Char -> Bool) -> ByteString -> ByteString
S8.dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') ByteString
bs1
        bs3 :: ByteString
bs3 = ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
bs2 (Maybe ByteString -> ByteString) -> Maybe ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
"qualified " ByteString
bs2
    case ByteString -> ByteString -> Maybe ByteString
stripPrefix ByteString
"\"" ByteString
bs3 of
      Just ByteString
bs4 -> do
        PackageName
pn <- FilePath -> Maybe PackageName
forall (m :: * -> *). MonadThrow m => FilePath -> m PackageName
parsePackageNameThrowing (FilePath -> Maybe PackageName) -> FilePath -> Maybe PackageName
forall a b. (a -> b) -> a -> b
$ ByteString -> FilePath
S8.unpack (ByteString -> FilePath) -> ByteString -> FilePath
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> ByteString -> ByteString
S8.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'"') ByteString
bs4
        (Set PackageName, Set a) -> Maybe (Set PackageName, Set a)
forall a. a -> Maybe a
Just (PackageName -> Set PackageName
forall a. a -> Set a
Set.singleton PackageName
pn, Set a
forall a. Set a
Set.empty)
      Maybe ByteString
Nothing -> (Set PackageName, Set a) -> Maybe (Set PackageName, Set a)
forall a. a -> Maybe a
Just
        ( Set PackageName
forall a. Set a
Set.empty
        , a -> Set a
forall a. a -> Set a
Set.singleton
            (a -> Set a) -> a -> Set a
forall a b. (a -> b) -> a -> b
$ FilePath -> a
forall a. IsString a => FilePath -> a
fromString
            (FilePath -> a) -> FilePath -> a
forall a b. (a -> b) -> a -> b
$ Text -> FilePath
T.unpack
            (Text -> FilePath) -> Text -> FilePath
forall a b. (a -> b) -> a -> b
$ OnDecodeError -> ByteString -> Text
decodeUtf8With OnDecodeError
lenientDecode
            (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> ByteString -> ByteString
S8.takeWhile (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
' ' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'(') ByteString
bs3
        )