Add invertRelDir to StrongPath utilities#3923
Conversation
@wasp.sh/wasp-cli
@wasp.sh/wasp-cli-darwin-arm64-unknown
@wasp.sh/wasp-cli-darwin-x64-unknown
@wasp.sh/wasp-cli-linux-x64-glibc
@wasp.sh/wasp-cli-linux-x64-musl
commit: |
| -- If any of those change significantly (their depth), this path should be adjusted. | ||
| waspProjectDirFromProjectRootDir :: Path' (Rel G.Common.ProjectRootDir) (Dir WaspProjectDir) | ||
| waspProjectDirFromProjectRootDir = [reldir|../../|] | ||
| waspProjectDirFromAppComponentDir = [reldir|../|] </> waspProjectDirFromGeneratedCodeDir |
There was a problem hiding this comment.
We don't have anything to invert for AppComponentRootDir since it is "abstract" path.
| serverRootDirFromDbRootDir :: Path' (Rel DbRootDir) (Dir ServerRootDir) | ||
| serverRootDirFromDbRootDir = [reldir|../server|] | ||
|
|
||
| webAppRootDirFromDbRootDir :: Path' (Rel DbRootDir) (Dir ServerRootDir) | ||
| webAppRootDirFromDbRootDir = [reldir|../web-app|] |
| libsSrcDirPathInDataDir = [reldir|Generator/libs|] | ||
|
|
||
| libsRootDirFromSdkDir :: Path' Rel' (Dir LibsRootDir) | ||
| libsRootDirFromSdkDir = [reldir|../../|] </> libsRootDirInGeneratedCodeDir |
There was a problem hiding this comment.
Moved to respective SDK and Server common files.
cprecioso
left a comment
There was a problem hiding this comment.
Lgtm, couple of minor suggestions.
| -- >>> invertRelDir [reldir|.|] -- "./" | ||
| -- >>> invertRelDir [reldir|types|] -- "../" | ||
| -- >>> invertRelDir [reldir|.wasp/out|] -- "../../" | ||
| invertRelDir :: SP.Path' (SP.Rel a) (SP.Dir b) -> SP.Path' (SP.Rel b) (SP.Dir a) |
There was a problem hiding this comment.
Should we have a version of this function that returns an Either String Path or Maybe Path in case we want to handle errors? And then a wrapper that errors out on Left so we keep the simple API for internal stuff.
There was a problem hiding this comment.
I would keep it simple for now, since we only use this helper for strong-path paths we have defined, and we know are valid.
waspc/src/Wasp/Util/StrongPath.hs
Outdated
| | ".." `elem` pathSegments = error $ "invertRelDir: path contains '..' segment: " ++ SP.fromRelDir relDir | ||
| | otherwise = fromJust . SP.parseRelDir $ FP.joinPath $ replicate (length pathSegments) ".." | ||
| where | ||
| pathSegments = filter (/= ".") $ FP.splitDirectories $ SP.fromRelDir relDir |
There was a problem hiding this comment.
instead of filter (/= ".") is there a normalize path fn or something?
There was a problem hiding this comment.
Good idea:
invertRelDir :: SP.Path' (SP.Rel a) (SP.Dir b) -> SP.Path' (SP.Rel b) (SP.Dir a)
invertRelDir relDir
| ".." `elem` pathSegments = error $ "invertRelDir: path contains '..' segment: " ++ SP.fromRelDir relDir
| otherwise = case pathSegments of
["."] -> [SP.reldir|.|]
_ -> fromJust . SP.parseRelDir $ FP.joinPath $ replicate (length pathSegments) ".."
where
pathSegments = FP.splitDirectories . FP.normalise $ SP.fromRelDir relDir| evaluate (invertRelDir [reldir|../../a|]) `shouldThrow` anyErrorCall | ||
|
|
||
| it "returns current dir for current dir input" $ do | ||
| invertRelDir [reldir|.|] `shouldBe` [reldir|./|] |
There was a problem hiding this comment.
We use strong-path on both sides so that tests work on any OS.
Mostly because both work on Path System.
Summary
Main change:
invertRelDirtoWasp.Util.StrongPath. Given a relative directory path fromatob, returns the inverse path frombback toa. The limitation is that the reverse path can't have any../segments.Side stuff:
invertRelDirinstead of a hardcoded[reldir|../../|]variationsWaspLibs.CommontoSDK/Server.Commonto avoid cyclic dependencies.FilePath.Extrawhich just existed doing nothing.serverRootDirFromDbRootDirandwebAppRootDirFromDbRootDir. Wanted to make them useinvertRelDirbut found they were unused.