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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf73847a243ca7bed5a7838e67f7ca4841818d6fa71645e5d6e5ab752be5c2e0
4
- data.tar.gz: 6b1acefc43253e642ea3ce012665a8288b9064927590e7b7a5a7410c43db14d6
3
+ metadata.gz: 82066eb2fad44d4ca47831672a31195bba96aa07206aa23e449d5c519b94b23d
4
+ data.tar.gz: 70a8566412fc3cf9004441a07e706c79807820003155b4bafdc03cf4c62042cc
5
5
  SHA512:
6
- metadata.gz: 808cebc5d15d61324aa00407e8932857ea93928cc2bf6948a253870f6cb5bc9e7a59e09e8172d01fdce61f01ee28667d4eae92ac09993a33bb769e34e2d31b8a
7
- data.tar.gz: 30c5e89a55fd489948e8aa7e3867fb9188b9b12abb8eb81ed5c106c6ae277f954fab9b7bca2a332a5a323c4e8508510c1b178a307e3fa3056581c1845fd00a71
6
+ metadata.gz: b33e55196d7d3e13097e7b101ee693111e5a49ad6d93015eba85886214abb125b5a938908ca0feadef772ef6eecdaa8874fc2d8d6ecff044d5506f685db8bc8d
7
+ data.tar.gz: aa7f05784e6e8710903c47e6616dac0a18bca66b0f04fc6b7089ff8d47f8c5c6d073bdb45fccb04b25765feed6b28313ea08dd80d166e0283cd931e2d9482229
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.15.0 (2022-11-30)
4
+
5
+ * Load snippet from github
6
+
3
7
  ## 1.14.2 (2022-11-19)
4
8
 
5
9
  * Specify `node_query`, `node_mutation` and `parser_node_ext` versions
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.14.2)
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.2.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://synvert-playground.xinminlabs.com/ruby}
30
+ # {https://playground.synvert.net/ruby}
31
31
  class Node
32
32
  # Get the file name of node.
33
33
  #
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SnippetNotFoundError < StandardError
4
+ end
@@ -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
- eval(uri.open.read)
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
- eval(File.read(snippet_name))
20
+ return File.read(snippet_name)
14
21
  else
15
- eval(File.read(File.join(default_snippets_home, 'lib', "#{snippet_name}.rb")))
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.14.2'
5
+ VERSION = '1.15.0'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -19,6 +19,7 @@ module Synvert
19
19
  autoload :Engine, 'synvert/core/engine'
20
20
  autoload :Utils, 'synvert/core/utils'
21
21
  autoload :Strategy, 'synvert/core/strategy'
22
+ autoload :SnippetNotFoundError, 'synvert/core/snippet_not_found_error'
22
23
  end
23
24
  end
24
25
 
@@ -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
- it 'evals snippet by http url' do
7
- expect_any_instance_of(URI).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'name' do\nend"))
8
- rewriter = described_class.eval_snippet('http://example.com/rewriter.rb')
9
- expect(rewriter.group).to eq 'group'
10
- expect(rewriter.name).to eq 'name'
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
- it 'adds snippet by file path' do
14
- expect(File).to receive(:exist?).and_return(true)
15
- expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
16
- rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
17
- expect(rewriter.group).to eq 'group'
18
- expect(rewriter.name).to eq 'name'
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
- it 'adds snippet by snippet name' do
22
- expect(File).to receive(:exist?).and_return(false)
23
- expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
24
- rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
25
- expect(rewriter.group).to eq 'group'
26
- expect(rewriter.name).to eq 'name'
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.14.2
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-19 00:00:00.000000000 Z
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.22
191
+ rubygems_version: 3.3.26
191
192
  signing_key:
192
193
  specification_version: 4
193
194
  summary: convert ruby code to better syntax.