Skip to content

Commit dd9c39f

Browse files
committed
extract synvert-core from synvert
0 parents  commit dd9c39f

29 files changed

+2370
-0
lines changed

Diff for: .gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Diff for: .rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format documentation

Diff for: .travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: ruby
2+
rvm:
3+
- 1.9.3
4+
- 2.0.0
5+
- 2.1.0
6+
- 2.1.1

Diff for: Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in synvert.gemspec
4+
gemspec
5+
6+
gem 'coveralls', require: false

Diff for: LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014 Richard Huang
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Synvert::Core
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'synvert-core'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install synvert-core
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it ( https://github.com/[my-github-username]/synvert-core/fork )
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create a new Pull Request

Diff for: Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "bundler/gem_tasks"
2+

Diff for: lib/synvert/core.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "synvert/core/version"
2+
3+
# coding: utf-8
4+
require "synvert/core/version"
5+
require 'bundler'
6+
require 'parser'
7+
require 'parser/current'
8+
require 'ast'
9+
require 'active_support/inflector'
10+
require 'synvert/core/node_ext'
11+
12+
module Synvert
13+
module Core
14+
autoload :Configuration, 'synvert/core/configuration'
15+
autoload :Rewriter, 'synvert/core/rewriter'
16+
autoload :RewriterNotFound, 'synvert/core/exceptions'
17+
autoload :GemfileLockNotFound, 'synvert/core/exceptions'
18+
autoload :MethodNotSupported, 'synvert/core/exceptions'
19+
end
20+
end
21+
22+
module Synvert
23+
Rewriter = Core::Rewriter
24+
end

Diff for: lib/synvert/core/cli.rb

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# coding: utf-8
2+
require 'optparse'
3+
require 'open-uri'
4+
5+
module Synvert::Core
6+
# Synvert command line interface.
7+
class CLI
8+
# Initialize the cli and run.
9+
#
10+
# @param args [Array] arguments, default is ARGV.
11+
# @return [Boolean] true if command runs successfully.
12+
def self.run(args = ARGV)
13+
new.run(args)
14+
end
15+
16+
# Initialize a CLI.
17+
def initialize
18+
@options = {command: 'run', snippet_paths: [], snippet_names: []}
19+
Configuration.instance.set :skip_files, []
20+
end
21+
22+
# Run the CLI.
23+
# @param args [Array] arguments.
24+
# @return [Boolean] true if command runs successfully.
25+
def run(args)
26+
run_option_parser(args)
27+
load_rewriters
28+
29+
case @options[:command]
30+
when 'list' then list_available_rewriters
31+
when 'query' then query_available_rewriters
32+
when 'show' then show_rewriter
33+
else
34+
@options[:snippet_names].each do |snippet_name|
35+
puts "===== #{snippet_name} started ====="
36+
rewriter = Rewriter.call snippet_name
37+
puts rewriter.todo if rewriter.todo
38+
puts "===== #{snippet_name} done ====="
39+
end
40+
end
41+
true
42+
rescue SystemExit
43+
true
44+
rescue Parser::SyntaxError => e
45+
puts "Syntax error: #{e.message}"
46+
puts "file #{e.diagnostic.location.source_buffer.name}"
47+
puts "line #{e.diagnostic.location.line}"
48+
false
49+
rescue Exception => e
50+
print "Error: "
51+
p e
52+
false
53+
end
54+
55+
private
56+
57+
# Run OptionParser to parse arguments.
58+
def run_option_parser(args)
59+
optparse = OptionParser.new do |opts|
60+
opts.banner = "Usage: synvert [project_path]"
61+
opts.on '-d', '--load SNIPPET_PATHS', 'load additional snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
62+
@options[:snippet_paths] = snippet_paths.split(',').map(&:strip)
63+
end
64+
opts.on '-l', '--list', 'list all available snippets' do
65+
@options[:command] = 'list'
66+
end
67+
opts.on '-q', '--query QUERY', 'query specified snippets' do |query|
68+
@options[:command] = 'query'
69+
@options[:query] = query
70+
end
71+
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|
72+
@options[:skip_file_patterns] = file_patterns.split(',')
73+
end
74+
opts.on '-s', '--show SNIPPET_NAME', 'show specified snippet description' do |snippet_name|
75+
@options[:command] = 'show'
76+
@options[:snippet_name] = snippet_name
77+
end
78+
opts.on '-r', '--run SNIPPET_NAMES', 'run specified snippets' do |snippet_names|
79+
@options[:snippet_names] = snippet_names.split(',').map(&:strip)
80+
end
81+
opts.on '-v', '--version', 'show this version' do
82+
puts Synvert::Core::VERSION
83+
exit
84+
end
85+
end
86+
paths = optparse.parse(args)
87+
Configuration.instance.set :path, paths.first || Dir.pwd
88+
if @options[:skip_file_patterns] && !@options[:skip_file_patterns].empty?
89+
skip_files = @options[:skip_file_patterns].map { |file_pattern|
90+
full_file_pattern = File.join(Configuration.instance.get(:path), file_pattern)
91+
Dir.glob(full_file_pattern)
92+
}.flatten
93+
Configuration.instance.set :skip_files, skip_files
94+
end
95+
end
96+
97+
# Load all rewriters.
98+
def load_rewriters
99+
Dir.glob(File.join(File.dirname(__FILE__), 'snippets/**/*.rb')).each { |file| eval(File.read(file)) }
100+
101+
@options[:snippet_paths].each do |snippet_path|
102+
if snippet_path =~ /^http/
103+
uri = URI.parse snippet_path
104+
eval(uri.read)
105+
else
106+
eval(File.read(snippet_path))
107+
end
108+
end
109+
end
110+
111+
# List and print all available rewriters.
112+
def list_available_rewriters
113+
Rewriter.availables.each do |rewriter|
114+
print rewriter.name.to_s + " "
115+
end
116+
puts
117+
end
118+
119+
# Query and print available rewriters.
120+
def query_available_rewriters
121+
Rewriter.availables.each do |rewriter|
122+
if rewriter.name.include? @options[:query]
123+
print rewriter.name + " "
124+
end
125+
end
126+
puts
127+
end
128+
129+
# Show and print one rewriter.
130+
def show_rewriter
131+
rewriter = Rewriter.fetch(@options[:snippet_name])
132+
if rewriter
133+
rewriter.process_with_sandbox
134+
puts rewriter.description
135+
rewriter.sub_snippets.each do |sub_rewriter|
136+
puts
137+
puts "=" * 80
138+
puts "snippet: #{sub_rewriter.name}"
139+
puts "=" * 80
140+
puts sub_rewriter.description
141+
end
142+
else
143+
puts "snippet #{@options[:snippet_name]} not found"
144+
end
145+
end
146+
end
147+
end

Diff for: lib/synvert/core/configuration.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# encoding: utf-8
2+
require 'singleton'
3+
4+
module Synvert::Core
5+
# Synvert global configuration.
6+
class Configuration < Hash
7+
include Singleton
8+
9+
# Set the configuration.
10+
#
11+
# @param key [String] configuration key.
12+
# @param value [Object] configuration value.
13+
def set(key, value)
14+
self[key] = value
15+
end
16+
17+
# Get the configuration.
18+
#
19+
# @param key [String] configuration key.
20+
# @return [Object] configuration value.
21+
def get(key)
22+
self[key]
23+
end
24+
end
25+
end

Diff for: lib/synvert/core/exceptions.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Synvert::Core
2+
# Rewriter not found exception.
3+
class RewriterNotFound < Exception
4+
end
5+
6+
# Gemfile.lock not found exception.
7+
class GemfileLockNotFound < Exception
8+
end
9+
10+
# Method not supported exception.
11+
class MethodNotSupported < Exception
12+
end
13+
end

0 commit comments

Comments
 (0)