Skip to content
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

Revert "YAML parser for Dependency and Target" #352

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions spec/unit/dependency_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@ module Shards
describe Dependency do
it "version" do
dependency = Dependency.new("app")
dependency.version?.should be_nil
dependency.version.should eq("*")

dependency = Dependency.new("app")
dependency.version = "<= 1.0.0"
dependency.version?.should eq("<= 1.0.0")
dependency.version.should eq("<= 1.0.0")
dependency = Dependency.new("app", {version: "*"})
dependency.version.should eq("*")

dependency = Dependency.new("app")
dependency.version = "<= 2.0.0"
dependency.version?.should eq("<= 2.0.0")
dependency = Dependency.new("app", {version: "1.0.0"})
dependency.version.should eq("1.0.0")

dependency = Dependency.new("app", {version: "<= 2.0.0"})
dependency.version.should eq("<= 2.0.0")
end

it "version with tags" do
dependency = Dependency.new("app")
dependency.tag = "fix/something"
dependency = Dependency.new("app", {tag: "fix/something"})
dependency.version.should eq("*")

dependency = Dependency.new("app")
dependency.tag = "1.2.3"
dependency = Dependency.new("app", {tag: "1.2.3"})
dependency.version.should eq("*")

# version tag is considered a version:
dependency = Dependency.new("app")
dependency.tag = "v1.2.3-pre1"
dependency = Dependency.new("app", {tag: "v1.2.3-pre1"})
dependency.version.should eq("1.2.3-pre1")
end
end
Expand Down
13 changes: 7 additions & 6 deletions spec/unit/git_resolver_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "./spec_helper"

private def resolver(name)
dependency = Shards::Dependency.new(name, git: git_url(name))
private def resolver(name, config = {} of String => String)
config = config.merge({"git" => git_url(name)})
dependency = Shards::Dependency.from_name_config(name, config)
Shards::GitResolver.new(dependency)
end

Expand Down Expand Up @@ -44,15 +45,15 @@ module Shards
end

it "origin changed" do
dependency = Dependency.new("library", git: git_url("library"))
dependency = Dependency.new("library", {"git" => git_url("library")})
library = GitResolver.new(dependency)
library.install("0.1.2")

# Change the origin in the cache repo to https://github.com/foo/bar
Dir.cd(library.local_path) do
run "git remote set-url origin https://github.com/foo/bar"
end
#

# All of these alternatives should not trigger origin as changed
same_origins = [
"https://github.com/foo/bar",
Expand All @@ -67,7 +68,7 @@ module Shards
]

same_origins.each do |origin|
dependency.git = origin
dependency["git"] = origin
library.origin_changed?.should be_false
end

Expand All @@ -83,7 +84,7 @@ module Shards
]

changed_origins.each do |origin|
dependency.git = origin
dependency["git"] = origin
library.origin_changed?.should be_true
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/unit/lock_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ module Shards
shards.size.should eq(2)

shards[0].name.should eq("repo")
shards[0].path.should be_nil
shards[0].git.should eq("https://github.com/user/repo.git")
shards[0]["github"].should eq("user/repo")
shards[0].version.should eq("1.2.3")

shards[1].name.should eq("example")
shards[1].path.should be_nil
shards[1].git.should eq("https://example.com/example-crystal.git")
shards[1]["git"].should eq("https://example.com/example-crystal.git")
shards[1].refs.should eq("0d246ee6c52d4e758651b8669a303f04be9a2a96")
end

Expand Down
5 changes: 3 additions & 2 deletions spec/unit/path_resolver_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "./spec_helper"

private def resolver(name)
dependency = Shards::Dependency.new(name, path: git_path(name))
private def resolver(name, config = {} of String => String)
config["path"] = git_path(name)
dependency = Shards::Dependency.from_name_config(name, config)
Shards::PathResolver.new(dependency)
end

Expand Down
5 changes: 4 additions & 1 deletion spec/unit/resolver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ require "./spec_helper"
module Shards
describe Resolver do
it "find resolver with unordered dependency keys" do
dependency = Dependency.new("test", git: "file:///tmp/test")
dependency = Dependency.new("test", {
"branch" => "master",
"git" => "file:///tmp/test",
})
Shards.find_resolver(dependency).class.should eq(GitResolver)
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ require "../support/factories"

module Shards
set_warning_log_level

class Dependency
def self.from_name_config(name, config) : self
Dependency.new(name, config)
end
end
end

Spec.before_each do
Expand Down
65 changes: 5 additions & 60 deletions spec/unit/spec_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,21 @@ module Shards
spec.dependencies.size.should eq(3)

spec.dependencies[0].name.should eq("repo")
spec.dependencies[0].path.should be_nil
spec.dependencies[0].git.should eq("https://github.com/user/repo.git")
spec.dependencies[0]["github"].should eq("user/repo")
spec.dependencies[0].version.should eq("1.2.3")
spec.dependencies[0].refs.should be_nil

spec.dependencies[1].name.should eq("example")
spec.dependencies[1].path.should be_nil
spec.dependencies[1].git.should eq("https://example.com/example-crystal.git")
spec.dependencies[1]["git"].should eq("https://example.com/example-crystal.git")
spec.dependencies[1].version.should eq("*")
spec.dependencies[1].refs.should eq("master")

spec.dependencies[2].name.should eq("local")
spec.dependencies[2].path.should eq("/var/clones/local")
spec.dependencies[2].git.should be_nil
spec.dependencies[2]["path"].should eq("/var/clones/local")
spec.dependencies[2].version.should eq("*")
spec.dependencies[2].refs.should eq("unreleased")
end

it "fails dependency with duplicate resolver" do
expect_raises Shards::ParseError, %(Duplicate resolver mapping for dependency "foo" at line 6, column 5) do
Spec.from_yaml <<-YAML
name: orm
version: 1.0.0
dependencies:
foo:
github: user/repo
gitlab: user/repo
YAML
end
end

it "fails dependency with missing resolver" do
expect_raises Shards::ParseError, %(Missing resolver for dependency "foo" at line 4, column 3) do
Spec.from_yaml <<-YAML
name: orm
version: 1.0.0
dependencies:
foo:
branch: master
YAML
end
end

it "accepts dependency with extra attributes" do
spec = Spec.from_yaml <<-YAML
name: orm
version: 1.0.0
dependencies:
foo:
github: user/repo
extra: foobar
YAML
dep = Dependency.new("foo", git: "https://github.com/user/repo.git")
spec.dependencies[0].should eq dep
end

it "parse development dependencies" do
spec = Spec.from_yaml <<-YAML
name: orm
Expand All @@ -136,13 +95,11 @@ module Shards
spec.development_dependencies.size.should eq(2)

spec.development_dependencies[0].name.should eq("minitest")
spec.development_dependencies[0].path.should be_nil
spec.development_dependencies[0].git.should eq("https://github.com/ysbaddaden/minitest.cr.git")
spec.development_dependencies[0]["github"].should eq("ysbaddaden/minitest.cr")
spec.development_dependencies[0].version.should eq("0.1.4")

spec.development_dependencies[1].name.should eq("webmock")
spec.development_dependencies[1].path.should be_nil
spec.development_dependencies[1].git.should eq("https://github.com/manastech/webcmok-crystal.git")
spec.development_dependencies[1]["git"].should eq("https://github.com/manastech/webcmok-crystal.git")
spec.development_dependencies[1].refs.should eq("master")
end

Expand All @@ -166,18 +123,6 @@ module Shards
spec.targets[1].main.should eq("src/command/cli.cr")
end

it "fails target missing main" do
expect_raises Shards::ParseError, %(Missing property "main" for target "foo" at line 4, column 3) do
Spec.from_yaml <<-YAML
name: orm
version: 1.0.0
targets:
foo:
foo: bar
YAML
end
end

it "parse executables" do
spec = Spec.from_yaml <<-YAML
name: test
Expand Down
2 changes: 1 addition & 1 deletion src/commands/check.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Shards
return false
end

if version = lock.version { nil }
if version = lock["version"]?
if Versions.resolve([version], dependency.version).empty?
Log.debug { "#{dependency.name}: lock conflict" }
return false
Expand Down
4 changes: 2 additions & 2 deletions src/commands/install.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Shards
private def validate(packages)
packages.each do |package|
if lock = locks.find { |d| d.name == package.name }
if commit = lock.commit
if commit = lock["commit"]?
validate_locked_commit(package, commit)
elsif version = lock.version?
validate_locked_version(package, version)
Expand Down Expand Up @@ -86,7 +86,7 @@ module Shards

private def outdated_lockfile?(packages)
a = packages.map { |x| {x.name, x.version, x.commit} }
b = locks.map { |x| {x.name, x.version?, x.commit} }
b = locks.map { |x| {x.name, x["version"]?, x["commit"]?} }
a != b
end
end
Expand Down
89 changes: 24 additions & 65 deletions src/dependency.cr
Original file line number Diff line number Diff line change
@@ -1,71 +1,26 @@
require "./ext/yaml"

module Shards
class Dependency
class Dependency < Hash(String, String)
property name : String
setter version : String?
property path : String?
property git : String?
property tag : String?
property branch : String?
property commit : String?

def self.new(pull : YAML::PullParser) : self
start_pos = pull.location
dependency = Dependency.new(pull.read_scalar)

pull.each_in_mapping do
mapping_start = pull.location
case key = pull.read_scalar
when "version"
dependency.version = pull.read_scalar
when "tag"
dependency.tag = pull.read_scalar
when "branch"
dependency.branch = pull.read_scalar
when "commit"
dependency.commit = pull.read_scalar
when "path"
if dependency.path || dependency.git
raise YAML::ParseException.new("Duplicate resolver mapping for dependency #{dependency.name.inspect}", *mapping_start)
end

dependency.path = pull.read_scalar
when "git", "github", "gitlab", "bitbucket"
if dependency.path || dependency.git
raise YAML::ParseException.new("Duplicate resolver mapping for dependency #{dependency.name.inspect}", *mapping_start)
end

dependency.git = GitResolver.expand_resolver_url(pull.read_scalar, key)
else
# ignore unknown dependency mapping for future extensions
Dependency.new(pull.read_scalar).tap do |dependency|
pull.each_in_mapping do
dependency[pull.read_scalar] = pull.read_scalar
end
end

unless dependency.git || dependency.path
raise YAML::ParseException.new("Missing resolver for dependency #{dependency.name.inspect}", *start_pos)
end

dependency
end

def self.new(name, *, git)
new(name).tap do |dependency|
dependency.git = git
end
end

def self.new(name, *, path)
new(name).tap do |dependency|
dependency.path = path
end
protected def initialize(@name)
super()
end

def initialize(@name)
protected def initialize(@name, config)
super()
config.each { |k, v| self[k.to_s] = v.to_s }
end

def_equals_and_hash @name, @version, @git, @path, @tag, @branch, @commit

def version
version { "*" }
end
Expand All @@ -74,32 +29,36 @@ module Shards
version { nil }
end

def version
if version = @version
def prerelease?
Versions.prerelease? version
end

private def version
if version = self["version"]?
version
elsif tag =~ VERSION_TAG
elsif self["tag"]? =~ VERSION_TAG
$1
else
yield
end
end

def prerelease?
Versions.prerelease? version
def refs
self["branch"]? || self["tag"]? || self["commit"]?
end

def refs
branch || tag || commit
def path
self["path"]?
end

def to_human_requirement
if version?
if version = version?
version
elsif branch
elsif branch = self["branch"]?
"branch #{branch}"
elsif tag
elsif tag = self["tag"]?
"tag #{tag}"
elsif commit
elsif commit = self["commit"]?
"commit #{commit}"
else
"*"
Expand Down
Loading