Skip to content

Commit

Permalink
Merge pull request #87 from FelixHerrmann/feature/package-list-revision
Browse files Browse the repository at this point in the history
[Feature] Package List Revision
  • Loading branch information
FelixHerrmann authored Jan 4, 2024
2 parents 10a3199 + dc4d308 commit 479f12f
Show file tree
Hide file tree
Showing 23 changed files with 492 additions and 286 deletions.
3 changes: 3 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ identifier_name:
- x
- y

indentation_width:
include_compiler_directives: false

line_length:
warning: 130
error: 200
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,29 @@ let package = Package(
),
.target(
name: "SwiftPackageListCore",
dependencies: ["SwiftPackageList"]
dependencies: [.target(name: "SwiftPackageList")]
),
.target(name: "SwiftPackageList"),
.target(name: "SwiftPackageListObjc"),
.target(
name: "SwiftPackageListUI",
dependencies: ["SwiftPackageList"],
dependencies: [.target(name: "SwiftPackageList")],
resources: [.process("Resources")]
),
.testTarget(name: "swift-package-list-tests"),
.testTarget(
name: "SwiftPackageListCoreTests",
dependencies: ["SwiftPackageListCore"],
dependencies: [.target(name: "SwiftPackageListCore")],
resources: [.copy("Resources")]
),
.testTarget(
name: "SwiftPackageListTests",
dependencies: ["SwiftPackageList"],
dependencies: [.target(name: "SwiftPackageList")],
resources: [.process("Resources")]
),
.testTarget(
name: "SwiftPackageListObjcTests",
dependencies: ["SwiftPackageListObjc"],
dependencies: [.target(name: "SwiftPackageListObjc")],
resources: [.process("Resources")]
),
]
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A command-line tool to get all used Swift Package dependencies.
The output includes all the `Package.resolved` informations and the license from the checkouts.
You can also generate a JSON, PLIST, Settings.bundle or PDF file.

Additionally there is a Swift Package to read the generated package-list file from the application's bundle with a top-level function or pre-build UI.
Additionally there is a Swift Package to read the generated package-list file from the application's bundle or to use pre-build UI for SwiftUI and UIKit.


## Command-Line Tool
Expand Down Expand Up @@ -164,7 +164,7 @@ You can then use [QuickLook](https://developer.apple.com/documentation/quicklook
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FFelixHerrmann%2Fswift-package-list%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/FelixHerrmann/swift-package-list)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FFelixHerrmann%2Fswift-package-list%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/FelixHerrmann/swift-package-list)

Load `package-list.json` or `package-list.plist` from the bundle with a single function call or use the pre-build UI components.
Load the generated package-list file from the bundle or use some pre-build UI components.

### Requirements

Expand All @@ -186,11 +186,10 @@ and `SwiftPackageListUI` to get an iOS Settings-like user interface.
```swift
import SwiftPackageList

let packageProvider = JSONPackageProvider()
do {
let packages = try packageList()
let packages = try packageProvider.packages()
// use packages
} catch PackageListError.noPackageList {
print("There is no package-list file")
} catch {
print(error)
}
Expand Down
51 changes: 51 additions & 0 deletions Sources/SwiftPackageList/JSONPackageProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// JSONPackageProvider.swift
// SwiftPackageList
//
// Created by Felix Herrmann on 02.01.24.
//

import Foundation

/// A package provider for `.json` package-lists stored in a `Bundle`.
public struct JSONPackageProvider: PackageProvider {

/// The bundle to read the JSON file from.
///
/// Default value is `Bundle.main`.
public var bundle: Bundle

/// The name of the JSON file, usually specified with the `--custom-file-name` option.
///
/// Default value is `"package-list"`.
public var fileName: String

/// Creates a JSON package provider.
/// - Parameters:
/// - bundle: The bundle to read the JSON file from.
/// - fileName: The name of the JSON file.
public init(bundle: Bundle = .main, fileName: String = "package-list") {
self.bundle = bundle
self.fileName = fileName
}

public func packages() throws -> [Package] {
guard let path = bundle.path(forResource: fileName, ofType: "json") else {
throw CocoaError(.fileNoSuchFile, userInfo: ["file_name": fileName, "file_type": "json"])
}
let data = try Data(contentsOf: URL(fileURLWithPath: path))
let decoder = JSONDecoder()
return try decoder.decode([Package].self, from: data)
}
}

extension PackageProvider where Self == JSONPackageProvider {
/// A JSON package provider.
/// - Parameters:
/// - bundle: The bundle to read the JSON file from.
/// - fileName: The name of the JSON file.
/// - Returns: A ``JSONPackageProvider`` object.
public static func json(bundle: Bundle = .main, fileName: String = "package-list") -> Self {
return JSONPackageProvider(bundle: bundle, fileName: fileName)
}
}
31 changes: 0 additions & 31 deletions Sources/SwiftPackageList/PackageList.swift

This file was deleted.

13 changes: 0 additions & 13 deletions Sources/SwiftPackageList/PackageListError.swift

This file was deleted.

15 changes: 15 additions & 0 deletions Sources/SwiftPackageList/PackageProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// PackageProvider.swift
// SwiftPackageList
//
// Created by Felix Herrmann on 02.01.24.
//

/// A type that provides ``Package``s from some arbitrary source.
public protocol PackageProvider {

/// Provides the array of packages.
/// - Returns: An array of ``Package`` objects.
/// - Throws: This method can throw if something went wrong during the process.
func packages() throws -> [Package]
}
51 changes: 51 additions & 0 deletions Sources/SwiftPackageList/PropertyListPackageProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// PropertyListPackageProvider.swift
// SwiftPackageList
//
// Created by Felix Herrmann on 02.01.24.
//

import Foundation

/// A package provider for `.plist` package-lists stored in a `Bundle`.
public struct PropertyListPackageProvider: PackageProvider {

/// The bundle to read the Property List file from.
///
/// Default value is `Bundle.main`.
public var bundle: Bundle

/// The name of the Property List file, usually specified with the `--custom-file-name` option.
///
/// Default value is `"package-list"`.
public var fileName: String

/// Creates a Property List package provider.
/// - Parameters:
/// - bundle: The bundle to read the Property List file from.
/// - fileName: The name of the Property List file.
public init(bundle: Bundle = .main, fileName: String = "package-list") {
self.bundle = bundle
self.fileName = fileName
}

public func packages() throws -> [Package] {
guard let path = bundle.path(forResource: fileName, ofType: "plist") else {
throw CocoaError(.fileNoSuchFile, userInfo: ["file_name": fileName, "file_type": "plist"])
}
let data = try Data(contentsOf: URL(fileURLWithPath: path))
let decoder = PropertyListDecoder()
return try decoder.decode([Package].self, from: data)
}
}

extension PackageProvider where Self == PropertyListPackageProvider {
/// A Property List package provider.
/// - Parameters:
/// - bundle: The bundle to read the Property List file from.
/// - fileName: The name of the Property List file.
/// - Returns: A ``PropertyListPackageProvider`` object.
public static func propertyList(bundle: Bundle = .main, fileName: String = "package-list") -> Self {
return PropertyListPackageProvider(bundle: bundle, fileName: fileName)
}
}
51 changes: 42 additions & 9 deletions Sources/SwiftPackageListUI/SwiftUI/AcknowledgmentsList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#if canImport(SwiftUI)

import SwiftUI
import OSLog
import SwiftPackageList

/// A `List` that shows all licenses from the package-list file.
///
Expand Down Expand Up @@ -35,30 +37,61 @@ import SwiftUI
/// ```
///
/// - Important: This view must be used inside a `NavigationStack` to function properly.
public struct AcknowledgmentsList: View {
@ObservedObject private var _viewModel: _AcknowledgmentsListViewModel
public struct AcknowledgmentsList<Provider: PackageProvider>: View {
private let _packageProvider: Provider
@State private var _packages: [Package] = []

/// Creates a ``AcknowledgmentsList`` for a package-list file.
/// Creates a ``AcknowledgmentsList`` for a package provider.
/// - Parameters:
/// - packageListBundle: The bundle where the package-list file is stored. Default's to `Bundle.main`.
/// - packageListFileName: The name of the package-list file. Default's to `package-list`.
public init(packageListBundle: Bundle = .main, packageListFileName: String = "package-list") {
_viewModel = _AcknowledgmentsListViewModel(packageListBundle: packageListBundle, packageListFileName: packageListFileName)
/// - packageProvider: The package provider object used as the source of data.
public init(packageProvider: Provider = .json()) {
self._packageProvider = packageProvider
}

public var body: some View {
List {
Section(
header: Text("acknowledgments.section-title", bundle: .module, comment: "Section title for the license list")
) {
ForEach(_viewModel._packages, id: \.self) { package in
ForEach(_packages, id: \.self) { package in
NavigationLink(package.name) {
_LicenseText(_package: package)
}
}
}
}
._navigationTitle(Text("acknowledgments.title", bundle: .module, comment: "Navigation bar title of the license list"))
#if os(visionOS)
.navigationTitle(
Text("acknowledgments.title", bundle: .module, comment: "Navigation bar title of the license list")
)
.navigationBarTitleDisplayMode(.inline)
.task {
_loadPackages()
}
#else
.backport.navigationTitle(
Text("acknowledgments.title", bundle: .module, comment: "Navigation bar title of the license list")
)
#if os(iOS) || os(watchOS)
.backport.navigationBarTitleDisplayMode(.inline)
#endif
.backport.task {
_loadPackages()
}
#endif
}

private func _loadPackages() {
do {
_packages = try _packageProvider.packages()
} catch {
os_log(
"Error: %@",
log: OSLog(subsystem: "com.felixherrmann.swift-package-list", category: "AcknowledgmentsList"),
type: .error,
String(describing: error)
)
}
}
}

Expand Down

This file was deleted.

23 changes: 15 additions & 8 deletions Sources/SwiftPackageListUI/SwiftUI/_LicenseText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ import SwiftUI
import SwiftPackageList

internal struct _LicenseText: View {
internal var _package: Package
internal let _package: Package

internal var body: some View {
ZStack {
#if os(iOS)
#if os(iOS)
Color(.systemGroupedBackground)
.edgesIgnoringSafeArea(.all)
#endif

#if os(tvOS)
#endif
#if os(tvOS)
_TVOSTextView(_text: _package.license ?? "")
#else
#else
ScrollView {
Text(_package.license ?? "")
.font(.caption)
.foregroundColor(.secondary)
.padding()
.frame(maxWidth: .infinity)
}
#endif
#endif
}
._navigationTitle(Text(_package.name))
#if os(visionOS)
.navigationTitle(Text(_package.name))
.navigationBarTitleDisplayMode(.inline)
#else
.backport.navigationTitle(Text(_package.name))
#if os(iOS) || os(watchOS) || os(visionOS)
.backport.navigationBarTitleDisplayMode(.inline)
#endif
#endif
}
}

Expand Down
Loading

0 comments on commit 479f12f

Please sign in to comment.