dbuenzli / bos

Basic OS interaction for OCaml
http://erratique.ch/software/bos
ISC License
63 stars 16 forks source link

Feature request: XDG Directories, Known Folders, and Standard Directories #93

Open favonia opened 2 years ago

favonia commented 2 years ago

I have been looking for an OCaml library with a robust implementation that provides a uniform, cross-platform interface for XDG Directories / Known Folders (Windows) / Standard Directories (macOS), and I believe bos is one of the best places to implement these functions. More specifically, I need these features for my development:

  1. Basic path expansion for the home directory. For example, I hope there would be a function expanding both $HOME/blah and ~/blah to /home/user/blah on Linux. This is essentially #77.
  2. Reasonable default directories for (at least) data (XDG_DATA_HOME), configurations (XDG_CONFIG_HOME), and cache (XDG_CACHE_HOME) on major platforms.

I have checked other OCaml libraries and I feel a robust implementation would inevitably reimplement many functions in bos. The most non-trivial part is perhaps the OS detection (usually done by uname -s, but could also be done by the uname system call) and some other system calls to get known folders on Windows. As a bonus, I also hope the implementation has the following features, though I personally do not need them:

  1. Understand that on Plan 9, the environment variable HOME should be home and various directories are placed differently.
  2. Support other base directories, user directories, font directories, etc.
  3. Parse $XDG_CONFIG_HOME/user-dirs.dirs for user directories, if we wish to support them.
  4. Perhaps provide an option for macOS to mimic the Linux convention for easier Linux/macOS synchronization? (I'm not sure what would be the best practice, especially for command-line tools.)

One of the most robust and feature-rich libraries I found is adrg/xdg for Go. The only major missing piece in that library is the parsing of $XDG_CONFIG_HOME/user-dirs.dirs for user directories.

I am happy to make a PR containing a half-baked implementation that would address my need, leaving a truly robust implementation to others. My plan is to add OS.Path.expand_home, OS.Dir.cache, OS.Dir.data, and OS.Dir.home (as an alias of OS.Dir.user), and then make the existing OS.Dir.user a bit more cross-platform. I'd love to hear some feedback before putting my plan into action.