synvert-core 1.14.2 → 1.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +2 -2
- data/lib/synvert/core/node_ext.rb +1 -1
- data/lib/synvert/core/snippet_not_found_error.rb +4 -0
- data/lib/synvert/core/utils.rb +29 -3
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +1 -0
- data/spec/synvert/core/utils_spec.rb +44 -17
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82066eb2fad44d4ca47831672a31195bba96aa07206aa23e449d5c519b94b23d
|
4
|
+
data.tar.gz: 70a8566412fc3cf9004441a07e706c79807820003155b4bafdc03cf4c62042cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b33e55196d7d3e13097e7b101ee693111e5a49ad6d93015eba85886214abb125b5a938908ca0feadef772ef6eecdaa8874fc2d8d6ecff044d5506f685db8bc8d
|
7
|
+
data.tar.gz: aa7f05784e6e8710903c47e6616dac0a18bca66b0f04fc6b7089ff8d47f8c5c6d073bdb45fccb04b25765feed6b28313ea08dd80d166e0283cd931e2d9482229
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
synvert-core (1.
|
4
|
+
synvert-core (1.15.0)
|
5
5
|
activesupport (< 7.0.0)
|
6
6
|
erubis
|
7
7
|
node_mutation (>= 1.7.1)
|
@@ -59,7 +59,7 @@ GEM
|
|
59
59
|
nenv (~> 0.1)
|
60
60
|
shellany (~> 0.0)
|
61
61
|
parallel (1.22.1)
|
62
|
-
parser (3.1.
|
62
|
+
parser (3.1.3.0)
|
63
63
|
ast (~> 2.4.1)
|
64
64
|
parser_node_ext (0.4.1)
|
65
65
|
parser
|
@@ -27,7 +27,7 @@ module Parser::AST
|
|
27
27
|
# +type: 'send', receiver: { type: 'send', receiver: { type: 'send', message: 'config' }, message: 'active_record' }, message: 'identity_map='+
|
28
28
|
#
|
29
29
|
# Source Code to Ast Node
|
30
|
-
# {https://
|
30
|
+
# {https://playground.synvert.net/ruby}
|
31
31
|
class Node
|
32
32
|
# Get the file name of node.
|
33
33
|
#
|
data/lib/synvert/core/utils.rb
CHANGED
@@ -1,18 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'net/http'
|
3
4
|
require 'uri'
|
5
|
+
require 'open-uri'
|
4
6
|
|
5
7
|
module Synvert::Core
|
6
8
|
class Utils
|
7
9
|
class << self
|
8
10
|
def eval_snippet(snippet_name)
|
11
|
+
eval(load_snippet(snippet_name))
|
12
|
+
end
|
13
|
+
|
14
|
+
def load_snippet(snippet_name)
|
9
15
|
if is_valid_url?(snippet_name)
|
10
16
|
uri = URI.parse(format_url(snippet_name))
|
11
|
-
|
17
|
+
return uri.open.read if remote_snippet_exists?(uri)
|
18
|
+
raise SnippetNotFoundError.new("#{snippet_name} nout found")
|
12
19
|
elsif is_valid_file?(snippet_name)
|
13
|
-
|
20
|
+
return File.read(snippet_name)
|
14
21
|
else
|
15
|
-
|
22
|
+
snippet_path = snippet_expand_path(snippet_name)
|
23
|
+
return File.read(snippet_path) if File.exist?(snippet_path)
|
24
|
+
snippet_uri = URI.parse(format_url(remote_snippet_url(snippet_name)))
|
25
|
+
return snippet_uri.open.read if remote_snippet_exists?(snippet_uri)
|
26
|
+
raise SnippetNotFoundError.new("#{snippet_name} nout found")
|
16
27
|
end
|
17
28
|
end
|
18
29
|
|
@@ -26,10 +37,25 @@ module Synvert::Core
|
|
26
37
|
File.exist?(path)
|
27
38
|
end
|
28
39
|
|
40
|
+
def remote_snippet_exists?(uri)
|
41
|
+
req = Net::HTTP.new(uri.host, uri.port)
|
42
|
+
req.use_ssl = uri.scheme == 'https'
|
43
|
+
res = req.request_head(uri.path)
|
44
|
+
res.code == "200"
|
45
|
+
end
|
46
|
+
|
47
|
+
def snippet_expand_path(snippet_name)
|
48
|
+
File.join(default_snippets_home(), 'lib', "#{snippet_name}.rb")
|
49
|
+
end
|
50
|
+
|
29
51
|
def default_snippets_home
|
30
52
|
ENV['SYNVERT_SNIPPETS_HOME'] || File.join(ENV['HOME'], '.synvert-ruby')
|
31
53
|
end
|
32
54
|
|
55
|
+
def remote_snippet_url(snippet_name)
|
56
|
+
"https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/#{snippet_name}.rb"
|
57
|
+
end
|
58
|
+
|
33
59
|
def format_url(url)
|
34
60
|
convert_to_github_raw_url(url)
|
35
61
|
end
|
data/lib/synvert/core/version.rb
CHANGED
data/lib/synvert/core.rb
CHANGED
@@ -3,27 +3,54 @@ require 'spec_helper'
|
|
3
3
|
module Synvert::Core
|
4
4
|
RSpec.describe Utils do
|
5
5
|
describe '.eval_snippet' do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
context "by http url" do
|
7
|
+
it 'evals snippet' do
|
8
|
+
expect(described_class).to receive(:remote_snippet_exists?).with(URI.parse('http://example.com/rewriter.rb')).and_return(true)
|
9
|
+
expect_any_instance_of(URI::HTTP).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'name' do\nend"))
|
10
|
+
rewriter = described_class.eval_snippet('http://example.com/rewriter.rb')
|
11
|
+
expect(rewriter.group).to eq 'group'
|
12
|
+
expect(rewriter.name).to eq 'name'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'raises error' do
|
16
|
+
expect(described_class).to receive(:remote_snippet_exists?).and_return(false)
|
17
|
+
expect do
|
18
|
+
described_class.eval_snippet('http://example.com/rewriter.rb')
|
19
|
+
end.to raise_error(SnippetNotFoundError)
|
20
|
+
end
|
11
21
|
end
|
12
22
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
23
|
+
context 'by file path' do
|
24
|
+
it 'evals snippet' do
|
25
|
+
expect(File).to receive(:exist?).and_return(true)
|
26
|
+
expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
|
27
|
+
rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
|
28
|
+
expect(rewriter.group).to eq 'group'
|
29
|
+
expect(rewriter.name).to eq 'name'
|
30
|
+
end
|
19
31
|
end
|
20
32
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
33
|
+
context 'by snippet name' do
|
34
|
+
it 'evals snippet as file' do
|
35
|
+
expect(File).to receive(:exist?).with("group/name").and_return(false)
|
36
|
+
expect(described_class).to receive(:default_snippets_home).and_return('/home/richard/.synvert-ruby')
|
37
|
+
expect(File).to receive(:exist?).with("/home/richard/.synvert-ruby/lib/group/name.rb").and_return(true)
|
38
|
+
expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
|
39
|
+
rewriter = described_class.eval_snippet('group/name')
|
40
|
+
expect(rewriter.group).to eq 'group'
|
41
|
+
expect(rewriter.name).to eq 'name'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'evals snippet as github url' do
|
45
|
+
expect(File).to receive(:exist?).with("group/name").and_return(false)
|
46
|
+
expect(described_class).to receive(:default_snippets_home).and_return('/home/richard/.synvert-ruby')
|
47
|
+
expect(File).to receive(:exist?).with("/home/richard/.synvert-ruby/lib/group/name.rb").and_return(false)
|
48
|
+
expect(described_class).to receive(:remote_snippet_exists?).with(URI.parse("https://raw.githubusercontent.com/xinminlabs/synvert-snippets-ruby/main/lib/group/name.rb")).and_return(true)
|
49
|
+
expect_any_instance_of(URI::HTTP).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'name' do\nend"))
|
50
|
+
rewriter = described_class.eval_snippet('group/name')
|
51
|
+
expect(rewriter.group).to eq 'group'
|
52
|
+
expect(rewriter.name).to eq 'name'
|
53
|
+
end
|
27
54
|
end
|
28
55
|
end
|
29
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synvert-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -145,6 +145,7 @@ files:
|
|
145
145
|
- lib/synvert/core/rewriter/scope/goto_scope.rb
|
146
146
|
- lib/synvert/core/rewriter/scope/within_scope.rb
|
147
147
|
- lib/synvert/core/rewriter/warning.rb
|
148
|
+
- lib/synvert/core/snippet_not_found_error.rb
|
148
149
|
- lib/synvert/core/strategy.rb
|
149
150
|
- lib/synvert/core/utils.rb
|
150
151
|
- lib/synvert/core/version.rb
|
@@ -187,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
187
188
|
- !ruby/object:Gem::Version
|
188
189
|
version: '0'
|
189
190
|
requirements: []
|
190
|
-
rubygems_version: 3.3.
|
191
|
+
rubygems_version: 3.3.26
|
191
192
|
signing_key:
|
192
193
|
specification_version: 4
|
193
194
|
summary: convert ruby code to better syntax.
|