dpu 0.4.3 → 0.5.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: 226d037ff45361a6b68bd6626166b3d70d818e89ec7f6adbd9b11b97f143948b
4
- data.tar.gz: 9440a89d950b210f409b1d776d2a03c2ee203e2fa52910f59dd7cf004be1abcb
3
+ metadata.gz: dac668681b4d1ec193e02d8f9762392b575a944e3d5f6c14401af8df6ce48059
4
+ data.tar.gz: 51a3a6f5eb287a6ed2a190cdcb0054e641e51adb9b88a27d9a24802a2a6ee7ac
5
5
  SHA512:
6
- metadata.gz: c658a1fc92c4939456eb11f2dee2d4e350a325a4a212705bef1007f3312678f9872edddca2fa4629840548ef4490baf09ed99b6fb0182336ffadf2ef073e2582
7
- data.tar.gz: 1bf97e092add6701a49ce53d7a4057d00f953ecb47f46512fcc918a8c51dff95d7cab5552e2148dd7fc3c9d89835e2e92abce9647d2073c131a12336d70ba51a
6
+ metadata.gz: c1a2519ff236aa25fef3ac00807f0a1e2e172deeb8492e9a3612863b5b306755568d2ecf0ec786ff4df5ead9f31df0483046c3639a709bc8b137d72421c67e3d
7
+ data.tar.gz: b0dae8f855296d97ef063f60b94bf28e7d8c2593ce38ab423f9a6064c0496040c8f670e4b5ac459f601a9fda82eea87e159fdcd7fbfe268592f4bce9d98afc91
data/README.md CHANGED
@@ -67,7 +67,7 @@ Then type `C-x L` to copy permanent URI. `C-y` to paste it.
67
67
  define_command(:copy_permanent_uri, doc: "Copy permanent URI") do
68
68
  require "dpu"
69
69
  b = Buffer.current
70
- uri = Dpu.determine_permanent_uri(Pathname(b.file_name), b.current_line)
70
+ uri = Dpu.determine_permanent_uri(Pathname(b.file_name), start_line_number: b.current_line)
71
71
  KILL_RING.push(uri)
72
72
  Clipboard.copy(uri) if CLIPBOARD_AVAILABLE
73
73
  message("Copied: #{uri}")
data/lib/dpu/cli.rb CHANGED
@@ -15,7 +15,11 @@ class Dpu::Cli
15
15
  start_line_number = s_start_line_number&.to_i
16
16
  end_line_number = s_end_line_number&.to_i
17
17
 
18
- uri = Dpu.determine_permanent_uri(path, start_line_number, end_line_number)
18
+ uri = Dpu.determine_permanent_uri(
19
+ path,
20
+ start_line_number: start_line_number,
21
+ end_line_number: end_line_number,
22
+ )
19
23
  puts(uri)
20
24
  end
21
25
  end
@@ -0,0 +1,25 @@
1
+ class Dpu::ScmService::Base
2
+ def determine_repository_uri(repository_http_or_ssh_url)
3
+ md = self.class::REMOTE_URL_PATTERN.match(repository_http_or_ssh_url)
4
+ return nil if !md
5
+
6
+ url = self.class::REPOSITORY_URI_TEMPLATE % {
7
+ account_name: md[:account_name],
8
+ repository_name: md[:repository_name],
9
+ }
10
+ return URI(url)
11
+ end
12
+
13
+ def ref_prefix
14
+ return self.class::REF_PREFIX
15
+ end
16
+
17
+ def determine_fragment(start_line_number, end_line_number)
18
+ return nil if !start_line_number
19
+ return "L#{start_line_number}" if !end_line_number || start_line_number == end_line_number
20
+ return self.class::START_AND_END_LINE_NUMBER_FRAGMENT_TEMPLATE % {
21
+ start_line_number: start_line_number,
22
+ end_line_number: end_line_number,
23
+ }
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ class Dpu::ScmService::Github < Dpu::ScmService::Base
2
+ REPOSITORY_URI_TEMPLATE = "https://github.com/%{account_name}/%{repository_name}"
3
+
4
+ REMOTE_URL_PATTERN = [
5
+ %r{\Agit://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
6
+ %r{\Ahttps?://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
7
+ %r{\Agit@github\.com:(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
8
+ %r{\Assh://git@github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
9
+ ].then { |patterns|
10
+ Regexp.union(*patterns)
11
+ }
12
+
13
+ REF_PREFIX = "blob"
14
+
15
+ START_AND_END_LINE_NUMBER_FRAGMENT_TEMPLATE = "L%{start_line_number}-L%{end_line_number}"
16
+ end
17
+
18
+ Dpu::SCM_SERVICES << Dpu::ScmService::Github.new
@@ -0,0 +1,16 @@
1
+ class Dpu::ScmService::Sourcehut < Dpu::ScmService::Base
2
+ REPOSITORY_URI_TEMPLATE = "https://git.sr.ht/~%{account_name}/%{repository_name}"
3
+
4
+ REMOTE_URL_PATTERN = [
5
+ %r{\Ahttps://git\.sr\.ht/\~(?<account_name>[^/]+)/(?<repository_name>.+)},
6
+ %r{\Agit@git\.sr\.ht:\~(?<account_name>[^/]+)/(?<repository_name>.+)},
7
+ ].then { |patterns|
8
+ Regexp.union(*patterns)
9
+ }
10
+
11
+ REF_PREFIX = "tree"
12
+
13
+ START_AND_END_LINE_NUMBER_FRAGMENT_TEMPLATE = "L%{start_line_number}-%{end_line_number}"
14
+ end
15
+
16
+ Dpu::SCM_SERVICES << Dpu::ScmService::Sourcehut.new
@@ -0,0 +1,2 @@
1
+ module Dpu::ScmService
2
+ end
data/lib/dpu/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dpu
4
- VERSION = "0.4.3"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/dpu.rb CHANGED
@@ -8,53 +8,24 @@ module Dpu
8
8
  autoload :Cli, "dpu/cli"
9
9
  autoload :VERSION, "dpu/vesion"
10
10
 
11
+ SCM_SERVICES = []
12
+
11
13
  class << self
12
- GITHUB_REPOSITORY_URI_TEMPLATE = "https://github.com/%{account_name}/%{repository_name}"
13
- SOURCEHUT_REPOSITORY_URI_TEMPLATE = "https://git.sr.ht/~%{account_name}/%{repository_name}"
14
- REPOSITORY_URI_TEMPLATES = {
15
- github: GITHUB_REPOSITORY_URI_TEMPLATE,
16
- sourcehut: SOURCEHUT_REPOSITORY_URI_TEMPLATE,
17
- }
18
-
19
- GITHUB_REMOTE_URL_PATTERN = [
20
- %r{\Agit://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
21
- %r{\Ahttps?://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
22
- %r{\Agit@github\.com:(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
23
- %r{\Assh://git@github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
24
- ].then { |patterns|
25
- Regexp.union(*patterns)
26
- }
27
- SOURCEHUT_REMOTE_URL_PATTERN = [
28
- %r{\Ahttps://git\.sr\.ht/\~(?<account_name>[^/]+)/(?<repository_name>.+)},
29
- %r{\Agit@git\.sr\.ht:\~(?<account_name>[^/]+)/(?<repository_name>.+)},
30
- ].then { |patterns|
31
- Regexp.union(*patterns)
32
- }
33
- REMOTE_URL_PATTERNS = {
34
- github: GITHUB_REMOTE_URL_PATTERN,
35
- sourcehut: SOURCEHUT_REMOTE_URL_PATTERN,
36
- }
37
-
38
- REF_PREFIX = {
39
- github: 'blob',
40
- sourcehut: 'tree',
41
- }
42
-
43
- def determine_permanent_uri(path_or_link, start_line_number = nil, end_line_number = nil)
14
+ def determine_permanent_uri(path_or_link, start_line_number: nil, end_line_number: nil)
44
15
  path = path_or_link.realpath
45
16
  relative_path = determine_relative_path(path)
46
17
 
47
18
  remote_url = get_remote_url(path)
48
- vcs_service = determine_vcs_service(remote_url)
19
+ scm_service, repository_uri = determine_scm_service_and_repository_uri(remote_url)
49
20
 
50
21
  permanent_uri_parts = [
51
- determine_repository_uri(vcs_service, remote_url),
52
- REF_PREFIX[vcs_service],
22
+ repository_uri,
23
+ scm_service.ref_prefix,
53
24
  find_same_content_version(path, relative_path) || determine_commit_id(path),
54
25
  relative_path,
55
26
  ]
56
27
  permanent_uri = URI(permanent_uri_parts.join("/"))
57
- permanent_uri.fragment = determine_fragment(vcs_service, start_line_number, end_line_number)
28
+ permanent_uri.fragment = scm_service.determine_fragment(start_line_number, end_line_number)
58
29
  return permanent_uri
59
30
  end
60
31
 
@@ -79,28 +50,13 @@ module Dpu
79
50
  return run_command("git remote get-url origin", chdir: path.dirname).chomp
80
51
  end
81
52
 
82
- def determine_vcs_service(repository_http_or_ssh_url)
83
- if GITHUB_REMOTE_URL_PATTERN.match?(repository_http_or_ssh_url)
84
- return :github
85
- end
86
- if SOURCEHUT_REMOTE_URL_PATTERN.match?(repository_http_or_ssh_url)
87
- return :sourcehut
88
- end
89
-
90
- raise "unknown VCS service: #{repository_http_or_ssh_url}"
91
- end
92
-
93
- def determine_repository_uri(vcs_service, repository_http_or_ssh_url)
94
- md = REMOTE_URL_PATTERNS[vcs_service].match(repository_http_or_ssh_url)
95
- if !md
96
- return URI(repository_http_or_ssh_url)
53
+ def determine_scm_service_and_repository_uri(repository_http_or_ssh_url)
54
+ SCM_SERVICES.each do |scm_service|
55
+ repository_uri = scm_service.determine_repository_uri(repository_http_or_ssh_url)
56
+ return scm_service, repository_uri if repository_uri
97
57
  end
98
58
 
99
- url = REPOSITORY_URI_TEMPLATES[vcs_service] % {
100
- account_name: md[:account_name],
101
- repository_name: md[:repository_name],
102
- }
103
- return URI(url)
59
+ raise "unknown SCM service: #{repository_http_or_ssh_url}"
104
60
  end
105
61
 
106
62
  def determine_commit_id(path)
@@ -123,12 +79,10 @@ module Dpu
123
79
  }
124
80
  return same_content_version
125
81
  end
126
-
127
- def determine_fragment(vcs_service, start_line_number, end_line_number)
128
- return nil if !start_line_number
129
- return "L#{start_line_number}" if !end_line_number || start_line_number == end_line_number
130
- return "L#{start_line_number}-L#{end_line_number}" if vcs_service == :github
131
- return "L#{start_line_number}-#{end_line_number}" if vcs_service == :sourcehut
132
- end
133
82
  end
134
83
  end
84
+
85
+ require "dpu/scm_service"
86
+ require "dpu/scm_service/base"
87
+ require "dpu/scm_service/github"
88
+ require "dpu/scm_service/sourcehut"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dpu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya.Nishida.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-23 00:00:00.000000000 Z
11
+ date: 2023-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: version_sorter
@@ -81,6 +81,10 @@ files:
81
81
  - exe/dpu
82
82
  - lib/dpu.rb
83
83
  - lib/dpu/cli.rb
84
+ - lib/dpu/scm_service.rb
85
+ - lib/dpu/scm_service/base.rb
86
+ - lib/dpu/scm_service/github.rb
87
+ - lib/dpu/scm_service/sourcehut.rb
84
88
  - lib/dpu/version.rb
85
89
  - sig/dpu.rbs
86
90
  homepage: https://github.com/nishidayuya/dpu