lib/synvert/cli.rb in synvert-0.9.0 vs lib/synvert/cli.rb in synvert-0.10.0
- old
+ new
@@ -13,12 +13,10 @@
end
# Initialize a CLI.
def initialize
@options = {command: 'run', custom_snippet_paths: [], snippet_names: []}
- Core::Configuration.instance.set :skip_files, []
- Core::Configuration.instance.set :default_snippets_path, File.join(ENV['HOME'], '.synvert')
end
# Run the CLI.
# @param args [Array] arguments.
# @return [Boolean] true if command runs successfully.
@@ -37,18 +35,18 @@
when 'show'
load_rewriters
show_rewriter
when 'sync'
sync_snippets
- else
+ else # run
load_rewriters
@options[:snippet_names].each do |snippet_name|
puts "===== #{snippet_name} started ====="
group, name = snippet_name.split('/')
rewriter = Core::Rewriter.call group, name
rewriter.warnings.each do |warning|
- puts "[Warn] " + warning.message
+ puts '[Warn] ' + warning.message
end
puts rewriter.todo if rewriter.todo
puts "===== #{snippet_name} done ====="
end
end
@@ -68,11 +66,11 @@
private
# Run OptionParser to parse arguments.
def run_option_parser(args)
optparse = OptionParser.new do |opts|
- opts.banner = "Usage: synvert [project_path]"
+ opts.banner = 'Usage: synvert [project_path]'
opts.on '-d', '--load SNIPPET_PATHS', 'load custom snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
@options[:custom_snippet_paths] = snippet_paths.split(',').map(&:strip)
end
opts.on '-l', '--list', 'list all available snippets' do
@options[:command] = 'list'
@@ -102,56 +100,56 @@
puts Core::VERSION
exit
end
end
paths = optparse.parse(args)
- Core::Configuration.instance.set :path, paths.first || Dir.pwd
+ Core::Configuration.path = paths.first || Dir.pwd
if @options[:skip_file_patterns] && !@options[:skip_file_patterns].empty?
- skip_files = @options[:skip_file_patterns].map { |file_pattern|
- full_file_pattern = File.join(Core::Configuration.instance.get(:path), file_pattern)
+ skip_files = @options[:skip_file_patterns].map do |file_pattern|
+ full_file_pattern = File.join(Core::Configuration.path, file_pattern)
Dir.glob(full_file_pattern)
- }.flatten
- Core::Configuration.instance.set :skip_files, skip_files
+ end.flatten
+ Core::Configuration.skip_files = skip_files
end
end
# Load all rewriters.
def load_rewriters
- Dir.glob(File.join(default_snippets_path, 'lib/**/*.rb')).each { |file| eval(File.read(file)) }
+ Dir.glob(File.join(default_snippets_path, 'lib/**/*.rb')).each { |file| require file }
@options[:custom_snippet_paths].each do |snippet_path|
if snippet_path =~ /^http/
uri = URI.parse snippet_path
eval(uri.read)
else
- eval(File.read(snippet_path))
+ require snippet_path
end
end
rescue
FileUtils.rm_rf default_snippets_path
retry
end
# List and print all available rewriters.
def list_available_rewriters
if Core::Rewriter.availables.empty?
- puts "There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets."
+ puts 'There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets.'
else
Core::Rewriter.availables.each do |group, rewriters|
puts group
rewriters.each do |name, rewriter|
- puts " " + name
+ puts ' ' + name
end
end
puts
end
end
# Open one rewriter.
def open_rewriter
editor = [ENV['SYNVERT_EDITOR'], ENV['EDITOR']].find { |e| !e.nil? && !e.empty? }
- return puts "To open a synvert snippet, set $EDITOR or $SYNVERT_EDITOR" unless editor
+ return puts 'To open a synvert snippet, set $EDITOR or $SYNVERT_EDITOR' unless editor
path = File.expand_path(File.join(default_snippets_path, "lib/#{@options[:snippet_name]}.rb"))
if File.exist? path
system editor, path
else
@@ -163,16 +161,16 @@
def query_available_rewriters
Core::Rewriter.availables.each do |group, rewriters|
if group.include? @options[:query]
puts group
rewriters.each do |name, rewriter|
- puts " " + name
+ puts ' ' + name
end
elsif rewriters.keys.any? { |name| name.include? @options[:query] }
puts group
rewriters.each do |name, rewriter|
- puts " " + name if name.include?(@options[:query])
+ puts ' ' + name if name.include?(@options[:query])
end
end
end
puts
end
@@ -184,30 +182,30 @@
if rewriter
rewriter.process_with_sandbox
puts rewriter.description
rewriter.sub_snippets.each do |sub_rewriter|
puts
- puts "=" * 80
+ puts '=' * 80
puts "snippet: #{sub_rewriter.name}"
- puts "=" * 80
+ puts '=' * 80
puts sub_rewriter.description
end
else
puts "snippet #{@options[:snippet_name]} not found"
end
end
# sync snippets
def sync_snippets
- Snippet.sync
- puts "synvert snippets are synced"
+ Snippet.new(default_snippets_path).sync
+ puts 'synvert snippets are synced'
core_version = Snippet.fetch_core_version
if Gem::Version.new(core_version) > Gem::Version.new(Synvert::Core::VERSION)
puts "synvert-core is updated, please install synvert-core #{core_version}"
end
end
def default_snippets_path
- Core::Configuration.instance.get :default_snippets_path
+ File.join(ENV['HOME'], '.synvert')
end
end
end