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.
- checksums.yaml +7 -0
- data/.version +1 -0
- data/CREDITS.md +20 -0
- data/README.md +253 -0
- data/bin/wile +102 -0
- data/cache/.bin/pane +99 -0
- data/cache/.bin/panes +54 -0
- data/cache/.config.fish +2 -0
- data/cache/.dictionary +0 -0
- data/cache/.gemignore +16 -0
- data/cache/.gitignore +9 -0
- data/cache/.greeting.erb +1 -0
- data/cache/.irbrc +30 -0
- data/cache/.lexicon +0 -0
- data/cache/.prompt.erb +1 -0
- data/cache/.reek-strict.yml +1 -0
- data/cache/.reek.yml +1 -0
- data/cache/.rubocop-strict.yml +4 -0
- data/cache/.rubocop.yml +4 -0
- data/cache/.ruby-version-floor +1 -0
- data/cache/.ruby-version.erb +1 -0
- data/cache/.solargraph.yml +21 -0
- data/cache/.title.erb +1 -0
- data/cache/.version +1 -0
- data/cache/.wile/example.rb +56 -0
- data/cache/.wileignore +3 -0
- data/cache/CREDITS.md +10 -0
- data/cache/Gemfile +5 -0
- data/cache/Gemfile.lock +1 -0
- data/cache/History.txt +0 -0
- data/cache/README.md.erb +54 -0
- data/cache/TODO.md +3 -0
- data/cache/bin/project.erb +12 -0
- data/cache/cucumber.yml +1 -0
- data/cache/examples/README.md +3 -0
- data/cache/features/help.feature.erb +30 -0
- data/cache/features/step_definitions/command_steps.rb +171 -0
- data/cache/features/support/env.rb +11 -0
- data/cache/features/version.feature.erb +19 -0
- data/cache/git/hooks/pre-commit +93 -0
- data/cache/lib/project.rb.erb +4 -0
- data/cache/man/project.md.erb +24 -0
- data/cache/pkg/README.md +3 -0
- data/cache/test/cons.gz +0 -0
- data/cache/test/tc_comments +216 -0
- data/cache/test/tc_cucumber_slow +67 -0
- data/cache/test/tc_dependency_scrapes +126 -0
- data/cache/test/tc_dictionary +51 -0
- data/cache/test/tc_encoding +53 -0
- data/cache/test/tc_lexicon +34 -0
- data/cache/test/tc_lib_tests +37 -0
- data/cache/test/tc_lints +71 -0
- data/cache/test/tc_readme_rocket_check +128 -0
- data/cache/test/tc_reek +20 -0
- data/cache/test/tc_rubocop +20 -0
- data/cache/test/tc_ruby_syntax +28 -0
- data/cache/test/tc_tags +32 -0
- data/cache/test/tc_tasks_test +31 -0
- data/cache/test/tc_version +97 -0
- data/cache/test/tc_weird_filenames +23 -0
- data/cache/test/test_helper.rb +230 -0
- data/cache/tmp/README.md +8 -0
- data/data/command_version.tsv +281 -0
- data/dependencies.txt +70 -0
- data/lib/wile/autoloads/cache.rb +123 -0
- data/lib/wile/autoloads/code_content.rb +225 -0
- data/lib/wile/autoloads/code_data.rb +153 -0
- data/lib/wile/autoloads/code_data_function.rb +79 -0
- data/lib/wile/autoloads/code_data_valuation.rb +60 -0
- data/lib/wile/autoloads/code_stats.rb +185 -0
- data/lib/wile/autoloads/command_version.rb +119 -0
- data/lib/wile/autoloads/content.rb +196 -0
- data/lib/wile/autoloads/dependencies.rb +170 -0
- data/lib/wile/autoloads/gem_utils.rb +69 -0
- data/lib/wile/autoloads/gemspec.rb +163 -0
- data/lib/wile/autoloads/git.rb +59 -0
- data/lib/wile/autoloads/info.rb +91 -0
- data/lib/wile/autoloads/manpage.rb +44 -0
- data/lib/wile/autoloads/methods.rb +130 -0
- data/lib/wile/autoloads/nvim_files.rb +54 -0
- data/lib/wile/autoloads/options_dictionary.rb +345 -0
- data/lib/wile/autoloads/order_optimizer.rb +185 -0
- data/lib/wile/autoloads/project.rb +60 -0
- data/lib/wile/autoloads/readme.rb +137 -0
- data/lib/wile/autoloads/receivers.rb +101 -0
- data/lib/wile/autoloads/responds.rb +36 -0
- data/lib/wile/autoloads/test.rb +78 -0
- data/lib/wile/autoloads/thread_pool.rb +64 -0
- data/lib/wile/help.rb +41 -0
- data/lib/wile/requires.rb +7 -0
- data/lib/wile/run.rb +108 -0
- data/lib/wile/sysexits.rb +43 -0
- data/lib/wile/tasks/build.rb +46 -0
- data/lib/wile/tasks/bump.rb +61 -0
- data/lib/wile/tasks/compare.rb +139 -0
- data/lib/wile/tasks/content.rb +44 -0
- data/lib/wile/tasks/cp.rb +52 -0
- data/lib/wile/tasks/dependencies.rb +58 -0
- data/lib/wile/tasks/dictionary.rb +59 -0
- data/lib/wile/tasks/gemcheck.rb +142 -0
- data/lib/wile/tasks/gemfile.rb +60 -0
- data/lib/wile/tasks/help.rb +29 -0
- data/lib/wile/tasks/info.rb +77 -0
- data/lib/wile/tasks/install.rb +53 -0
- data/lib/wile/tasks/lexicon.rb +59 -0
- data/lib/wile/tasks/list.rb +97 -0
- data/lib/wile/tasks/methods.rb +154 -0
- data/lib/wile/tasks/mkdir.rb +19 -0
- data/lib/wile/tasks/note.rb +49 -0
- data/lib/wile/tasks/nvim.rb +60 -0
- data/lib/wile/tasks/order.rb +93 -0
- data/lib/wile/tasks/oscheck.rb +49 -0
- data/lib/wile/tasks/publish.rb +67 -0
- data/lib/wile/tasks/receivers.rb +34 -0
- data/lib/wile/tasks/review.rb +157 -0
- data/lib/wile/tasks/ruby.rb +38 -0
- data/lib/wile/tasks/tc.rb +114 -0
- data/lib/wile/tasks/test.rb +57 -0
- data/lib/wile/tasks/todo.rb +55 -0
- data/lib/wile/tasks/update.rb +67 -0
- data/lib/wile/tasks.rb +34 -0
- data/lib/wile/usage.rb +56 -0
- data/lib/wile/usage_error.rb +18 -0
- data/lib/wile/utilities.rb +283 -0
- data/lib/wile/utilities_refinements.rb +41 -0
- data/lib/wile/wile_refinements.rb +137 -0
- data/lib/wile.rb +39 -0
- data/man/wile.1 +1121 -0
- data/man/wile.md +786 -0
- data/pkg/README.md +3 -0
- metadata +399 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Optimizes the order of elements using a caller-supplied valuation block.
|
|
6
|
+
class OrderOptimizer
|
|
7
|
+
SORTS = %i[bcmv nswp cswp bnswp bcswp bprm].freeze
|
|
8
|
+
PERM_WIDTH = 7
|
|
9
|
+
|
|
10
|
+
def initialize(order, sort: :bcmv, &valuation)
|
|
11
|
+
raise 'Invalid sort method' unless SORTS.include?(sort)
|
|
12
|
+
|
|
13
|
+
@state = State.new(order, valuation)
|
|
14
|
+
@sort = sort
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_a = @state.to_a
|
|
18
|
+
|
|
19
|
+
def self.elapsed_since(start) = Time.now - start
|
|
20
|
+
|
|
21
|
+
def optimize
|
|
22
|
+
start = Time.now
|
|
23
|
+
warn "# Start: #{padded_value}\tSort: #{@sort}\tN: #{@state.size}".gray
|
|
24
|
+
method(@sort).call
|
|
25
|
+
elapsed = OrderOptimizer.elapsed_since(start)
|
|
26
|
+
warn "# End: #{padded_value}\tTime: #{elapsed}".gray
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
# category: sorts
|
|
32
|
+
|
|
33
|
+
# Neighbor swaps
|
|
34
|
+
def nswp
|
|
35
|
+
pairs = @state.neighbor_pairs
|
|
36
|
+
seq = pairs + pairs[0...-1].reverse
|
|
37
|
+
repeat_improved { seq.each { @state.try_swap(it) } }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Best permutation
|
|
41
|
+
def bprm
|
|
42
|
+
size = @state.size
|
|
43
|
+
width = [size, PERM_WIDTH].min
|
|
44
|
+
loop { break unless sweep_perm(size - width, width) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Best combination move
|
|
48
|
+
def bcmv
|
|
49
|
+
pairs = @state.indices.permutation(2)
|
|
50
|
+
repeat_best(pairs, @state.method(:move_score)) do |best, min|
|
|
51
|
+
@state.apply_move(best, min)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Combination swaps
|
|
56
|
+
def cswp
|
|
57
|
+
pairs = @state.indices.combination(2)
|
|
58
|
+
repeat_improved { pairs.each { @state.try_swap(it) } }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Best combination swaps
|
|
62
|
+
def bcswp
|
|
63
|
+
pairs = @state.indices.combination(2)
|
|
64
|
+
repeat_best(pairs, @state.method(:swap_score)) do |best, min|
|
|
65
|
+
@state.apply_swap(best, min)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Best neighbor swaps
|
|
70
|
+
def bnswp
|
|
71
|
+
repeat_best(@state.neighbor_pairs,
|
|
72
|
+
@state.method(:swap_score)) do |best, min|
|
|
73
|
+
@state.apply_swap(best, min)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# category: helpers
|
|
78
|
+
|
|
79
|
+
def repeat_improved(&) = loop { break unless @state.improved?(&) }
|
|
80
|
+
|
|
81
|
+
def padded_value = @state.value.to_s.rjust(8)
|
|
82
|
+
|
|
83
|
+
def sweep_perm(last, width)
|
|
84
|
+
[0.upto(last), (last - 1).downto(0)].all? do |range|
|
|
85
|
+
@state.improved? { range.each { @state.permute_slice(it, width) } }
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def repeat_best(pairs, scorer)
|
|
90
|
+
loop do
|
|
91
|
+
best, min = best_pair(pairs, scorer)
|
|
92
|
+
break unless best
|
|
93
|
+
|
|
94
|
+
yield(best, min)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def best_pair(pairs, scorer)
|
|
99
|
+
best, min = pairs.map { [it, scorer.call(it)] }.min_by(&:last)
|
|
100
|
+
best && min < @state.value ? [best, min] : nil
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Holds mutable ordering state and primitive move/swap/permutation operations.
|
|
105
|
+
# rubocop:disable-next Style/ClassAndModuleChildren
|
|
106
|
+
class OrderOptimizer::State
|
|
107
|
+
attr_reader :value
|
|
108
|
+
|
|
109
|
+
def improved?
|
|
110
|
+
prev = @value
|
|
111
|
+
yield
|
|
112
|
+
@value < prev
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def indices = (0...@order.size).to_a
|
|
116
|
+
|
|
117
|
+
def initialize(order, valuation)
|
|
118
|
+
@order = order
|
|
119
|
+
@valuation = valuation
|
|
120
|
+
@value = @valuation.call(@order)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def move_score(pair)
|
|
124
|
+
from, dest = pair
|
|
125
|
+
@order.insert(dest, @order.delete_at(from))
|
|
126
|
+
val = @valuation.call(@order)
|
|
127
|
+
@order.insert(from, @order.delete_at(dest))
|
|
128
|
+
val
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def neighbor_pairs = (0..(@order.size - 2)).map { [it, it + 1] }
|
|
132
|
+
|
|
133
|
+
def size = @order.size
|
|
134
|
+
|
|
135
|
+
def to_a = @order
|
|
136
|
+
|
|
137
|
+
def apply_move(pair, val)
|
|
138
|
+
from, dest = pair
|
|
139
|
+
@order.insert(dest, @order.delete_at(from))
|
|
140
|
+
@value = val
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def try_swap(pair)
|
|
144
|
+
val = swap_score(pair)
|
|
145
|
+
apply_swap(pair, val) if val < @value
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def swap_score(pair)
|
|
149
|
+
items = @order.values_at(*pair)
|
|
150
|
+
swap_at(pair, items.reverse)
|
|
151
|
+
val = @valuation.call(@order)
|
|
152
|
+
swap_at(pair, items)
|
|
153
|
+
val
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def apply_swap(pair, val)
|
|
157
|
+
swap_at(pair, @order.values_at(*pair).reverse)
|
|
158
|
+
@value = val
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def permute_slice(start, width)
|
|
162
|
+
slice = @order[start, width]
|
|
163
|
+
best = slice
|
|
164
|
+
slice.permutation { best = it if better_slice?(start, width, it) }
|
|
165
|
+
@order[start, width] = best
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private
|
|
169
|
+
|
|
170
|
+
def swap_at(pair, items)
|
|
171
|
+
first, second = pair
|
|
172
|
+
@order[first], @order[second] = items
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def better_slice?(start, width, cand)
|
|
176
|
+
@order[start, width] = cand
|
|
177
|
+
val = @valuation.call(@order)
|
|
178
|
+
return false unless val < @value
|
|
179
|
+
|
|
180
|
+
@value = val
|
|
181
|
+
true
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
OrderOptimizer.private_constant :State
|
|
185
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# wile: os `md5sum`
|
|
4
|
+
|
|
5
|
+
# Wile namespace.
|
|
6
|
+
module Wile
|
|
7
|
+
# Project module provides methods to manage project files.
|
|
8
|
+
module Project
|
|
9
|
+
using UtilitiesRefinements
|
|
10
|
+
|
|
11
|
+
def self.archive(pkg)
|
|
12
|
+
pkg.weird_filename!
|
|
13
|
+
raise "Package not found: #{pkg}" unless File.file?(pkg)
|
|
14
|
+
|
|
15
|
+
version = pkg.scan(/\d+\.\d+\.\d+/).first
|
|
16
|
+
raise "Cannot extract version from: #{pkg}" unless version
|
|
17
|
+
|
|
18
|
+
record_history(version, pkg)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.record_history(version, pkg)
|
|
22
|
+
FileUtils.mkdir_p('pkg')
|
|
23
|
+
File.rename(pkg, "pkg/#{pkg}")
|
|
24
|
+
File.open('History.txt', 'a') do |file|
|
|
25
|
+
file.puts "# #{version} / #{Time.now}"
|
|
26
|
+
file.puts `md5sum pkg/#{pkg.sw}`
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
private_class_method :record_history
|
|
30
|
+
|
|
31
|
+
def self.files(dir)
|
|
32
|
+
prefix = "#{dir.delete_prefix('./').delete_suffix('/')}/"
|
|
33
|
+
find(dir).map { it.delete_prefix(prefix) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.find(dir)
|
|
37
|
+
files = Git.files
|
|
38
|
+
return files if %w[. ./].include?(dir)
|
|
39
|
+
|
|
40
|
+
prefix = "#{dir.delete_prefix('./').delete_suffix('/')}/"
|
|
41
|
+
files.select { it.start_with?(prefix) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.pfiles = Git.files
|
|
45
|
+
|
|
46
|
+
def self.bang_ruby?(filename)
|
|
47
|
+
return false unless File.file?(filename) &&
|
|
48
|
+
!File.empty?(filename) &&
|
|
49
|
+
File.executable?(filename) &&
|
|
50
|
+
!/\.erb$/.match?(filename)
|
|
51
|
+
|
|
52
|
+
first_line = File.read(filename, 80).lines.first
|
|
53
|
+
first_line ? /^#!.*\bruby\b/.match?(first_line.rstrip) : false
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.rbfiles
|
|
57
|
+
Git.files.select { /\.rb$/.match?(it) || bang_ruby?(it) }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Readme class provides methods to read and update the README file.
|
|
6
|
+
class Readme
|
|
7
|
+
using WileRefinements
|
|
8
|
+
|
|
9
|
+
attr_reader :info, :path
|
|
10
|
+
|
|
11
|
+
alias readme path
|
|
12
|
+
|
|
13
|
+
def initialize(info = Info.info)
|
|
14
|
+
@info = info
|
|
15
|
+
@path = info.readme
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
DESCRIPTION = /\n#+\s*DESCRIPTION[^\n]*(.*?)\n#/mi
|
|
19
|
+
private_constant :DESCRIPTION
|
|
20
|
+
|
|
21
|
+
def description
|
|
22
|
+
@description ||= File.match!(@path, DESCRIPTION)[1].strip
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def summary
|
|
26
|
+
@summary ||= description.split("\n\n").first.strip
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def update!
|
|
30
|
+
File.write(@path, update)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def update
|
|
34
|
+
lines = File.readlines(@path)
|
|
35
|
+
update_version(lines)
|
|
36
|
+
update_copyright(lines)
|
|
37
|
+
update_console(lines).join
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def update_console(lines)
|
|
43
|
+
project = @info.project
|
|
44
|
+
return lines unless File.exist?("bin/#{project}")
|
|
45
|
+
|
|
46
|
+
ConsoleBlocks.new(lines, project).update
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def update_version(lines)
|
|
50
|
+
index = lines.index { /\d+\.\d+\.\d+/.match? it }
|
|
51
|
+
if index
|
|
52
|
+
lines[index] = lines[index].sub(/\d+\.\d+\.\d+/, @info.version)
|
|
53
|
+
else
|
|
54
|
+
warn 'Warning: README does not have VERSION'.yellow
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def update_copyright(lines)
|
|
59
|
+
index = lines.index { /^Copyright\s\D+\d\d\d\d\s/.match? it }
|
|
60
|
+
if index
|
|
61
|
+
lines[index] = "Copyright (c) #{@info.year} #{@info.username}\n"
|
|
62
|
+
else
|
|
63
|
+
warn 'Warning: README does not have Copyright'.yellow
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Helper class to update active console blocks in README lines.
|
|
69
|
+
# rubocop:disable-next Style/ClassAndModuleChildren
|
|
70
|
+
class Readme::ConsoleBlocks
|
|
71
|
+
def initialize(lines, project)
|
|
72
|
+
@lines = lines
|
|
73
|
+
@project = project
|
|
74
|
+
@updated = []
|
|
75
|
+
@found = false
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def update
|
|
79
|
+
process_lines
|
|
80
|
+
warn_missing unless @found
|
|
81
|
+
@updated
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.run_console(cmd)
|
|
85
|
+
output = `./bin/#{cmd}`
|
|
86
|
+
output.end_with?("\n") ? output : "#{output}\n"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def warn_missing
|
|
92
|
+
warn 'Warning: README does not have an active console output'.yellow
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def process_lines
|
|
96
|
+
index = 0
|
|
97
|
+
index = append_line(index) while index < @lines.length
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def replace_block(cmd_index)
|
|
101
|
+
cmd = @lines[cmd_index].chomp[2..-5]
|
|
102
|
+
@updated << "$ #{cmd} #>>\n"
|
|
103
|
+
@updated << self.class.run_console(cmd)
|
|
104
|
+
skip_to_fence(cmd_index + 1)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def update_block(cmd_index)
|
|
108
|
+
@found = true
|
|
109
|
+
replace_block(cmd_index)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def append_line(index)
|
|
113
|
+
@updated << @lines[index]
|
|
114
|
+
next_index = index + 1
|
|
115
|
+
console_start?(index) ? update_block(next_index) : next_index
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def skip_to_fence(index)
|
|
119
|
+
index += 1 until fence?(index)
|
|
120
|
+
index
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def fence?(index)
|
|
124
|
+
@lines[index]&.start_with?('```') || index >= @lines.length
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def console_start?(index)
|
|
128
|
+
@lines[index]&.chomp == '```console' &&
|
|
129
|
+
target_cmd?(@lines[index + 1])
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def target_cmd?(line)
|
|
133
|
+
line&.start_with?("$ #{@project} ") && line.chomp.end_with?(' #>>')
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
Readme.private_constant :ConsoleBlocks
|
|
137
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Scans Ruby source files with Prism for variable and constant assignments
|
|
6
|
+
# whose RHS resolves to a known built-in receiver type.
|
|
7
|
+
class Receivers
|
|
8
|
+
TYPES = {
|
|
9
|
+
Array: [[Prism::ArrayNode], %i[new []]],
|
|
10
|
+
Float: [[Prism::FloatNode], []],
|
|
11
|
+
Hash: [[Prism::HashNode], %i[new []]],
|
|
12
|
+
Integer: [[Prism::IntegerNode], []],
|
|
13
|
+
Set: [[], %i[new []]],
|
|
14
|
+
String: [[Prism::StringNode, Prism::InterpolatedStringNode], %i[new]],
|
|
15
|
+
Time: [[], %i[new now at utc gm local mktime parse strptime iso8601]]
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
WRITE_NODES = [
|
|
19
|
+
Prism::LocalVariableWriteNode, Prism::LocalVariableOrWriteNode,
|
|
20
|
+
Prism::InstanceVariableWriteNode, Prism::InstanceVariableOrWriteNode,
|
|
21
|
+
Prism::ClassVariableWriteNode, Prism::ClassVariableOrWriteNode,
|
|
22
|
+
Prism::GlobalVariableWriteNode, Prism::GlobalVariableOrWriteNode,
|
|
23
|
+
Prism::ConstantWriteNode, Prism::ConstantOrWriteNode
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
PATH_NODES = [
|
|
27
|
+
Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode
|
|
28
|
+
].freeze
|
|
29
|
+
|
|
30
|
+
private_constant :WRITE_NODES, :PATH_NODES
|
|
31
|
+
|
|
32
|
+
def initialize(files = Dir.glob('lib/**/*.rb'))
|
|
33
|
+
@files = files
|
|
34
|
+
@found = TYPES.keys.to_h { [it, []] }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.const_match?(node, type)
|
|
38
|
+
return false unless node.is_a?(Prism::ConstantReadNode) ||
|
|
39
|
+
(node.is_a?(Prism::ConstantPathNode) && !node.parent)
|
|
40
|
+
|
|
41
|
+
node.name == type
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.ctor_call?(node, type, ctors)
|
|
45
|
+
return false unless node.is_a?(Prism::CallNode)
|
|
46
|
+
|
|
47
|
+
recv = node.receiver
|
|
48
|
+
name = node.name
|
|
49
|
+
return name == type unless recv
|
|
50
|
+
|
|
51
|
+
const_match?(recv, type) && ctors.include?(name)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.unwrap_freeze(node)
|
|
55
|
+
return node unless node.is_a?(Prism::CallNode) && node.name == :freeze
|
|
56
|
+
|
|
57
|
+
node.receiver || node
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.classify(node)
|
|
61
|
+
value = unwrap_freeze(node)
|
|
62
|
+
TYPES.each do |type, (ast_classes, ctors)|
|
|
63
|
+
return type if ast_classes.any? { value.is_a?(it) } ||
|
|
64
|
+
ctor_call?(value, type, ctors)
|
|
65
|
+
end
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.write_name(node)
|
|
70
|
+
case node
|
|
71
|
+
when *WRITE_NODES then node.name.to_s
|
|
72
|
+
when *PATH_NODES then node.target.name.to_s
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def scan
|
|
77
|
+
@files.each { walk(Prism.parse_file(it).value) }
|
|
78
|
+
@found.transform_values { it.uniq.sort }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def scan_source(source)
|
|
82
|
+
walk(Prism.parse(source).value)
|
|
83
|
+
@found.transform_values { it.uniq.sort }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def walk(node)
|
|
89
|
+
record(node)
|
|
90
|
+
node.compact_child_nodes.each { walk(it) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def record(node)
|
|
94
|
+
name = Receivers.write_name(node)
|
|
95
|
+
return unless name
|
|
96
|
+
|
|
97
|
+
type = Receivers.classify(node.value)
|
|
98
|
+
@found[type] << name if type
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Responds class provides a way to check if a class responds to a method
|
|
6
|
+
# in a pristine Ruby environment.
|
|
7
|
+
class Responds
|
|
8
|
+
# One-liner script that checks if a given class is defined and
|
|
9
|
+
# responds to a given method in a pristine Ruby environment.
|
|
10
|
+
SCRIPT = 'a, s, b = $_.strip.partition(/[.#]/); ' \
|
|
11
|
+
'c = Object.const_defined?(a) && Object.const_get(a); ' \
|
|
12
|
+
'puts (c && (s == "." ? c.respond_to?(b) : ' \
|
|
13
|
+
'c.method_defined?(b))); $stdout.flush'
|
|
14
|
+
ONELINER = ['ruby', '-n', '-e', SCRIPT].freeze
|
|
15
|
+
METHOD_CALL = /^[A-Z]\w*(?:::[A-Z]\w*)*[.#](?:\w+[!=?]?|\[\])$/
|
|
16
|
+
|
|
17
|
+
private_constant :METHOD_CALL, :ONELINER, :SCRIPT
|
|
18
|
+
|
|
19
|
+
def initialize
|
|
20
|
+
@cache = {}
|
|
21
|
+
IO.popen(ONELINER, 'r+') do |io| # permit
|
|
22
|
+
@io = io
|
|
23
|
+
yield self
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def ok?(string)
|
|
28
|
+
return false unless METHOD_CALL.match?(string)
|
|
29
|
+
|
|
30
|
+
@cache.fetch(string) do
|
|
31
|
+
@io.puts(string)
|
|
32
|
+
@cache[string] = (@io.gets&.chomp == 'true')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Test module provides methods to run tests.
|
|
6
|
+
module Test
|
|
7
|
+
# Matches long-running test suites ending in '_slow'. Prioritizes them
|
|
8
|
+
# at the front of the batch queue so worker threads start them immediately,
|
|
9
|
+
# reducing overall completion time across parallel workers.
|
|
10
|
+
SLOW_TESTS = /_slow$/
|
|
11
|
+
|
|
12
|
+
def self.test_files
|
|
13
|
+
Dir['test/**/tc_*'].select do |tf|
|
|
14
|
+
File.file?(tf) && %r{/tc_\w+$}.match?(tf)
|
|
15
|
+
end.sort
|
|
16
|
+
end
|
|
17
|
+
private_class_method :test_files
|
|
18
|
+
|
|
19
|
+
def self.matched_files(filematch)
|
|
20
|
+
files = test_files
|
|
21
|
+
return files unless filematch
|
|
22
|
+
|
|
23
|
+
matched = files.grep(Regexp.new(filematch))
|
|
24
|
+
matched.empty? ? raise("No tests matched: #{filematch}") : matched
|
|
25
|
+
end
|
|
26
|
+
private_class_method :matched_files
|
|
27
|
+
|
|
28
|
+
# :reek:ControlParameter
|
|
29
|
+
def self.options(methodmatch, quiet)
|
|
30
|
+
[
|
|
31
|
+
'--verbose=important-only',
|
|
32
|
+
'--ignore-name=test_rubocop_and_reek',
|
|
33
|
+
('--progress-style=fault-only' if quiet),
|
|
34
|
+
("--name=/#{methodmatch}/" if methodmatch)
|
|
35
|
+
].compact
|
|
36
|
+
end
|
|
37
|
+
private_class_method :options
|
|
38
|
+
|
|
39
|
+
# :reek:BooleanParameter :reek:ControlParameter
|
|
40
|
+
# :reek:LongParameterList
|
|
41
|
+
def self.test!(filematch = nil, methodmatch = nil,
|
|
42
|
+
batch: false, list: false, quiet: false)
|
|
43
|
+
files = matched_files(filematch)
|
|
44
|
+
return puts files if list
|
|
45
|
+
|
|
46
|
+
opt = options(methodmatch, quiet)
|
|
47
|
+
batch ? batch_test(files, opt) : sequential_test(files, opt)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.sequential_test(files, opt)
|
|
51
|
+
files.each do |tf|
|
|
52
|
+
puts "#{tf}: ".blue
|
|
53
|
+
raise "failed: #{tf}" unless system(tf, *opt) # permit
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
private_class_method :sequential_test
|
|
57
|
+
|
|
58
|
+
def self.batch_test(files, opt)
|
|
59
|
+
failed = ThreadPool.new.reject(prioritized_files(files)) do |tf|
|
|
60
|
+
system(tf, *opt, out: File::NULL, err: File::NULL) # permit
|
|
61
|
+
end
|
|
62
|
+
report_failures(failed.sort) unless failed.empty?
|
|
63
|
+
end
|
|
64
|
+
private_class_method :batch_test
|
|
65
|
+
|
|
66
|
+
def self.prioritized_files(files)
|
|
67
|
+
slow, normal = files.partition { it.match?(SLOW_TESTS) }
|
|
68
|
+
slow + normal
|
|
69
|
+
end
|
|
70
|
+
private_class_method :prioritized_files
|
|
71
|
+
|
|
72
|
+
def self.report_failures(failed)
|
|
73
|
+
msg = failed.size == 1 ? failed.first : "\n#{failed.join("\n")}"
|
|
74
|
+
raise "failed: #{msg}"
|
|
75
|
+
end
|
|
76
|
+
private_class_method :report_failures
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Runs a block concurrently over items using a bounded pool of threads.
|
|
6
|
+
class ThreadPool
|
|
7
|
+
def self.default_workers(ratio = 0.5)
|
|
8
|
+
[1, (Etc.nprocessors * ratio).round].max
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize(workers: ThreadPool.default_workers)
|
|
12
|
+
@workers = [1, workers].max
|
|
13
|
+
@mutex = Mutex.new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.build_queue(items)
|
|
17
|
+
Thread::Queue.new.tap do |queue|
|
|
18
|
+
items.each { queue << it }
|
|
19
|
+
queue.close
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def stream(worker)
|
|
24
|
+
queue = Thread::Queue.new
|
|
25
|
+
threads = start_workers(queue, worker)
|
|
26
|
+
yield queue
|
|
27
|
+
ensure
|
|
28
|
+
queue.close
|
|
29
|
+
threads&.each(&:join)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def reject(items, &)
|
|
33
|
+
queue = ThreadPool.build_queue(items)
|
|
34
|
+
[].tap { |failed| spawn_workers(queue, failed, &) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# :reek:NilCheck
|
|
38
|
+
def self.drain_queue(queue)
|
|
39
|
+
until (item = queue.pop).nil?
|
|
40
|
+
yield item
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def start_workers(queue, worker)
|
|
47
|
+
Array.new(@workers) do
|
|
48
|
+
Thread.new { ThreadPool.drain_queue(queue) { worker.call(it) } }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def spawn_workers(queue, failed, &)
|
|
53
|
+
Array.new(@workers) do
|
|
54
|
+
Thread.new { drain(queue, failed, &) }
|
|
55
|
+
end.each(&:join)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def drain(queue, failed)
|
|
59
|
+
ThreadPool.drain_queue(queue) do |item|
|
|
60
|
+
@mutex.synchronize { failed << item } unless yield(item)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/wile/help.rb
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Wile namespace.
|
|
4
|
+
module Wile
|
|
5
|
+
# Scrape help documentation from task files.
|
|
6
|
+
module Help
|
|
7
|
+
def self.task_file(task)
|
|
8
|
+
custom = File.expand_path("#{CUSTOM}/#{task}.rb")
|
|
9
|
+
file = File.file?(custom) ? custom : "#{__dir__}/tasks/#{task}.rb"
|
|
10
|
+
return file if File.file?(file)
|
|
11
|
+
|
|
12
|
+
raise ArgumentError, "does not exist: #{file}"
|
|
13
|
+
end
|
|
14
|
+
private_class_method :task_file
|
|
15
|
+
|
|
16
|
+
def self.scrape_help(task)
|
|
17
|
+
continue = false
|
|
18
|
+
File.foreach(task_file(task)) do |line|
|
|
19
|
+
next unless continue ||= /^# #/.match?(line)
|
|
20
|
+
break unless /^#/.match?(line)
|
|
21
|
+
|
|
22
|
+
yield(line[2..] || '')
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.[](task)
|
|
27
|
+
help = String.new
|
|
28
|
+
scrape_help(task) { help << it }
|
|
29
|
+
help.empty? ? nil : help
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.usage(task)
|
|
33
|
+
usage = "wile #{task}"
|
|
34
|
+
scrape_help(task) do |line|
|
|
35
|
+
text = line.strip
|
|
36
|
+
usage = text unless text.empty?
|
|
37
|
+
end
|
|
38
|
+
usage
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|