macos Mac OS X 上 XDG_CONFIG_HOME 和 XDG_DATA_HOME 的等价物?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3373948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:05:44  来源:igfitidea点击:

Equivalents of XDG_CONFIG_HOME and XDG_DATA_HOME on Mac OS X?

macosconfiguration-files

提问by LeafStorm

I am planning to develop a cross-platform script. On Linux and other operating systems, it will store configuration in XDG_CONFIG_HOMEand data files (specifically, downloaded plugins) in XDG_DATA_HOME. On Windows, it will use APPDATAfor both (unless someone has a better idea). However, what would be the proper thing to do on Mac OS X?

我打算开发一个跨平台的脚本。在 Linux 和其他操作系统上,它会将配置存储XDG_CONFIG_HOMEXDG_DATA_HOME. 在 Windows 上,它将APPDATA用于两者(除非有人有更好的主意)。但是,在 Mac OS X 上应该做什么?

On my first glance through a handy Macbook's ~/Librarydirectory, I saw Preferencesand Application Supportfolders. I was originally planning to use those, but Preferencesseems to just contain plists with reverse domain names like com.apple.foo.bar.baz.plist, and every folder in Application Supportcorresponds to a bundle in /Applications, so I'm not sure how well the system would react to files that don't match its standards. Storing them directly in ~/Librarymight be an option, but I don't want to pollute it with a stray myscript.conffile if there's a better place for it.

通过一个方便的MacBook的我第一眼~/Library目录,我看到了PreferencesApplication Support文件夹。我最初计划使用这些,但Preferences似乎只包含具有反向域名的 plist,例如com.apple.foo.bar.baz.plist,并且中的每个文件夹都Application Support对应于 中的一个包/Applications,因此我不确定系统对与其不匹配的文件的反应如何标准。将它们直接存储在其中~/Library可能是一种选择,但myscript.conf如果有更好的地方,我不想用杂散文件污染它。

Where should I store these files? (And please don't say just ~/.myscript. I know it's the Unix tradition, but it annoys me to see random dotfiles in the home directory.)

我应该在哪里存储这些文件?(请不要只说~/.myscript。我知道这是 Unix 的传统,但是在主目录中看到随机的点文件让我很恼火。)

采纳答案by Yuji

I would use ~/Library/Application Support/script_name/. The subdirectories inside Application Supportare used conventionally by various apps, including Apple's own softwares. But it's not enforced by the OS and not tied to apps inside /Applications. So you're perfectly free to create your own directory in it.

我会使用~/Library/Application Support/script_name/. 里面的子目录Application Support通常被各种应用程序使用,包括苹果自己的软件。但它不是由操作系统强制执行的,也不受内部应用程序的约束/Applications。因此,您可以完全自由地在其中创建自己的目录。

For the directory structure of OS X in general, see this Apple document.

有关 OS X 的一般目录结构,请参阅此 Apple 文档

回答by jla

Comparing Apple's documentation for the various paths to the XDG Base Directory specifications approximates to the following locations:

比较 Apple 的文档以了解 XDG 基本目录规范的各种路径,大致位于以下位置:

  • XDG_CONFIG_HOME?? ~/Library/Preferences/
  • XDG_DATA_HOME?? ~/Library/
  • XDG_CACHE_HOME?? ~/Library/Caches/
  • XDG_CONFIG_HOME?? ~/Library/Preferences/
  • XDG_DATA_HOME?? ~/Library/
  • XDG_CACHE_HOME?? ~/Library/Caches/

Mapping XDG Base Directory Specification locations for "My App" on Mac OS X could look like this:

在 Mac OS X 上为“我的应用程序”映射 XDG 基本目录规范位置可能如下所示:

  • XDG_CONFIG_HOME?? ~/Library/Preferences/name.often.with.domain.myapp.plist
  • XDG_DATA_HOME?? ~/Library/My App/
  • XDG_CACHE_HOME?? ~/Library/Caches/My App/
  • XDG_CONFIG_HOME?? ~/Library/Preferences/name.often.with.domain.myapp.plist
  • XDG_DATA_HOME?? ~/Library/My App/
  • XDG_CACHE_HOME?? ~/Library/Caches/My App/

These mappings seem pretty reasonable but they aren't exact. Some kinds of cache or data may be appropriate for ~/Library/Application Support/My App, and other may be best in the temp locations or the App bundle. All of it is by convention and the same reasons for using the best XDG_locations apply to using the best locations on the Mac OS X system.

这些映射看起来很合理,但并不准确。某些类型的缓存或数据可能适用于~/Library/Application Support/My App,而其他类型的缓存或数据可能最适合临时位置或应用程序包。所有这些都是按照惯例,使用最佳XDG_位置的相同原因也适用于在 Mac OS X 系统上使用最佳位置。

Your annoyance at ~/.myscriptis in line with Apple's guidelines: "Don't pollute user space".

你的烦恼~/.myscript符合苹果的指导方针:“不要污染用户空间”。

References:

参考:

回答by Franklin Yu

I agree with OJFord's comment: if you are writing a script(I think this applies to all CLI-only application), simply follow the XDG Base Directory would be better.

我同意OJFord 的评论:如果您正在编写脚本(我认为这适用于所有仅 CLI 的应用程序),只需遵循 XDG Base Directory 会更好。

Rationales:

理由:

  1. Names in XDG_CONFIG_HOMEare typically small-case bare-names like git; the ones in ~/Library/Preferences/are typically scoped with reversed domain names like com.apple.foo-bar, or first-letter-capital space-delimited names like Foo Bar.
  2. GUI applications have been following macOS conventions when they are placed in /Applications. You don't place your script in /Applications; you place them in UNIX-specific directorys. Better to be consistent.
  3. ~/Library/Preferences/is full of .plists. No cross-platform script that I know is using property lists.
  4. Users may want to sync cross-platform configuration and macOS-only one separately.
  1. 中的名称XDG_CONFIG_HOME通常是小写的裸名,例如git; 中的那些~/Library/Preferences/通常以反向域名为范围,例如com.apple.foo-bar,或首字母大写空格分隔的名称,例如Foo Bar
  2. GUI 应用程序在放置在/Applications. 你没有把你的脚本放在/Applications; 您将它们放在UNIX 特定的目录中。最好保持一致。
  3. ~/Library/Preferences/充满了.plists。据我所知,没有跨平台脚本使用属性列表。
  4. 用户可能希望分别同步跨平台配置和仅限 macOS 的配置。

For example, Git places their config file in XDG_CONFIG_HOME/git/config, not in ~/Library/Preferences/Git/config. Makes sense to me.

例如,Git 将他们的配置文件XDG_CONFIG_HOME/git/config放在~/Library/Preferences/Git/config. 我感觉合理。

note

笔记

I made the point only for configuration files ($XDG_CONFIG_HOME) and data files ($XDG_DATA_HOME); for cache files it gets subtle. According to How-To Geek, ~/Library/Cachesdirectory is excluded from Time Machineby default. I don't care about cache folder since I back up neither directories anyway; but for ones who care, I recommend them to link ~/.cacheto somewhere in the default cache folder, such as:

我只针对配置文件 ( $XDG_CONFIG_HOME) 和数据文件 ( $XDG_DATA_HOME)提出了这一点;对于缓存文件,它变得微妙。根据How-To Geek,默认情况下~/Library/CachesTime Machine 中排除目录。我不关心缓存文件夹,因为无论如何我都不备份目录;但是对于那些关心的人,我建议他们链接~/.cache到默认缓存文件夹中的某个位置,例如:

cd ~; mv .cache ~/Library/Caches/XDG-cache; ln -s ~/Library/Caches/XDG-cache .cache