lib/synvert/cli.rb in synvert-0.0.17 vs lib/synvert/cli.rb in synvert-0.1.0
- old
+ new
@@ -13,12 +13,13 @@
new.run(args)
end
# Initialize a CLI.
def initialize
- @options = {command: 'run', snippet_paths: [], snippet_names: []}
- Configuration.instance.set :skip_files, []
+ @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.
@@ -28,14 +29,15 @@
case @options[:command]
when 'list' then list_available_rewriters
when 'query' then query_available_rewriters
when 'show' then show_rewriter
+ when 'sync' then sync_snippets
else
@options[:snippet_names].each do |snippet_name|
puts "===== #{snippet_name} started ====="
- rewriter = Rewriter.call snippet_name
+ rewriter = Core::Rewriter.call snippet_name
puts rewriter.todo if rewriter.todo
puts "===== #{snippet_name} done ====="
end
end
true
@@ -56,12 +58,12 @@
# Run OptionParser to parse arguments.
def run_option_parser(args)
optparse = OptionParser.new do |opts|
opts.banner = "Usage: synvert [project_path]"
- opts.on '-d', '--load SNIPPET_PATHS', 'load additional snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
- @options[:snippet_paths] = snippet_paths.split(',').map(&:strip)
+ 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'
end
opts.on '-q', '--query QUERY', 'query specified snippets' do |query|
@@ -73,34 +75,38 @@
end
opts.on '-s', '--show SNIPPET_NAME', 'show specified snippet description' do |snippet_name|
@options[:command] = 'show'
@options[:snippet_name] = snippet_name
end
+ opts.on '--sync', 'sync snippets' do
+ @options[:command] = 'sync'
+ end
opts.on '-r', '--run SNIPPET_NAMES', 'run specified snippets' do |snippet_names|
@options[:snippet_names] = snippet_names.split(',').map(&:strip)
end
opts.on '-v', '--version', 'show this version' do
- puts Synvert::VERSION
+ puts Core::VERSION
exit
end
end
paths = optparse.parse(args)
- Configuration.instance.set :path, paths.first || Dir.pwd
+ Core::Configuration.instance.set :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(Configuration.instance.get(:path), file_pattern)
+ full_file_pattern = File.join(Core::Configuration.instance.get(:path), file_pattern)
Dir.glob(full_file_pattern)
}.flatten
- Configuration.instance.set :skip_files, skip_files
+ Core::Configuration.instance.set :skip_files, skip_files
end
end
# Load all rewriters.
def load_rewriters
- Dir.glob(File.join(File.dirname(__FILE__), 'snippets/**/*.rb')).each { |file| eval(File.read(file)) }
+ default_snippets_path = Core::Configuration.instance.get :default_snippets_path
+ Dir.glob(File.join(default_snippets_path, 'lib/**/*.rb')).each { |file| eval(File.read(file)) }
- @options[:snippet_paths].each do |snippet_path|
+ @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))
@@ -108,29 +114,29 @@
end
end
# List and print all available rewriters.
def list_available_rewriters
- Rewriter.availables.each do |rewriter|
+ Core::Rewriter.availables.each do |rewriter|
print rewriter.name.to_s + " "
end
puts
end
# Query and print available rewriters.
def query_available_rewriters
- Rewriter.availables.each do |rewriter|
+ Core::Rewriter.availables.each do |rewriter|
if rewriter.name.include? @options[:query]
print rewriter.name + " "
end
end
puts
end
# Show and print one rewriter.
def show_rewriter
- rewriter = Rewriter.fetch(@options[:snippet_name])
+ rewriter = Core::Rewriter.fetch(@options[:snippet_name])
if rewriter
rewriter.process_with_sandbox
puts rewriter.description
rewriter.sub_snippets.each do |sub_rewriter|
puts
@@ -140,8 +146,14 @@
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"
end
end
end