simple_xdg 0.0.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +11 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE.md +21 -0
- data/README.md +66 -7
- data/lib/simple_xdg/version.rb +9 -0
- data/lib/simple_xdg.rb +407 -6
- metadata +38 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8db25c3801d49ace447881c87f3dbdd9fb2ca4ff2df955adbf7554f5af69928b
|
|
4
|
+
data.tar.gz: b9ad844c2bab45f09c856025d9458a60955fd9d6966c01b52d68920eb0b78915
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0f249d40422399854b4f2f40e73be9a40087a022a27fd62d199160a744ccf5495a556fa971a7085d3098f25376f10d7222571159e2dd3dbd96b8db59be1e2dc5
|
|
7
|
+
data.tar.gz: 6e48291c6b2c2b684f88ba660d7298c2540202143f8bfb87e0838d738b8d018a133aaa6992f82fec9aba1c17b080567065c6e662433c0753f9333f97e81f09d4
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# License
|
|
2
|
+
|
|
3
|
+
Copyright 2026 Daniel Azuma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,9 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SimpleXDG
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
`SimpleXDG` is a no-frills Ruby class implementing the XDG Base Directory
|
|
4
|
+
Specification. This spech defines where certain user-specific application
|
|
5
|
+
files, such as configuration, cache, and saved state, should live on the user's
|
|
6
|
+
file system. It specifies environment variables that contain this information,
|
|
7
|
+
and defaults that should be used if those environment variables are unset. The
|
|
8
|
+
spec itself is at https://specifications.freedesktop.org/basedir/latest/
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
`
|
|
10
|
+
## Getting started
|
|
11
|
+
|
|
12
|
+
Install `SimpleXDG` via the
|
|
13
|
+
[simple_xdg gem](https://rubygems.org/gems/simple_xdg).
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
% gem install simple_xdg
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
or add it to your Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem "simple_xdg"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
To use the service, instantiate `SimpleXDG`, and call methods to obtain the
|
|
26
|
+
directory paths:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require "simple_xdg"
|
|
30
|
+
xdg = SimpleXDG.new
|
|
31
|
+
my_config_file_path = xdg.lookup_config("my_app_config.toml")
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Contributing
|
|
35
|
+
|
|
36
|
+
Development is done in GitHub at https://github.com/dazuma/simple_xdg.
|
|
37
|
+
|
|
38
|
+
* To file issues: https://github.com/dazuma/simple_xdg/issues.
|
|
39
|
+
* For questions and discussion, please do not file an issue. Instead, use the
|
|
40
|
+
discussions feature: https://github.com/dazuma/simple_xdg/discussions.
|
|
41
|
+
* Pull requests are welcome, but in general please open an issue first before
|
|
42
|
+
contributing significant changes.
|
|
43
|
+
|
|
44
|
+
The library uses [toys](https://dazuma.github.io/toys) for testing and CI. To
|
|
45
|
+
run the test suite, `gem install toys` and then run `toys ci`. You can also run
|
|
46
|
+
unit tests, rubocop, and build tests independently.
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
Copyright 2026 Daniel Azuma
|
|
51
|
+
|
|
52
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
53
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
54
|
+
in the Software without restriction, including without limitation the rights
|
|
55
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
56
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
57
|
+
furnished to do so, subject to the following conditions:
|
|
58
|
+
|
|
59
|
+
The above copyright notice and this permission notice shall be included in
|
|
60
|
+
all copies or substantial portions of the Software.
|
|
61
|
+
|
|
62
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
63
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
64
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
65
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
66
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
67
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
68
|
+
IN THE SOFTWARE.
|
data/lib/simple_xdg.rb
CHANGED
|
@@ -1,8 +1,409 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
##
|
|
4
|
+
# A class that provides tools for working with the XDG Base Directory
|
|
5
|
+
# Specification.
|
|
1
6
|
#
|
|
2
|
-
# This
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
# released in a timely manner, you can contact the owner at
|
|
7
|
-
# dazuma@gmail.com
|
|
7
|
+
# This class provides utility methods that locate base directories and
|
|
8
|
+
# search paths for application state, configuration, caches, and other
|
|
9
|
+
# data, according to the [XDG Base Directory Spec version
|
|
10
|
+
# 0.8](https://specifications.freedesktop.org/basedir/0.8/).
|
|
8
11
|
#
|
|
12
|
+
# ### Example
|
|
13
|
+
#
|
|
14
|
+
# require "simple_xdg"
|
|
15
|
+
#
|
|
16
|
+
# xdg = SimpleXDG.new
|
|
17
|
+
#
|
|
18
|
+
# # Get config file paths, in order from most to least important
|
|
19
|
+
# config_files = xdg.lookup_config("my-config.toml")
|
|
20
|
+
# config_files.each { |path| read_my_config(path) }
|
|
21
|
+
#
|
|
22
|
+
# ### Windows operation
|
|
23
|
+
#
|
|
24
|
+
# The Spec assumes a unix-like environment, and cannot be applied directly
|
|
25
|
+
# to Windows without modification. In general, this class will function on
|
|
26
|
+
# Windows, but with the following caveats:
|
|
27
|
+
#
|
|
28
|
+
# * All file paths must use Windows-style absolute paths, beginning with
|
|
29
|
+
# the drive letter.
|
|
30
|
+
# * Environment variables that can contain multiple paths (`XDG_*_DIRS`)
|
|
31
|
+
# use the Windows path delimiter (`;`) rather than the unix path
|
|
32
|
+
# delimiter (`:`).
|
|
33
|
+
# * Defaults for home directories (`XDG_*_HOME`) will follow unix
|
|
34
|
+
# conventions, using subdirectories under the user's profile directory
|
|
35
|
+
# rather than the Windows known folder paths.
|
|
36
|
+
# * Defaults for search paths (`XDG_*_DIRS`) will be empty and will not
|
|
37
|
+
# use the Windows known folder paths.
|
|
38
|
+
#
|
|
39
|
+
class SimpleXDG
|
|
40
|
+
##
|
|
41
|
+
# An error raised in certain cases when a lookup fails.
|
|
42
|
+
#
|
|
43
|
+
class Error < ::StandardError
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
##
|
|
47
|
+
# Create an instance of XDG.
|
|
48
|
+
#
|
|
49
|
+
# @param env [Hash{String=>String}] the environment variables. Normally,
|
|
50
|
+
# you can omit this argument, as it will default to `::ENV`.
|
|
51
|
+
#
|
|
52
|
+
def initialize(env: ::ENV)
|
|
53
|
+
require "fileutils"
|
|
54
|
+
@env = env
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
##
|
|
58
|
+
# Returns the absolute path to the current user's home directory.
|
|
59
|
+
#
|
|
60
|
+
# @return [String]
|
|
61
|
+
#
|
|
62
|
+
def home_dir
|
|
63
|
+
@home_dir ||= validate_dir_env("HOME") || ::Dir.home
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
##
|
|
67
|
+
# Returns the absolute path to the single base directory relative to
|
|
68
|
+
# which user-specific data files should be written.
|
|
69
|
+
#
|
|
70
|
+
# Corresponds to the value of the `$XDG_DATA_HOME` environment variable
|
|
71
|
+
# and its defaults according to the XDG Base Directory Spec.
|
|
72
|
+
#
|
|
73
|
+
# @return [String]
|
|
74
|
+
#
|
|
75
|
+
def data_home
|
|
76
|
+
@data_home ||= validate_dir_env("XDG_DATA_HOME") || ::File.join(home_dir, ".local", "share")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
##
|
|
80
|
+
# Returns the absolute path to the single base directory relative to
|
|
81
|
+
# which user-specific configuration files should be written.
|
|
82
|
+
#
|
|
83
|
+
# Corresponds to the value of the `$XDG_CONFIG_HOME` environment variable
|
|
84
|
+
# and its defaults according to the XDG Base Directory Spec.
|
|
85
|
+
#
|
|
86
|
+
# @return [String]
|
|
87
|
+
#
|
|
88
|
+
def config_home
|
|
89
|
+
@config_home ||= validate_dir_env("XDG_CONFIG_HOME") || ::File.join(home_dir, ".config")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
##
|
|
93
|
+
# Returns the absolute path to the single base directory relative to
|
|
94
|
+
# which user-specific state files should be written.
|
|
95
|
+
#
|
|
96
|
+
# Corresponds to the value of the `$XDG_STATE_HOME` environment variable
|
|
97
|
+
# and its defaults according to the XDG Base Directory Spec.
|
|
98
|
+
#
|
|
99
|
+
# @return [String]
|
|
100
|
+
#
|
|
101
|
+
def state_home
|
|
102
|
+
@state_home ||= validate_dir_env("XDG_STATE_HOME") || ::File.join(home_dir, ".local", "state")
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
##
|
|
106
|
+
# Returns the absolute path to the single base directory relative to
|
|
107
|
+
# which user-specific non-essential (cached) data should be written.
|
|
108
|
+
#
|
|
109
|
+
# Corresponds to the value of the `$XDG_CACHE_HOME` environment variable
|
|
110
|
+
# and its defaults according to the XDG Base Directory Spec.
|
|
111
|
+
#
|
|
112
|
+
# @return [String]
|
|
113
|
+
#
|
|
114
|
+
def cache_home
|
|
115
|
+
@cache_home ||= validate_dir_env("XDG_CACHE_HOME") || ::File.join(home_dir, ".cache")
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
##
|
|
119
|
+
# Returns the absolute path to the single base directory relative to
|
|
120
|
+
# which user-specific executable files may be written.
|
|
121
|
+
#
|
|
122
|
+
# Returns the value of `$HOME/.local/bin` as specified by the XDG Base
|
|
123
|
+
# Directory Spec.
|
|
124
|
+
#
|
|
125
|
+
# @return [String]
|
|
126
|
+
#
|
|
127
|
+
def executable_home
|
|
128
|
+
@executable_home ||= ::File.join(home_dir, ".local", "bin")
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
##
|
|
132
|
+
# Returns the set of preference ordered base directories relative to
|
|
133
|
+
# which data files should be searched, as an array of absolute paths.
|
|
134
|
+
# The array is ordered from most to least important, and does _not_
|
|
135
|
+
# include the data home directory.
|
|
136
|
+
#
|
|
137
|
+
# Corresponds to the value of the `$XDG_DATA_DIRS` environment variable
|
|
138
|
+
# and its defaults according to the XDG Base Directory Spec.
|
|
139
|
+
#
|
|
140
|
+
# @return [Array<String>]
|
|
141
|
+
#
|
|
142
|
+
def data_dirs
|
|
143
|
+
@data_dirs ||= validate_dirs_env("XDG_DATA_DIRS") ||
|
|
144
|
+
validate_dirs(["/usr/local/share", "/usr/share"])
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
##
|
|
148
|
+
# Returns the set of preference ordered base directories relative to
|
|
149
|
+
# which configuration files should be searched, as an array of absolute
|
|
150
|
+
# paths. The array is ordered from most to least important, and does
|
|
151
|
+
# _not_ include the config home directory.
|
|
152
|
+
#
|
|
153
|
+
# Corresponds to the value of the `$XDG_CONFIG_DIRS` environment variable
|
|
154
|
+
# and its defaults according to the XDG Base Directory Spec.
|
|
155
|
+
#
|
|
156
|
+
# @return [Array<String>]
|
|
157
|
+
#
|
|
158
|
+
def config_dirs
|
|
159
|
+
@config_dirs ||= validate_dirs_env("XDG_CONFIG_DIRS") ||
|
|
160
|
+
validate_dirs(["/etc/xdg"])
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
##
|
|
164
|
+
# Returns the absolute path to the single base directory relative to
|
|
165
|
+
# which user-specific runtime files and other file objects should be
|
|
166
|
+
# placed.
|
|
167
|
+
#
|
|
168
|
+
# Corresponds to the value of the `$XDG_RUNTIME_DIR` environment variable
|
|
169
|
+
# according to the XDG Base Directory Spec.
|
|
170
|
+
#
|
|
171
|
+
# **Important:** Returns `nil` if the `$XDG_RUNTIME_DIR` environment
|
|
172
|
+
# variable is unset or invalid. In such a case, it is the caller's
|
|
173
|
+
# responsibility to determine a fallback strategy, as this library cannot
|
|
174
|
+
# by itself implement a compliant fallback without OS help.
|
|
175
|
+
#
|
|
176
|
+
# @return [String,nil]
|
|
177
|
+
#
|
|
178
|
+
def runtime_dir
|
|
179
|
+
@runtime_dir = validate_dir_env("XDG_RUNTIME_DIR") unless defined? @runtime_dir
|
|
180
|
+
@runtime_dir
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
##
|
|
184
|
+
# Returns the absolute path to the single base directory relative to
|
|
185
|
+
# which user-specific runtime files and other file objects should be
|
|
186
|
+
# placed.
|
|
187
|
+
#
|
|
188
|
+
# Corresponds to the value of the `$XDG_RUNTIME_DIR` environment variable
|
|
189
|
+
# according to the XDG Base Directory Spec.
|
|
190
|
+
#
|
|
191
|
+
# Raises {SimpleXDG::Error} if the `$XDG_RUNTIME_DIR` environment
|
|
192
|
+
# variable is unset or invalid. Unlike {#runtime_dir}, does not return
|
|
193
|
+
# nil.
|
|
194
|
+
#
|
|
195
|
+
# @return [String]
|
|
196
|
+
#
|
|
197
|
+
def runtime_dir!
|
|
198
|
+
runtime_dir || raise(::SimpleXDG::Error, "XDG_RUNTIME_DIR is unset or invalid")
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
##
|
|
202
|
+
# Searches the data directories for an object with the given relative
|
|
203
|
+
# path, and returns an array of absolute paths to all objects found in
|
|
204
|
+
# all data directories (i.e. {#data_home} and {#data_dirs}), in order
|
|
205
|
+
# from most to least important. Returns the empty array if no suitable
|
|
206
|
+
# objects are found.
|
|
207
|
+
#
|
|
208
|
+
# If multiple objects are found, the caller should implement its own
|
|
209
|
+
# logic to resolve them. For example, it can select the first (most
|
|
210
|
+
# important) object, or implement logic to combine the contents.
|
|
211
|
+
#
|
|
212
|
+
# @param path [String] Relative path of the object to search for
|
|
213
|
+
# @param type [String,Symbol,Array<String,Symbol>] The type(s) of objects
|
|
214
|
+
# to find. You can specify any of the types defined by
|
|
215
|
+
# [File::Stat#ftype](https://ruby-doc.org/core/File/Stat.html#method-i-ftype),
|
|
216
|
+
# such as `file` or `directory`, or the special type `any`. Types can
|
|
217
|
+
# be specified as strings or the corresponding symbols. If this
|
|
218
|
+
# argument is not provided, the default of `file` is used.
|
|
219
|
+
# @return [Array<String>]
|
|
220
|
+
#
|
|
221
|
+
def lookup_data(path, type: :file)
|
|
222
|
+
lookup_internal([data_home] + data_dirs, path, type)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
##
|
|
226
|
+
# Searches the config directories for an object with the given relative
|
|
227
|
+
# path, and returns an array of absolute paths to all objects found in
|
|
228
|
+
# all config directories (i.e. {#config_home} and {#config_dirs}), in
|
|
229
|
+
# order from most to least important. Returns the empty array if no
|
|
230
|
+
# suitable objects are found.
|
|
231
|
+
#
|
|
232
|
+
# If multiple objects are found, the caller should implement its own
|
|
233
|
+
# logic to resolve them. For example, it can select the first (most
|
|
234
|
+
# important) object, or implement logic to combine the contents.
|
|
235
|
+
#
|
|
236
|
+
# @param path [String] Relative path of the object to search for
|
|
237
|
+
# @param type [String,Symbol,Array<String,Symbol>] The type(s) of objects
|
|
238
|
+
# to find. You can specify any of the types defined by
|
|
239
|
+
# [File::Stat#ftype](https://ruby-doc.org/core/File/Stat.html#method-i-ftype),
|
|
240
|
+
# such as `file` or `directory`, or the special type `any`. Types can
|
|
241
|
+
# be specified as strings or the corresponding symbols. If this
|
|
242
|
+
# argument is not provided, the default of `file` is used.
|
|
243
|
+
# @return [Array<String>]
|
|
244
|
+
#
|
|
245
|
+
def lookup_config(path, type: :file)
|
|
246
|
+
lookup_internal([config_home] + config_dirs, path, type)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
##
|
|
250
|
+
# Searches the state directory ({#state_home}) for an object with the
|
|
251
|
+
# given relative path, and returns an array of zero or one absolute paths
|
|
252
|
+
# to any found object. Because the XDG basedir spec does not provide for
|
|
253
|
+
# a list of fallback directories for state files (i.e. there is no
|
|
254
|
+
# `XDG_STATE_DIRS` variable or list of default paths), this will return a
|
|
255
|
+
# maximum of one result. However, it returns an array for consistency
|
|
256
|
+
# with the {#lookup_data} and {#lookup_config} methods.
|
|
257
|
+
#
|
|
258
|
+
# @param path [String] Relative path of the object to search for
|
|
259
|
+
# @param type [String,Symbol,Array<String,Symbol>] The type(s) of objects
|
|
260
|
+
# to find. You can specify any of the types defined by
|
|
261
|
+
# [File::Stat#ftype](https://ruby-doc.org/core/File/Stat.html#method-i-ftype),
|
|
262
|
+
# such as `file` or `directory`, or the special type `any`. Types can
|
|
263
|
+
# be specified as strings or the corresponding symbols. If this
|
|
264
|
+
# argument is not provided, the default of `file` is used.
|
|
265
|
+
# @return [Array<String>]
|
|
266
|
+
#
|
|
267
|
+
def lookup_state(path, type: :file)
|
|
268
|
+
lookup_internal([state_home], path, type)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
##
|
|
272
|
+
# Searches the cache directory ({#cache_home}) for an object with the
|
|
273
|
+
# given relative path, and returns an array of zero or one absolute paths
|
|
274
|
+
# to any found object. Because the XDG basedir spec does not provide for
|
|
275
|
+
# a list of fallback directories for cache files (i.e. there is no
|
|
276
|
+
# `XDG_CACHE_DIRS` variable or list of default paths), this will return a
|
|
277
|
+
# maximum of one result. However, it returns an array for consistency
|
|
278
|
+
# with the {#lookup_data} and {#lookup_config} methods.
|
|
279
|
+
#
|
|
280
|
+
# @param path [String] Relative path of the object to search for
|
|
281
|
+
# @param type [String,Symbol,Array<String,Symbol>] The type(s) of objects
|
|
282
|
+
# to find. You can specify any of the types defined by
|
|
283
|
+
# [File::Stat#ftype](https://ruby-doc.org/core/File/Stat.html#method-i-ftype),
|
|
284
|
+
# such as `file` or `directory`, or the special type `any`. Types can
|
|
285
|
+
# be specified as strings or the corresponding symbols. If this
|
|
286
|
+
# argument is not provided, the default of `file` is used.
|
|
287
|
+
# @return [Array<String>]
|
|
288
|
+
#
|
|
289
|
+
def lookup_cache(path, type: :file)
|
|
290
|
+
lookup_internal([cache_home], path, type)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
##
|
|
294
|
+
# Returns the absolute path to a directory under {#data_home}, creating
|
|
295
|
+
# it if it doesn't already exist.
|
|
296
|
+
#
|
|
297
|
+
# @param path [String] The relative path to the subdir within the base
|
|
298
|
+
# data directory.
|
|
299
|
+
# @return [String] The absolute path to the subdir.
|
|
300
|
+
# @raise [SystemCallError] If a non-directory already exists there. It is
|
|
301
|
+
# unspecified which specific error will be raised; it typically could
|
|
302
|
+
# be `Errno::EEXIST` or `Errno::ENOTDIR`.
|
|
303
|
+
#
|
|
304
|
+
def ensure_data_subdir(path)
|
|
305
|
+
ensure_subdir_internal(data_home, path)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
##
|
|
309
|
+
# Returns the absolute path to a directory under {#config_home}, creating
|
|
310
|
+
# it if it doesn't already exist.
|
|
311
|
+
#
|
|
312
|
+
# @param path [String] The relative path to the subdir within the base
|
|
313
|
+
# config directory.
|
|
314
|
+
# @return [String] The absolute path to the subdir.
|
|
315
|
+
# @raise [SystemCallError] If a non-directory already exists there. It is
|
|
316
|
+
# unspecified which specific error will be raised; it typically could
|
|
317
|
+
# be `Errno::EEXIST` or `Errno::ENOTDIR`.
|
|
318
|
+
#
|
|
319
|
+
def ensure_config_subdir(path)
|
|
320
|
+
ensure_subdir_internal(config_home, path)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
##
|
|
324
|
+
# Returns the absolute path to a directory under {#state_home}, creating
|
|
325
|
+
# it if it doesn't already exist.
|
|
326
|
+
#
|
|
327
|
+
# @param path [String] The relative path to the subdir within the base
|
|
328
|
+
# state directory.
|
|
329
|
+
# @return [String] The absolute path to the subdir.
|
|
330
|
+
# @raise [SystemCallError] If a non-directory already exists there. It is
|
|
331
|
+
# unspecified which specific error will be raised; it typically could
|
|
332
|
+
# be `Errno::EEXIST` or `Errno::ENOTDIR`.
|
|
333
|
+
#
|
|
334
|
+
def ensure_state_subdir(path)
|
|
335
|
+
ensure_subdir_internal(state_home, path)
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
##
|
|
339
|
+
# Returns the absolute path to a directory under {#cache_home}, creating
|
|
340
|
+
# it if it doesn't already exist.
|
|
341
|
+
#
|
|
342
|
+
# @param path [String] The relative path to the subdir within the base
|
|
343
|
+
# cache directory.
|
|
344
|
+
# @return [String] The absolute path to the subdir.
|
|
345
|
+
# @raise [SystemCallError] If a non-directory already exists there. It is
|
|
346
|
+
# unspecified which specific error will be raised; it typically could
|
|
347
|
+
# be `Errno::EEXIST` or `Errno::ENOTDIR`.
|
|
348
|
+
#
|
|
349
|
+
def ensure_cache_subdir(path)
|
|
350
|
+
ensure_subdir_internal(cache_home, path)
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
private
|
|
354
|
+
|
|
355
|
+
##
|
|
356
|
+
# Given an environment variable name, returns the value if it is a legal
|
|
357
|
+
# absolute path, otherwise returns nil. Used to interpret `XDG_*_HOME`
|
|
358
|
+
# variables.
|
|
359
|
+
#
|
|
360
|
+
def validate_dir_env(name)
|
|
361
|
+
path = @env[name].to_s
|
|
362
|
+
::File.absolute_path?(path) ? path : nil
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
##
|
|
366
|
+
# Given an environment variable name, returns nil if unset or empty,
|
|
367
|
+
# otherwise returns a (possibly empty) array of the valid paths.
|
|
368
|
+
#
|
|
369
|
+
def validate_dirs_env(name)
|
|
370
|
+
raw_value = @env[name].to_s
|
|
371
|
+
return nil if raw_value.empty?
|
|
372
|
+
validate_dirs(raw_value.split(::File::PATH_SEPARATOR))
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
##
|
|
376
|
+
# Given an array of paths, returns a (possibly empty) array of which ones
|
|
377
|
+
# are valid absolute paths.
|
|
378
|
+
#
|
|
379
|
+
def validate_dirs(paths)
|
|
380
|
+
paths.find_all { |path| ::File.absolute_path?(path) }
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
##
|
|
384
|
+
# Given an array of directories, a relative path, and an array of types,
|
|
385
|
+
# find and return all objects found as absolute paths.
|
|
386
|
+
#
|
|
387
|
+
def lookup_internal(dirs, path, types)
|
|
388
|
+
results = []
|
|
389
|
+
types = Array(types).map(&:to_s)
|
|
390
|
+
any_type = types.include?("any")
|
|
391
|
+
dirs.each do |dir|
|
|
392
|
+
to_check = ::File.join(dir, path)
|
|
393
|
+
stat = ::File.stat(to_check) rescue nil # rubocop:disable Style/RescueModifier
|
|
394
|
+
if stat&.readable? && (any_type || types.include?(stat.ftype))
|
|
395
|
+
results << to_check
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
results
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
##
|
|
402
|
+
# Ensure directory exists, and return its absolute path.
|
|
403
|
+
#
|
|
404
|
+
def ensure_subdir_internal(base_dir, path)
|
|
405
|
+
path = ::File.join(base_dir, path)
|
|
406
|
+
::FileUtils.mkdir_p(path, mode: 0o700)
|
|
407
|
+
path
|
|
408
|
+
end
|
|
409
|
+
end
|
metadata
CHANGED
|
@@ -1,26 +1,53 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_xdg
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Daniel Azuma
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
-
dependencies:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: logger
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: This class provides a simple no-frills implementation of the XDG Base
|
|
27
|
+
Directory Specification, which defines where certain user-specific application files,
|
|
28
|
+
such as configuration, cache, and saved state, should live on the user's file system.
|
|
29
|
+
It specifies environment variables that contain this information, and defaults that
|
|
30
|
+
should be used if those environment variables are unset. The spec itself is at https://specifications.freedesktop.org/basedir/latest/
|
|
31
|
+
email:
|
|
32
|
+
- dazuma@gmail.com
|
|
16
33
|
executables: []
|
|
17
34
|
extensions: []
|
|
18
35
|
extra_rdoc_files: []
|
|
19
36
|
files:
|
|
37
|
+
- ".yardopts"
|
|
38
|
+
- CHANGELOG.md
|
|
39
|
+
- LICENSE.md
|
|
20
40
|
- README.md
|
|
21
41
|
- lib/simple_xdg.rb
|
|
22
|
-
|
|
23
|
-
|
|
42
|
+
- lib/simple_xdg/version.rb
|
|
43
|
+
homepage: https://github.com/dazuma/simple_xdg
|
|
44
|
+
licenses:
|
|
45
|
+
- MIT
|
|
46
|
+
metadata:
|
|
47
|
+
bug_tracker_uri: https://github.com/dazuma/simple_xdg/issues
|
|
48
|
+
changelog_uri: https://rubydoc.info/gems/simple_xdg/0.1.1/file/CHANGELOG.md
|
|
49
|
+
documentation_uri: https://rubydoc.info/gems/simple_xdg/0.1.1
|
|
50
|
+
homepage_uri: https://github.com/dazuma/simple_xdg
|
|
24
51
|
rdoc_options: []
|
|
25
52
|
require_paths:
|
|
26
53
|
- lib
|
|
@@ -28,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
28
55
|
requirements:
|
|
29
56
|
- - ">="
|
|
30
57
|
- !ruby/object:Gem::Version
|
|
31
|
-
version: '
|
|
58
|
+
version: '2.7'
|
|
32
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
33
60
|
requirements:
|
|
34
61
|
- - ">="
|
|
@@ -37,5 +64,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
37
64
|
requirements: []
|
|
38
65
|
rubygems_version: 4.0.6
|
|
39
66
|
specification_version: 4
|
|
40
|
-
summary:
|
|
67
|
+
summary: A simple implementation of the XDG Base Directory Specification.
|
|
41
68
|
test_files: []
|