-
-
Notifications
You must be signed in to change notification settings - Fork 104
Dependency cli parsing #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bcardiff
wants to merge
5
commits into
master
Choose a base branch
from
dependency-cli-parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e95757
Add parts_from_cli helper method to parse cli arguments
bcardiff aae1479
Allow Dependency to preserve resolver key and source
bcardiff eafca30
Extract logic from Dependency, update supported syntax
bcardiff 040cbdd
single logic for known git providers
bcardiff 53353dc
normalize paths from windows to posix
bcardiff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require "./spec_helper" | ||
require "../../src/dependency_definition" | ||
|
||
private def expect_parses(value, resolver_key : String, source : String, requirement : Shards::Requirement) | ||
Shards::DependencyDefinition.parts_from_cli(value).should eq(Shards::DependencyDefinition::Parts.new(resolver_key: resolver_key, source: source, requirement: requirement)) | ||
end | ||
|
||
module Shards | ||
describe DependencyDefinition do | ||
it ".parts_from_cli" do | ||
# GitHub short syntax | ||
expect_parses("github:foo/bar", "github", "foo/bar", Any) | ||
expect_parses("github:Foo/[email protected]", "github", "Foo/Bar", VersionReq.new("~> 1.2.3")) | ||
|
||
# GitHub urls | ||
expect_parses("https://github.com/foo/bar", "github", "foo/bar", Any) | ||
|
||
# GitHub urls from clone popup | ||
expect_parses("https://github.com/foo/bar.git", "github", "foo/bar", Any) | ||
expect_parses("[email protected]:foo/bar.git", "git", "[email protected]:foo/bar.git", Any) | ||
|
||
# GitLab short syntax | ||
expect_parses("gitlab:foo/bar", "gitlab", "foo/bar", Any) | ||
|
||
# GitLab urls | ||
expect_parses("https://gitlab.com/foo/bar", "gitlab", "foo/bar", Any) | ||
|
||
# GitLab urls from clone popup | ||
expect_parses("https://gitlab.com/foo/bar.git", "gitlab", "foo/bar", Any) | ||
expect_parses("[email protected]:foo/bar.git", "git", "[email protected]:foo/bar.git", requirement: Any) | ||
|
||
# Bitbucket short syntax | ||
expect_parses("bitbucket:foo/bar", "bitbucket", "foo/bar", Any) | ||
|
||
# bitbucket urls | ||
expect_parses("https://bitbucket.com/foo/bar", "bitbucket", "foo/bar", Any) | ||
|
||
# Git convenient syntax since resolver matches scheme | ||
expect_parses("git://git.example.org/crystal-library.git", "git", "git://git.example.org/crystal-library.git", Any) | ||
expect_parses("[email protected]:foo/bar.git", "git", "[email protected]:foo/bar.git", Any) | ||
|
||
# Local paths | ||
local_absolute = "/an/absolute/path" | ||
local_relative = "an/relative/path" | ||
|
||
# Path short syntax | ||
expect_parses("./#{local_relative}", "path", "./#{local_relative}", Any) | ||
expect_parses("../#{local_relative}", "path", "../#{local_relative}", Any) | ||
{% if flag?(:windows) %} | ||
expect_parses(".\\relative\\windows", "path", "./relative/windows", Any) | ||
expect_parses("..\\relative\\windows", "path", "../relative/windows", Any) | ||
{% end %} | ||
# Path file schema | ||
expect_parses("file://#{local_relative}", "path", local_relative, Any) | ||
expect_parses("file://#{local_absolute}", "path", local_absolute, Any) | ||
# Path resolver syntax | ||
expect_parses("path:#{local_absolute}", "path", local_absolute, Any) | ||
expect_parses("path:#{local_relative}", "path", local_relative, Any) | ||
# Other resolvers short | ||
expect_parses("git:git://git.example.org/crystal-library.git", "git", "git://git.example.org/crystal-library.git", Any) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
require "./dependency" | ||
|
||
module Shards | ||
class DependencyDefinition | ||
record Parts, resolver_key : String, source : String, requirement : Requirement | ||
|
||
property dependency : Dependency | ||
# resolver's key and source are normalized. We preserve the key and source to be used | ||
# in the shard.yml file in these field. This is used to generate the shard.yml file | ||
# in a more human-readable way. | ||
property resolver_key : String | ||
property source : String | ||
|
||
def initialize(@dependency : Dependency, @resolver_key : String, @source : String) | ||
end | ||
|
||
# Used to generate the shard.yml file. | ||
def to_yaml(yaml : YAML::Builder) | ||
yaml.scalar dependency.name | ||
yaml.mapping do | ||
yaml.scalar resolver_key | ||
yaml.scalar source | ||
dependency.requirement.to_yaml(yaml) | ||
end | ||
end | ||
|
||
# Parse a dependency from a CLI argument | ||
def self.from_cli(value : String) : DependencyDefinition | ||
parts = parts_from_cli(value) | ||
|
||
# We need to check the actual shard name to create a dependency. | ||
# This requires getting the actual spec file from some matching version. | ||
resolver = Resolver.find_resolver(parts.resolver_key, "unknown", parts.source) | ||
version = resolver.versions_for(parts.requirement).first || raise Shards::Error.new("No versions found for dependency: #{value}") | ||
spec = resolver.spec(version) | ||
name = spec.name || raise Shards::Error.new("No name found for dependency: #{value}") | ||
|
||
DependencyDefinition.new(Dependency.new(name, resolver, parts.requirement), parts.resolver_key, parts.source) | ||
end | ||
|
||
# :nodoc: | ||
# | ||
# Parse the dependency from a CLI argument | ||
# and return the parts needed to create the proper dependency. | ||
# | ||
# Split to allow better unit testing. | ||
def self.parts_from_cli(value : String) : Parts | ||
resolver_key = nil | ||
source = "" | ||
requirement = Any | ||
|
||
if value.starts_with?("file://") | ||
resolver_key = "path" | ||
source = value[7..-1] # drop "file://" | ||
end | ||
|
||
# relative paths | ||
path = Path[value].to_posix.to_s | ||
if path.starts_with?("./") || path.starts_with?("../") | ||
resolver_key = "path" | ||
source = path | ||
end | ||
|
||
uri = URI.parse(value) | ||
if uri.scheme != "file" && uri.host && | ||
(resolver_key = GitResolver::KNOWN_PROVIDERS[uri.host]?) | ||
source = uri.path[1..-1].rchop(".git") # drop first "/"" | ||
end | ||
|
||
if value.starts_with?("git://") | ||
resolver_key = "git" | ||
source = value | ||
end | ||
|
||
if value.starts_with?("git@") | ||
resolver_key = "git" | ||
source = value | ||
end | ||
|
||
unless resolver_key | ||
Resolver.resolver_keys.each do |key| | ||
key_schema = "#{key}:" | ||
if value.starts_with?(key_schema) | ||
resolver_key = key | ||
source = value.sub(key_schema, "") | ||
|
||
# narrow down requirement | ||
if source.includes?("@") | ||
source, version = source.split("@") | ||
requirement = VersionReq.new("~> #{version}") | ||
end | ||
|
||
break | ||
end | ||
end | ||
end | ||
|
||
raise Shards::Error.new("Invalid dependency format: #{value}") unless resolver_key | ||
|
||
Parts.new(resolver_key: resolver_key, source: source, requirement: requirement) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I'm wondering about why this gets normalized to
github
resolver instead of"git", "https://github.com/foo/bar"
.Both options are valid. So we only need to decide which one we want to pick.
I suppose the reason for
github
is that it's more concise? That's nice but not strictly necessary.git
would be closer to the original input.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me closer to the intent is to have
github: foo/bar
, because I'm assuming the user is copy-pasting the url in the browser. We do preserve the format with trailing .git as those are copied from the clone popup.At some point maybe it's worth configuring shards to map all github source to be resolved in some specific way, but is a separate story.