Skip to content

Commit

Permalink
Add ci (#14)
Browse files Browse the repository at this point in the history
* add ci

* format

* fix swift version
  • Loading branch information
zunda-pixel authored Aug 12, 2024
1 parent c18168e commit b33365f
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 75 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "swift" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: CI

on: pull_request

jobs:
build:
runs-on: ubuntu-latest
container: swiftlang/swift:nightly-6.0-jammy
steps:
- uses: actions/checkout@v4
- run: swift build
lint:
runs-on: ubuntu-latest
container: swiftlang/swift:nightly-6.0-jammy
steps:
- uses: actions/checkout@v4
- run: swift format lint -r -p -s .
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ let package = Package(
.tvOS(.v12),
.watchOS(.v4),
.macCatalyst(.v13),
.visionOS(.v1),
],
products: [
.plugin(
name: "LicenseProviderPlugin",
targets: [
"LicenseProviderPlugin",
"LicenseProviderPlugin"
]
),
)
],
targets: [
.executableTarget(
Expand All @@ -29,6 +30,6 @@ let package = Package(
dependencies: [
.target(name: "LicenseProviderExec")
]
)
),
]
)
62 changes: 33 additions & 29 deletions Plugins/LicenseProviderPlugin/LicenseProviderPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
import PackagePlugin
import Foundation
import PackagePlugin

@main
struct LicenseViewPlugin {
static let commandName = "LicenseProviderExec"

func sourcePackagesPath(workDirectory: URL) -> URL? {
var workDirectory = workDirectory

guard workDirectory.absoluteString.contains("SourcePackages") else {
return nil
}

while workDirectory.lastPathComponent != "SourcePackages" {
workDirectory = workDirectory.deletingLastPathComponent()
}

return workDirectory
}

func buildCommands(executablePath: URL, workDirectory: URL) -> Command? {
let fileName = "LicenseProvider.swift"

let output = workDirectory.appending(path: fileName)
guard let sourcePackages = sourcePackagesPath(workDirectory: workDirectory) else {
return nil
}

return .buildCommand(
displayName: "LicenseProviderPlugin",
executable: executablePath,
arguments: [
output.path(),
sourcePackages.path()
sourcePackages.path(),
],
outputFiles: [ output ]
outputFiles: [output]
)
}
}

extension LicenseViewPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
let executablePath = try context.tool(named: LicenseViewPlugin.commandName).url

guard let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
) else {

guard
let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
)
else {
return []
}

return [command]
}
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
import XcodeProjectPlugin

extension LicenseViewPlugin: XcodeBuildToolPlugin {
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
let executablePath = try context.tool(named: LicenseViewPlugin.commandName).url

guard let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
) else {
return []
extension LicenseViewPlugin: XcodeBuildToolPlugin {
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
let executablePath = try context.tool(named: LicenseViewPlugin.commandName).url

guard
let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
)
else {
return []
}

return [command]
}

return [command]
}
}
#endif
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Generate License List that Project depends on.

<img src="https://img.shields.io/badge/Swift-5.7-orange" alt="Support Swift 5.7" /> <a href="https://github.com/apple/swift-package-manager" alt="HTTPClient on Swift Package Manager" title="HTTPClient on Swift Package Manager"><img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" /></a>
<img src="https://img.shields.io/badge/Swift-6.0-orange" alt="Support Swift 6.0" /> <a href="https://github.com/swiftlang/swift-package-manager" alt="HTTPClient on Swift Package Manager" title="HTTPClient on Swift Package Manager"><img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" /></a>

<div>
<img width="250" src="https://user-images.githubusercontent.com/47569369/211776957-57ecef9a-bdff-4ee4-af47-da39c890541a.png" />
Expand Down
11 changes: 7 additions & 4 deletions Sources/LicenseProviderExec/WorkSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import Foundation

struct WorkSpace: Decodable {
let packages: [WorkSpacePackage]

private enum ObjectCodingKeys: CodingKey {
case object
}

private enum DependenciesCodingKeys: CodingKey {
case dependencies
}

init(from decoder: Decoder) throws {
let objectContainer = try decoder.container(keyedBy: ObjectCodingKeys.self)
let dependenciesContainer = try objectContainer.nestedContainer(keyedBy: DependenciesCodingKeys.self, forKey: .object)
let dependenciesContainer = try objectContainer.nestedContainer(
keyedBy: DependenciesCodingKeys.self,
forKey: .object
)
self.packages = try dependenciesContainer.decode([WorkSpacePackage].self, forKey: .dependencies)
}
}
15 changes: 9 additions & 6 deletions Sources/LicenseProviderExec/WorkSpacePackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ struct WorkSpacePackage: Decodable, Hashable {
let name: String
let location: URL
let subPath: String

enum PackageRefCodingKeys: CodingKey {
case packageRef
case subpath
}

enum CodingKeys: CodingKey {
case name
case location
}

init(from decoder: Decoder) throws {
let packageContainer = try decoder.container(keyedBy: PackageRefCodingKeys.self)

self.subPath = try packageContainer.decode(String.self, forKey: .subpath)
let container = try packageContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .packageRef)

let container = try packageContainer.nestedContainer(
keyedBy: CodingKeys.self,
forKey: .packageRef
)

self.name = try container.decode(String.self, forKey: .name)
self.location = try container.decode(URL.self, forKey: .location)
}
Expand Down
68 changes: 36 additions & 32 deletions Sources/LicenseProviderExec/main.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
import Foundation

func generateSourceCode(packages: [WorkSpacePackage: String]) -> String {
let workspaceInits = packages.sorted(by: \.key.name).map { """
.init(
name: "\($0.key.name)",
location: URL(string: "\($0.key.location)")!,
license: \"""
\($0.value)
\"""
)
"""
let workspaceInits = packages.sorted(by: \.key.name).map {
"""
.init(
name: "\($0.key.name)",
location: URL(string: "\($0.key.location)")!,
license: \"""
\($0.value)
\"""
)
"""
}

let sourceCode = """
import Foundation
enum LicenseProvider {
static let packages: [Package] = [
\(workspaceInits.joined(separator: ",\n"))
]
}
import Foundation
enum LicenseProvider {
static let packages: [Package] = [
\(workspaceInits.joined(separator: ",\n"))
]
}
struct Package: Hashable, Identifiable {
let id = UUID()
let name: String
let location: URL
let license: String
}
"""
struct Package: Hashable, Identifiable {
let id = UUID()
let name: String
let location: URL
let license: String
}
"""
return sourceCode
}

let sourcePackagesPath = URL(fileURLWithPath: CommandLine.arguments[2])

let jsonData = try Data(contentsOf: sourcePackagesPath.appendingPathComponent("workspace-state.json"))
let jsonData = try Data(
contentsOf: sourcePackagesPath.appendingPathComponent("workspace-state.json"))
let workspace = try JSONDecoder().decode(WorkSpace.self, from: jsonData)

var packages: [WorkSpacePackage: String] = [:]

for package in workspace.packages {
let subPath = sourcePackagesPath.appendingPathComponent("checkouts").appendingPathComponent(package.subPath)
let contents = try FileManager.default.contentsOfDirectory(at: subPath, includingPropertiesForKeys: nil).filter { path in
for package in workspace.packages {
let subPath = sourcePackagesPath.appendingPathComponent("checkouts").appendingPathComponent(
package.subPath)
let contents = try FileManager.default.contentsOfDirectory(
at: subPath, includingPropertiesForKeys: nil
).filter { path in
let pathWithoutExtension = path.deletingPathExtension()

return pathWithoutExtension.lastPathComponent.lowercased() == "license"
}

if let content = contents.first {
let fileData = try Data(contentsOf: content)

packages[package] = String(decoding: fileData, as: UTF8.self)
}
}
Expand All @@ -60,7 +65,6 @@ let outputFilePath = URL(fileURLWithPath: CommandLine.arguments[1])

try sourceCodeData.write(to: outputFilePath)


extension Sequence {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, isAscending: Bool = true) -> [Element]
{
Expand Down

0 comments on commit b33365f

Please sign in to comment.