wile 0.9.261203

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.
Files changed (131) hide show
  1. checksums.yaml +7 -0
  2. data/.version +1 -0
  3. data/CREDITS.md +20 -0
  4. data/README.md +253 -0
  5. data/bin/wile +102 -0
  6. data/cache/.bin/pane +99 -0
  7. data/cache/.bin/panes +54 -0
  8. data/cache/.config.fish +2 -0
  9. data/cache/.dictionary +0 -0
  10. data/cache/.gemignore +16 -0
  11. data/cache/.gitignore +9 -0
  12. data/cache/.greeting.erb +1 -0
  13. data/cache/.irbrc +30 -0
  14. data/cache/.lexicon +0 -0
  15. data/cache/.prompt.erb +1 -0
  16. data/cache/.reek-strict.yml +1 -0
  17. data/cache/.reek.yml +1 -0
  18. data/cache/.rubocop-strict.yml +4 -0
  19. data/cache/.rubocop.yml +4 -0
  20. data/cache/.ruby-version-floor +1 -0
  21. data/cache/.ruby-version.erb +1 -0
  22. data/cache/.solargraph.yml +21 -0
  23. data/cache/.title.erb +1 -0
  24. data/cache/.version +1 -0
  25. data/cache/.wile/example.rb +56 -0
  26. data/cache/.wileignore +3 -0
  27. data/cache/CREDITS.md +10 -0
  28. data/cache/Gemfile +5 -0
  29. data/cache/Gemfile.lock +1 -0
  30. data/cache/History.txt +0 -0
  31. data/cache/README.md.erb +54 -0
  32. data/cache/TODO.md +3 -0
  33. data/cache/bin/project.erb +12 -0
  34. data/cache/cucumber.yml +1 -0
  35. data/cache/examples/README.md +3 -0
  36. data/cache/features/help.feature.erb +30 -0
  37. data/cache/features/step_definitions/command_steps.rb +171 -0
  38. data/cache/features/support/env.rb +11 -0
  39. data/cache/features/version.feature.erb +19 -0
  40. data/cache/git/hooks/pre-commit +93 -0
  41. data/cache/lib/project.rb.erb +4 -0
  42. data/cache/man/project.md.erb +24 -0
  43. data/cache/pkg/README.md +3 -0
  44. data/cache/test/cons.gz +0 -0
  45. data/cache/test/tc_comments +216 -0
  46. data/cache/test/tc_cucumber_slow +67 -0
  47. data/cache/test/tc_dependency_scrapes +126 -0
  48. data/cache/test/tc_dictionary +51 -0
  49. data/cache/test/tc_encoding +53 -0
  50. data/cache/test/tc_lexicon +34 -0
  51. data/cache/test/tc_lib_tests +37 -0
  52. data/cache/test/tc_lints +71 -0
  53. data/cache/test/tc_readme_rocket_check +128 -0
  54. data/cache/test/tc_reek +20 -0
  55. data/cache/test/tc_rubocop +20 -0
  56. data/cache/test/tc_ruby_syntax +28 -0
  57. data/cache/test/tc_tags +32 -0
  58. data/cache/test/tc_tasks_test +31 -0
  59. data/cache/test/tc_version +97 -0
  60. data/cache/test/tc_weird_filenames +23 -0
  61. data/cache/test/test_helper.rb +230 -0
  62. data/cache/tmp/README.md +8 -0
  63. data/data/command_version.tsv +281 -0
  64. data/dependencies.txt +70 -0
  65. data/lib/wile/autoloads/cache.rb +123 -0
  66. data/lib/wile/autoloads/code_content.rb +225 -0
  67. data/lib/wile/autoloads/code_data.rb +153 -0
  68. data/lib/wile/autoloads/code_data_function.rb +79 -0
  69. data/lib/wile/autoloads/code_data_valuation.rb +60 -0
  70. data/lib/wile/autoloads/code_stats.rb +185 -0
  71. data/lib/wile/autoloads/command_version.rb +119 -0
  72. data/lib/wile/autoloads/content.rb +196 -0
  73. data/lib/wile/autoloads/dependencies.rb +170 -0
  74. data/lib/wile/autoloads/gem_utils.rb +69 -0
  75. data/lib/wile/autoloads/gemspec.rb +163 -0
  76. data/lib/wile/autoloads/git.rb +59 -0
  77. data/lib/wile/autoloads/info.rb +91 -0
  78. data/lib/wile/autoloads/manpage.rb +44 -0
  79. data/lib/wile/autoloads/methods.rb +130 -0
  80. data/lib/wile/autoloads/nvim_files.rb +54 -0
  81. data/lib/wile/autoloads/options_dictionary.rb +345 -0
  82. data/lib/wile/autoloads/order_optimizer.rb +185 -0
  83. data/lib/wile/autoloads/project.rb +60 -0
  84. data/lib/wile/autoloads/readme.rb +137 -0
  85. data/lib/wile/autoloads/receivers.rb +101 -0
  86. data/lib/wile/autoloads/responds.rb +36 -0
  87. data/lib/wile/autoloads/test.rb +78 -0
  88. data/lib/wile/autoloads/thread_pool.rb +64 -0
  89. data/lib/wile/help.rb +41 -0
  90. data/lib/wile/requires.rb +7 -0
  91. data/lib/wile/run.rb +108 -0
  92. data/lib/wile/sysexits.rb +43 -0
  93. data/lib/wile/tasks/build.rb +46 -0
  94. data/lib/wile/tasks/bump.rb +61 -0
  95. data/lib/wile/tasks/compare.rb +139 -0
  96. data/lib/wile/tasks/content.rb +44 -0
  97. data/lib/wile/tasks/cp.rb +52 -0
  98. data/lib/wile/tasks/dependencies.rb +58 -0
  99. data/lib/wile/tasks/dictionary.rb +59 -0
  100. data/lib/wile/tasks/gemcheck.rb +142 -0
  101. data/lib/wile/tasks/gemfile.rb +60 -0
  102. data/lib/wile/tasks/help.rb +29 -0
  103. data/lib/wile/tasks/info.rb +77 -0
  104. data/lib/wile/tasks/install.rb +53 -0
  105. data/lib/wile/tasks/lexicon.rb +59 -0
  106. data/lib/wile/tasks/list.rb +97 -0
  107. data/lib/wile/tasks/methods.rb +154 -0
  108. data/lib/wile/tasks/mkdir.rb +19 -0
  109. data/lib/wile/tasks/note.rb +49 -0
  110. data/lib/wile/tasks/nvim.rb +60 -0
  111. data/lib/wile/tasks/order.rb +93 -0
  112. data/lib/wile/tasks/oscheck.rb +49 -0
  113. data/lib/wile/tasks/publish.rb +67 -0
  114. data/lib/wile/tasks/receivers.rb +34 -0
  115. data/lib/wile/tasks/review.rb +157 -0
  116. data/lib/wile/tasks/ruby.rb +38 -0
  117. data/lib/wile/tasks/tc.rb +114 -0
  118. data/lib/wile/tasks/test.rb +57 -0
  119. data/lib/wile/tasks/todo.rb +55 -0
  120. data/lib/wile/tasks/update.rb +67 -0
  121. data/lib/wile/tasks.rb +34 -0
  122. data/lib/wile/usage.rb +56 -0
  123. data/lib/wile/usage_error.rb +18 -0
  124. data/lib/wile/utilities.rb +283 -0
  125. data/lib/wile/utilities_refinements.rb +41 -0
  126. data/lib/wile/wile_refinements.rb +137 -0
  127. data/lib/wile.rb +39 -0
  128. data/man/wile.1 +1121 -0
  129. data/man/wile.md +786 -0
  130. data/pkg/README.md +3 -0
  131. metadata +399 -0
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Wile namespace.
4
+ module Wile
5
+ # CodeData class provides data about the code in a file.
6
+ class CodeData
7
+ attr_reader :data, :filename
8
+
9
+ def attrs = @meta[:attrs]
10
+ def constants = @meta[:constants]
11
+
12
+ def initialize(filename)
13
+ @filename = filename
14
+ @meta = { attrs: [], constants: [], index: {} }
15
+ @data = CodeData.build_data(filename)
16
+ Parser.new(self).populate
17
+ end
18
+
19
+ def self.build_data(filename)
20
+ CodeStats.method_stats(filename) do |stat|
21
+ { lines: stat, size: stat.size, category: stat.category,
22
+ dependencies: [], dependants: [], external: [], stdlib: [] }
23
+ end
24
+ end
25
+
26
+ def index = @meta[:index]
27
+
28
+ # category: index
29
+ def dependency_index(name, dependency) = index["#{name}-#{dependency}"]
30
+
31
+ # Number of lines in the method:
32
+ # category: table
33
+ def names = @data.keys
34
+ def size(name) = @data[name][:size]
35
+ # Set by category comment, private, or defaulted to public:
36
+ def category(name) = @data[name][:category]
37
+ def dependants(name) = @data[name][:dependants]
38
+ def dependencies(name) = @data[name][:dependencies]
39
+ def external(name) = @data[name][:external]
40
+ end
41
+
42
+ # Parses CodeContent output to populate dependencies and dependants.
43
+ # rubocop:disable-next Style/ClassAndModuleChildren
44
+ class CodeData::Parser
45
+ using WileRefinements
46
+
47
+ # pp is autoloaded:
48
+ KERNEL = [*Kernel.public_methods.map(&:to_s), 'pp'].freeze
49
+ DEF_METHOD = /^def\s(?<fullname>(?<prefix>[:\w]*)[.#]\S+)$/
50
+ METHOD_CALL = /^\s\s(?<name>\S+)$/
51
+ CASGN_CONST = /^casgn\s(?<const>[:\w]+)\s\w+$/
52
+ ATTR_METHOD =
53
+ /^#attr_(?:reader|writer|accessor)\s(?<attr>[:\w]+[.#]\w+=?)$/
54
+
55
+ private_constant :KERNEL, :DEF_METHOD, :METHOD_CALL, :CASGN_CONST,
56
+ :ATTR_METHOD
57
+
58
+ def initialize(code_data)
59
+ @code_data = code_data
60
+ @fullname = nil
61
+ @prefix = nil
62
+ @responds = nil
63
+ end
64
+
65
+ def populate
66
+ Responds.new do |responds|
67
+ @responds = responds
68
+ CodeContent.generate(@code_data.filename) { parse_content_line(it) }
69
+ end
70
+ populate_dependants
71
+ end
72
+
73
+ private
74
+
75
+ def populate_dependants
76
+ @code_data.names.permutation(2) do |name, other|
77
+ @code_data.dependants(name) << other if
78
+ @code_data.dependencies(other).include?(name)
79
+ end
80
+ end
81
+
82
+ def parse_content_line(line)
83
+ if (match = DEF_METHOD.match(line))
84
+ @fullname = match[:fullname]
85
+ @prefix = match[:prefix]
86
+ elsif (match = METHOD_CALL.match(line))
87
+ record_call(match[:name])
88
+ elsif (match = CASGN_CONST.match(line))
89
+ @code_data.constants << match[:const]
90
+ elsif (match = ATTR_METHOD.match(line))
91
+ @code_data.attrs << match[:attr]
92
+ end
93
+ end
94
+
95
+ def resolve_local_method(call_name)
96
+ candidates = if call_name == '.new' then ["#{@prefix}#initialize"]
97
+ else
98
+ ["#{@prefix}#{call_name}", "#{@prefix}::#{call_name}",
99
+ "#{@prefix.split('::').first}::#{call_name}", call_name]
100
+ end
101
+ candidates.find { @code_data.data.key?(it) }
102
+ end
103
+
104
+ def record_call(call_name)
105
+ if (dependency = resolve_local_method(call_name))
106
+ add_dependency(dependency, call_name)
107
+ else
108
+ add_external(call_name) unless known_constant?(call_name)
109
+ end
110
+ end
111
+
112
+ def add_dependency(dependency, call_name)
113
+ entry = @code_data.data[@fullname]
114
+ entry[:dependencies] << dependency
115
+ pattern = call_name.namex
116
+ @code_data.index["#{@fullname}-#{dependency}"] =
117
+ entry[:lines].index { pattern.match?(it) }
118
+ end
119
+
120
+ def known_constant?(call_name)
121
+ const = call_name.split('.').first
122
+ parts = @prefix.split('::')
123
+ parts.size.downto(0).any? do |len|
124
+ @code_data.constants.include?([*parts[0, len], const].join('::'))
125
+ end
126
+ end
127
+
128
+ def add_external(call_name)
129
+ if call_name.start_with?('.', '#')
130
+ add_scoped_external("#{@prefix}#{call_name}") unless
131
+ KERNEL.include?(call_name[1..])
132
+ else
133
+ classify_external(call_name)
134
+ end
135
+ end
136
+
137
+ def add_scoped_external(scoped_call)
138
+ unqualified = scoped_call.split('::').last
139
+ candidate = @responds.ok?(scoped_call) ? scoped_call : unqualified
140
+ classify_external(scoped_call, candidate)
141
+ end
142
+
143
+ def classify_external(call_name, candidate = call_name)
144
+ entry = @code_data.data[@fullname]
145
+ if @responds.ok?(candidate)
146
+ entry[:stdlib] << candidate
147
+ else
148
+ entry[:external] << call_name
149
+ end
150
+ end
151
+ end
152
+ CodeData.private_constant :Parser
153
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Wile namespace.
4
+ module Wile
5
+ # CodeDataFunction module provides functions to look at code data.
6
+ module CodeDataFunction
7
+ def self.dump(data)
8
+ puts 'Data:'.blue
9
+ puts JSON.pretty_generate data.data
10
+ puts 'Index:'.blue
11
+ puts JSON.pretty_generate data.index
12
+ end
13
+
14
+ def self.isolate(data)
15
+ data.names.each do |name|
16
+ puts name if data.dependencies(name).empty? &&
17
+ data.dependants(name).empty?
18
+ end
19
+ end
20
+
21
+ def self.total(data)
22
+ valuation = CodeDataValuation.new(data, trace: true)
23
+ puts valuation.total(data.names)
24
+ end
25
+
26
+ def self.qc(data)
27
+ unless data.names.map { qc_name?(it, data) }.all?
28
+ raise 'DATAERR: Quality check failed.'
29
+ end
30
+
31
+ puts 'OK'.green
32
+ end
33
+
34
+ def self.index(data)
35
+ data.names.each { print_dependencies(it, data) }
36
+ end
37
+
38
+ # category: private
39
+
40
+ def self.print_dependencies(name, data)
41
+ data.dependencies(name).each do |dep|
42
+ idx = data.dependency_index(name, dep)
43
+ puts "#{name}-#{dep}:\t#{idx}"
44
+ end
45
+ end
46
+
47
+ def self.qc_name?(name, data)
48
+ [
49
+ qc_circular?(name, data),
50
+ qc_dependencies?(name, data),
51
+ qc_dependants?(name, data)
52
+ ].all?
53
+ end
54
+
55
+ def self.qc_dependencies?(name, data)
56
+ qc_symmetric?(name, data.dependencies(name), '=>') { data.dependants(it) }
57
+ end
58
+
59
+ def self.qc_dependants?(name, data)
60
+ qc_symmetric?(name, data.dependants(name), '<=') { data.dependencies(it) }
61
+ end
62
+
63
+ def self.qc_symmetric?(name, targets, arrow)
64
+ missing = targets.reject { yield(it).include?(name) }
65
+ missing.each { warn "#{name} #{arrow} #{it}".red }.empty?
66
+ end
67
+
68
+ def self.qc_circular?(name, data)
69
+ circular = data.dependencies(name) & data.dependants(name)
70
+ return true if circular.empty?
71
+
72
+ warn "#{name}: #{circular.join(', ')}".red
73
+ false
74
+ end
75
+
76
+ private_class_method :print_dependencies, :qc_name?, :qc_symmetric?,
77
+ :qc_circular?, :qc_dependencies?, :qc_dependants?
78
+ end
79
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Wile namespace.
4
+ module Wile
5
+ # CodeDataValuation class sums up the total distance among dependencies.
6
+ class CodeDataValuation
7
+ # category: initialization
8
+ # :reek:BooleanParameter
9
+ def initialize(data, trace: false)
10
+ @data = data
11
+ @trace = trace
12
+ @sections = get_sections(data.names)
13
+ @order = nil
14
+ end
15
+
16
+ # category: public
17
+ def total(order)
18
+ # Reject if the sections are different
19
+ return Float::INFINITY unless @sections == get_sections(order)
20
+
21
+ # Sum alphabetical and dependency distances:
22
+ (@order = order).each_with_index.sum(alphabetical(order)) do |name, start|
23
+ dep_sum = dependencies_sum(name, start)
24
+ warn "#{name}: #{dep_sum}".gray if @trace
25
+ dep_sum
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def get_sections(order)
32
+ order.map { @data.category it }.chunk { it }.map(&:first)
33
+ end
34
+
35
+ def alphabetical(order)
36
+ sum = order.each_cons(2).count { |prev, curr| prev > curr }
37
+ warn "Alphabetical: #{sum}".gray if @trace
38
+ sum
39
+ end
40
+
41
+ def dependencies_sum(name, start)
42
+ @data.dependencies(name).sum do |dep|
43
+ dep_idx = @order.index(dep)
44
+ if dep_idx < start
45
+ distance_squared(dflipped(name, dep), dep_idx, start)
46
+ else
47
+ distance_squared(dinit(name, dep), start, dep_idx)
48
+ end
49
+ end
50
+ end
51
+
52
+ def dflipped(name, dep) = @data.dependency_index(name, dep)
53
+ def dinit(name, dep) = 1 + @data.size(name) - dflipped(name, dep)
54
+
55
+ def distance_squared(distance, start, stop)
56
+ distance += (start + 1).upto(stop - 1).sum { @data.size @order[it] }
57
+ distance * distance
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Wile namespace.
4
+ module Wile
5
+ # CodeStats supplements CodeContent with method source and line counts.
6
+ class CodeStats
7
+ # :reek:UtilityFunction
8
+ def method_stats(filename, &func)
9
+ Scanner.new(File.readlines(filename), func, ScopeTracker.new).scan
10
+ end
11
+
12
+ def self.method_line_count(filename) = new.method_stats(filename, &:size)
13
+
14
+ def self.method_source(filename) = new.method_stats(filename) { it }
15
+
16
+ def self.method_stats(filename, &) = new.method_stats(filename, &)
17
+
18
+ def self.source(source = [])
19
+ def source.category(cat = nil)
20
+ cat ? @category = cat : @category
21
+ end
22
+ source
23
+ end
24
+ end
25
+
26
+ # Tracks lexical scope, indentation levels, and method categories.
27
+ # rubocop:disable-next Style/ClassAndModuleChildren
28
+ class CodeStats::ScopeTracker
29
+ attr_reader :category
30
+
31
+ def initialize
32
+ @clevel = [-1]
33
+ @scope = []
34
+ @category = '.public'
35
+ end
36
+
37
+ def pop_to(length)
38
+ while @clevel.last >= length
39
+ @clevel.pop
40
+ @scope.pop
41
+ end
42
+ end
43
+
44
+ def push(pad, scope)
45
+ @clevel.push(pad)
46
+ @scope.push(scope)
47
+ @category = "#{scope_str}.public" unless scope == '.'
48
+ end
49
+
50
+ def scope_str = @scope.reject { it == '.' }.join('::')
51
+
52
+ def update_category(cat) = @category = "#{scope_str}.#{cat.split.last}"
53
+
54
+ def method_name(rcvr, name)
55
+ dot = @scope.last == '.' || rcvr ? '.' : '#'
56
+ "#{def_scope(rcvr)}#{dot}#{name}"
57
+ end
58
+
59
+ private
60
+
61
+ def def_scope(rcvr)
62
+ return scope_str unless rcvr && rcvr != 'self.'
63
+
64
+ named = rcvr.delete_suffix('.')
65
+ return scope_str if scope_str.split('::').last == named
66
+ return named if scope_str.empty? || named.start_with?(scope_str)
67
+
68
+ "#{scope_str}::#{named}"
69
+ end
70
+ end
71
+
72
+ # Scans Ruby source lines to collect per-method source arrays and categories.
73
+ # rubocop:disable-next Style/ClassAndModuleChildren
74
+ class CodeStats::Scanner
75
+ MATCHERS = {
76
+ /^(?<pad>\s*)
77
+ (?:(?:module|class|refine)\s+(?<scope>(?:::)?\w+(?:::\w+)*)
78
+ (?<sclass>\.singleton_class)?|
79
+ (?<sclass>class\s*<<\s*self))/x => :module_name,
80
+ %r{^(?<pad>\s*)def\s+
81
+ (?<rcvr>(?:self|[A-Z]\w*(?:::[A-Z]\w*)*)\.)?
82
+ (?<name>\[\]=?|<=>|===?|!=|[!=]~|<<|>>|\*\*|[+\-!~]@|
83
+ [<>]=?|[+\-*/%&|^~]|\w+[!=?]?)}x => :method_name,
84
+ /^(?<pad>\s*)end$/ => :scope_end,
85
+ /^\s*(?<cat>private|public|protected|# category: \w+)$/ => :category_set,
86
+ /^(?<pad>\s*)(?:(?<scope>[A-Z]\w*)\s*=\s*(?:Struct\.new|Data\.define)\b|
87
+ [A-Z]\w*\s*=[^=~<>])/x => :casgn
88
+ }.freeze
89
+ ENDLESS_DEF = /\A\s*def\s+[^\s(]+(?:\([^)]*\))?\s+=\s/
90
+ private_constant :MATCHERS, :ENDLESS_DEF
91
+
92
+ def initialize(lines, func, tracker)
93
+ @lines = lines
94
+ @func = func
95
+ @tracker = tracker
96
+ @state = { hash: {}, dlevel: 0, name: nil, line: nil,
97
+ source: CodeStats.source }
98
+ end
99
+
100
+ def scan
101
+ msh_populate while lines_shift
102
+ @state[:hash]
103
+ end
104
+
105
+ private
106
+
107
+ def method_name(match)
108
+ rcvr = match[:rcvr]
109
+ return if !rcvr && /^\s*def\s+\S+\./.match?(@state[:line])
110
+
111
+ name = @tracker.method_name(rcvr, match[:name])
112
+ source = CodeStats.source(@state[:source][-1..])
113
+ store_method(name, match[:pad].length, source)
114
+ end
115
+
116
+ def reset_method
117
+ @state[:dlevel] = 0
118
+ @state[:name] = nil
119
+ @state[:source] = CodeStats.source
120
+ end
121
+
122
+ def store_method(name, dlevel, source)
123
+ source.category(@tracker.category)
124
+ @state.merge!(dlevel:, name:, source:)
125
+ @state[:hash][name] = @func[source]
126
+ reset_method if ENDLESS_DEF.match?(@state[:line])
127
+ end
128
+
129
+ def msh_populate
130
+ MATCHERS.each do |pattern, handler|
131
+ next unless (match = pattern.match(@state[:line]))
132
+
133
+ return send(handler, match)
134
+ end
135
+ end
136
+
137
+ def lines_shift
138
+ return unless (line = @lines.shift)
139
+
140
+ @state[:line] = line
141
+ @state[:source] << line
142
+ end
143
+
144
+ def finish_method
145
+ src = @state[:source]
146
+ src.pop
147
+ src.category(@tracker.category)
148
+ @state[:hash][@state[:name]] = @func[src]
149
+ reset_method
150
+ end
151
+
152
+ def casgn(match, length = match[:pad].length)
153
+ if (scope = match[:scope])
154
+ lines_shift until /\bdo\s*$|\)\s*$/.match?(@state[:line]) ||
155
+ @lines.empty?
156
+ push_struct_scope(length, scope)
157
+ else
158
+ lines_shift while @lines[0]&.match?(/\A(?:\s*$|\s{#{length + 1}})/)
159
+ end
160
+ end
161
+
162
+ def push_struct_scope(length, scope)
163
+ @tracker.push(length, scope) if /\bdo\s*$/.match?(@state[:line])
164
+ end
165
+
166
+ def scope_end(match)
167
+ length = match[:pad].length
168
+ if @state[:name] && @state[:dlevel] == length
169
+ finish_method
170
+ else
171
+ @tracker.pop_to(length)
172
+ end
173
+ end
174
+
175
+ def category_set(match) = @tracker.update_category(match[:cat])
176
+
177
+ def module_name(match)
178
+ pad, scope, sclass = match.values_at(:pad, :scope, :sclass)
179
+ len = pad.length
180
+ @tracker.push(len, scope) if scope
181
+ @tracker.push(len, '.') if sclass
182
+ end
183
+ end
184
+ CodeStats.private_constant :ScopeTracker, :Scanner
185
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ # wile: os `man`
4
+
5
+ # Wile namespace.
6
+ module Wile
7
+ # This class knows how to get the version of a command.
8
+ # :reek:MissingSafeMethod
9
+ class CommandVersion
10
+ include Enumerable
11
+
12
+ DEFAULT_FILE = File.join(ROOT, 'data/command_version.tsv')
13
+
14
+ def fish_plugin(plugin) = @probe.fish_plugin(plugin)
15
+
16
+ def initialize(filename = DEFAULT_FILE, probe: Probe)
17
+ @command_version = probe.parse_tsv(filename)
18
+ @probe = probe
19
+ end
20
+
21
+ def key?(command) = @command_version.key?(command)
22
+
23
+ def size = @command_version.size
24
+ alias length size
25
+
26
+ def commands = @command_version.keys
27
+
28
+ def each(&) = @command_version.each(&)
29
+
30
+ def empty? = @command_version.empty?
31
+
32
+ def expected!(command)
33
+ self[command] || raise("Failed to determine version for #{command}")
34
+ end
35
+
36
+ def guessing_flag(command) = @probe.guessing_flag(command)
37
+
38
+ def version(command) = self[command] || guess(command)
39
+
40
+ def guess(command)
41
+ guessing_flag(command) || man(command)
42
+ rescue StandardError
43
+ nil
44
+ end
45
+
46
+ def man(cmd) = @probe.man(cmd)
47
+
48
+ def [](command)
49
+ return nil unless (probe = @command_version[command])
50
+
51
+ probing(probe, command)
52
+ rescue StandardError
53
+ nil
54
+ end
55
+
56
+ private
57
+
58
+ def probing(probe, command)
59
+ case probe
60
+ when String then @probe.probing_string(probe) || man(command)
61
+ when Symbol then send(probe, command)
62
+ end
63
+ end
64
+ end
65
+
66
+ # Stateless helpers for parsing and probing command versions.
67
+ # rubocop:disable-next Style/ClassAndModuleChildren
68
+ class CommandVersion::Probe
69
+ using UtilitiesRefinements
70
+
71
+ def self.fish_plugin(plugin)
72
+ path = File.expand_path('~/.config/fish/fish_plugins')
73
+ return nil unless File.file?(path)
74
+
75
+ name, ver = File.foreach(path, chomp: true)
76
+ .map { it.split('@') }
77
+ .assoc(plugin)
78
+ name && (ver&.slice(/\d+(\.\d+)*/) || '0')
79
+ end
80
+
81
+ def self.man(cmd)
82
+ return nil unless (last_line = `man #{cmd.sw} 2>/dev/null`.lines.last)
83
+
84
+ scrape(last_line)
85
+ end
86
+
87
+ def self.scrape(str)
88
+ return nil if str.start_with?('sh:')
89
+
90
+ str[/\d+\.\d+/] || str[/[12]\d\d\d/] || str[/\d+/]
91
+ end
92
+
93
+ def self.probing_string(probe)
94
+ return nil unless (first_line = `#{probe} 2>&1`.strip.lines.first)
95
+
96
+ scrape(first_line)
97
+ end
98
+
99
+ def self.guessing_flag(command)
100
+ %w[--version -v -V].each do |flag|
101
+ version = probing_string("#{command.sw} #{flag}")
102
+ return version if version
103
+ end
104
+ nil
105
+ end
106
+
107
+ def self.parse_probe(probe)
108
+ probe.start_with?(':') ? probe[1..].to_sym : probe
109
+ end
110
+
111
+ def self.parse_tsv(filename)
112
+ File.foreach(filename, chomp: true).to_h do |line|
113
+ cmd, probe = line.split("\t")
114
+ [cmd, parse_probe(probe)]
115
+ end.freeze
116
+ end
117
+ end
118
+ CommandVersion.private_constant :Probe
119
+ end