utils 0.85.0 → 0.86.0
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/bin/changes +5 -5
- data/bin/code_comment +2 -1
- data/bin/commit_message +5 -4
- data/lib/utils/config_file/block_config.rb +110 -0
- data/lib/utils/config_file.rb +1 -109
- data/lib/utils/finder.rb +4 -6
- data/lib/utils/irb/irb_server.rb +3 -4
- data/lib/utils/version.rb +1 -1
- data/lib/utils/xdg.rb +169 -0
- data/lib/utils.rb +1 -1
- data/utils.gemspec +5 -5
- metadata +6 -4
- data/lib/utils/config_dir.rb +0 -128
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 45dd9d630295ca603cefea5e05cc519293f601ca3870238d964346a0e152cf36
|
|
4
|
+
data.tar.gz: 599507eeab4466076016b42fda07110222c54820fcfc79bfa718664a9d3e0c43
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fd971b240d210063700fa81259cd64e9d3795cfa5cc50c7bdbff1e5f17e67240f24ca8c2bd6b00ee714fd0d168c70b361a605703094cdd2a56a9ba19c9a43a9e
|
|
7
|
+
data.tar.gz: a9602ee4790e2be111ee2994e859f6d6a3c0ef4f6b5c518b9db46c4ee591558ece2fb38411bbdf0822a7bc35931ae2684065a39c5aa06ee54c2abc149fa7ddb7
|
data/bin/changes
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
require 'ollama'
|
|
29
29
|
include Ollama
|
|
30
30
|
require 'utils'
|
|
31
|
+
include Utils::XDG
|
|
31
32
|
|
|
32
33
|
# Executes a shell command and returns its output.
|
|
33
34
|
#
|
|
@@ -101,13 +102,12 @@ def compute_change(range_from, range_to)
|
|
|
101
102
|
EOT
|
|
102
103
|
end
|
|
103
104
|
|
|
104
|
-
config
|
|
105
|
-
|
|
105
|
+
config = XDG_CONFIG_HOME + 'changes'
|
|
106
106
|
base_url = ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
|
|
107
107
|
model = ENV.fetch('OLLAMA_MODEL', 'llama3.1')
|
|
108
|
-
system =
|
|
109
|
-
prompt =
|
|
110
|
-
client_config = Client::Config.load_from_json(
|
|
108
|
+
system = config.read('system.txt')
|
|
109
|
+
prompt = config.read('prompt.txt') + "\n\n#{log}\n"
|
|
110
|
+
client_config = Client::Config.load_from_json(config + 'client.json')
|
|
111
111
|
client_config.base_url = base_url
|
|
112
112
|
|
|
113
113
|
if ENV['DEBUG'].to_i == 1
|
data/bin/code_comment
CHANGED
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
require 'ollama'
|
|
33
33
|
include Ollama
|
|
34
34
|
require 'utils'
|
|
35
|
+
include Utils::XDG
|
|
35
36
|
require 'context_spook'
|
|
36
37
|
|
|
37
38
|
META_METHOD = /(
|
|
@@ -184,7 +185,7 @@ end
|
|
|
184
185
|
filename_linenumber = ARGV.shift or fail "require file_name as second argument"
|
|
185
186
|
$config = Utils::ConfigFile.new
|
|
186
187
|
$config.configure_from_paths
|
|
187
|
-
$config_dir =
|
|
188
|
+
$config_dir = XDG_CONFIG_HOME + 'code_comment'
|
|
188
189
|
base_url = ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
|
|
189
190
|
model = ENV.fetch('OLLAMA_MODEL', 'llama3.1')
|
|
190
191
|
construct, construct_type = fetch_construct(filename_linenumber)
|
data/bin/commit_message
CHANGED
|
@@ -26,10 +26,11 @@
|
|
|
26
26
|
# - prompt.txt: Commit message template, contains %{branch} and %{stdin}
|
|
27
27
|
|
|
28
28
|
require 'utils'
|
|
29
|
+
include Utils::XDG
|
|
29
30
|
|
|
30
|
-
config =
|
|
31
|
+
config = XDG_CONFIG_HOME + 'commit_message'
|
|
31
32
|
|
|
32
33
|
branch = `git rev-parse --abbrev-ref HEAD`.chomp
|
|
33
|
-
exec 'ollama_cli', '-c',
|
|
34
|
-
'-M',
|
|
35
|
-
'-s',
|
|
34
|
+
exec 'ollama_cli', '-c', config + 'client.json',
|
|
35
|
+
'-M', config + 'options.json', '-P', "branch=#{branch}",
|
|
36
|
+
'-s', config + 'system.txt', '-p', config + 'prompt.txt'
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
class Utils::ConfigFile
|
|
2
|
+
# Base class for defining configuration blocks with DSL accessors.
|
|
3
|
+
#
|
|
4
|
+
# This class provides a foundation for creating configuration classes that
|
|
5
|
+
# support dynamic attribute definition through DSL-style accessor methods. It
|
|
6
|
+
# includes functionality for registering configuration settings and
|
|
7
|
+
# generating Ruby code representations of the configuration state.
|
|
8
|
+
class BlockConfig
|
|
9
|
+
class << self
|
|
10
|
+
# The inherited method extends the module with DSL accessor functionality
|
|
11
|
+
# and calls the superclass implementation.
|
|
12
|
+
#
|
|
13
|
+
# @param modul [ Module ] the module that inherited this class
|
|
14
|
+
def inherited(modul)
|
|
15
|
+
modul.extend DSLKit::DSLAccessor
|
|
16
|
+
super
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# The config method sets up a configuration accessor with the specified
|
|
20
|
+
# name and options.
|
|
21
|
+
#
|
|
22
|
+
# This method registers a new configuration setting by adding it to the
|
|
23
|
+
# list of configuration settings and then creates an accessor for it
|
|
24
|
+
# using the dsl_accessor method, allowing for easy retrieval and
|
|
25
|
+
# assignment of configuration values.
|
|
26
|
+
#
|
|
27
|
+
# @param name [ Object ] the name of the configuration setting
|
|
28
|
+
# @param r [ Array ] additional arguments passed to the dsl_accessor method
|
|
29
|
+
#
|
|
30
|
+
# @yield [ block ] optional block to be passed to the dsl_accessor method
|
|
31
|
+
#
|
|
32
|
+
# @return [ Object ] returns self to allow for method chaining
|
|
33
|
+
def config(name, *r, &block)
|
|
34
|
+
self.config_settings ||= []
|
|
35
|
+
config_settings << name.to_sym
|
|
36
|
+
dsl_accessor name, *r, &block
|
|
37
|
+
self
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The lazy_config method configures a lazy-loaded configuration option
|
|
41
|
+
# with a default value.
|
|
42
|
+
#
|
|
43
|
+
# This method registers a new configuration setting that will be
|
|
44
|
+
# initialized lazily, meaning the default value or the set value is only
|
|
45
|
+
# computed when the configuration is actually accessed. It adds the
|
|
46
|
+
# setting to the list of configuration settings and creates a lazy
|
|
47
|
+
# accessor for it.
|
|
48
|
+
#
|
|
49
|
+
# @param name [ Object ] the name of the configuration setting to define
|
|
50
|
+
# @yield [ default ] optional block that provides the default value for
|
|
51
|
+
# the configuration
|
|
52
|
+
#
|
|
53
|
+
# @return [ Object ] returns self to allow for method chaining
|
|
54
|
+
def lazy_config(name, &default)
|
|
55
|
+
self.config_settings ||= []
|
|
56
|
+
config_settings << name.to_sym
|
|
57
|
+
dsl_lazy_accessor(name, &default)
|
|
58
|
+
self
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The config_settings method provides access to the configuration
|
|
62
|
+
# settings.
|
|
63
|
+
#
|
|
64
|
+
# This method returns the configuration settings stored in the instance
|
|
65
|
+
# variable, allowing for reading and modification of the object's
|
|
66
|
+
# configuration state.
|
|
67
|
+
#
|
|
68
|
+
# @return [ Object ] the current configuration settings stored in the
|
|
69
|
+
# instance variable
|
|
70
|
+
attr_accessor :config_settings
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The initialize method sets up the instance by evaluating the provided
|
|
74
|
+
# block in the instance's context.
|
|
75
|
+
#
|
|
76
|
+
# This method allows for dynamic configuration of the object by executing
|
|
77
|
+
# the given block within the instance's scope, enabling flexible
|
|
78
|
+
# initialization patterns.
|
|
79
|
+
#
|
|
80
|
+
# @param block [ Proc ] the block to be evaluated for instance setup
|
|
81
|
+
def initialize(&block)
|
|
82
|
+
block and instance_eval(&block)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The to_ruby method generates a Ruby configuration block representation by
|
|
86
|
+
# recursively processing the object's configuration settings and their
|
|
87
|
+
# values.
|
|
88
|
+
# It creates a nested structure with proper indentation and formatting
|
|
89
|
+
# suitable for configuration files.
|
|
90
|
+
#
|
|
91
|
+
# @param depth [ Integer ] the current nesting depth for indentation purposes
|
|
92
|
+
#
|
|
93
|
+
# @return [ String ] a formatted Ruby string representing the configuration block
|
|
94
|
+
def to_ruby(depth = 0)
|
|
95
|
+
result = ''
|
|
96
|
+
result << ' ' * 2 * depth <<
|
|
97
|
+
"#{self.class.name[/::([^:]+)\z/, 1].underscore} do\n"
|
|
98
|
+
for name in self.class.config_settings
|
|
99
|
+
value = __send__(name)
|
|
100
|
+
if value.respond_to?(:to_ruby)
|
|
101
|
+
result << ' ' * 2 * (depth + 1) << value.to_ruby(depth + 1)
|
|
102
|
+
else
|
|
103
|
+
result << ' ' * 2 * (depth + 1) <<
|
|
104
|
+
"#{name} #{Array(value).map(&:inspect) * ', '}\n"
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
result << ' ' * 2 * depth << "end\n"
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
data/lib/utils/config_file.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'tins'
|
|
2
|
+
require 'utils/config_file/block_config'
|
|
2
3
|
|
|
3
4
|
# Configuration file manager for Utils library.
|
|
4
5
|
#
|
|
@@ -93,115 +94,6 @@ class Utils::ConfigFile
|
|
|
93
94
|
self
|
|
94
95
|
end
|
|
95
96
|
|
|
96
|
-
# Base class for defining configuration blocks with DSL accessors.
|
|
97
|
-
#
|
|
98
|
-
# This class provides a foundation for creating configuration classes that
|
|
99
|
-
# support dynamic attribute definition through DSL-style accessor methods. It
|
|
100
|
-
# includes functionality for registering configuration settings and
|
|
101
|
-
# generating Ruby code representations of the configuration state.
|
|
102
|
-
class BlockConfig
|
|
103
|
-
class << self
|
|
104
|
-
# The inherited method extends the module with DSL accessor functionality
|
|
105
|
-
# and calls the superclass implementation.
|
|
106
|
-
#
|
|
107
|
-
# @param modul [ Module ] the module that inherited this class
|
|
108
|
-
def inherited(modul)
|
|
109
|
-
modul.extend DSLKit::DSLAccessor
|
|
110
|
-
super
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
# The config method sets up a configuration accessor with the specified
|
|
114
|
-
# name and options.
|
|
115
|
-
#
|
|
116
|
-
# This method registers a new configuration setting by adding it to the
|
|
117
|
-
# list of configuration settings and then creates an accessor for it
|
|
118
|
-
# using the dsl_accessor method, allowing for easy retrieval and
|
|
119
|
-
# assignment of configuration values.
|
|
120
|
-
#
|
|
121
|
-
# @param name [ Object ] the name of the configuration setting
|
|
122
|
-
# @param r [ Array ] additional arguments passed to the dsl_accessor method
|
|
123
|
-
#
|
|
124
|
-
# @yield [ block ] optional block to be passed to the dsl_accessor method
|
|
125
|
-
#
|
|
126
|
-
# @return [ Object ] returns self to allow for method chaining
|
|
127
|
-
def config(name, *r, &block)
|
|
128
|
-
self.config_settings ||= []
|
|
129
|
-
config_settings << name.to_sym
|
|
130
|
-
dsl_accessor name, *r, &block
|
|
131
|
-
self
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
# The lazy_config method configures a lazy-loaded configuration option
|
|
135
|
-
# with a default value.
|
|
136
|
-
#
|
|
137
|
-
# This method registers a new configuration setting that will be
|
|
138
|
-
# initialized lazily, meaning the default value or the set value is only
|
|
139
|
-
# computed when the configuration is actually accessed. It adds the
|
|
140
|
-
# setting to the list of configuration settings and creates a lazy
|
|
141
|
-
# accessor for it.
|
|
142
|
-
#
|
|
143
|
-
# @param name [ Object ] the name of the configuration setting to define
|
|
144
|
-
# @yield [ default ] optional block that provides the default value for
|
|
145
|
-
# the configuration
|
|
146
|
-
#
|
|
147
|
-
# @return [ Object ] returns self to allow for method chaining
|
|
148
|
-
def lazy_config(name, &default)
|
|
149
|
-
self.config_settings ||= []
|
|
150
|
-
config_settings << name.to_sym
|
|
151
|
-
dsl_lazy_accessor(name, &default)
|
|
152
|
-
self
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
# The config_settings method provides access to the configuration
|
|
156
|
-
# settings.
|
|
157
|
-
#
|
|
158
|
-
# This method returns the configuration settings stored in the instance
|
|
159
|
-
# variable, allowing for reading and modification of the object's
|
|
160
|
-
# configuration state.
|
|
161
|
-
#
|
|
162
|
-
# @return [ Object ] the current configuration settings stored in the
|
|
163
|
-
# instance variable
|
|
164
|
-
attr_accessor :config_settings
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# The initialize method sets up the instance by evaluating the provided
|
|
168
|
-
# block in the instance's context.
|
|
169
|
-
#
|
|
170
|
-
# This method allows for dynamic configuration of the object by executing
|
|
171
|
-
# the given block within the instance's scope, enabling flexible
|
|
172
|
-
# initialization patterns.
|
|
173
|
-
#
|
|
174
|
-
# @param block [ Proc ] the block to be evaluated for instance setup
|
|
175
|
-
def initialize(&block)
|
|
176
|
-
block and instance_eval(&block)
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
# The to_ruby method generates a Ruby configuration block representation by
|
|
180
|
-
# recursively processing the object's configuration settings and their
|
|
181
|
-
# values.
|
|
182
|
-
# It creates a nested structure with proper indentation and formatting
|
|
183
|
-
# suitable for configuration files.
|
|
184
|
-
#
|
|
185
|
-
# @param depth [ Integer ] the current nesting depth for indentation purposes
|
|
186
|
-
#
|
|
187
|
-
# @return [ String ] a formatted Ruby string representing the configuration block
|
|
188
|
-
def to_ruby(depth = 0)
|
|
189
|
-
result = ''
|
|
190
|
-
result << ' ' * 2 * depth <<
|
|
191
|
-
"#{self.class.name[/::([^:]+)\z/, 1].underscore} do\n"
|
|
192
|
-
for name in self.class.config_settings
|
|
193
|
-
value = __send__(name)
|
|
194
|
-
if value.respond_to?(:to_ruby)
|
|
195
|
-
result << ' ' * 2 * (depth + 1) << value.to_ruby(depth + 1)
|
|
196
|
-
else
|
|
197
|
-
result << ' ' * 2 * (depth + 1) <<
|
|
198
|
-
"#{name} #{Array(value).map(&:inspect) * ', '}\n"
|
|
199
|
-
end
|
|
200
|
-
end
|
|
201
|
-
result << ' ' * 2 * depth << "end\n"
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
|
|
205
97
|
# A configuration class for test execution settings.
|
|
206
98
|
#
|
|
207
99
|
# This class manages the configuration options related to running tests,
|
data/lib/utils/finder.rb
CHANGED
|
@@ -19,6 +19,7 @@ require 'mize'
|
|
|
19
19
|
class Utils::Finder
|
|
20
20
|
include Tins::Find
|
|
21
21
|
include Utils::Patterns
|
|
22
|
+
include Utils::XDG
|
|
22
23
|
include Term::ANSIColor
|
|
23
24
|
|
|
24
25
|
# The initialize method sets up the finder instance with the provided options.
|
|
@@ -91,7 +92,7 @@ class Utils::Finder
|
|
|
91
92
|
end
|
|
92
93
|
true
|
|
93
94
|
}
|
|
94
|
-
find(*@roots, visit:
|
|
95
|
+
find(*@roots, visit:) do |filename|
|
|
95
96
|
filename.stat.directory? and filename << ?/
|
|
96
97
|
paths << filename
|
|
97
98
|
end
|
|
@@ -110,11 +111,8 @@ class Utils::Finder
|
|
|
110
111
|
# @return [ String ] the full file path where finder results should be stored
|
|
111
112
|
def index_path
|
|
112
113
|
roots = @roots.map { |r| File.expand_path(r) }.uniq.sort
|
|
113
|
-
filename = "finder-paths-" +
|
|
114
|
-
|
|
115
|
-
dirname = File.join(Dir.tmpdir, File.basename($0))
|
|
116
|
-
FileUtils.mkdir_p dirname
|
|
117
|
-
File.join(dirname, filename)
|
|
114
|
+
filename = "finder-paths-" + Digest::MD5.new.update(roots.inspect).hexdigest
|
|
115
|
+
XDG_CACHE_HOME.sub_dir_path('utils') + filename
|
|
118
116
|
end
|
|
119
117
|
|
|
120
118
|
# The create_paths method generates and stores path information by building a
|
data/lib/utils/irb/irb_server.rb
CHANGED
|
@@ -17,6 +17,8 @@ require 'fileutils'
|
|
|
17
17
|
# client.store_snippet('puts "Hello World"')
|
|
18
18
|
# result = server.eval_snippet('2 + 2')
|
|
19
19
|
class Utils::IRB::IRBServer
|
|
20
|
+
include Utils::XDG
|
|
21
|
+
|
|
20
22
|
# The initialize method sets up a new IRBServer instance with the specified
|
|
21
23
|
# URL.
|
|
22
24
|
#
|
|
@@ -172,10 +174,7 @@ class Utils::IRB::IRBServer
|
|
|
172
174
|
# @return [ String ] the path to the log file that will be used for logging
|
|
173
175
|
def setup_logger
|
|
174
176
|
unless @log_out
|
|
175
|
-
|
|
176
|
-
log_path = Pathname.new(xdg_dir) + 'utils'
|
|
177
|
-
FileUtils.mkdir_p log_path
|
|
178
|
-
log_path += 'irb-server.log'
|
|
177
|
+
log_path = XDG_STATE_HOME.sub_dir_path('utils') + 'irb-server.log'
|
|
179
178
|
@log_out = File.new(log_path, ?a)
|
|
180
179
|
end
|
|
181
180
|
@log_out.sync = true
|
data/lib/utils/version.rb
CHANGED
data/lib/utils/xdg.rb
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
|
|
4
|
+
# Module for handling XDG base directory specifications and application
|
|
5
|
+
# directory management.
|
|
6
|
+
#
|
|
7
|
+
# Provides constants and methods for working with XDG (Cross-Desktop Group)
|
|
8
|
+
# base directories including data home, configuration home, state home, and
|
|
9
|
+
# cache home directories.
|
|
10
|
+
#
|
|
11
|
+
# The module defines standard XDG directory paths and includes functionality
|
|
12
|
+
# for creating application-specific directories within these base locations.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# Utils::XDG::XDG_DATA_HOME # => Pathname object for the data home directory
|
|
16
|
+
# Utils::XDG::XDG_CONFIG_HOME # => Pathname object for the config home directory
|
|
17
|
+
# Utils::XDG::XDG_STATE_HOME # => Pathname object for the state home directory
|
|
18
|
+
# Utils::XDG::XDG_CACHE_HOME # => Pathname object for the cache home directory
|
|
19
|
+
module Utils::XDG
|
|
20
|
+
# A Pathname subclass that provides additional XDG directory functionality
|
|
21
|
+
#
|
|
22
|
+
# This class extends the standard Pathname class to include methods for
|
|
23
|
+
# working with XDG (Cross-Desktop Group) base directory specifications. It
|
|
24
|
+
# adds capabilities for creating subdirectories, reading files, and handling
|
|
25
|
+
# path operations within the context of XDG-compliant directory structures.
|
|
26
|
+
#
|
|
27
|
+
# @example
|
|
28
|
+
# pathname = XDGPathname.new('/home/user')
|
|
29
|
+
# sub_dir = pathname.sub_dir_path('documents')
|
|
30
|
+
# content = pathname.read('config.txt')
|
|
31
|
+
class XDGPathname < ::Pathname
|
|
32
|
+
# The sub_dir_path method creates a subdirectory path and ensures it exists
|
|
33
|
+
#
|
|
34
|
+
# This method takes a directory name, combines it with the current path to
|
|
35
|
+
# form a subdirectory path, and then checks if the path already exists. If
|
|
36
|
+
# the path exists but is not a directory, it raises an ArgumentError. If
|
|
37
|
+
# the path does not exist, it creates the directory structure using
|
|
38
|
+
# FileUtils.
|
|
39
|
+
#
|
|
40
|
+
# @param dirname [ String ] the name of the subdirectory to create or access
|
|
41
|
+
#
|
|
42
|
+
# @return [ Pathname ] the Pathname object representing the subdirectory path
|
|
43
|
+
#
|
|
44
|
+
# @raise [ ArgumentError ] if the path exists but is not a directory
|
|
45
|
+
def sub_dir_path(dirname)
|
|
46
|
+
path = self + dirname
|
|
47
|
+
if path.exist?
|
|
48
|
+
path.directory? or raise ArgumentError,
|
|
49
|
+
"path #{path.to_s.inspect} exists and is not a directory"
|
|
50
|
+
else
|
|
51
|
+
FileUtils.mkdir_p path
|
|
52
|
+
end
|
|
53
|
+
path
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The read method reads file contents or yields to a block for processing.
|
|
57
|
+
#
|
|
58
|
+
# This method attempts to read a file at the specified path, returning the
|
|
59
|
+
# file's contents as a string.
|
|
60
|
+
# If a block is provided, it opens the file and yields to the block with a
|
|
61
|
+
# File object.
|
|
62
|
+
# If the file does not exist and a default value is provided, it returns
|
|
63
|
+
# the default value.
|
|
64
|
+
# When a default value is provided along with a block, the block is invoked
|
|
65
|
+
# with a StringIO object containing the default value.
|
|
66
|
+
#
|
|
67
|
+
# @param path [ String ] the path to the file to read
|
|
68
|
+
# @param default [ String, nil ] the default value to return if the file
|
|
69
|
+
# does not exist
|
|
70
|
+
#
|
|
71
|
+
# @yield [ file ] optional block to process the file
|
|
72
|
+
# @yieldparam file [ File ] the file object for processing
|
|
73
|
+
#
|
|
74
|
+
# @return [ String, nil ] the file contents if no block is given, the
|
|
75
|
+
# result of the block if given, or the default value if the file does not
|
|
76
|
+
# exist
|
|
77
|
+
def read(path, default: nil, &block)
|
|
78
|
+
full_path = join(path)
|
|
79
|
+
if File.exist?(full_path)
|
|
80
|
+
if block
|
|
81
|
+
File.new(full_path, &block)
|
|
82
|
+
else
|
|
83
|
+
File.read(full_path, encoding: 'UTF-8')
|
|
84
|
+
end
|
|
85
|
+
else
|
|
86
|
+
if default && block
|
|
87
|
+
block.(StringIO.new(default))
|
|
88
|
+
else
|
|
89
|
+
default
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The join method creates a new path by combining the current path with a
|
|
95
|
+
# given path component.
|
|
96
|
+
#
|
|
97
|
+
# This method takes a path string and appends it to the current path,
|
|
98
|
+
# returning a new Pathname object that represents the combined path.
|
|
99
|
+
#
|
|
100
|
+
# @param path [ String ] the path component to append to the current path
|
|
101
|
+
#
|
|
102
|
+
# @return [ Pathname ] a new Pathname object representing the joined path
|
|
103
|
+
def join(path)
|
|
104
|
+
self.class.new(File.join(to_s, path))
|
|
105
|
+
end
|
|
106
|
+
alias + join
|
|
107
|
+
|
|
108
|
+
alias to_str to_s
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Module for handling XDG application directories.
|
|
112
|
+
#
|
|
113
|
+
# This module provides methods for creating and managing application-specific
|
|
114
|
+
# directories within the XDG base directories.
|
|
115
|
+
module AppDir
|
|
116
|
+
# Converts a path string to a XDGPathname object with expanded path
|
|
117
|
+
#
|
|
118
|
+
# @param path [String] The path to convert
|
|
119
|
+
# @return [XDGPathname] A path as XDGPathname
|
|
120
|
+
def self.pathify(path)
|
|
121
|
+
XDGPathname.new(path).expand_path
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class << self
|
|
126
|
+
private
|
|
127
|
+
|
|
128
|
+
# Retrieves an environment variable value or returns a default.
|
|
129
|
+
#
|
|
130
|
+
# @param name [String] The name of the environment variable
|
|
131
|
+
# @param default [String] The default value if the environment variable is not set
|
|
132
|
+
# @return [String] The value of the environment variable or the default
|
|
133
|
+
def env_for(name, default:)
|
|
134
|
+
ENV.fetch(name, default)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# XDG Data Home directory path.
|
|
139
|
+
#
|
|
140
|
+
# This is the base directory relative to which user-specific data files should be stored.
|
|
141
|
+
# The default value is `~/.local/share`.
|
|
142
|
+
#
|
|
143
|
+
# @return [Pathname] The data home directory path
|
|
144
|
+
XDG_DATA_HOME = AppDir.pathify(env_for('XDG_DATA_HOME', default: '~/.local/share'))
|
|
145
|
+
|
|
146
|
+
# XDG Configuration Home directory path.
|
|
147
|
+
#
|
|
148
|
+
# This is the base directory relative to which user-specific configuration files should be stored.
|
|
149
|
+
# The default value is `~/.config`.
|
|
150
|
+
#
|
|
151
|
+
# @return [Pathname] The configuration home directory path
|
|
152
|
+
XDG_CONFIG_HOME = AppDir.pathify(env_for('XDG_CONFIG_HOME', default: '~/.config'))
|
|
153
|
+
|
|
154
|
+
# XDG State Home directory path.
|
|
155
|
+
#
|
|
156
|
+
# This is the base directory relative to which user-specific state files should be stored.
|
|
157
|
+
# The default value is `~/.local/state`.
|
|
158
|
+
#
|
|
159
|
+
# @return [Pathname] The state home directory path
|
|
160
|
+
XDG_STATE_HOME = AppDir.pathify(env_for('XDG_STATE_HOME', default: '~/.local/state'))
|
|
161
|
+
|
|
162
|
+
# XDG Cache Home directory path.
|
|
163
|
+
#
|
|
164
|
+
# This is the base directory relative to which user-specific non-essential cache files should be stored.
|
|
165
|
+
# The default value is `~/.cache`.
|
|
166
|
+
#
|
|
167
|
+
# @return [Pathname] The cache home directory path
|
|
168
|
+
XDG_CACHE_HOME = AppDir.pathify(env_for('XDG_CACHE_HOME', default: '~/.cache'))
|
|
169
|
+
end
|
data/lib/utils.rb
CHANGED
|
@@ -10,6 +10,7 @@ module Utils
|
|
|
10
10
|
require 'utils/version'
|
|
11
11
|
require 'utils/md5'
|
|
12
12
|
require 'utils/patterns'
|
|
13
|
+
require 'utils/xdg'
|
|
13
14
|
require 'utils/config_file'
|
|
14
15
|
require 'utils/editor'
|
|
15
16
|
require 'utils/finder'
|
|
@@ -17,7 +18,6 @@ module Utils
|
|
|
17
18
|
require 'utils/probe'
|
|
18
19
|
require 'utils/ssh_tunnel_specification'
|
|
19
20
|
require 'utils/line_blamer'
|
|
20
|
-
require 'utils/config_dir'
|
|
21
21
|
|
|
22
22
|
require 'utils/xt/source_location_extension'
|
|
23
23
|
class ::Object
|
data/utils.gemspec
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
# stub: utils 0.
|
|
2
|
+
# stub: utils 0.86.0 ruby lib
|
|
3
3
|
|
|
4
4
|
Gem::Specification.new do |s|
|
|
5
5
|
s.name = "utils".freeze
|
|
6
|
-
s.version = "0.
|
|
6
|
+
s.version = "0.86.0".freeze
|
|
7
7
|
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
|
9
9
|
s.require_paths = ["lib".freeze]
|
|
@@ -12,12 +12,12 @@ Gem::Specification.new do |s|
|
|
|
12
12
|
s.description = "This ruby gem provides some useful command line utilities".freeze
|
|
13
13
|
s.email = "flori@ping.de".freeze
|
|
14
14
|
s.executables = ["ascii7".freeze, "blameline".freeze, "changes".freeze, "classify".freeze, "code_comment".freeze, "code_indexer".freeze, "commit_message".freeze, "discover".freeze, "edit".freeze, "edit_wait".freeze, "enum".freeze, "git-empty".freeze, "git-versions".freeze, "irb_client".freeze, "json_check".freeze, "long_lines".freeze, "myex".freeze, "on_change".freeze, "path".freeze, "print_method".freeze, "probe".freeze, "rainbow".freeze, "rd2md".freeze, "search".freeze, "sedit".freeze, "serve".freeze, "ssh-tunnel".freeze, "strip_spaces".freeze, "sync_dir".freeze, "untest".freeze, "utils-utilsrc".freeze, "vcf2alias".freeze, "yaml_check".freeze]
|
|
15
|
-
s.extra_rdoc_files = ["README.md".freeze, "lib/utils.rb".freeze, "lib/utils/
|
|
16
|
-
s.files = ["Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "bin/ascii7".freeze, "bin/blameline".freeze, "bin/changes".freeze, "bin/classify".freeze, "bin/code_comment".freeze, "bin/code_indexer".freeze, "bin/commit_message".freeze, "bin/discover".freeze, "bin/edit".freeze, "bin/edit_wait".freeze, "bin/enum".freeze, "bin/git-empty".freeze, "bin/git-versions".freeze, "bin/irb_client".freeze, "bin/json_check".freeze, "bin/long_lines".freeze, "bin/myex".freeze, "bin/on_change".freeze, "bin/path".freeze, "bin/print_method".freeze, "bin/probe".freeze, "bin/rainbow".freeze, "bin/rd2md".freeze, "bin/search".freeze, "bin/sedit".freeze, "bin/serve".freeze, "bin/ssh-tunnel".freeze, "bin/strip_spaces".freeze, "bin/sync_dir".freeze, "bin/untest".freeze, "bin/utils-utilsrc".freeze, "bin/vcf2alias".freeze, "bin/yaml_check".freeze, "lib/utils.rb".freeze, "lib/utils/
|
|
15
|
+
s.extra_rdoc_files = ["README.md".freeze, "lib/utils.rb".freeze, "lib/utils/config_file.rb".freeze, "lib/utils/config_file/block_config.rb".freeze, "lib/utils/editor.rb".freeze, "lib/utils/finder.rb".freeze, "lib/utils/grepper.rb".freeze, "lib/utils/irb.rb".freeze, "lib/utils/irb/irb_server.rb".freeze, "lib/utils/line_blamer.rb".freeze, "lib/utils/line_formatter.rb".freeze, "lib/utils/md5.rb".freeze, "lib/utils/patterns.rb".freeze, "lib/utils/probe.rb".freeze, "lib/utils/probe/probe_client.rb".freeze, "lib/utils/probe/probe_server.rb".freeze, "lib/utils/probe/process_job.rb".freeze, "lib/utils/probe/server_handling.rb".freeze, "lib/utils/ssh_tunnel_specification.rb".freeze, "lib/utils/version.rb".freeze, "lib/utils/xdg.rb".freeze, "lib/utils/xt/source_location_extension.rb".freeze]
|
|
16
|
+
s.files = ["Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "bin/ascii7".freeze, "bin/blameline".freeze, "bin/changes".freeze, "bin/classify".freeze, "bin/code_comment".freeze, "bin/code_indexer".freeze, "bin/commit_message".freeze, "bin/discover".freeze, "bin/edit".freeze, "bin/edit_wait".freeze, "bin/enum".freeze, "bin/git-empty".freeze, "bin/git-versions".freeze, "bin/irb_client".freeze, "bin/json_check".freeze, "bin/long_lines".freeze, "bin/myex".freeze, "bin/on_change".freeze, "bin/path".freeze, "bin/print_method".freeze, "bin/probe".freeze, "bin/rainbow".freeze, "bin/rd2md".freeze, "bin/search".freeze, "bin/sedit".freeze, "bin/serve".freeze, "bin/ssh-tunnel".freeze, "bin/strip_spaces".freeze, "bin/sync_dir".freeze, "bin/untest".freeze, "bin/utils-utilsrc".freeze, "bin/vcf2alias".freeze, "bin/yaml_check".freeze, "lib/utils.rb".freeze, "lib/utils/config_file.rb".freeze, "lib/utils/config_file/block_config.rb".freeze, "lib/utils/editor.rb".freeze, "lib/utils/finder.rb".freeze, "lib/utils/grepper.rb".freeze, "lib/utils/irb.rb".freeze, "lib/utils/irb/irb_server.rb".freeze, "lib/utils/line_blamer.rb".freeze, "lib/utils/line_formatter.rb".freeze, "lib/utils/md5.rb".freeze, "lib/utils/patterns.rb".freeze, "lib/utils/probe.rb".freeze, "lib/utils/probe/probe_client.rb".freeze, "lib/utils/probe/probe_server.rb".freeze, "lib/utils/probe/process_job.rb".freeze, "lib/utils/probe/server_handling.rb".freeze, "lib/utils/ssh_tunnel_specification.rb".freeze, "lib/utils/version.rb".freeze, "lib/utils/xdg.rb".freeze, "lib/utils/xt/source_location_extension.rb".freeze, "tests/test_helper.rb".freeze, "tests/utils_test.rb".freeze, "utils.gemspec".freeze]
|
|
17
17
|
s.homepage = "http://github.com/flori/utils".freeze
|
|
18
18
|
s.licenses = ["GPL-2.0".freeze]
|
|
19
19
|
s.rdoc_options = ["--title".freeze, "Utils - Some useful command line utilities".freeze, "--main".freeze, "README.md".freeze]
|
|
20
|
-
s.rubygems_version = "4.0.
|
|
20
|
+
s.rubygems_version = "4.0.3".freeze
|
|
21
21
|
s.summary = "Some useful command line utilities".freeze
|
|
22
22
|
s.test_files = ["tests/test_helper.rb".freeze, "tests/utils_test.rb".freeze]
|
|
23
23
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: utils
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.86.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Florian Frank
|
|
@@ -315,8 +315,8 @@ extensions: []
|
|
|
315
315
|
extra_rdoc_files:
|
|
316
316
|
- README.md
|
|
317
317
|
- lib/utils.rb
|
|
318
|
-
- lib/utils/config_dir.rb
|
|
319
318
|
- lib/utils/config_file.rb
|
|
319
|
+
- lib/utils/config_file/block_config.rb
|
|
320
320
|
- lib/utils/editor.rb
|
|
321
321
|
- lib/utils/finder.rb
|
|
322
322
|
- lib/utils/grepper.rb
|
|
@@ -333,6 +333,7 @@ extra_rdoc_files:
|
|
|
333
333
|
- lib/utils/probe/server_handling.rb
|
|
334
334
|
- lib/utils/ssh_tunnel_specification.rb
|
|
335
335
|
- lib/utils/version.rb
|
|
336
|
+
- lib/utils/xdg.rb
|
|
336
337
|
- lib/utils/xt/source_location_extension.rb
|
|
337
338
|
files:
|
|
338
339
|
- Gemfile
|
|
@@ -373,8 +374,8 @@ files:
|
|
|
373
374
|
- bin/vcf2alias
|
|
374
375
|
- bin/yaml_check
|
|
375
376
|
- lib/utils.rb
|
|
376
|
-
- lib/utils/config_dir.rb
|
|
377
377
|
- lib/utils/config_file.rb
|
|
378
|
+
- lib/utils/config_file/block_config.rb
|
|
378
379
|
- lib/utils/editor.rb
|
|
379
380
|
- lib/utils/finder.rb
|
|
380
381
|
- lib/utils/grepper.rb
|
|
@@ -391,6 +392,7 @@ files:
|
|
|
391
392
|
- lib/utils/probe/server_handling.rb
|
|
392
393
|
- lib/utils/ssh_tunnel_specification.rb
|
|
393
394
|
- lib/utils/version.rb
|
|
395
|
+
- lib/utils/xdg.rb
|
|
394
396
|
- lib/utils/xt/source_location_extension.rb
|
|
395
397
|
- tests/test_helper.rb
|
|
396
398
|
- tests/utils_test.rb
|
|
@@ -417,7 +419,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
417
419
|
- !ruby/object:Gem::Version
|
|
418
420
|
version: '0'
|
|
419
421
|
requirements: []
|
|
420
|
-
rubygems_version: 4.0.
|
|
422
|
+
rubygems_version: 4.0.3
|
|
421
423
|
specification_version: 4
|
|
422
424
|
summary: Some useful command line utilities
|
|
423
425
|
test_files:
|
data/lib/utils/config_dir.rb
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
require 'pathname'
|
|
2
|
-
require 'stringio'
|
|
3
|
-
|
|
4
|
-
module Utils
|
|
5
|
-
# A configuration directory manager that handles path resolution and file
|
|
6
|
-
# operations within a specified directory structure.
|
|
7
|
-
#
|
|
8
|
-
# This class provides functionality for managing configuration directories by
|
|
9
|
-
# deriving paths based on a root directory and name, and offering methods to
|
|
10
|
-
# read files with optional default values and block handling. It supports
|
|
11
|
-
# environment variable-based root path resolution and uses Pathname for
|
|
12
|
-
# robust path manipulation.
|
|
13
|
-
#
|
|
14
|
-
# @example
|
|
15
|
-
# config_dir = Utils::ConfigDir.new('myapp')
|
|
16
|
-
# config_dir.to_s # => returns the string representation of the configuration directory path
|
|
17
|
-
# config_dir.join('config.txt') # => returns a Pathname object for the joined path
|
|
18
|
-
# config_dir.read('settings.rb') # => reads and returns the content of 'settings.rb' or nil if not found
|
|
19
|
-
# config_dir.read('missing.txt', default: 'default content') # => returns 'default content' if file is missing
|
|
20
|
-
class ConfigDir
|
|
21
|
-
# Initializes a new ConfigDir instance with the specified name and optional
|
|
22
|
-
# root path or environment variable.
|
|
23
|
-
#
|
|
24
|
-
# @param name [ String ] the name of the directory to be used
|
|
25
|
-
# @param root_path [ String, nil ] the root path to use; if nil, the
|
|
26
|
-
# default root path is used
|
|
27
|
-
# @param env_var [ String, nil ] the name of the environment variable to
|
|
28
|
-
# check for the root path
|
|
29
|
-
def initialize(name, root_path: nil, env_var: nil)
|
|
30
|
-
root_path ||= env_var_path(env_var)
|
|
31
|
-
@directory_path = derive_directory_path(name, root_path)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# Returns the string representation of the configuration directory path.
|
|
35
|
-
#
|
|
36
|
-
# @return [ String ] the path of the configuration directory as a string
|
|
37
|
-
def to_s
|
|
38
|
-
@directory_path.to_s
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Joins the directory path with the given path and returns the combined
|
|
42
|
-
# result.
|
|
43
|
-
#
|
|
44
|
-
# @param path [ String ] the path to be joined with the directory path
|
|
45
|
-
#
|
|
46
|
-
# @return [ Pathname ] the combined path as a Pathname object
|
|
47
|
-
def join(path)
|
|
48
|
-
@directory_path + path
|
|
49
|
-
end
|
|
50
|
-
alias + join
|
|
51
|
-
|
|
52
|
-
# Reads the content of a file at the given path within the configuration
|
|
53
|
-
# directory.
|
|
54
|
-
#
|
|
55
|
-
# If the file exists, it returns the file's content as a string encoded in
|
|
56
|
-
# UTF-8. If a block is given and the file exists, it opens the file and
|
|
57
|
-
# yields to the block.
|
|
58
|
-
# If the file does not exist and a default value is provided, it returns
|
|
59
|
-
# the default. If a block is given and the file does not exist, it yields a
|
|
60
|
-
# StringIO object containing
|
|
61
|
-
# the default value to the block.
|
|
62
|
-
#
|
|
63
|
-
# @param path [ String ] the path to the file relative to the configuration
|
|
64
|
-
# directory
|
|
65
|
-
# @param default [ String, nil ] the default value to return if the file
|
|
66
|
-
# does not exist
|
|
67
|
-
#
|
|
68
|
-
# @yield [ io ]
|
|
69
|
-
#
|
|
70
|
-
# @return [ String, nil ] the content of the file or the default value if
|
|
71
|
-
# the file does not exist
|
|
72
|
-
def read(path, default: nil, &block)
|
|
73
|
-
full_path = join(path)
|
|
74
|
-
if File.exist?(full_path)
|
|
75
|
-
if block
|
|
76
|
-
File.new(full_path, &block)
|
|
77
|
-
else
|
|
78
|
-
File.read(full_path, encoding: 'UTF-8')
|
|
79
|
-
end
|
|
80
|
-
else
|
|
81
|
-
if default && block
|
|
82
|
-
block.(StringIO.new(default))
|
|
83
|
-
else
|
|
84
|
-
default
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
private
|
|
90
|
-
|
|
91
|
-
# Derives the full directory path by combining the root path and the given
|
|
92
|
-
# name.
|
|
93
|
-
#
|
|
94
|
-
# @param name [ String ] the name of the directory to be appended to the root path
|
|
95
|
-
# @param root_path [ String, nil ] the root path to use; if nil, the default root path is used
|
|
96
|
-
#
|
|
97
|
-
# @return [ Pathname ] the combined directory path as a Pathname object
|
|
98
|
-
def derive_directory_path(name, root_path)
|
|
99
|
-
root = if path = root_path
|
|
100
|
-
Pathname.new(path)
|
|
101
|
-
else
|
|
102
|
-
Pathname.new(default_root_path)
|
|
103
|
-
end
|
|
104
|
-
root + name
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
# Returns the environment variable path if it is set and not empty.
|
|
108
|
-
#
|
|
109
|
-
# @param env_var [ String ] the name of the environment variable to check
|
|
110
|
-
# @return [ String, nil ] the value of the environment variable if it
|
|
111
|
-
# exists and is not empty, otherwise nil
|
|
112
|
-
def env_var_path(env_var)
|
|
113
|
-
env_var.full? { ENV[it].full? }
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
# Returns the default configuration directory path based on the HOME
|
|
117
|
-
# environment variable.
|
|
118
|
-
#
|
|
119
|
-
# This method constructs and returns a Pathname object pointing to the
|
|
120
|
-
# standard configuration directory location, which is typically
|
|
121
|
-
# $HOME/.config.
|
|
122
|
-
#
|
|
123
|
-
# @return [ Pathname ] the default configuration directory path
|
|
124
|
-
def default_root_path
|
|
125
|
-
Pathname.new(ENV.fetch('HOME') + '.config')
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|