Class: Fix::Command
- Inherits:
-
Object
- Object
- Fix::Command
- Defined in:
- lib/fix/command.rb
Overview
Open the command class.
Class Method Summary collapse
-
.config_file ⇒ String
The configuration file.
-
.config_paths ⇒ Array
Different locations of command-line configuration options.
-
.fetch_file_paths(diff, prefix, suffix, path) ⇒ Set
A list of absolute paths.
-
.git_diff_paths ⇒ Set
The list of modified files.
- .process_args(load_file, args) ⇒ Object
-
.report(config, *file_paths) ⇒ String
Report the command-line.
-
.run(*args) ⇒ Object
Handle the parameters passed to fix command from the console.
Class Method Details
.config_file ⇒ String
Returns The configuration file.
158 159 160 |
# File 'lib/fix/command.rb', line 158 def self.config_file '.fix' end |
.config_paths ⇒ Array
Returns Different locations of command-line configuration options.
163 164 165 |
# File 'lib/fix/command.rb', line 163 def self.config_paths %w[~ .].map { |char| File.join(File.(char), config_file) } end |
.fetch_file_paths(diff, prefix, suffix, path) ⇒ Set
Returns A list of absolute paths.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/fix/command.rb', line 170 def self.fetch_file_paths(diff, prefix, suffix, path) unless File.directory?(path) warn 'Sorry, invalid directory.' exit false end path = File.absolute_path(path) unless path.start_with?(File::SEPARATOR) spec_paths = Dir.glob(File.join(path, '**', "#{prefix}*#{suffix}.rb")).to_set return spec_paths unless diff Dir.chdir(path) do modified_paths = git_diff_paths # select only modified specs modified_spec_paths = spec_paths & modified_paths # select only unmodified specs unmodified_spec_paths = spec_paths - modified_spec_paths # select only modified code modified_code_paths = modified_paths - spec_paths modified_code_filenames = modified_code_paths.map do |p| p.split(File::SEPARATOR).last.gsub(/\.rb\z/, '') end.to_set # select only specs of the modified code additional_spec_paths = unmodified_spec_paths.select do |unmodified_spec_path| unmodified_spec_filename = unmodified_spec_path.split(File::SEPARATOR).last.gsub(/\.rb\z/, '') modified_code_filenames.any? do |modified_code_filename| unmodified_spec_filename.include?(modified_code_filename) end end modified_spec_paths + additional_spec_paths end end |
.git_diff_paths ⇒ Set
Returns The list of modified files.
209 210 211 212 213 214 |
# File 'lib/fix/command.rb', line 209 def self.git_diff_paths `git status --porcelain` .split("\n") .map { |p| File.absolute_path(p.split.last) } .to_set end |
.process_args(load_file, args) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fix/command.rb', line 88 def self.process_args(load_file, args) = { debug: false, warnings: false, diff: false, random: Random.new_seed, prefix: '', suffix: '_fix' } # rubocop:disable BlockLength opt_parser = OptionParser.new do |opts| opts. = 'Usage: fix <directory> [options]' opts.separator '' opts.separator 'Specific options:' opts.on('--[no-]debug', 'Enable ruby debug') do |b| [:debug] = $DEBUG = b end opts.on('--[no-]warnings', 'Enable ruby warnings') do |b| [:warnings] = $VERBOSE = b end opts.on('--[no-]diff', 'Regression test selection') do |b| [:diff] = b end opts.on('--random [SEED]', Integer, 'Predictable randomization') do |i| [:random] = Integer(i) end opts.on('--prefix [PREFIX]', String, 'Prefix of the spec files') do |s| [:prefix] = s.to_s end opts.on('--suffix [SUFFIX]', String, 'Suffix of the spec files') do |s| [:suffix] = s.to_s end opts.separator '' opts.separator 'Common options:' opts.on_tail '-h', '--help', 'Show this message' do puts opts exit end opts.on_tail '-v', '--version', 'Show the version' do puts File.read(File.join(File.(File.dirname(__FILE__)), '..', '..', 'VERSION.semver')).chomp exit end end # rubocop:enable BlockLength if load_file config_paths.each { |config_path| opt_parser.load(config_path) } end opt_parser.parse!(args) end |
.report(config, *file_paths) ⇒ String
Returns Report the command-line.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fix/command.rb', line 72 def self.report(config, *file_paths) str = '> fix ' + file_paths.join(' ') str += ' --debug' if $DEBUG str += ' --warnings' if $VERBOSE str += ' --diff' if config.fetch(:diff) str += " --random #{config.fetch(:random)}" str += " --prefix #{config.fetch(:prefix).inspect}" str += " --suffix #{config.fetch(:suffix).inspect}" ["\e[37m", str, "\e[0m"].join end |
.run(*args) ⇒ Object
Handle the parameters passed to fix command from the console.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fix/command.rb', line 33 def self.run(*args) config = process_args(true, args) if args.size > 1 raise ArgumentError, "wrong number of directories (given #{args.size})" end file_paths = fetch_file_paths( config.fetch(:diff), config.fetch(:prefix), config.fetch(:suffix), (args.first || '.') ).to_a.sort.shuffle(random: Random.new(config.fetch(:random))) puts report(config, *file_paths) if file_paths.empty? puts 'Specs not found.' exit end status = true file_paths.each do |file_path| begin puts print "\e[30m#{file_path}\e[0m " require file_path rescue SystemExit => e status = false unless e.success? end end exit(status) end |