lexy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5af84bb657dda88dd9f2e8959e6331f28a5cf4787e024ab1114c9714c33f0b00
4
+ data.tar.gz: 03d035b62c0031d655a6d62f925db61b4214c56872ef00a779865bb781c9e171
5
+ SHA512:
6
+ metadata.gz: 9c3a3a499d1ca8d382a95c846841be516e558f05fc2c0a293d947bb489cdd84b5d73c97708b6d748bde094edb52059efd6d89318e326554a7888fe1463160f85
7
+ data.tar.gz: 828a82bbc296d0a316efd7fdd694b97f8d297e93f07963742530395804554edd860aea10ce30260f9dc5be341cb1a9f86b50fd5a16d8f3c51c1f85b1aec4ee92
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/MethodLength:
8
+ Enabled: false
9
+
10
+ Metrics/AbcSize
11
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.6
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lexy.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lexy (0.1.0)
5
+ parser
6
+ thor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ coderay (1.1.2)
13
+ method_source (1.0.0)
14
+ minitest (5.14.0)
15
+ parallel (1.19.1)
16
+ parser (2.7.1.2)
17
+ ast (~> 2.4.0)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.0.0)
22
+ rexml (3.2.4)
23
+ rubocop (0.83.0)
24
+ parallel (~> 1.10)
25
+ parser (>= 2.7.0.1)
26
+ rainbow (>= 2.2.2, < 4.0)
27
+ rexml
28
+ ruby-progressbar (~> 1.7)
29
+ unicode-display_width (>= 1.4.0, < 2.0)
30
+ ruby-progressbar (1.10.1)
31
+ thor (1.0.1)
32
+ unicode-display_width (1.7.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.17)
39
+ lexy!
40
+ minitest (~> 5.0)
41
+ pry
42
+ rubocop
43
+
44
+ BUNDLED WITH
45
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Rostislav Katin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Specs creator
2
+ I want to have a tool to generate a spec for a given file
3
+
4
+ ## Interface:
5
+
6
+ ```bash
7
+ spec_creator $(pwd)
8
+ ```
9
+
10
+ ## Todo:
11
+
12
+ * Reverse file path to find 'specs' folder
13
+ * While reversing the path, build a stack with folders. It will be the spec path
14
+ * Extract the file name as file path[-1]
15
+ * Use regular expressions (AST?) to get class name
16
+
17
+ ## After:
18
+
19
+ * Add thor
20
+ * Add AST
21
+ * Add vim integration with %:p
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lexy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/lexy ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'bundler/setup'
6
+ require 'lexy'
7
+
8
+ Lexy::CLI.start(ARGV)
data/lexy.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lexy/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lexy'
9
+ spec.version = Lexy::VERSION
10
+ spec.authors = ['Rostislav Katin']
11
+ spec.email = ['poctek5@gmail.com']
12
+
13
+ spec.summary = 'Development helpers'
14
+ spec.homepage = 'https://github.com/poctek/lexy'
15
+ spec.license = 'MIT'
16
+
17
+ spec.metadata = { 'source_code_uri' => 'https://github.com/poctek' }
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = ['lexy']
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'thor', '~> 0.19.0'
27
+ spec.add_dependency 'parser', '~> 2.7.0.0'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.17'
30
+ spec.add_development_dependency 'minitest', '~> 5.0'
31
+ spec.add_development_dependency 'rubocop', '~> 0.8.3'
32
+
33
+ spec.add_development_dependency 'pry', '~> 0.13.1'
34
+ end
data/lib/lexy/cli.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module Lexy
6
+ class CLI < Thor
7
+ desc 'create_spec_file /full/path/to/file.rb', 'Create if not exists spec file for given file'
8
+ def create_spec_file(path)
9
+ Lexy::SpecCreator.new.call(path)
10
+ end
11
+
12
+ private
13
+
14
+ def self.exit_on_failure?
15
+ false
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'parser/current'
5
+
6
+ module Lexy
7
+ class SpecCreator
8
+ def call(path)
9
+ stack = build_dirs_stack(path)
10
+ spec_path = build_spec_path(path, stack)
11
+ ensure_folder_created(spec_path)
12
+ const = extract_constant_name(path)
13
+ create_spec_file(spec_path, const)
14
+ end
15
+
16
+ def build_dirs_stack(path)
17
+ current_path = path.split('/')
18
+ dirs_stack = []
19
+ dir_found = false
20
+
21
+ until dir_found
22
+ if specs_dir?(File.join(*current_path))
23
+ dir_found = true
24
+ else
25
+ dirs_stack << current_path[-1]
26
+
27
+ current_path = current_path[0...-1]
28
+
29
+ raise 'No specs folder in given tree' if current_path.length.zero?
30
+ end
31
+ end
32
+
33
+ dirs_stack + ['spec']
34
+ end
35
+
36
+ def specs_dir?(dir)
37
+ Dir.glob('spec', base: dir).any?
38
+ end
39
+
40
+ def build_spec_path(path, dirs_stack)
41
+ base_path = path.split('/') - dirs_stack
42
+ spec_name = dirs_stack[0].split('.')[0] + '_spec.rb'
43
+ spec_path = dirs_stack[1..-1].reverse
44
+
45
+ base_path + spec_path + [spec_name]
46
+ end
47
+
48
+ def ensure_folder_created(path)
49
+ FileUtils.mkdir_p(File.join(path[0...-1]))
50
+ end
51
+
52
+ def create_spec_file(path, const)
53
+ path = File.join(path)
54
+
55
+ if File.exists?(path)
56
+ puts 'Spec file already created'
57
+ return
58
+ end
59
+
60
+ template = <<~SPEC
61
+ # frozen_string_literal: true
62
+
63
+ RSpec.describe #{const.join('::')} do
64
+ subject { described_class.new }
65
+ end
66
+ SPEC
67
+
68
+ File.open(path, 'a+') { |file| file.write(template) }
69
+ end
70
+
71
+ def extract_constant_name(path)
72
+ file_contents = File.read(path)
73
+
74
+ ast = Parser::CurrentRuby.parse(file_contents)
75
+
76
+ process_scope(ast)
77
+ end
78
+
79
+ def process_scope(ast, const = [])
80
+ return unless ast.is_a?(Parser::AST::Node)
81
+
82
+ names = []
83
+ local_const = const.dup
84
+
85
+ case ast.type
86
+ when :module
87
+ name = node_name(ast)
88
+
89
+ local_const << name
90
+ when :class
91
+ name = node_name(ast)
92
+
93
+ local_const << name
94
+ names << local_const
95
+ end
96
+
97
+ nodes_names = ast.to_a.flat_map { |node| process_scope(node, local_const) }.reject(&:nil?)
98
+ names.push(*nodes_names)
99
+
100
+ return if names.empty?
101
+
102
+ names
103
+ end
104
+
105
+ def node_name(ast)
106
+ ast.to_a.first.to_a[1]
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module Lexy
2
+ VERSION = "0.1.1"
3
+ end
data/lib/lexy.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lexy/version'
4
+ require 'lexy/spec_creator'
5
+ require 'lexy/cli'
6
+
7
+ module Lexy
8
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lexy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Rostislav Katin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.19.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.13.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.13.1
97
+ description:
98
+ email:
99
+ - poctek5@gmail.com
100
+ executables:
101
+ - lexy
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rubocop.yml"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - exe/lexy
117
+ - lexy.gemspec
118
+ - lib/lexy.rb
119
+ - lib/lexy/cli.rb
120
+ - lib/lexy/spec_creator.rb
121
+ - lib/lexy/version.rb
122
+ homepage: https://github.com/poctek/lexy
123
+ licenses:
124
+ - MIT
125
+ metadata:
126
+ source_code_uri: https://github.com/poctek
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.0.3
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Development helpers
146
+ test_files: []