Skip to content

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
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Allow Dependency to preserve resolver key and source
  • Loading branch information
bcardiff committed May 16, 2025
commit aae1479410a963c99fc796714be4eafe7d120af2
34 changes: 33 additions & 1 deletion src/dependency.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,29 @@ module Shards
property name : String
property resolver : Resolver
property requirement : Requirement
# 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.
# A Dependency can still be created without them, but it will not be possible to
# generate the shard.yml file.
property! resolver_key : String
property! source : String

def initialize(@name : String, @resolver : Resolver, @requirement : Requirement = Any, @resolver_key : String? = nil, @source : String? = nil)
end

# Parse a dependency from a CLI argument
def self.from_cli(value : String) : Dependency
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}")

def initialize(@name : String, @resolver : Resolver, @requirement : Requirement = Any)
Dependency.new(name, resolver, parts[:requirement], parts[:resolver_key], parts[:source])
end

# :nodoc:
Expand Down Expand Up @@ -120,6 +141,7 @@ module Shards
end
end

# Used to generate the shard.lock file.
def to_yaml(yaml : YAML::Builder)
yaml.scalar name
yaml.mapping do
Expand All @@ -129,6 +151,16 @@ module Shards
end
end

# Used to generate the shard.yml file.
def to_shard_yaml(yaml : YAML::Builder)
yaml.scalar name
yaml.mapping do
yaml.scalar resolver_key
yaml.scalar source
requirement.to_yaml(yaml)
end
end

def as_package?
version =
case req = @requirement
Expand Down