open_source_location 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: 61d2d5169811832ef4c13b63cc916da92ea1ea8d0850e62d5f78217a22076f38
4
+ data.tar.gz: 64abde7fa0a7a4eb71fd99076602d023af953fbd7139734302a61c87267ca868
5
+ SHA512:
6
+ metadata.gz: 5a2b42a4ebb255d2beb1d9db015481a2b3ba3958d14d26b5658f41223d6e9334f495af71a3b2c61447494ad3445e16bfa2db359d5d3544b1fe9f37b5f5bb6588
7
+ data.tar.gz: 012b9d0a3e46fe156ddc47ce0e39f8282eb1b59ab41f287bc6408e96027ddb14cce707b4cb0fa32d116bab687e98c74df5b6e0648455fdbbd0890d211bdc9dfe
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in open_source.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ open_source_location (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (13.0.6)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ open_source_location!
30
+ rake (~> 13.0)
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Joseph Johansen
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,53 @@
1
+ # Open Source
2
+
3
+ Quickly open the source file of a Class, Module, or Method in your favourite editor, from Ruby.
4
+
5
+ ## Usage
6
+
7
+ First make sure your EDITOR environment variable is set ([How do I find and set my $EDITOR environment variable?](https://askubuntu.com/questions/432524/how-do-i-find-and-set-my-editor-environment-variable)).
8
+
9
+ Then use as you wish
10
+
11
+ ```ruby
12
+ # probably in a console, though doesn't have to be
13
+ open_source "User" # opens file where User class is defined
14
+ oso User # same as above; use the aliases for ease of use!
15
+ oso Authenticable # opens file where Authenticable module is defined
16
+ oso :log # opens file where "log" method is defined
17
+ oso "log" # same
18
+ oso method :log # you get the idea
19
+ oso User.instance_method(:name)
20
+ ```
21
+
22
+ Line numbers are supported too, currently for Vi and VsCode.
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'open_source_location'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle install
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install open_source_location
39
+
40
+
41
+ ## Development
42
+
43
+ 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.
44
+
45
+ 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).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/johansenja/open_source.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "open_source"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenSource
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "open_source/version"
4
+
5
+ module OpenSource
6
+ class UnopenableError < StandardError; end
7
+ class UnreachableError < StandardError; end
8
+ class UnknownLocationError < StandardError; end
9
+ class NoEditorError < StandardError; end
10
+
11
+ module Public
12
+ def open_source(obj)
13
+ case obj.class
14
+ when Class, Module
15
+ case obj
16
+ when String
17
+ begin
18
+ Utils.handle_constant! Object.const_get obj
19
+ rescue NameError
20
+ Utils.handle_method_or_name! obj
21
+ end
22
+ when Symbol
23
+ Utils.handle_method_or_name! obj
24
+ when Method, UnboundMethod
25
+ Utils.handle_method_or_name! obj
26
+ else
27
+ Utils.handle_constant! obj
28
+ end
29
+ else
30
+ raise UnopenableError, "Cannot open #{obj.inspect} (#{obj.class}) in an editor"
31
+ end
32
+ end
33
+
34
+ # TODO: consider if any other aliases could be useful
35
+ %i[oso].each { |m| alias_method m, :open_source }
36
+ end
37
+
38
+ module Utils
39
+ module_function
40
+
41
+ def handle_constant!(const)
42
+ raise UnopenableError, "Cannot open an anonymous #{const.class}" unless const.class.name
43
+
44
+ loc = Object.const_source_location const.to_s
45
+
46
+ if !loc
47
+ raise UnopenableError, "Cannot open #{const} in an editor"
48
+ elsif loc.empty?
49
+ raise UnknownLocationError, "Cannot find the location of #{const} - perhaps it is defined in native code."
50
+ else
51
+ open_location loc
52
+ end
53
+ end
54
+
55
+ def handle_method_or_name!(meth)
56
+ case meth
57
+ when Symbol, String
58
+ meth = method meth
59
+ end
60
+
61
+ loc = meth.source_location
62
+ if loc
63
+ open_location loc
64
+ else
65
+ raise UnknownLocationError, "Cannot find the location of #{meth} - perhaps it is defined in native code."
66
+ end
67
+ rescue NameError
68
+ raise UnopenableError, "Cannot open #{meth.inspect} (#{meth.class}) in an editor"
69
+ end
70
+
71
+ def open_location(loc)
72
+ file, line = loc
73
+ command = Utils.get_command file, line
74
+ Runner.run *command
75
+ end
76
+
77
+ def get_command(file, line)
78
+ editor = ENV["EDITOR"]
79
+
80
+ # different editors handle going to specific lines differently
81
+ case editor
82
+ when nil
83
+ raise NoEditorError, "the EDITOR env variable must be set to use this feature"
84
+ when /\s?n?vim?(\s|\z)/
85
+ [editor, "+#{line}", file]
86
+ when /\s?code(\s|\z)/
87
+ [editor, "--goto", "#{file}:#{line}"]
88
+ else
89
+ # fallback - ignore line number
90
+ # TODO: add support for other editors
91
+ [editor, file]
92
+ end
93
+ end
94
+
95
+ class Runner
96
+ def self.run(*cmd)
97
+ system *cmd
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ include OpenSource::Public
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/open_source/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "open_source_location"
7
+ spec.version = OpenSource::VERSION
8
+ spec.authors = ["Joseph Johansen"]
9
+ spec.email = ["joe@stotles.com"]
10
+
11
+ spec.summary = "Open a method, class or module in your favourite editor"
12
+ spec.homepage = "https://github.com/johansenja/open_source"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/johansenja/open_source"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_source_location
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Johansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - joe@stotles.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - lib/open_source.rb
30
+ - lib/open_source/version.rb
31
+ - open_source.gemspec
32
+ homepage: https://github.com/johansenja/open_source
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org/
37
+ homepage_uri: https://github.com/johansenja/open_source
38
+ source_code_uri: https://github.com/johansenja/open_source
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.4.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.1.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Open a method, class or module in your favourite editor
58
+ test_files: []