Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt-in nbOutDir and nbBaseDir #53

Merged
merged 4 commits into from
Jun 3, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/nimib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ template nbInit*() =
nbThisName {.inject, used.}: string = thisTuple.name
nbThisExt {.inject, used.}: string = thisTuple.ext
nbInitDir {.inject, used.} = getCurrentDir().AbsoluteDir # current directory at initialization
var
nbUser {.inject.}: string = getUser()
nbHomeDir {.inject.}: AbsoluteDir = findNimbleDir(nbThisDir)
if dirExists(nbHomeDir / "docs".RelativeDir):
nbHomeDir = nbHomeDir / "docs".RelativeDir
var nbUser {.inject.}: string = getUser()

const nimibOutDir {.strdefine, inject.} = "" # must inject otherwise it is always its default ""
const nimibSrcDir {.strdefine, inject} = ""
when defined(nimibSrcDir):
let nimibSrcDirAbs = nimibSrcDir.toAbsoluteDir
when defined(nimibOutDir):
var nbHomeDir {.inject.}: AbsoluteDir = nimibOutDir.toAbsoluteDir
else:
var nbHomeDir {.inject.}: AbsoluteDir = findNimbleDir(nbThisDir)
if dirExists(nbHomeDir / "docs".RelativeDir):
nbHomeDir = nbHomeDir / "docs".RelativeDir
setCurrentDir nbHomeDir

# could change to nb.rel with nb global object
Expand Down Expand Up @@ -74,6 +81,8 @@ template nbInit*() =
# - in case you need to manage additional exceptions for a specific document add a new set of partials before calling nbSave
nbDoc.context.searchDirs(nbDoc.templateDirs)
nbDoc.context.searchTable(nbDoc.partials)
when defined(nimibSrcDir):
nbDoc.filename = (nbDoc.filename.toAbsoluteDir.relativeTo nimibSrcDirAbs).string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this says toAbsoluteDir but it should actually be a toAbsoluteFile. Should we use toAbsolute with nbHomeDir as base?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, good catch should indeed be File and not Dir 😄
Not really sure exactly how you intend to use toAbsolute here. Add it to the existing code or replace the existing code with it? It has a check that if the file-path is already absolute it will ignore the the base.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure either how to use it but there is not a toAbsoluteFile available right?

Copy link
Collaborator Author

@HugoGranstrom HugoGranstrom Jun 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, just assumed it did exist... Yes then we have to use it or AbsoluteFile(file) which it uses internally. That is if we know that nbDoc.filename is a absolute path to begin with...

If nbDoc.filename is absolute (currently the case) it doesn't matter what base is because it won't be used. If it on the other hand is a relative file then we'd want it to return its actual path in the nimibSrcDir to be able to give the correct relative path later for saving the file in nbHomeDir. This is why we need nimibSrcDir to begin with. So it depends on what the relative path is relative to I guess. But I don't think using nbHomeDir as base will work in any of the cases.

I think what would work best as base in this case is nimibSrcDir as one will be able to use it in the future to redefine relPath to use it instead of nbHomeDir. This was the way we did it in the early versions of getting-started as well, setting paths relative to nbSrcDir: https://github.com/SciNim/getting-started/blob/a15d840fda541730fe9c513cbefeb1ca04fedce3/nbPostInit.nim#L2

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok think I've got this sorted now. We split it into two scenarios:

  • nbDoc.filename.isAbsolute: We calculate the relative path from nimibSrcDir
  • isRelative: we do nothing, we trust the user has already set it to the appropriate relative path.

How does this sound?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good! 🚀

withDir(nbHomeDir):
write nbDoc

Expand Down