-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle cases where package repo URLs might have different protocol (s… (
#3) * Handle cases where package repo URLs might have different protocol (ssh or https) and/or .git suffix * Improved ignore_repos, prep for 0.1.3 release * Factor out Xcode and Git modules * added tests for git --------- Co-authored-by: Harold <[email protected]>
- Loading branch information
Showing
10 changed files
with
575 additions
and
136 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module SpmVersionUpdates | ||
VERSION = "0.1.2" | ||
VERSION = "0.1.3" | ||
end |
This file contains 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,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module Git | ||
# Removes protocol and trailing .git from a repo URL | ||
# @param [String] repo_url | ||
# The URL of the repository | ||
# @return [String] | ||
def self.trim_repo_url(repo_url) | ||
repo_url.split("://").last.gsub(/\.git$/, "") | ||
end | ||
|
||
# Extract a readable name for the repo given the url, generally org/repo | ||
# @return [String] | ||
def self.repo_name(repo_url) | ||
match = repo_url.match(%r{([\w-]+/[\w-]+)(.git)?$}) | ||
|
||
if match | ||
match[1] || match[0] | ||
else | ||
repo_url | ||
end | ||
end | ||
|
||
# Call git to list tags | ||
# @param [String] repo_url | ||
# The URL of the dependency's repository | ||
# @return [Array<Semantic::Version>] | ||
def self.version_tags(repo_url) | ||
versions = `git ls-remote -t #{repo_url}` | ||
.split("\n") | ||
.map { |line| line.split("/tags/").last } | ||
.filter_map { |line| | ||
begin | ||
Semantic::Version.new(line) | ||
rescue ArgumentError | ||
nil | ||
end | ||
} | ||
versions.sort! | ||
versions.reverse! | ||
versions | ||
end | ||
|
||
# Call git to find the last commit on a branch | ||
# @param [String] repo_url | ||
# The URL of the dependency's repository | ||
# @param [String] branch_name | ||
# The name of the branch on which to find the last commit | ||
# @return [String] | ||
def self.branch_last_commit(repo_url, branch_name) | ||
`git ls-remote -h #{repo_url}` | ||
.split("\n") | ||
.find { |line| line.split("\trefs/heads/")[1] == branch_name } | ||
.split("\trefs/heads/")[0] | ||
end | ||
end |
This file contains 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 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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "xcodeproj" | ||
|
||
module Xcode | ||
# Find the configured SPM dependencies in the xcodeproj | ||
# @param [String] xcodeproj_path | ||
# The path of the Xcode project | ||
# @return [Hash<String, Hash>] | ||
def self.get_packages(xcodeproj_path) | ||
raise(XcodeprojPathMustBeSet) if xcodeproj_path.nil? || xcodeproj_path.empty? | ||
|
||
project = Xcodeproj::Project.open(xcodeproj_path) | ||
project.objects.select { |obj| | ||
obj.kind_of?(Xcodeproj::Project::Object::XCRemoteSwiftPackageReference) && | ||
obj.requirement["kind"] != "commit" | ||
} | ||
.to_h { |package| | ||
[Git.trim_repo_url(package.repositoryURL), package.requirement] | ||
} | ||
end | ||
|
||
# Extracts resolved versions from Package.resolved relative to an Xcode project | ||
# @param [String] xcodeproj_path | ||
# The path to your Xcode project | ||
# @raise [CouldNotFindResolvedFile] if no Package.resolved files were found | ||
# @return [Hash<String, String>] | ||
def self.get_resolved_versions(xcodeproj_path) | ||
resolved_paths = find_packages_resolved_file(xcodeproj_path) | ||
raise(CouldNotFindResolvedFile) if resolved_paths.empty? | ||
|
||
resolved_versions = resolved_paths.map { |resolved_path| | ||
JSON.load_file!(resolved_path)["pins"] | ||
.to_h { |pin| | ||
[Git.trim_repo_url(pin["location"]), pin["state"]["version"] || pin["state"]["revision"]] | ||
} | ||
} | ||
resolved_versions.reduce(:merge!) | ||
end | ||
|
||
# Find the Packages.resolved file | ||
# @return [Array<String>] | ||
def self.find_packages_resolved_file(xcodeproj_path) | ||
locations = [] | ||
# First check the workspace for a resolved file | ||
workspace = xcodeproj_path.sub("xcodeproj", "xcworkspace") | ||
if Dir.exist?(workspace) | ||
path = File.join(workspace, "xcshareddata", "swiftpm", "Package.resolved") | ||
locations << path if File.exist?(path) | ||
end | ||
|
||
# Then check the project for a resolved file | ||
path = File.join(xcodeproj_path, "project.xcworkspace", "xcshareddata", "swiftpm", "Package.resolved") | ||
locations << path if File.exist?(path) | ||
|
||
$stderr.puts("Searching for resolved packages in: #{locations}") | ||
locations | ||
end | ||
|
||
private_class_method :find_packages_resolved_file | ||
|
||
class XcodeprojPathMustBeSet < StandardError | ||
end | ||
|
||
class CouldNotFindResolvedFile < StandardError | ||
end | ||
end |
Oops, something went wrong.