Skip to content

Refactoring Resolver #659

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 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
renaming fields before merging
  • Loading branch information
nanobowers committed Dec 3, 2024
commit 7825fccca04edd1bfa39f049a46de48dacd5825b
62 changes: 31 additions & 31 deletions src/resolvers/fossil.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ module Shards
end

class FossilResolver < Resolver
@@has_fossil_command : Bool?
@@command : Bool?
@@fossil_version_maj : Int8?
@@fossil_version_min : Int8?
@@fossil_version_rev : Int8?
@@fossil_version : String?
@@version : String?

@origin_url : String?
@local_fossil_file : String?
Expand All @@ -112,37 +112,37 @@ module Shards
end
end

protected def self.has_fossil_command?
if @@has_fossil_command.nil?
@@has_fossil_command = (Process.run("fossil version", shell: true).success? rescue false)
protected def self.command?
if @@command.nil?
@@command = (Process.run("fossil version", shell: true).success? rescue false)
end
@@has_fossil_command
@@command
end

protected def self.fossil_version
unless @@fossil_version
@@fossil_version = `fossil version`[/version\s+([^\s]*)/, 1]
pieces = @@fossil_version.not_nil!.split('.')
protected def self.version
unless @@version
@@version = `fossil version`[/version\s+([^\s]*)/, 1]
pieces = @@version.not_nil!.split('.')
@@fossil_version_maj = pieces[0].to_i8
@@fossil_version_min = pieces[1].to_i8
@@fossil_version_rev = (pieces[2]?.try &.to_i8 || 0i8)
end

@@fossil_version
@@version
end

protected def self.fossil_version_maj
self.fossil_version unless @@fossil_version_maj
self.version unless @@fossil_version_maj
@@fossil_version_maj.not_nil!
end

protected def self.fossil_version_min
self.fossil_version unless @@fossil_version_min
self.version unless @@fossil_version_min
@@fossil_version_min.not_nil!
end

protected def self.fossil_version_rev
self.fossil_version unless @@fossil_version_rev
self.version unless @@fossil_version_rev
@@fossil_version_rev.not_nil!
end

Expand Down Expand Up @@ -270,7 +270,7 @@ module Shards

def local_path
@local_path ||= begin
uri = parse_uri(fossil_url)
uri = parse_uri(vcs_url)

path = uri.path
path = Path[path]
Expand All @@ -291,7 +291,7 @@ module Shards
@local_fossil_file ||= Path[local_path].join("#{name}.fossil").normalize.to_s
end

def fossil_url
def vcs_url
source.strip
end

Expand All @@ -313,7 +313,7 @@ module Shards

record FossilVersion, value : String, commit : String? = nil

private def parse_fossil_version(version : Version) : FossilVersion
private def parse_version(version : Version) : FossilVersion
case version.value
when VERSION_REFERENCE
FossilVersion.new version.value
Expand All @@ -325,11 +325,11 @@ module Shards
end

private def fossil_ref(version : Version) : FossilRef
fossil_version = parse_fossil_version(version)
if commit = fossil_version.commit
version = parse_version(version)
if commit = version.commit
FossilCommitRef.new commit
else
FossilTagRef.new "v#{fossil_version.value}"
FossilTagRef.new "v#{version.value}"
end
end

Expand All @@ -340,7 +340,7 @@ module Shards
end

return if Shards.local? || @updated_cache
Log.info { "Fetching #{fossil_url}" }
Log.info { "Fetching #{vcs_url}" }

if cloned_repository?
# repositories cloned with shards v0.8.0 won't fetch any new remote
Expand All @@ -364,7 +364,7 @@ module Shards
Dir.mkdir_p(path)
FileUtils.rm(fossil_file) if File.exists?(fossil_file)

source = fossil_url
source = vcs_url
# Remove a "file://" from the beginning, otherwise the path might be invalid
# on Windows.
source = source.lchop("file://")
Expand All @@ -375,7 +375,7 @@ module Shards
end

private def fetch_repository
fossil_retry(err: "Failed to update #{fossil_url}") do
fossil_retry(err: "Failed to update #{vcs_url}") do
run "fossil pull -R #{Process.quote(local_fossil_file)}"
end
end
Expand Down Expand Up @@ -418,13 +418,13 @@ module Shards

# Returns whether origin URLs have differing hosts and/or paths.
protected def origin_changed?
return false if origin_url == fossil_url
return true if origin_url.nil? || fossil_url.nil?
return false if origin_url == vcs_url
return true if origin_url.nil? || vcs_url.nil?

origin_parsed = parse_uri(origin_url)
fossil_parsed = parse_uri(fossil_url)
vcs_parsed = parse_uri(vcs_url)

(origin_parsed.host != fossil_parsed.host) || (origin_parsed.path != fossil_parsed.path)
(origin_parsed.host != vcs_parsed.host) || (origin_parsed.path != vcs_parsed.path)
end

# Parses a URI string
Expand Down Expand Up @@ -473,7 +473,7 @@ module Shards
end

private def run_in_current_folder(command, capture = false)
unless FossilResolver.has_fossil_command?
unless FossilResolver.command?
raise Error.new("Error missing fossil command line tool. Please install Fossil first!")
end

Expand All @@ -493,9 +493,9 @@ module Shards
end

def report_version(version : Version) : String
fossil_version = parse_fossil_version(version)
if commit = fossil_version.commit
"#{fossil_version.value} at #{commit[0...7]}"
version = parse_version(version)
if commit = version.commit
"#{version.value} at #{commit[0...7]}"
else
version.value
end
Expand Down
56 changes: 28 additions & 28 deletions src/resolvers/git.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ module Shards
end

class GitResolver < Resolver
@@has_git_command : Bool?
@@command : Bool?
@@git_column_never : String?
@@git_version : String?
@@version : String?

@origin_url : String?

Expand Down Expand Up @@ -136,19 +136,19 @@ module Shards
end
end

protected def self.has_git_command?
if @@has_git_command.nil?
@@has_git_command = (Process.run("git", ["--version"]).success? rescue false)
protected def self.command?
if @@command.nil?
@@command = (Process.run("git", ["--version"]).success? rescue false)
end
@@has_git_command
@@command
end

protected def self.git_version
@@git_version ||= `git --version`.strip[12..-1]
protected def self.version
@@version ||= `git --version`.strip[12..-1]
end

protected def self.git_column_never
@@git_column_never ||= Versions.compare(git_version, "1.7.11") < 0 ? "--column=never" : ""
@@git_column_never ||= Versions.compare(version, "1.7.11") < 0 ? "--column=never" : ""
end

def read_spec(version : Version) : String?
Expand Down Expand Up @@ -234,7 +234,7 @@ module Shards

def local_path
@local_path ||= begin
uri = parse_uri(git_url)
uri = parse_uri(vcs_url)

path = uri.path
path += ".git" unless path.ends_with?(".git")
Expand All @@ -252,7 +252,7 @@ module Shards
end
end

def git_url
def vcs_url
source.strip
end

Expand All @@ -274,7 +274,7 @@ module Shards

record GitVersion, value : String, commit : String? = nil

private def parse_git_version(version : Version) : GitVersion
private def parse_version(version : Version) : GitVersion
case version.value
when VERSION_REFERENCE
GitVersion.new version.value
Expand All @@ -286,11 +286,11 @@ module Shards
end

private def git_ref(version : Version) : GitRef
git_version = parse_git_version(version)
if commit = git_version.commit
version = parse_version(version)
if commit = version.commit
GitCommitRef.new commit
else
GitTagRef.new "v#{git_version.value}"
GitTagRef.new "v#{version.value}"
end
end

Expand All @@ -301,7 +301,7 @@ module Shards
end

return if Shards.local? || @updated_cache
Log.info { "Fetching #{git_url}" }
Log.info { "Fetching #{vcs_url}" }

if cloned_repository?
# repositories cloned with shards v0.8.0 won't fetch any new remote
Expand All @@ -327,13 +327,13 @@ module Shards
# be used interactively.
# This configuration can be overridden by defining the environment
# variable `GIT_ASKPASS`.
git_retry(err: "Failed to clone #{git_url}") do
run_in_folder "git clone -c core.askPass=true -c init.templateDir= --mirror --quiet -- #{Process.quote(git_url)} #{Process.quote(local_path)}"
git_retry(err: "Failed to clone #{vcs_url}") do
run_in_folder "git clone -c core.askPass=true -c init.templateDir= --mirror --quiet -- #{Process.quote(vcs_url)} #{Process.quote(local_path)}"
end
end

private def fetch_repository
git_retry(err: "Failed to update #{git_url}") do
git_retry(err: "Failed to update #{vcs_url}") do
run "git fetch --all --quiet"
end
end
Expand All @@ -352,7 +352,7 @@ module Shards
end

private def delete_repository
Log.debug { "rm -rf #{Process.quote(local_path)}'" }
Log.debug { "rm -rf #{Process.quote(local_path)}" }
Shards::Helpers.rm_rf(local_path)
@origin_url = nil
end
Expand All @@ -378,13 +378,13 @@ module Shards

# Returns whether origin URLs have differing hosts and/or paths.
protected def origin_changed?
return false if origin_url == git_url
return true if origin_url.nil? || git_url.nil?
return false if origin_url == vcs_url
return true if origin_url.nil? || vcs_url.nil?

origin_parsed = parse_uri(origin_url)
git_parsed = parse_uri(git_url)
vcs_parsed = parse_uri(vcs_url)

(origin_parsed.host != git_parsed.host) || (origin_parsed.path != git_parsed.path)
(origin_parsed.host != vcs_parsed.host) || (origin_parsed.path != vcs_parsed.path)
end

# Parses a URI string, with additional support for ssh+git URI schemes.
Expand Down Expand Up @@ -433,7 +433,7 @@ module Shards
# Chdir to a folder and run command.
# Runs in current folder if `path` is nil.
private def run_in_folder(command, path : String? = nil, capture = false)
unless GitResolver.has_git_command?
unless GitResolver.command?
raise Error.new("Error missing git command line tool. Please install Git first!")
end

Expand All @@ -457,9 +457,9 @@ module Shards
end

def report_version(version : Version) : String
git_version = parse_git_version(version)
if commit = git_version.commit
"#{git_version.value} at #{commit[0...7]}"
version = parse_version(version)
if commit = version.commit
"#{version.value} at #{commit[0...7]}"
else
version.value
end
Expand Down
Loading