Skip to content

Commit

Permalink
Remove redundant functions used for starting a process
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Oct 11, 2024
1 parent 7a68d1d commit f46bbd2
Showing 1 changed file with 0 additions and 116 deletions.
116 changes: 0 additions & 116 deletions cardano-testnet/src/Testnet/Process/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -236,119 +236,3 @@ resourceAndIOExceptionHandlers = [ Handler $ pure . ProcessIOException
, Handler $ pure . ResourceException
]

procFlexNew
:: MonadIO m
=> String
-- ^ Cabal package name corresponding to the executable
-> String
-- ^ Environment variable pointing to the binary to run
-> [String]
-- ^ Arguments to the CLI command
-> ExceptT ExecutableError m CreateProcess
-- ^ Captured stdout
procFlexNew = procFlexNew' H.defaultExecConfig

procFlexNew'
:: MonadIO m
=> H.ExecConfig
-> String
-- ^ Cabal package name corresponding to the executable
-> String
-- ^ Environment variable pointing to the binary to run
-> [String]
-- ^ Arguments to the CLI command
-> ExceptT ExecutableError m CreateProcess
-- ^ Captured stdout
procFlexNew' execConfig pkg binaryEnv arguments = GHC.withFrozenCallStack $ do
bin <- binFlexNew pkg binaryEnv
pure (IO.proc bin arguments)
{ IO.env = getLast $ H.execConfigEnv execConfig
, IO.cwd = getLast $ H.execConfigCwd execConfig
-- this allows sending signals to the created processes, without killing the test-suite process
, IO.create_group = True
}

-- | Compute the path to the binary given a package name or an environment variable override.
binFlexNew
:: MonadIO m
=> String
-- ^ Package name
-> String
-- ^ Environment variable pointing to the binary to run
-> ExceptT ExecutableError m FilePath
-- ^ Path to executable
binFlexNew pkg binaryEnv = do
maybeEnvBin <- liftIO $ IO.lookupEnv binaryEnv
case maybeEnvBin of
Just envBin -> return envBin
Nothing -> binDist pkg

-- | Find the nearest plan.json going upwards from the current directory.
findDefaultPlanJsonFile :: IO FilePath
findDefaultPlanJsonFile = IO.getCurrentDirectory >>= go
where go :: FilePath -> IO FilePath
go d = do
let file = d </> "dist-newstyle/cache/plan.json"
exists <- IO.doesFileExist file
if exists
then return file
else do
let parent = takeDirectory d
if parent == d
then return "dist-newstyle/cache/plan.json"
else go parent


-- | Discover the location of the plan.json file.
planJsonFile :: IO FilePath
planJsonFile = do
maybeBuildDir <- liftIO $ IO.lookupEnv "CABAL_BUILDDIR"
case maybeBuildDir of
Just buildDir -> return $ ".." </> buildDir </> "cache/plan.json"
Nothing -> findDefaultPlanJsonFile
{-# NOINLINE planJsonFile #-}

data ExecutableError
= CannotDecodePlanJSON FilePath String
| RetrievePlanJsonFailure IOException
| ReadFileFailure IOException
| ExecutableMissingInComponent FilePath String
-- ^ Component with key @component-name@ is found, but it is missing
-- the @bin-file@ key.
| ExecutableNotFoundInPlan String
-- ^ Component with key @component-name@ cannot be found
deriving Show


-- | Consult the "plan.json" generated by cabal to get the path to the executable corresponding.
-- to a haskell package. It is assumed that the project has already been configured and the
-- executable has been built.
binDist
:: MonadIO m
=> String
-- ^ Package name
-> ExceptT ExecutableError m FilePath
-- ^ Path to executable
binDist pkg = do
pJsonFp <- handleIOExceptT RetrievePlanJsonFailure planJsonFile
contents <- handleIOExceptT ReadFileFailure $ LBS.readFile pJsonFp

case Aeson.eitherDecode contents of
Right plan -> case List.filter matching (plan & installPlan) of
(component:_) -> case component & binFile of
Just bin -> return $ addExeSuffix (Text.unpack bin)
Nothing -> left $ ExecutableMissingInComponent pJsonFp $ "missing \"bin-file\" key in plan component: " <> show component
[] -> left $ ExecutableNotFoundInPlan $ "Cannot find \"component-name\" key with value \"exe:" <> pkg <> "\""
Left message -> left $ CannotDecodePlanJSON pJsonFp $ "Cannot decode plan: " <> message
where matching :: Component -> Bool
matching component = case componentName component of
Just name -> name == Text.pack ("exe:" <> pkg)
Nothing -> False

addExeSuffix :: String -> String
addExeSuffix s = if ".exe" `List.isSuffixOf` s
then s
else s <> exeSuffix

exeSuffix :: String
exeSuffix = if OS.isWin32 then ".exe" else ""

0 comments on commit f46bbd2

Please sign in to comment.