dpu 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a136a963872583eb77ab03abe04b4f06a281e33f6bf3e74703eb1920545c2839
4
+ data.tar.gz: 112faf5ef0d4ab660844a3c81eacf5cda68072ad00015fe0862ed7d242b26371
5
+ SHA512:
6
+ metadata.gz: 95b54d43d7a9352b305047415af4f0eb9c379162ab52acbf89007b72e48df6253e21faf622e454584944b0fd8d4e4674b57af013f568389c92dcadefe9303259
7
+ data.tar.gz: 58c2fe2c850536c1c3dcafbcac54e7a56c40a9b268570937ae25b4360e6ef172bac9d24e6e524a1d4b799fcb3e0b8c05073136df8f19e76c130b30b571bdc4eb
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in dpu.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2023 Yuya.Nishida.
2
+
3
+ X11 License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # dpu: determine permanent URI
2
+
3
+ `dpu` command shows us permanent source-code URI.
4
+
5
+ ```console
6
+ $ dpu array.c 4133 4136
7
+ https://github.com/ruby/ruby/blob/v3_2_2/array.c#L4133-L4136
8
+ ```
9
+
10
+ ## Requirements
11
+
12
+ - Ruby
13
+
14
+ ## Installation
15
+
16
+ ```console
17
+ $ gem install dpu
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Get permanent source code URI:
23
+
24
+ ```console
25
+ $ dpu path_to_code
26
+ ```
27
+
28
+ Get permanent source code URI with line range:
29
+
30
+ ```console
31
+ $ dpu path_to_code start_line_number end_line_number
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nishidayuya/dpu .
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/exe/dpu ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "dpu"
4
+
5
+ Dpu::Cli.run(ARGV)
data/lib/dpu/cli.rb ADDED
@@ -0,0 +1,22 @@
1
+ class Dpu::Cli
2
+ USAGE = <<~EOS
3
+ #{File.basename(Process.argv0)} path start_line_number end_line_number
4
+ EOS
5
+
6
+ class << self
7
+ def run(argv)
8
+ s_path, s_start_line_number, s_end_line_number = *argv
9
+ if !s_path
10
+ $stderr.puts(USAGE)
11
+ exit(1)
12
+ end
13
+
14
+ path = Pathname(s_path).expand_path
15
+ start_line_number = s_start_line_number&.to_i
16
+ end_line_number = s_end_line_number&.to_i
17
+
18
+ uri = Dpu.determine_permanent_uri(path, start_line_number, end_line_number)
19
+ puts(uri)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dpu
4
+ VERSION = "0.1.0"
5
+ end
data/lib/dpu.rb ADDED
@@ -0,0 +1,86 @@
1
+ require "open3"
2
+ require "pathname"
3
+ require "uri"
4
+
5
+ module Dpu
6
+ autoload :Cli, "dpu/cli"
7
+ autoload :VERSION, "dpu/vesion"
8
+
9
+ class << self
10
+ GITHUB_REPOSITORY_URI_TEMPLATE = "https://github.com/%{account_name}/%{repository_name}"
11
+
12
+ def determine_permanent_uri(path, start_line_number, end_line_number)
13
+ relative_path = determine_relative_path(path)
14
+
15
+ permanent_uri_parts = [
16
+ determine_repository_uri(path),
17
+ "blob",
18
+ find_same_content_version(path, relative_path) || determine_commit_id(path),
19
+ relative_path,
20
+ ]
21
+ permanent_uri = URI(permanent_uri_parts.join("/"))
22
+ permanent_uri.fragment = determine_fragment(start_line_number, end_line_number)
23
+ return permanent_uri
24
+ end
25
+
26
+ private
27
+
28
+ def run_command(*args, **kwargs)
29
+ stdout, status = *Open3.capture2(*args, **kwargs)
30
+ if !status.success?
31
+ raise "failure to run command: args=#{args.inspect} kwargs=#{kwargs.inspect}"
32
+ end
33
+ return stdout
34
+ end
35
+
36
+ def determine_relative_path(path)
37
+ stdout = run_command("git rev-parse --show-toplevel", chdir: path.dirname)
38
+ repository_top_path = Pathname(stdout.chomp)
39
+ relative_path = path.relative_path_from(repository_top_path)
40
+ return relative_path
41
+ end
42
+
43
+ def determine_repository_uri(path)
44
+ stdout = run_command("git remote get-url origin", chdir: path.dirname)
45
+ repository_http_or_ssh_url = stdout.chomp
46
+
47
+ md = %r{\Agit@github\.com:(?<account_name>[^/]+)/(?<repository_name>.+)(?=\.git)}.match(repository_http_or_ssh_url)
48
+ if !md
49
+ return URI(repository_http_or_ssh_url)
50
+ end
51
+
52
+ md => {account_name:, repository_name:}
53
+ url = GITHUB_REPOSITORY_URI_TEMPLATE % {account_name:, repository_name:}
54
+ URI(url)
55
+ end
56
+
57
+ def determine_commit_id(path)
58
+ stdout = run_command("git rev-parse HEAD", chdir: path.dirname)
59
+ commit_id = stdout.chomp
60
+ return commit_id
61
+ end
62
+
63
+ def find_same_content_version(path, relative_path_from_repository_root)
64
+ stdout = run_command(*%w[git tag --list [0-9]* v[0-9]*], chdir: path.dirname)
65
+ versions = stdout.each_line(chomp: true).sort_by { |v|
66
+ comparable_version =
67
+ v.sub(/\Av/, "").
68
+ gsub(/[_@]+/, ".") # https://github.com/ruby/ruby/tree/v1_8_5_55%4013008
69
+ Gem::Version.new(comparable_version)
70
+ }
71
+
72
+ content_in_head = path.read
73
+ same_content_version = versions.reverse_each.find { |version|
74
+ content_in_version, _status = *Open3.capture2(*%W[git show #{version}:#{relative_path_from_repository_root}], chdir: path.dirname, err: "/dev/null")
75
+ content_in_head == content_in_version
76
+ }
77
+ return same_content_version
78
+ end
79
+
80
+ def determine_fragment(start_line_number, end_line_number)
81
+ return nil if !start_line_number
82
+ return "L#{start_line_number}" if !end_line_number || start_line_number == end_line_number
83
+ return "L#{start_line_number}-L#{end_line_number}"
84
+ end
85
+ end
86
+ end
data/sig/dpu.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Dpu
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dpu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuya.Nishida.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: debug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - yuya@j96.org
30
+ executables:
31
+ - dpu
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - exe/dpu
40
+ - lib/dpu.rb
41
+ - lib/dpu/cli.rb
42
+ - lib/dpu/version.rb
43
+ - sig/dpu.rbs
44
+ homepage: https://github.com/nishidayuya/dpu
45
+ licenses: []
46
+ metadata:
47
+ homepage_uri: https://github.com/nishidayuya/dpu
48
+ source_code_uri: https://github.com/nishidayuya/dpu
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.6.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.4.10
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: determine permanent URI
68
+ test_files: []