filepath
filepath copied to clipboard
Add function to get root directory
Is there really no function to get the root directory? 😳
rootDirectory :: FilePath -> FilePat
rootDirectory "/directory/other.ext" == "/"
rootDirectory "directory/other.ext" == "directory"
rootDirectory "/foo/bar/baz" == "/"
rootDirectory "foo/bar/baz" == "foo"
Found it (kind of): https://hackage.haskell.org/package/shake-0.16.1/docs/Development-Shake-FilePath.html#v:takeDirectory1 Wow, that's weird function name. Definitely not what I was expecting 🙈
What's the use case? Generally there's not much use in the "root" directory - Shake has takeDirectory1 because it assumes relative file paths and build-system style operations, which are less common in other contexts. That said, given Shake has it, if it's generally useful it could certainly be considered.
Well, thinking about it I actually just need takeDirectory1.
Not sure either if there is a use case for the function I described ...
So feel free to close this issue from my side.
So the one that works on relative paths only? It's arguable that if takeDirectory1 is useful outside build systems it should be renamed to something sensible and moved over. What was your use case? Do you think it's common enough to go in filepath itself?
Ah, I understand. Yeah I'd definitely say that takeDirectory1 could be useful in other contexts.
So I guess it should be moved over (with a better name 😛)
What might be a common use case (which I at least often do) is, to build a tool which recursively analyzes a set of directories (e.g. all modules in a mono repo) and then only executes commands in the takeDirectory1 directories. (e.g. find all changed files and git add them, or analyze some files and write the result in the directory).
@adius re that mono-repo, does this assume that all git submodules are located at the same level? If not, wouldn't you rather need a more generic way to locate the olders each submodule was "mounted" in (by analysing either .gitmodules or git submodule's output), and then operate with a kind of isPrefixOf operator to check which submodule an argument belongs to?
Sorry, I mixed things up a little. I was actually talking about 2 different things: Monorepo (of packages) and a directory of projects (git repos). And they were just 2 examples of the same pattern:
~/modules $ tr -L 1
.
├── moduleA
├── moduleB
└── moduleC
~/projects $ tr -L 1
.
├── repoA
├── repoB
└── repoC
~/photos $ tr -L 1
.
├── 2016
├── 2017
└── 2018
To handle operations in subtrees with this pattern takeDirectory1 is really helpful & important.
OK, so I'm open to adding these functions. You assumedly want:
takeFirstDirectory :: FilePath -> FilePath
dropFirstDirectory :: FilePath -> FilePath
What name should they have? What should their semantics be? What should they do on Windows? Should they subsume splitDrive?