@@ -12,6 +12,7 @@ import Data.Aeson (Value (String), (.=))
12
12
import Data.Aeson qualified as Aeson
13
13
import Data.Aeson.Lens (atKey , key , _Number )
14
14
import Data.Fixed (Centi )
15
+ import Data.Text (pack )
15
16
import Data.Text qualified as Text
16
17
import Data.Time.Clock.POSIX (posixSecondsToUTCTime , utcTimeToPOSIXSeconds )
17
18
import Hydra.Cardano.Api (
@@ -30,9 +31,17 @@ import Hydra.Cluster.Fixture (KnownNetwork (..), toNetworkId)
30
31
import Hydra.Cluster.Util (readConfigFile )
31
32
import Hydra.Options (BlockfrostOptions (.. ), DirectOptions (.. ))
32
33
import Network.HTTP.Simple (getResponseBody , httpBS , parseRequestThrow )
33
- import System.Directory (createDirectoryIfMissing , doesFileExist , removeFile )
34
+ import System.Directory (
35
+ createDirectoryIfMissing ,
36
+ doesFileExist ,
37
+ getCurrentDirectory ,
38
+ removeFile ,
39
+ )
34
40
import System.Exit (ExitCode (.. ))
35
- import System.FilePath (takeDirectory , (</>) )
41
+ import System.FilePath (
42
+ takeDirectory ,
43
+ (</>) ,
44
+ )
36
45
import System.Posix (ownerReadMode , setFileMode )
37
46
import System.Process (
38
47
CreateProcess (.. ),
@@ -169,9 +178,34 @@ withBlockfrostBackend ::
169
178
withBlockfrostBackend _tracer stateDirectory action = do
170
179
args <- setupCardanoDevnet stateDirectory
171
180
shelleyGenesis <- readFileBS >=> unsafeDecodeJson $ stateDirectory </> nodeShelleyGenesisFile args
172
- let backend = BlockfrostBackend $ BlockfrostOptions {projectPath = Backend. blockfrostProjectPath}
181
+ bfProjectPath <- findFileStartingAtDirectory 3 Backend. blockfrostProjectPath
182
+ let backend = BlockfrostBackend $ BlockfrostOptions {projectPath = bfProjectPath}
173
183
action (getShelleyGenesisBlockTime shelleyGenesis) backend
174
184
185
+ -- | Find the given file in the current directory or its parents.
186
+ --
187
+ -- This function starts from the current working directory and checks if the specified file exists there.
188
+ -- If not found, it recursively checks the parent directories up to the given maximum depth.
189
+ findFileStartingAtDirectory :: Int -> FilePath -> IO FilePath
190
+ findFileStartingAtDirectory maxDepth fileName = do
191
+ cwd <- getCurrentDirectory
192
+ findInDir maxDepth cwd
193
+ where
194
+ findInDir :: Int -> FilePath -> IO FilePath
195
+ findInDir depth dir = do
196
+ let path = dir </> fileName
197
+ exists <- doesFileExist path
198
+ if exists
199
+ then pure path
200
+ else
201
+ if depth <= 0
202
+ then error $ " Could not locate the Blockfrost project file at or above: " <> pack dir
203
+ else do
204
+ let parent = " .." </> takeDirectory dir
205
+ if parent == dir
206
+ then error " Reached root directory without finding the Blockfrost project file."
207
+ else findInDir (depth - 1 ) parent
208
+
175
209
withBackend ::
176
210
forall a .
177
211
Tracer IO NodeLog ->
0 commit comments