Skip to content

Commit

Permalink
extract synvert-core from synvert
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed May 4, 2014
0 parents commit dd9c39f
Show file tree
Hide file tree
Showing 29 changed files with 2,370 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--format documentation
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1.1
6 changes: 6 additions & 0 deletions Gemfile
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in synvert.gemspec
gemspec

gem 'coveralls', require: false
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2014 Richard Huang

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
# Synvert::Core

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'synvert-core'

And then execute:

$ bundle

Or install it yourself as:

$ gem install synvert-core

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it ( https://github.com/[my-github-username]/synvert-core/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"

24 changes: 24 additions & 0 deletions lib/synvert/core.rb
@@ -0,0 +1,24 @@
require "synvert/core/version"

# coding: utf-8
require "synvert/core/version"
require 'bundler'
require 'parser'
require 'parser/current'
require 'ast'
require 'active_support/inflector'
require 'synvert/core/node_ext'

module Synvert
module Core
autoload :Configuration, 'synvert/core/configuration'
autoload :Rewriter, 'synvert/core/rewriter'
autoload :RewriterNotFound, 'synvert/core/exceptions'
autoload :GemfileLockNotFound, 'synvert/core/exceptions'
autoload :MethodNotSupported, 'synvert/core/exceptions'
end
end

module Synvert
Rewriter = Core::Rewriter
end
147 changes: 147 additions & 0 deletions lib/synvert/core/cli.rb
@@ -0,0 +1,147 @@
# coding: utf-8
require 'optparse'
require 'open-uri'

module Synvert::Core
# Synvert command line interface.
class CLI
# Initialize the cli and run.
#
# @param args [Array] arguments, default is ARGV.
# @return [Boolean] true if command runs successfully.
def self.run(args = ARGV)
new.run(args)
end

# Initialize a CLI.
def initialize
@options = {command: 'run', snippet_paths: [], snippet_names: []}
Configuration.instance.set :skip_files, []
end

# Run the CLI.
# @param args [Array] arguments.
# @return [Boolean] true if command runs successfully.
def run(args)
run_option_parser(args)
load_rewriters

case @options[:command]
when 'list' then list_available_rewriters
when 'query' then query_available_rewriters
when 'show' then show_rewriter
else
@options[:snippet_names].each do |snippet_name|
puts "===== #{snippet_name} started ====="
rewriter = Rewriter.call snippet_name
puts rewriter.todo if rewriter.todo
puts "===== #{snippet_name} done ====="
end
end
true
rescue SystemExit
true
rescue Parser::SyntaxError => e
puts "Syntax error: #{e.message}"
puts "file #{e.diagnostic.location.source_buffer.name}"
puts "line #{e.diagnostic.location.line}"
false
rescue Exception => e
print "Error: "
p e
false
end

private

# 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)
end
opts.on '-l', '--list', 'list all available snippets' do
@options[:command] = 'list'
end
opts.on '-q', '--query QUERY', 'query specified snippets' do |query|
@options[:command] = 'query'
@options[:query] = query
end
opts.on '--skip FILE_PATTERNS', 'skip specified files or directories, separated by comma, e.g. app/models/post.rb,vendor/plugins/**/*.rb' do |file_patterns|
@options[:skip_file_patterns] = file_patterns.split(',')
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 '-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::Core::VERSION
exit
end
end
paths = optparse.parse(args)
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)
Dir.glob(full_file_pattern)
}.flatten
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)) }

@options[:snippet_paths].each do |snippet_path|
if snippet_path =~ /^http/
uri = URI.parse snippet_path
eval(uri.read)
else
eval(File.read(snippet_path))
end
end
end

# List and print all available rewriters.
def list_available_rewriters
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|
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])
if rewriter
rewriter.process_with_sandbox
puts rewriter.description
rewriter.sub_snippets.each do |sub_rewriter|
puts
puts "=" * 80
puts "snippet: #{sub_rewriter.name}"
puts "=" * 80
puts sub_rewriter.description
end
else
puts "snippet #{@options[:snippet_name]} not found"
end
end
end
end
25 changes: 25 additions & 0 deletions lib/synvert/core/configuration.rb
@@ -0,0 +1,25 @@
# encoding: utf-8
require 'singleton'

module Synvert::Core
# Synvert global configuration.
class Configuration < Hash
include Singleton

# Set the configuration.
#
# @param key [String] configuration key.
# @param value [Object] configuration value.
def set(key, value)
self[key] = value
end

# Get the configuration.
#
# @param key [String] configuration key.
# @return [Object] configuration value.
def get(key)
self[key]
end
end
end
13 changes: 13 additions & 0 deletions lib/synvert/core/exceptions.rb
@@ -0,0 +1,13 @@
module Synvert::Core
# Rewriter not found exception.
class RewriterNotFound < Exception
end

# Gemfile.lock not found exception.
class GemfileLockNotFound < Exception
end

# Method not supported exception.
class MethodNotSupported < Exception
end
end

0 comments on commit dd9c39f

Please sign in to comment.