mj 0.1.0

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: c2e748dee2e36cb0160773697c160a224fd1f835852c4d9654e5be3fa6eae4a7
4
+ data.tar.gz: 3cdd9fcbe46b922e7d5fa59c4cc728a3f93a16206259ec81fa66af9ce53728b8
5
+ SHA512:
6
+ metadata.gz: '0854440ae12e23d51031874cb521e3bf265b11cd36fd1bce4ab7badfa86f46f1b5471d19937479ff33eb301d0407e92040a1417d8b146337ae5dd20d5b8b090b'
7
+ data.tar.gz: 21352d2b67831e6c8969e5c0668ac6f23f2586631a2962d3ac03c1b8a20f8a9296171a010a10f5f257abe9262c8a2f9e0814d47c24b8fc0b8c6919db73ea12d5
data/.project.vim ADDED
@@ -0,0 +1,33 @@
1
+ autocmd FileType ruby nnoremap <buffer> <leader>T <esc>:call RunTestFile()<cr>
2
+ autocmd FileType ruby nnoremap <buffer> <leader>t <esc>:call RunTestFileFilteredByLine()<cr>
3
+ autocmd FileType ruby nnoremap <buffer> <leader>at <esc>:call RunAllTests()<cr>
4
+ function! RubocopFixCs(target)
5
+ let options=''
6
+ let cmd = 'bundle exec rubocop'
7
+
8
+ if filereadable('./bin/bundle')
9
+ let cmd = './bin/bundle exec rubocop'
10
+ endif
11
+
12
+ if filereadable('.rubocop.yml')
13
+ let options = ' --config=.rubocop.yml '
14
+ endif
15
+
16
+ let full_command = cmd . " -A " . options . a:target
17
+ call ClearEchoAndExecute(full_command)
18
+ endfunction
19
+
20
+ function! RunTestFileFilteredByLine()
21
+ let command = "bundle exec run_test " . expand('%') . " --line=" . line(".")
22
+ call ClearEchoAndExecute(command)
23
+ endfunction
24
+
25
+ function! RunTestFile()
26
+ let command = "bundle exec run_test " . expand('%')
27
+ call ClearEchoAndExecute(command)
28
+ endfunction
29
+
30
+ function! RunAllTests()
31
+ let command = "bundle exec run_test " . expand('%') . " --all"
32
+ call ClearEchoAndExecute(command)
33
+ endfunction
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,96 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.7
8
+ NewCops: enable
9
+ Exclude:
10
+ - 'tmp/**/*'
11
+ - 'node_modules/**/*'
12
+ - 'script/**/*'
13
+ - 'log/**/*'
14
+ - 'docker/**/*'
15
+ - 'doc/**/*'
16
+ - 'bin/**/*'
17
+ - 'Rakefile'
18
+ - 'config.ru'
19
+ - 'config/**/*'
20
+ - 'db/schema.rb'
21
+ - 'node_modules/**/*'
22
+ - 'vendor/**/*'
23
+
24
+
25
+ Layout/FirstArrayElementIndentation:
26
+ EnforcedStyle: consistent
27
+
28
+ Layout/LineLength:
29
+ Max: 120
30
+
31
+ Layout/MultilineMethodCallIndentation:
32
+ EnforcedStyle: indented
33
+
34
+ Layout/MultilineOperationIndentation:
35
+ Enabled: true
36
+ EnforcedStyle: indented
37
+
38
+ Lint/MissingSuper:
39
+ Enabled: false
40
+
41
+ Metrics/BlockLength:
42
+ Exclude:
43
+ - 'spec/**/*'
44
+
45
+ Naming/RescuedExceptionsVariableName:
46
+ PreferredName: 'exception'
47
+
48
+ Naming/PredicateName:
49
+ Enabled: false
50
+
51
+ Style/AccessorGrouping:
52
+ Enabled: false
53
+
54
+ Style/ClassAndModuleChildren:
55
+ Enabled: false
56
+
57
+ Style/Documentation:
58
+ Enabled: false
59
+
60
+ Style/FormatStringToken:
61
+ EnforcedStyle: template
62
+
63
+ Style/GuardClause:
64
+ Enabled: false
65
+
66
+ Style/IfUnlessModifier:
67
+ Enabled: false
68
+
69
+ Style/StringLiterals:
70
+ Enabled: true
71
+ EnforcedStyle: double_quotes
72
+
73
+ Style/StringLiteralsInInterpolation:
74
+ Enabled: true
75
+ EnforcedStyle: double_quotes
76
+
77
+ RSpec:
78
+ Exclude:
79
+ - 'spec/factories/**/*'
80
+
81
+ RSpec/ExampleLength:
82
+ Max: 10
83
+
84
+ RSpec/ExpectChange:
85
+ Enabled: false
86
+
87
+ RSpec/MultipleExpectations:
88
+ Enabled: false
89
+
90
+ RSpec/NestedGroups:
91
+ Max: 4
92
+
93
+ # while that is a good rule, AR objects can't be properly instance_double'd
94
+ RSpec/VerifiedDoubles:
95
+ Enabled: false
96
+
data/.test_runner.yml ADDED
@@ -0,0 +1,10 @@
1
+ # test_runner.yml
2
+ adapters:
3
+ rspec_with_spring:
4
+ adapter: custom
5
+ file_pattern: ".*_spec.rb"
6
+ command: "time bundle exec rspec"
7
+ commands:
8
+ all: "{command}"
9
+ file: "{command} {file}"
10
+ line: "{command} {file}:{line}"
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-04
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at marcelo.jacobus@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mj.gemspec
6
+ gemspec
7
+
8
+ gem "awesome_print"
9
+ gem "koine-test_runner"
10
+ gem "rake", "~> 13.0"
11
+ gem "rspec", "~> 3.0"
12
+ gem "rubocop", "~> 1.21"
13
+ gem "rubocop-performance"
14
+ gem "rubocop-rake"
15
+ gem "rubocop-rspec"
16
+ gem "simplecov", require: false
17
+ gem "simplecov-lcov", require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mj (0.1.0)
5
+ thor (~> 1.2.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ awesome_print (1.9.2)
12
+ diff-lcs (1.5.0)
13
+ docile (1.4.0)
14
+ koine-test_runner (0.4.0)
15
+ parallel (1.21.0)
16
+ parser (3.1.1.0)
17
+ ast (~> 2.4.1)
18
+ rainbow (3.1.1)
19
+ rake (13.0.6)
20
+ regexp_parser (2.2.1)
21
+ rexml (3.2.5)
22
+ rspec (3.11.0)
23
+ rspec-core (~> 3.11.0)
24
+ rspec-expectations (~> 3.11.0)
25
+ rspec-mocks (~> 3.11.0)
26
+ rspec-core (3.11.0)
27
+ rspec-support (~> 3.11.0)
28
+ rspec-expectations (3.11.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-mocks (3.11.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-support (3.11.0)
35
+ rubocop (1.25.1)
36
+ parallel (~> 1.10)
37
+ parser (>= 3.1.0.0)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8, < 3.0)
40
+ rexml
41
+ rubocop-ast (>= 1.15.1, < 2.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 3.0)
44
+ rubocop-ast (1.16.0)
45
+ parser (>= 3.1.1.0)
46
+ rubocop-performance (1.13.2)
47
+ rubocop (>= 1.7.0, < 2.0)
48
+ rubocop-ast (>= 0.4.0)
49
+ rubocop-rake (0.6.0)
50
+ rubocop (~> 1.0)
51
+ rubocop-rspec (2.9.0)
52
+ rubocop (~> 1.19)
53
+ ruby-progressbar (1.11.0)
54
+ simplecov (0.21.2)
55
+ docile (~> 1.1)
56
+ simplecov-html (~> 0.11)
57
+ simplecov_json_formatter (~> 0.1)
58
+ simplecov-html (0.12.3)
59
+ simplecov-lcov (0.8.0)
60
+ simplecov_json_formatter (0.1.4)
61
+ thor (1.2.1)
62
+ unicode-display_width (2.1.0)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ awesome_print
69
+ koine-test_runner
70
+ mj!
71
+ rake (~> 13.0)
72
+ rspec (~> 3.0)
73
+ rubocop (~> 1.21)
74
+ rubocop-performance
75
+ rubocop-rake
76
+ rubocop-rspec
77
+ simplecov
78
+ simplecov-lcov
79
+
80
+ BUNDLED WITH
81
+ 2.3.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Marcelo Jacobus
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,47 @@
1
+ # Mj
2
+
3
+ A collection of useful commands for my personal use.
4
+
5
+ [![Ruby](https://github.com/mjacobus/mj/actions/workflows/main.yml/badge.svg)](https://github.com/mjacobus/mj/actions/workflows/main.yml)
6
+ [![Coverage Status](https://coveralls.io/repos/github/mjacobus/mj/badge.svg?branch=main)](https://coveralls.io/github/mjacobus/mj?branch=main)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/52468dead5a8c7568a0f/maintainability)](https://codeclimate.com/github/mjacobus/mj/maintainability)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'mj'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install mj
24
+
25
+ ## Usage
26
+
27
+ ```bash
28
+ mj --help
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/mj. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mjacobus/mj/blob/main/CODE_OF_CONDUCT.md).
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+ ## Code of Conduct
46
+
47
+ Everyone interacting in the Mj project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mjacobus/mj/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/exe/mj ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require "mj"
7
+
8
+ Mj::Cli.start(ARGV)
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ class Candidate
6
+ attr_reader :path
7
+ attr_reader :type
8
+
9
+ def initialize(path:, type:)
10
+ @path = path
11
+ @type = type
12
+ end
13
+
14
+ def exist?
15
+ File.exist?(path)
16
+ end
17
+
18
+ def ==(other)
19
+ unless other.is_a?(self.class)
20
+ return false
21
+ end
22
+
23
+ other.path == path && other.type == type
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ class Candidates
6
+ include Enumerable
7
+
8
+ def initialize(candidates = [])
9
+ @items = candidates.dup
10
+ end
11
+
12
+ def each(&block)
13
+ @items.each(&block)
14
+ end
15
+
16
+ def add(candidate)
17
+ @items.push(candidate)
18
+ end
19
+
20
+ def of_types(types)
21
+ select { |item| types.include?(item.type) }
22
+ end
23
+
24
+ def existing
25
+ select(&:exist?)
26
+ end
27
+
28
+ def unique
29
+ new(@items.uniq(&:path))
30
+ end
31
+
32
+ def select(&block)
33
+ new(@items.select(&block))
34
+ end
35
+
36
+ def after(reference_file)
37
+ next_file(@items, reference_file)
38
+ end
39
+
40
+ def before(reference_file)
41
+ next_file(@items.reverse, reference_file)
42
+ end
43
+
44
+ private
45
+
46
+ def new(*args)
47
+ self.class.new(*args)
48
+ end
49
+
50
+ def next_file(items, reference_file)
51
+ index = items.find_index { |e| e.path.to_s == reference_file.path.to_s } || -1
52
+ items[index + 1] || items.first
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ class CurrentFile
6
+ attr_reader :path
7
+
8
+ def initialize(path)
9
+ @path = Pathname.new(path.to_s)
10
+ end
11
+
12
+ def to_s
13
+ path.to_s
14
+ end
15
+
16
+ def extension
17
+ parts = split(".")
18
+
19
+ if parts.length > 1
20
+ parts.last
21
+ end
22
+ end
23
+
24
+ def trim_slashes
25
+ to_s.gsub("//", "/").sub(%r{^/}, "").sub(%r{/$}, "")
26
+ end
27
+
28
+ def path_without_extension
29
+ sub(/.#{extension}$/, "")
30
+ end
31
+
32
+ def name
33
+ to_s.split("/").last
34
+ end
35
+
36
+ def split(char = "/")
37
+ to_s.split(char)
38
+ end
39
+
40
+ def start_with?(*args)
41
+ to_s.start_with?(*args)
42
+ end
43
+
44
+ def without_prefix(prefix)
45
+ sub(/^#{prefix}/, "")
46
+ end
47
+
48
+ def without_suffix(suffix)
49
+ sub(/#{suffix}$/, "")
50
+ end
51
+
52
+ def match?(*args)
53
+ to_s.match?(*args)
54
+ end
55
+
56
+ def sub(find, replace)
57
+ new(to_s.sub(find, replace))
58
+ end
59
+
60
+ def gsub(find, replace)
61
+ new(to_s.gsub(find, replace))
62
+ end
63
+
64
+ def join(other_part)
65
+ new(@path.join(other_part.to_s))
66
+ end
67
+
68
+ def prefix_with(prefix)
69
+ new(prefix).join(self)
70
+ end
71
+
72
+ def exist?
73
+ ::File.exist?(to_s)
74
+ end
75
+
76
+ private
77
+
78
+ def new(path)
79
+ self.class.new(path)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ class Resolver
6
+ def initialize
7
+ @stack = []
8
+ end
9
+
10
+ def add(resolver)
11
+ @stack.push(resolver)
12
+ end
13
+
14
+ def resolve(file)
15
+ file = AlternativeFile::CurrentFile.new(file.to_s)
16
+ resolved = []
17
+
18
+ @stack.each do |resolver|
19
+ resolved.push(resolver.resolve(file))
20
+ end
21
+
22
+ AlternativeFile::Candidates.new(resolved.flatten.compact)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Resolvers
6
+ class Base
7
+ def resolve(file)
8
+ [].tap do |alternatives|
9
+ if apply_to?(file)
10
+ create_alternatives(file, alternatives)
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def create_candidate(path, type)
18
+ AlternativeFile::Candidate.new(path: path, type: type)
19
+ end
20
+
21
+ def apply_to?(_file)
22
+ raise NoMethodError, "Method apply_to? not implemented"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Resolvers
6
+ module Ruby
7
+ class RailsControllerResolver < Resolvers::Base
8
+ private
9
+
10
+ def apply_to?(file)
11
+ file.extension == "rb" && file.start_with?(
12
+ "app/controllers",
13
+ "spec/integration",
14
+ "test/integration",
15
+ "test/controllers",
16
+ "spec/controllers"
17
+ )
18
+ end
19
+
20
+ def create_alternatives(file, alternatives)
21
+ if file.start_with?("app/controllers")
22
+ add_controller_test(file, alternatives, "spec")
23
+ add_controller_test(file, alternatives, "test")
24
+ add_integration_test(file, alternatives, "spec")
25
+ add_integration_test(file, alternatives, "test")
26
+ return
27
+ end
28
+
29
+ resolve_controller(file, alternatives)
30
+ end
31
+
32
+ def add_integration_test(file, alternatives, type)
33
+ path = file.without_prefix("app/controllers").without_suffix(".rb").trim_slashes
34
+ alternative = create_candidate("#{type}/integration/#{path}_#{type}.rb", "integration_#{type}")
35
+ alternatives.push(alternative)
36
+ end
37
+
38
+ def add_controller_test(file, alternatives, type)
39
+ path = file.without_prefix("app/controllers").without_suffix(".rb").trim_slashes
40
+ alternative = create_candidate("#{type}/controllers/#{path}_#{type}.rb", "controller_#{type}")
41
+ alternatives.push(alternative)
42
+ end
43
+
44
+ def resolve_controller(file, alternatives)
45
+ controller_path = file.sub("test/integration", "app/controllers")
46
+ .sub("spec/integration", "app/controllers")
47
+ .sub("spec/controllers", "app/controllers")
48
+ .sub("test/controllers", "app/controllers")
49
+ .sub("_spec.rb", ".rb")
50
+ .sub("_test.rb", ".rb")
51
+ .to_s
52
+
53
+ alternatives.push(create_candidate(controller_path, "controller"))
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Resolvers
6
+ module Ruby
7
+ class RailsResolver < Resolvers::Base
8
+ private
9
+
10
+ def apply_to?(file)
11
+ file.extension == "rb"
12
+ end
13
+
14
+ def create_alternatives(file, alternatives)
15
+ ruby_file = Ruby::RubyFile.new(file)
16
+ alternatives.push(create_candidate("app/#{ruby_file.class_path}.rb", "model"))
17
+ alternatives.push(create_candidate("spec/#{ruby_file.class_path}_spec.rb", "rspec"))
18
+ alternatives.push(create_candidate("test/#{ruby_file.class_path}_test.rb", "minitest"))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Resolvers
6
+ module Ruby
7
+ class RubyFile < AlternativeFile::CurrentFile
8
+ def class_path
9
+ path_without_extension
10
+ .without_prefix("app")
11
+ .without_prefix("spec")
12
+ .without_suffix("_spec")
13
+ .without_suffix("_test")
14
+ .trim_slashes
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require_relative "candidate"
5
+ require_relative "candidates"
6
+ require_relative "current_file"
7
+ require_relative "resolver"
8
+ require_relative "resolvers/base"
9
+ require_relative "resolvers/ruby/rails_resolver"
10
+ require_relative "resolvers/ruby/rails_controller_resolver"
11
+ require_relative "resolvers/ruby/ruby_file"
12
+
13
+ module Mj
14
+ module AlternativeFile
15
+ class ThorCommand < Thor
16
+ desc "list <reference-file>", "List all alternative files"
17
+ option :types, type: :string, banner: "<comma-separated-types>", aliases: :t
18
+ option :exists, type: :boolean, banner: "files that exist", aliases: :e
19
+ def list(reference_file)
20
+ file = CurrentFile.new(reference_file)
21
+ print_candidates(resolve(file))
22
+ end
23
+
24
+ desc "next <reference-file>", "Next alternative file"
25
+ option :types, type: :string, banner: "<comma-separated-types>", aliases: :t
26
+ option :exists, type: :boolean, banner: "files that exist", aliases: :e
27
+ def next(reference_file)
28
+ file = CurrentFile.new(reference_file)
29
+ candidate = resolve(file).after(file)
30
+ print_candidates([candidate].compact)
31
+ end
32
+
33
+ desc "prev <reference-file>", "Previous alternative file"
34
+ option :types, type: :string, banner: "<comma-separated-types>", aliases: :t
35
+ option :exists, type: :boolean, banner: "files that exist", aliases: :e
36
+ def prev(reference_file)
37
+ file = CurrentFile.new(reference_file)
38
+ candidate = resolve(file).before(file)
39
+ print_candidates([candidate].compact)
40
+ end
41
+
42
+ def self.resolvers
43
+ @resolvers ||= AlternativeFile::Resolver.new.tap do |resolvers|
44
+ resolvers.add(Resolvers::Ruby::RailsResolver.new)
45
+ resolvers.add(Resolvers::Ruby::RailsControllerResolver.new)
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def print_candidates(candidates)
52
+ $stdout.puts candidates.map(&:path).join(" ")
53
+ end
54
+
55
+ def resolve(file)
56
+ candidates = self.class.resolvers.resolve(file)
57
+
58
+ if options[:types]
59
+ candidates = candidates.of_types(options[:types].to_s.split(","))
60
+ end
61
+
62
+ if options[:exists]
63
+ candidates = candidates.existing
64
+ end
65
+
66
+ candidates.unique
67
+ end
68
+ end
69
+ end
70
+ end
data/lib/mj/cli.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require_relative "alternative_file/thor_command"
5
+
6
+ module Mj
7
+ class Cli < Thor
8
+ def self.exit_on_failure?
9
+ true
10
+ end
11
+
12
+ desc "alternative_file", "lists alternative files"
13
+ subcommand "alternative_file", AlternativeFile::ThorCommand
14
+ end
15
+ end
data/lib/mj/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ VERSION = "0.1.0"
5
+ end
data/lib/mj.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mj/version"
4
+ require_relative "mj/cli"
5
+
6
+ module Mj
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
data/mj.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mj/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mj"
7
+ spec.version = Mj::VERSION
8
+ spec.authors = ["Marcelo Jacobus"]
9
+ spec.email = ["marcelo.jacobus@gmail.com"]
10
+
11
+ spec.summary = "My personal CLI"
12
+ spec.description = "A collection of useful commands for my personal use"
13
+ spec.homepage = "https://github.com/mjacobus/mj"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "thor", "~> 1.2.1"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ spec.metadata["rubygems_mfa_required"] = "true"
39
+ end
data/sig/mj.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Mj
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Jacobus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-11 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: 1.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.1
27
+ description: A collection of useful commands for my personal use
28
+ email:
29
+ - marcelo.jacobus@gmail.com
30
+ executables:
31
+ - mj
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".project.vim"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - ".test_runner.yml"
39
+ - CHANGELOG.md
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - exe/mj
47
+ - lib/mj.rb
48
+ - lib/mj/alternative_file/candidate.rb
49
+ - lib/mj/alternative_file/candidates.rb
50
+ - lib/mj/alternative_file/current_file.rb
51
+ - lib/mj/alternative_file/resolver.rb
52
+ - lib/mj/alternative_file/resolvers/base.rb
53
+ - lib/mj/alternative_file/resolvers/ruby/rails_controller_resolver.rb
54
+ - lib/mj/alternative_file/resolvers/ruby/rails_resolver.rb
55
+ - lib/mj/alternative_file/resolvers/ruby/ruby_file.rb
56
+ - lib/mj/alternative_file/thor_command.rb
57
+ - lib/mj/cli.rb
58
+ - lib/mj/version.rb
59
+ - mj.gemspec
60
+ - sig/mj.rbs
61
+ homepage: https://github.com/mjacobus/mj
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ allowed_push_host: https://rubygems.org
66
+ homepage_uri: https://github.com/mjacobus/mj
67
+ source_code_uri: https://github.com/mjacobus/mj
68
+ changelog_uri: https://github.com/mjacobus/mj/blob/main/CHANGELOG.md
69
+ rubygems_mfa_required: 'true'
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.7.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.2.22
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: My personal CLI
89
+ test_files: []