swift
swift copied to clipboard
[Question] How to use Danger-Swift with plugins in a SwiftPM-based project?
I'm working on a SwiftPM-based project, which means it already has a Package.swift file, and on the other hand, has no .xcodeproj or .xcworkspace file.
First, I tried to directly install Danger-Swift from brew, and it works well if there's no plugins. I just run danger-swift edit
, and it creates a template Dangerfile.swift for me to work on, it's brilliant.
But then I tried to add some plugins like Danger-Swift XcodeSummary, and things started to be complicated.
I tried to directly add the plugin dependency in the xcode project which danger-swift edit
created, and as you may expect, it doesn't work. I simply can't import the plugin.
Then I tried to install danger-swift as well as the plugins directly from the project's Package.swift file, but swift run danger-swift edit
still can't work out the plugin's dependencies, I still can't import the plugin.
Lastly I tried to specify the source file directly from the .target
instance in Package.swift file and open the Dangerfile.swift directly from the project Xcode. This time, Xcode tells me that I can import the plugin, but since the file name is Dangerfile.swift, not main.swift, I'm not allowed to directly write message("something")
since it's not at the top level. And when I tried to run it with swift run danger-swift local
, it still tells me that it can't import the plugin.
What should I do next?
Update:
I also tried to move Danger-Swift related files (Package.swift which only contains Danger-Swift and its plugins, and Dangerfile.swift) into a subdirectory, but no help.
For using it with SPM: Can you please show me your Package.swift
?
For using it without SPM: try to add
import DangerXCodeSummary // package: https://github.com/f-meloni/danger-swift-xcodesummary.git
to your Dangerfile.swift, then run danger-swift edit
, and it should work
Hi @f-meloni , Thanks for the reply.
I tried the way without SPM, and at first it showed me the same error that it can't find the plugin module. But then after I quit Xcode and re-edit it with danger-swift edit
it then worked! Thanks!
BTW about the Package.swift file, I'm sorry I can't show you the original file because the project is not public, but it's something like this:
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Project",
platforms: [
.iOS(.v12),
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Project",
targets: ["Project"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
// Private dependencies...
.package(name: "danger-swift", url: "https://github.com/danger/swift", from: "3.0.0"),
.package(name: "DangerXCodeSummary", url: "https://github.com/f-meloni/danger-swift-xcodesummary", from: "1.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 which this package depends on.
.target(
name: "Project",
dependencies: [
"PrivateLib",
]),
.testTarget(
name: "ProjectTests",
dependencies: ["Project"]),
]
)
Hey @el-hoshino question , won't doing this accidentally pass on danger swift to anyone who uses your package as its a dependency ?
If it helps, you and give this a shot i got it to kinda work:
i basically Created a new package with only 1 main.swift file with just the foundation import.
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "DesignComponentLibrary",
platforms: [.iOS(.v13)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "DesignComponentLibrary",
targets: ["DesignComponentLibrary"]),
.library(name: "DangerDeps", type: .dynamic, targets: ["DangerDependencies"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/danger/swift.git", from: "3.10.0"),
.package(url: "https://github.com/f-meloni/danger-swift-xcodesummary", from: "1.2.1"),
.package(url: "https://github.com/f-meloni/danger-swift-coverage", from: "1.2.1"),
.package(url: "https://github.com/el-hoshino/DangerSwiftHammer.git", from: "0.1.1"),
.package(url: "https://github.com/HealthTap/DangerXcodeStaticAnalyzer", from: "1.1.0"),
.package(url: "https://github.com/hebertialmeida/MarkdownSyntax", from: "1.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: "DesignComponentLibrary",
resources: [
.process("Resources")
]
),
.executableTarget(
name: "DangerDependencies",
dependencies: [
.product(name: "Danger", package: "swift"),
.product(name: "DangerSwiftCoverage", package: "danger-swift-coverage"),
.product(name: "DangerXCodeSummary", package: "danger-swift-xcodesummary"),
"DangerSwiftHammer",
"DangerXcodeStaticAnalyzer",
"MarkdownSyntax",
],
path: "Sources/Tools/",
sources: ["main.swift"]),
.testTarget(
name: "DesignComponentLibraryTests",
dependencies: ["DesignComponentLibrary"]),
]
)
~~i am stuck on https://github.com/danger/danger-js/issues/1282 for m1~~, found a work around on the same thread - https://github.com/danger/swift/issues?q=is%3Aissue+is%3Aopen+incompatible+target+arm64-apple-macosx10.10%3A+#issuecomment-1005685266
UPDATE : This method doesn't work, as "swift test" starts throwing errors saying my DangerDependencies target cannot be tested. Please use @el-hoshino answer below
@theScud Thanks for the response, actually I forgot I've opened this issue 😅 and now I've already solved this problem by moving the whole danger related things into another subdirectory and passing --cwd
parameter when running danger-swift from that directory