lib/ruboclean/runner.rb in ruboclean-0.4.0 vs lib/ruboclean/runner.rb in ruboclean-0.5.0
- old
+ new
@@ -1,23 +1,74 @@
# frozen_string_literal: true
+require "pathname"
+require "yaml"
+
module Ruboclean
- # Proxy for invoking the cleaning
+ # Entry point for processing
class Runner
- def initialize(arguments)
- @arguments = arguments
+ def initialize(args = [])
+ @cli_arguments = CliArguments.new(args)
end
def run!
- rubocop_configuration_path = RubocopConfigurationPath.new(arguments.path)
- rubocop_configuration = rubocop_configuration_path.load
+ return if source_file_pathname.empty?
- return if rubocop_configuration.nil?
+ load_file.then(&method(:order))
+ .then(&method(:cleanup_paths))
+ .then(&method(:convert_to_yaml))
+ .then(&method(:write_file!))
+ end
- rubocop_configuration_path.write(rubocop_configuration.order, preserve_comments: arguments.preserve_comments?)
+ def verbose?
+ cli_arguments.verbose?
end
+ def path
+ cli_arguments.path
+ end
+
private
- attr_reader :arguments
+ attr_reader :cli_arguments
+
+ def source_yaml
+ @source_yaml ||= source_file_pathname.read
+ end
+
+ def load_file
+ YAML.safe_load(source_yaml, permitted_classes: [Regexp])
+ end
+
+ def order(configuration_hash)
+ Orderer.new(configuration_hash).order
+ end
+
+ def cleanup_paths(configuration_hash)
+ return configuration_hash if cli_arguments.preserve_paths?
+
+ PathCleanup.new(configuration_hash, source_file_pathname.dirname).cleanup
+ end
+
+ def convert_to_yaml(configuration_hash)
+ ToYamlConverter.new(configuration_hash, cli_arguments.preserve_comments?, source_yaml).to_yaml
+ end
+
+ def write_file!(target_yaml)
+ source_file_pathname.write(target_yaml)
+ end
+
+ def source_file_pathname
+ @source_file_pathname ||= find_source_file_pathname
+ end
+
+ def find_source_file_pathname
+ source_path = Pathname.new(cli_arguments.path)
+
+ source_path = source_path.join(".rubocop.yml") if source_path.directory?
+
+ return source_path if source_path.exist?
+
+ raise ArgumentError, "path does not exist: '#{source_path}'"
+ end
end
end