-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from mattt/swift
Add support for Swift / Swift Package Manager
- Loading branch information
Showing
12 changed files
with
285 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Swift | ||
|
||
The Swift source uses `swift package` subcommands | ||
to enumerate dependencies and properties. |
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,64 @@ | ||
# frozen_string_literal: true | ||
require "json" | ||
require "pathname" | ||
require "uri" | ||
|
||
module Licensed | ||
module Sources | ||
class Swift < Source | ||
def enabled? | ||
return unless Licensed::Shell.tool_available?("swift") && swift_package? | ||
File.exist?(package_resolved_file_path) | ||
end | ||
|
||
def enumerate_dependencies | ||
pins.map { |pin| | ||
name = pin["package"] | ||
version = pin.dig("state", "version") | ||
path = nil | ||
errors = [] | ||
|
||
begin | ||
path = dependency_path_for_url(pin["repositoryURL"]) | ||
rescue => e | ||
errors << e | ||
end | ||
|
||
Dependency.new( | ||
name: name, | ||
path: path, | ||
version: version, | ||
errors: errors | ||
) | ||
} | ||
end | ||
|
||
private | ||
|
||
def pins | ||
return @pins if defined?(@pins) | ||
|
||
@pins = begin | ||
json = JSON.parse(File.read(package_resolved_file_path)) | ||
json.dig("object", "pins") | ||
rescue => e | ||
message = "Licensed was unable to read the Package.resolved file. Error: #{e.message}" | ||
raise Licensed::Sources::Source::Error, message | ||
end | ||
end | ||
|
||
def dependency_path_for_url(url) | ||
last_path_component = URI(url).path.split("/").last.sub(/\.git$/, "") | ||
File.join(config.pwd, ".build", "checkouts", last_path_component) | ||
end | ||
|
||
def package_resolved_file_path | ||
File.join(config.pwd, "Package.resolved") | ||
end | ||
|
||
def swift_package? | ||
Licensed::Shell.success?("swift", "package", "describe") | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [ -z "$(which swift)" ]; then | ||
echo "A local swift installation is required for swift development." >&2 | ||
exit 127 | ||
fi | ||
|
||
swift --version | ||
|
||
BASE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" | ||
cd $BASE_PATH/test/fixtures/swift | ||
|
||
if [ "$1" == "-f" ]; then | ||
find . -not -regex "\.*" \ | ||
-and -not -path "*/Package.swift" \ | ||
-and -not -path "*/Sources*" \ | ||
-and -not -path "*/Tests*" \ | ||
-print0 | xargs -0 rm -rf | ||
fi | ||
|
||
swift package resolve |
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,5 @@ | ||
expected_dependency: DeckOfPlayingCards | ||
source_path: test/fixtures/swift | ||
cache_path: test/fixtures/swift/.licenses | ||
sources: | ||
swift: true |
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,34 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "DeckOfPlayingCards", | ||
"repositoryURL": "https://github.com/apple/example-package-deckofplayingcards.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "2c0e5ac3e10216151fc78ac1ec6bd9c2c0111a3a", | ||
"version": "3.0.4" | ||
} | ||
}, | ||
{ | ||
"package": "FisherYates", | ||
"repositoryURL": "https://github.com/apple/example-package-fisheryates.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "e729f197bbc3831b9a3005fa71ad6f38c1e7e17e", | ||
"version": "2.0.6" | ||
} | ||
}, | ||
{ | ||
"package": "PlayingCard", | ||
"repositoryURL": "https://github.com/apple/example-package-playingcard.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "39ddabb01e8102ab548a8c6bb3eb20b15f3b4fbc", | ||
"version": "3.0.5" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} |
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,31 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Fixtures", | ||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "Fixtures", | ||
targets: ["Fixtures"]), | ||
], | ||
dependencies: [ | ||
.package(name: "DeckOfPlayingCards", | ||
url: "https://github.com/apple/example-package-deckofplayingcards.git", | ||
from: "3.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "Fixtures", | ||
dependencies: [ | ||
.product(name: "DeckOfPlayingCards", package: "DeckOfPlayingCards") | ||
]), | ||
.testTarget( | ||
name: "FixturesTests", | ||
dependencies: ["Fixtures"]), | ||
] | ||
) |
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,3 @@ | ||
public struct Fixture { | ||
public init() {} | ||
} |
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,8 @@ | ||
import XCTest | ||
import Fixtures | ||
|
||
class FixturesTests: XCTestCase { | ||
func testFixtures() { | ||
XCTAssertNotNil(Fixture()) | ||
} | ||
} |
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,85 @@ | ||
# frozen_string_literal: true | ||
require "test_helper" | ||
require "tmpdir" | ||
require "pp" | ||
|
||
if Licensed::Shell.tool_available?("swift") | ||
describe Licensed::Sources::Swift do | ||
let(:fixtures) { File.expand_path("../../fixtures/swift", __FILE__) } | ||
let(:config) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } | ||
let(:source) { Licensed::Sources::Swift.new(config) } | ||
|
||
describe "enabled?" do | ||
it "is true if Swift package exists" do | ||
Dir.chdir(fixtures) do | ||
assert source.enabled? | ||
end | ||
end | ||
|
||
it "is false if Swift package doesn't exist" do | ||
Dir.chdir(Dir.tmpdir) do | ||
refute source.enabled? | ||
end | ||
end | ||
end | ||
|
||
describe "enumerate_dependencies" do | ||
it "does not include the source project" do | ||
Dir.chdir(fixtures) do | ||
config["name"] = "Fixtures" | ||
refute source.enumerate_dependencies.find { |d| d.name == "Fixtures" } | ||
end | ||
end | ||
|
||
it "finds dependencies from path sources" do | ||
Dir.chdir(fixtures) do | ||
dep = source.enumerate_dependencies.find { |d| d.name == "DeckOfPlayingCards" } | ||
assert dep | ||
assert_equal "3.0.4", dep.version | ||
|
||
dep = source.enumerate_dependencies.find { |d| d.name == "FisherYates" } | ||
assert dep | ||
assert_equal "2.0.6", dep.version | ||
|
||
dep = source.enumerate_dependencies.find { |d| d.name == "PlayingCard" } | ||
assert dep | ||
assert_equal "3.0.5", dep.version | ||
|
||
dep = source.enumerate_dependencies.find { |d| d.name == "Invalid" } | ||
refute dep | ||
end | ||
end | ||
|
||
it "handles invalid repositoryURL field" do | ||
source.stubs(:pins).returns( | ||
JSON.parse <<-JSON | ||
[{ | ||
"package": "Invalid", | ||
"repositoryURL": "Invalid", | ||
"state": { | ||
"version": "1.0.0" | ||
} | ||
}] | ||
JSON | ||
) | ||
|
||
dep = source.enumerate_dependencies.find { |d| d.name == "Invalid" } | ||
assert dep | ||
assert dep.errors | ||
end | ||
|
||
it "handles invalid Package.resolved file" do | ||
Dir.mktmpdir do |dir| | ||
FileUtils.cp_r(fixtures, dir) | ||
File.write(File.join(dir, "Package.resolved"), %("Invalid")) | ||
|
||
Dir.chdir(dir) do | ||
assert_raises ::Licensed::Sources::Source::Error do | ||
source.enumerate_dependencies | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |