From 7e0ad38f26c7a508ef37a829b6a3ed8aced6c2ac Mon Sep 17 00:00:00 2001 From: someodd Date: Mon, 7 Oct 2024 18:23:34 -0700 Subject: [PATCH] fix directory/unparseable file issue --- CHANGELOG.md | 6 +++++ src/Bore/FileLayout.hs | 61 +++++++++++++++++++----------------------- src/Bore/Parse.hs | 12 +++++---- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 991ba7a..ab1f9df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## [Unreleased] +### Fixed + +* Fix creating directories that have the name of the file name it's copying for every file + to simply be copied, not parsed (and fix up the directory creation logic in general for + some related things) + ## [0.5.0.0] - 2024-10-07 ### Fixed diff --git a/src/Bore/FileLayout.hs b/src/Bore/FileLayout.hs index 3b6daa7..d3ada4d 100644 --- a/src/Bore/FileLayout.hs +++ b/src/Bore/FileLayout.hs @@ -178,22 +178,7 @@ takeFullExtension path = let fileName = takeFileName path in if '.' `elem` fileName then dropWhile (/= '.') fileName else "" -{- | Create a destination directory. - -@outputDirectory@ is the full absolute path to the output directory. - --} -createDestinationDirectory - :: FilePath - -> FilePath - -> FilePath - -> IO FilePath -createDestinationDirectory projectDirectory outputDirectory sourceFilePath = do - - let fullDestination = outputDirectory makeRelative projectDirectory (projectDirectory sourceFilePath) - createDirectoryIfMissing True fullDestination - pure fullDestination - +-- FIXME: is this just crud? {- | Standaredized writing-to-destination. Ensures directories created. @@ -203,29 +188,37 @@ Ensures directories created. Not just for parseable files. -} -writeDest :: FilePath -> FilePath -> FilePath -> Text.Text -> IO (RelativePath, AbsolutePath) -writeDest projectDirectory sourceFilePath destinationDirectory fileContents = do - let - relativePath = makeRelative projectDirectory sourceFilePath - fullDestination = destinationDirectory relativePath - fullDestinationDir = takeDirectory fullDestination - - -- Create the directory if it does not exist - createDirectoryIfMissing True fullDestinationDir +writeDest :: FilePath -> AbsolutePath -> FilePath -> Text.Text -> IO (RelativePath, AbsolutePath) +writeDest sourceDirectory fullSourceFilePath outputDirectory fileContents = do + (fullFileDestination, relativeFilePath) <- createDestinationDirectoryForFile outputDirectory sourceDirectory fullSourceFilePath + TextIO.writeFile fullFileDestination fileContents + pure (relativeFilePath, fullFileDestination) - TextIO.writeFile fullDestination fileContents - pure (relativePath, fullDestination) +createDestinationDirectoryForFile :: FilePath -> FilePath -> AbsolutePath -> IO (AbsolutePath, FilePath) +createDestinationDirectoryForFile outputDirectory sourceDirectory fullSourceFilePath = do + let + -- Make the relative path to the source file, so it works for both output and source directories + relativeFilePath = makeRelative sourceDirectory fullSourceFilePath + -- now make the relative directory path based on above + relativeDirectoryPath = takeDirectory relativeFilePath + -- now we can figure the full destination/output directory to copy the file to + fullDirectoryDestination = outputDirectory relativeDirectoryPath + -- finally we also want to know the full destination path for the file itself + fullFileDestination = fullDirectoryDestination takeFileName fullSourceFilePath + + _ <- createDirectoryIfMissing True fullDirectoryDestination + pure (fullFileDestination, relativeFilePath) {- | Simply copy a file according to project layout rules. +@@fullSourceFilePath@@ is the full absolute path to the source file to be copied. + -} -onlyCopyFile :: FilePath -> FilePath -> FilePath -> IO (FilePath, RelativePath) -onlyCopyFile projectDirectory destination filePath = do - fullTargetDirectory <- createDestinationDirectory projectDirectory destination filePath - let fullTargetPath = fullTargetDirectory takeFileName filePath - copyFile filePath fullTargetPath - let relativePath = makeRelative fullTargetDirectory fullTargetPath - pure (fullTargetPath, relativePath) +onlyCopyFile :: FilePath -> FilePath -> AbsolutePath -> IO (FilePath, RelativePath) +onlyCopyFile sourceDirectory outputDirectory fullSourceFilePath = do + (fullFileDestination, relativeFilePath) <- createDestinationDirectoryForFile outputDirectory sourceDirectory fullSourceFilePath + copyFile fullSourceFilePath fullFileDestination + pure (fullFileDestination, relativeFilePath) {- | Clear out the output directory, but leave the assets directory alone. diff --git a/src/Bore/Parse.hs b/src/Bore/Parse.hs index 1ace3b0..f9e4778 100644 --- a/src/Bore/Parse.hs +++ b/src/Bore/Parse.hs @@ -110,12 +110,12 @@ If it's a directory, the return is nothing. Changes the output destination if the file is a `parseableGophermapFileName`. -@@destination@@ is the full absolute path to the output directory. +@@outputDirectory@@ is the full absolute path to the output/destination directory. @@filePath@@ absolute path to the file being handled. Should be absolute path to the source file. -} handleFile :: Library -> FilePath -> FilePath -> FilePath -> IO (Maybe (FilePath, FilePath, Maybe FrontMatter)) -handleFile library projectDirectory destination filePath = do +handleFile library sourceDirectory outputDirectory filePath = do -- It's important to check if it's a directory first, because we don't want to match -- directories as parseable files just because they have a file extension we are -- looking for. @@ -123,14 +123,16 @@ handleFile library projectDirectory destination filePath = do if isDir then do putStrLn $ "creating directory: " ++ filePath - createDestinationDirectory projectDirectory destination filePath >> pure Nothing + _ <- createDestinationDirectoryForFile outputDirectory sourceDirectory filePath + pure Nothing else if takeFullExtension filePath `elem` onlyParse then do putStrLn $ "parse the file: " ++ filePath - Just <$> parseFile library projectDirectory filePath destination + Just <$> parseFile library sourceDirectory filePath outputDirectory else do putStrLn $ "only copy file: " ++ filePath - (fullPath, relativePath) <- onlyCopyFile projectDirectory destination filePath + -- FIXME: this is copying files and making a directory for them that includes their name! + (fullPath, relativePath) <- onlyCopyFile sourceDirectory outputDirectory filePath pure $ Just (fullPath, relativePath, Nothing) {- | Helper function to get all the handle all files passed, returning metadata regarding