Skip to content

Commit 05483f2

Browse files
committed
Add # syntax to other source schemes
1 parent 05f3d97 commit 05483f2

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

spec/unit/dependency_definition_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ module Shards
1414

1515
# GitHub urls
1616
expect_parses("https://github.com/foo/bar", "github", "foo/bar", Any)
17+
expect_parses("https://github.com/foo/bar#1.2.3", "github", "foo/bar", VersionReq.new("~> 1.2.3"))
1718

1819
# GitHub urls from clone popup
1920
expect_parses("https://github.com/foo/bar.git", "github", "foo/bar", Any)
21+
expect_parses("https://github.com/foo/bar.git#1.2.3", "github", "foo/bar", VersionReq.new("~> 1.2.3"))
2022
expect_parses("[email protected]:foo/bar.git", "git", "[email protected]:foo/bar.git", Any)
23+
expect_parses("[email protected]:foo/bar.git#1.2.3", "git", "[email protected]:foo/bar.git", VersionReq.new("~> 1.2.3"))
2124

2225
# GitLab short syntax
2326
expect_parses("gitlab:foo/bar", "gitlab", "foo/bar", Any)
27+
expect_parses("gitlab:foo/bar#1.2.3", "gitlab", "foo/bar", VersionReq.new("~> 1.2.3"))
2428

2529
# GitLab urls
2630
expect_parses("https://gitlab.com/foo/bar", "gitlab", "foo/bar", Any)

src/dependency_definition.cr

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require "./dependency"
22

33
module Shards
44
class DependencyDefinition
5-
record Parts, resolver_key : String, source : String, requirement : Requirement = Any
5+
record Parts, resolver_key : String, source : String, requirement : Requirement
66

77
property dependency : Dependency
88
# resolver's key and source are normalized. We preserve the key and source to be used
@@ -47,28 +47,36 @@ module Shards
4747
def self.parts_from_cli(value : String) : Parts
4848
uri = URI.parse(value)
4949

50+
# fragment parsing for version requirement
51+
requirement = Any
52+
if fragment = uri.fragment
53+
uri.fragment = nil
54+
value = value.rchop("##{fragment}")
55+
requirement = VersionReq.new("~> #{fragment}")
56+
end
57+
5058
case scheme = uri.scheme
5159
when Nil
5260
case value
5361
when .starts_with?("./"), .starts_with?("../")
54-
Parts.new("path", Path[value].to_posix.to_s)
62+
Parts.new("path", Path[value].to_posix.to_s, Any)
5563
when .starts_with?(".\\"), .starts_with?("..\\")
5664
{% if flag?(:windows) %}
57-
Parts.new("path", Path[value].to_posix.to_s)
65+
Parts.new("path", Path[value].to_posix.to_s, Any)
5866
{% else %}
5967
raise Shards::Error.new("Invalid dependency format: #{value}")
6068
{% end %}
6169
when .starts_with?("git@")
62-
Parts.new("git", value)
70+
Parts.new("git", value, requirement)
6371
else
6472
raise Shards::Error.new("Invalid dependency format: #{value}")
6573
end
6674
when "file"
6775
raise Shards::Error.new("Invalid file URI: #{uri}") if !uri.host.in?(nil, "", "localhost") || uri.port || uri.user
68-
Parts.new("path", uri.path)
76+
Parts.new("path", uri.path, Any)
6977
when "https"
7078
if resolver_key = GitResolver::KNOWN_PROVIDERS[uri.host]?
71-
Parts.new(resolver_key, uri.path[1..-1].rchop(".git")) # drop first "/""
79+
Parts.new(resolver_key, uri.path[1..-1].rchop(".git"), requirement) # drop first "/""
7280
else
7381
raise Shards::Error.new("Cannot determine resolver for HTTPS URI: #{value}")
7482
end
@@ -79,12 +87,6 @@ module Shards
7987
if uri.host.nil? || subscheme
8088
uri.scheme = subscheme
8189
end
82-
# narrow down requirement
83-
requirement = Any
84-
if version = uri.fragment
85-
uri.fragment = nil
86-
requirement = VersionReq.new("~> #{version}")
87-
end
8890
return Parts.new(scheme, uri.to_s, requirement)
8991
end
9092
raise Shards::Error.new("Invalid dependency format: #{value}")

0 commit comments

Comments
 (0)