Skip to content

Commit

Permalink
Merge pull request #24 from cybozu/modify-for-xcode-cloud
Browse files Browse the repository at this point in the history
Changed to generate a swift file instead of a plist file so that it can be built with Xcode Cloud.
  • Loading branch information
Kyome22 committed Feb 20, 2024
2 parents 629f856 + 29dd00e commit 814e3a9
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 499 deletions.
15 changes: 3 additions & 12 deletions .github/workflows/dispatch-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ on:
jobs:
run-unit-test:
name: Run Unit Test
runs-on: macos-13
runs-on: macos-14
timeout-minutes: 30
env:
DEVELOPER_DIR: "/Applications/Xcode_15.0.1.app/Contents/Developer"
DEVELOPER_DIR: "/Applications/Xcode_15.2.app/Contents/Developer"

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Show Xcode Version
run: xcodebuild -version
Expand All @@ -26,18 +26,9 @@ jobs:
-resultBundlePath SourcePackagesParserTests | \
xcpretty -c && exit ${PIPESTATUS[0]}
- name: Run LicenseListTests
run: |
xcodebuild -scheme LicenseList-Package test \
-only-testing:LicenseListTests \
-destination "platform=iOS Simulator,name=iPhone 15,OS=17.0.1" \
-resultBundlePath LicenseListTests | \
xcpretty && exit ${PIPESTATUS[0]}
- name: Archive test results
if: success() || failure()
uses: kishikawakatsumi/xcresulttool@v1
with:
path: |
SourcePackagesParserTests.xcresult
LicenseListTests.xcresult
8 changes: 4 additions & 4 deletions .github/workflows/release-artifact-bundle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ name: Release Artifact Bundle
on:
push:
tags:
- '*.*.*'
- "*.*.*"

jobs:
release-artifact-bundle:
name: Release Artifact Bundle
runs-on: macos-13
runs-on: macos-14
timeout-minutes: 20
env:
DEVELOPER_DIR: "/Applications/Xcode_15.0.1.app/Contents/Developer"
DEVELOPER_DIR: "/Applications/Xcode_15.2.app/Contents/Developer"
ARTIFACT_BUNDLE: "swift-packages-parser.artifactbundle"

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Show Xcode Version
run: xcodebuild -version
Expand Down
7 changes: 3 additions & 4 deletions LicenseDemo/LicenseDemoForUIKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ class ViewController: UIViewController {
}

@IBAction func pushLicense(_ sender: Any) {
if let vc = LicenseListViewController() {
vc.title = "LICENSE"
navigationController?.pushViewController(vc, animated: true)
}
let vc = LicenseListViewController()
vc.title = "LICENSE"
navigationController?.pushViewController(vc, animated: true)
}
}
20 changes: 3 additions & 17 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ let package = Package(
name: "spp",
targets: ["spp"]
),
.plugin(
name: "PrepareLicenseList",
targets: ["PrepareLicenseList"]
),
.library(
name: "LicenseList",
targets: ["LicenseList"]
Expand Down Expand Up @@ -45,19 +41,9 @@ let package = Package(
.copy("Resources/SourcePackages")
]
),

.target(name: "LicenseList"),
.testTarget(
name: "LicenseListTests",
dependencies: [
.target(
name: "LicenseList",
condition: .when(platforms: [.iOS])
)
],
resources: [
.copy("license-list.plist")
]
.target(
name: "LicenseList",
plugins: ["PrepareLicenseList"]
)
]
)
6 changes: 3 additions & 3 deletions Plugins/PrepareLicenseList/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct PrepareLicenseList: BuildToolPlugin {
sourcePackagesPath.string
],
outputFiles: [
outputPath.appending(["license-list.plist"])
outputPath.appending(["LicenseList.swift"])
]
)
}
Expand All @@ -45,7 +45,7 @@ struct PrepareLicenseList: BuildToolPlugin {
makeBuildCommand(
executablePath: try context.tool(named: "spp").path,
sourcePackagesPath: try sourcePackages(context.pluginWorkDirectory),
outputPath: context.pluginWorkDirectory.appending(["Resources"])
outputPath: context.pluginWorkDirectory
)
]
}
Expand All @@ -62,7 +62,7 @@ extension PrepareLicenseList: XcodeBuildToolPlugin {
makeBuildCommand(
executablePath: try context.tool(named: "spp").path,
sourcePackagesPath: try sourcePackages(context.pluginWorkDirectory),
outputPath: context.pluginWorkDirectory.appending(["Resources"])
outputPath: context.pluginWorkDirectory
)
]
}
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ LicenseList is available through [Swift Package Manager](https://github.com/appl
- Application Target > `General` > `Frameworks, Libraries, and Embedded Content` > `+`
- Choose `LicenseList`
<img src="./Screenshots/installation-2.png" width="500px" />
3. Add PrepareLicenseList plugin to build phases
- Application Target > `Build Phases` > `Run Build Tool Plug-ins` > `+`
- Choose `PrepareLicenseList`
<img src="./Screenshots/installation-3.png" width="500px" />

## Usage

Expand Down Expand Up @@ -93,7 +89,7 @@ $ swift run spp [output directory path] [SourcePackages directory path]
```

- [output directory path]
Path to the directory where the license-list.plist file will be placed.
Path to the directory where the LicenseList.swift file will be placed.

- [SourcePackages directory path]
Example: `~/Library/Developer/Xcode/DerivedData/project-name-xxxxxxxx/SourcePackages`
Binary file removed Screenshots/installation-3.png
Binary file not shown.
16 changes: 5 additions & 11 deletions Sources/LicenseList/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ public struct Library: Identifiable, Hashable {

extension Library {
static public var libraries: [Library] {
guard let fileURL = Bundle.main.url(forResource: "license-list", withExtension: "plist"),
let data = try? Data(contentsOf: fileURL),
let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
let dict = plist as? [String: Any] else {
return []
}
return (dict["libraries"] as? [[String: Any]])?.compactMap({ info -> Library? in
guard let name = info["name"] as? String,
let url = info["url"] as? String,
let body = info["licenseBody"] as? String else {
return SPP.libraries.compactMap { info -> Library? in
guard let name = info["name"],
let url = info["url"],
let body = info["licenseBody"] else {
return nil
}
return Library(name: name, url: url, licenseBody: body)
}) ?? []
}
}
}
23 changes: 3 additions & 20 deletions Sources/LicenseList/LicenseListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@
// Created by ky0me22 on 2022/06/03.
//

import UIKit
import SwiftUI

public struct LicenseListView: View {
@Environment(\.licenseListViewStyle) private var licenseListViewStyle: LicenseListViewStyle

let libraries: [Library]
let libraries = Library.libraries
let useUINavigationController: Bool
let navigationHandler: ((Library) -> Void)?

public init(
fileURL: URL,
useUINavigationController: Bool = false,
navigationHandler: ((Library) -> Void)? = nil
) {
self.useUINavigationController = useUINavigationController
self.navigationHandler = navigationHandler
guard let data = try? Data(contentsOf: fileURL),
let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
let dict = plist as? [String: Any] else {
libraries = []
return
}
libraries = (dict["libraries"] as? [[String: Any]])?.compactMap({ info -> Library? in
guard let name = info["name"] as? String,
let url = info["url"] as? String,
let body = info["licenseBody"] as? String else {
return nil
}
return Library(name: name, url: url, licenseBody: body)
}) ?? []
}

public var body: some View {
Expand Down Expand Up @@ -83,8 +67,7 @@ public struct LicenseListView: View {
}

public extension LicenseListView {
init(bundle: Bundle = .main, useUINavigationController: Bool = false) {
let url = bundle.url(forResource: "license-list", withExtension: "plist") ?? URL(fileURLWithPath: "/")
self.init(fileURL: url, useUINavigationController: useUINavigationController, navigationHandler: { _ in })
init(useUINavigationController: Bool = false) {
self.init(useUINavigationController: useUINavigationController, navigationHandler: { _ in })
}
}
14 changes: 2 additions & 12 deletions Sources/LicenseList/LicenseListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,19 @@ import UIKit
import SwiftUI

public class LicenseListViewController: UIViewController {
let fileURL: URL

public var licenseListViewStyle: LicenseListViewStyle = .plain

public init(fileURL: URL) {
self.fileURL = fileURL
public init() {
super.init(nibName: nil, bundle: nil)
}

public convenience init?(bundle: Bundle = .main) {
guard let url = bundle.url(forResource: "license-list", withExtension: "plist") else {
return nil
}
self.init(fileURL: url)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public override func viewDidLoad() {
super.viewDidLoad()
let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true) { [weak self] library in
let licenseListView = LicenseListView(useUINavigationController: true) { [weak self] library in
self?.navigateTo(library: library)
}
let vc = UIHostingController(rootView: licenseListView)
Expand Down
1 change: 0 additions & 1 deletion Sources/SourcePackagesParser/SPPError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

enum SPPError: Error {
case couldNotReadFile(String)
case noLibraries
case couldNotExportLicenseList
}
45 changes: 27 additions & 18 deletions Sources/SourcePackagesParser/SourcePackagesParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,9 @@ final class SourcePackagesParser {
}
.sorted { $0.name.lowercased() < $1.name.lowercased() }

if libraries.isEmpty {
throw SPPError.noLibraries
}
printLibraries(libraries)

// Export license-list.plist
// Export LicenseList.swift
let saveURL = URL(fileURLWithPath: outputPath)
.appendingPathComponent("license-list.plist")
.appendingPathComponent("LicenseList.swift")
try exportLicenseList(libraries, to: saveURL)
}

Expand Down Expand Up @@ -85,22 +80,36 @@ final class SourcePackagesParser {
}

private func exportLicenseList(_ libraries: [Library], to saveURL: URL) throws {
let array: [[String: Any]] = libraries.map { library in
return [
"name": library.name,
"url": library.url,
"licenseBody": library.licenseBody
]
var text = "static let libraries: [[String: String]] = []"

if libraries.isEmpty {
print("Warning: No libraries.")
} else {
printLibraries(libraries)

let arrayText = libraries
.map { library in
return """
[
"name": "\(library.name)",
"url": "\(library.url)",
"licenseBody": \(library.licenseBody.debugDescription)
]
"""
}
.joined(separator: ",\n")
.nest()
text = "static let libraries: [[String: String]] = [\n\(arrayText)\n]"
}

text = "enum SPP {\n\(text.nest())\n}\n"

if FileManager.default.fileExists(atPath: saveURL.path) {
try FileManager.default.removeItem(at: saveURL)
}
let dict: [String: Any] = ["libraries": array]

do {
let data = try PropertyListSerialization.data(fromPropertyList: dict,
format: .xml,
options: .zero)
try data.write(to: saveURL)
try text.data(using: .utf8)?.write(to: saveURL)
} catch {
throw SPPError.couldNotExportLicenseList
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/SourcePackagesParser/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ extension String {
options: .regularExpression,
range: self.range(of: self))
}

func nest() -> String {
return self.components(separatedBy: .newlines)
.map { " \($0)" }
.joined(separator: "\n")
}
}
6 changes: 1 addition & 5 deletions Sources/SourcePackagesParser/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ func exitWithSPPError(_ sppError: SPPError) -> Never {
switch sppError {
case .couldNotReadFile(let fileName):
print("Error: Could not read \(fileName).")
exit(1)
case .noLibraries:
print("Error: No libraries.")
exit(0)
case .couldNotExportLicenseList:
print("Error: Could not export license-list.plist.")
exit(1)
}
exit(1)
}

func main() {
Expand Down
20 changes: 0 additions & 20 deletions Tests/LicenseListTests/LicenseListTests.swift

This file was deleted.

Loading

0 comments on commit 814e3a9

Please sign in to comment.