Skip to content

Commit

Permalink
fix directory/unparseable file issue
Browse files Browse the repository at this point in the history
  • Loading branch information
someodd committed Oct 8, 2024
1 parent 16dbc2e commit 7e0ad38
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 27 additions & 34 deletions src/Bore/FileLayout.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
12 changes: 7 additions & 5 deletions src/Bore/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,29 @@ 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.
isDir <- doesDirectoryExist filePath
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
Expand Down

0 comments on commit 7e0ad38

Please sign in to comment.