From dc9f29f3b33f82636283df88b782f1af0c646a2b Mon Sep 17 00:00:00 2001 From: Hiroshi Kimura Date: Thu, 31 Oct 2024 17:35:06 +0900 Subject: [PATCH 1/4] Swift6 (#45) --- Package.swift | 2 +- .../Internals/TargetViewControllerKey.swift | 19 +------------------ .../StorybookKit/Primitives/BookPage.swift | 8 ++++---- .../StorybookKit/Primitives/BookPresent.swift | 3 --- .../Primitives/BookProvider.swift | 15 ++------------- 5 files changed, 8 insertions(+), 39 deletions(-) diff --git a/Package.swift b/Package.swift index 65a3479..1f6096a 100644 --- a/Package.swift +++ b/Package.swift @@ -55,5 +55,5 @@ let package = Package( ] ), ], - swiftLanguageVersions: [.v5] + swiftLanguageModes: [.v6] ) diff --git a/Sources/StorybookKit/Internals/TargetViewControllerKey.swift b/Sources/StorybookKit/Internals/TargetViewControllerKey.swift index ddcbf92..de66384 100644 --- a/Sources/StorybookKit/Internals/TargetViewControllerKey.swift +++ b/Sources/StorybookKit/Internals/TargetViewControllerKey.swift @@ -1,22 +1,5 @@ import SwiftUI -public enum _ViewControllerKey: EnvironmentKey { - public static var defaultValue: UIViewController? -} - -enum TestKey: EnvironmentKey { - static var defaultValue: String { "default" } -} - - extension EnvironmentValues { - var _targetViewController: UIViewController? { - get { self[_ViewControllerKey.self] } - set { self[_ViewControllerKey.self] = newValue } - } - - var test: String { - get { self[TestKey.self] } - set { self[TestKey.self] = newValue } - } + @Entry var _targetViewController: UIViewController? } diff --git a/Sources/StorybookKit/Primitives/BookPage.swift b/Sources/StorybookKit/Primitives/BookPage.swift index e295337..281d564 100644 --- a/Sources/StorybookKit/Primitives/BookPage.swift +++ b/Sources/StorybookKit/Primitives/BookPage.swift @@ -23,7 +23,7 @@ import Foundation import SwiftUI import ResultBuilderKit -public struct DeclarationIdentifier: Hashable, Codable { +public struct DeclarationIdentifier: Hashable, Codable, Sendable { public let index: Int @@ -37,7 +37,7 @@ public struct DeclarationIdentifier: Hashable, Codable { } private let _lock = NSLock() -private var _counter: Int = 0 +private nonisolated(unsafe) var _counter: Int = 0 private func issueUniqueNumber() -> Int { _lock.lock() defer { @@ -52,13 +52,13 @@ public struct BookPage: BookView, Identifiable { @Environment(\.bookContext) var context - public var id: DeclarationIdentifier { + public nonisolated var id: DeclarationIdentifier { declarationIdentifier } public let title: String public let destination: AnyView - public let declarationIdentifier: DeclarationIdentifier + public nonisolated let declarationIdentifier: DeclarationIdentifier private let file: StaticString private let line: UInt diff --git a/Sources/StorybookKit/Primitives/BookPresent.swift b/Sources/StorybookKit/Primitives/BookPresent.swift index 6b1eb88..c4c32e4 100644 --- a/Sources/StorybookKit/Primitives/BookPresent.swift +++ b/Sources/StorybookKit/Primitives/BookPresent.swift @@ -25,7 +25,6 @@ import SwiftUI public struct BookPresent: BookView { @Environment(\._targetViewController) private var targetViewController - @Environment(\.test) var test public let declarationIdentifier: DeclarationIdentifier public let presentedViewControllerBlock: @MainActor () -> UIViewController @@ -44,8 +43,6 @@ public struct BookPresent: BookView { public var body: some View { Button(title) { let viewController = presentedViewControllerBlock() - print(test) - print(targetViewController) targetViewController?.present(viewController, animated: true) } } diff --git a/Sources/StorybookKit/Primitives/BookProvider.swift b/Sources/StorybookKit/Primitives/BookProvider.swift index f16f8ae..cc09e59 100644 --- a/Sources/StorybookKit/Primitives/BookProvider.swift +++ b/Sources/StorybookKit/Primitives/BookProvider.swift @@ -27,19 +27,8 @@ public protocol BookProvider { static var bookBody: BookPage { get } } -private enum BookContextKey: EnvironmentKey { - static var defaultValue: BookStore? -} - extension EnvironmentValues { - - public var bookContext: BookStore? { - get { - self[BookContextKey.self] - } - set { - self[BookContextKey.self] = newValue - } - } + + @Entry public var bookContext: BookStore? } From 4e0e90da0d5dd81c55f437c8cf7a9cdeee746ce9 Mon Sep 17 00:00:00 2001 From: Marandon Antoine Date: Wed, 13 Nov 2024 19:37:08 +0900 Subject: [PATCH 2/4] Antoine/swift6 prep (#46) The macro expansion causes warnings with swift 6. This is problematic because it belongs to the client module and blocks downstream swift6 compilation --- Package.swift | 2 +- Sources/StorybookKit/Primitives/Book.swift | 1 + Sources/StorybookKit/Primitives/BookProvider.swift | 1 + Sources/StorybookMacrosPlugin/StorybookPageMacro.swift | 1 + Sources/StorybookMacrosPlugin/StorybookPreviewMacro.swift | 1 + Sources/StorybookMacrosTests/StorybookPageTests.swift | 8 ++++++++ Sources/StorybookMacrosTests/StorybookPreviewTests.swift | 8 ++++++++ 7 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 1f6096a..2be8a49 100644 --- a/Package.swift +++ b/Package.swift @@ -55,5 +55,5 @@ let package = Package( ] ), ], - swiftLanguageModes: [.v6] + swiftLanguageModes: [.v6, .v5] ) diff --git a/Sources/StorybookKit/Primitives/Book.swift b/Sources/StorybookKit/Primitives/Book.swift index 7e9b15e..74f81e9 100644 --- a/Sources/StorybookKit/Primitives/Book.swift +++ b/Sources/StorybookKit/Primitives/Book.swift @@ -115,6 +115,7 @@ public struct FolderBuilder { public typealias Element = Book.Node + @MainActor public static func buildExpression(_ expression: Provider.Type) -> [FolderBuilder.Element] { return [.page(expression.bookBody)] } diff --git a/Sources/StorybookKit/Primitives/BookProvider.swift b/Sources/StorybookKit/Primitives/BookProvider.swift index cc09e59..8dd8092 100644 --- a/Sources/StorybookKit/Primitives/BookProvider.swift +++ b/Sources/StorybookKit/Primitives/BookProvider.swift @@ -24,6 +24,7 @@ import SwiftUI @_alwaysEmitConformanceMetadata public protocol BookProvider { + @MainActor static var bookBody: BookPage { get } } diff --git a/Sources/StorybookMacrosPlugin/StorybookPageMacro.swift b/Sources/StorybookMacrosPlugin/StorybookPageMacro.swift index 78584a5..f9f8050 100644 --- a/Sources/StorybookMacrosPlugin/StorybookPageMacro.swift +++ b/Sources/StorybookMacrosPlugin/StorybookPageMacro.swift @@ -48,6 +48,7 @@ public struct StorybookPageMacro: DeclarationMacro { .init( stringLiteral: """ enum \(enumName): BookProvider { + @MainActor static var bookBody: BookPage { .init( title: \(title), diff --git a/Sources/StorybookMacrosPlugin/StorybookPreviewMacro.swift b/Sources/StorybookMacrosPlugin/StorybookPreviewMacro.swift index cab24ef..eac2c08 100644 --- a/Sources/StorybookMacrosPlugin/StorybookPreviewMacro.swift +++ b/Sources/StorybookMacrosPlugin/StorybookPreviewMacro.swift @@ -48,6 +48,7 @@ public struct StorybookPreviewMacro: ExpressionMacro { stringLiteral: """ { enum \(enumName): BookProvider { + @MainActor static var bookBody: BookPage { .init( title: \(title), diff --git a/Sources/StorybookMacrosTests/StorybookPageTests.swift b/Sources/StorybookMacrosTests/StorybookPageTests.swift index 3d060f9..77621e4 100644 --- a/Sources/StorybookMacrosTests/StorybookPageTests.swift +++ b/Sources/StorybookMacrosTests/StorybookPageTests.swift @@ -47,6 +47,7 @@ final class StorybookPageTests: XCTestCase { } expansion: { """ enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName(UIView.self), @@ -74,6 +75,7 @@ final class StorybookPageTests: XCTestCase { } expansion: { """ enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", @@ -105,6 +107,7 @@ final class StorybookPageTests: XCTestCase { """ enum Namespace1 { enum Namespace2 { enum Namespace3 { class TestableView: UIView {} } } } enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName(target: Namespace1.Namespace2.Namespace3.TestableView.self), @@ -132,6 +135,7 @@ final class StorybookPageTests: XCTestCase { } expansion: { """ enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", @@ -163,6 +167,7 @@ final class StorybookPageTests: XCTestCase { } expansion: { """ enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName(UIView.self), @@ -193,6 +198,7 @@ final class StorybookPageTests: XCTestCase { } expansion: { """ enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", @@ -227,6 +233,7 @@ final class StorybookPageTests: XCTestCase { """ enum Namespace1 { enum Namespace2 { enum Namespace3 { class TestableView: UIView {} } } } enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName( @@ -258,6 +265,7 @@ final class StorybookPageTests: XCTestCase { } expansion: { """ enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", diff --git a/Sources/StorybookMacrosTests/StorybookPreviewTests.swift b/Sources/StorybookMacrosTests/StorybookPreviewTests.swift index c95f7b1..6722a00 100644 --- a/Sources/StorybookMacrosTests/StorybookPreviewTests.swift +++ b/Sources/StorybookMacrosTests/StorybookPreviewTests.swift @@ -51,6 +51,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview("Some title") { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName(UIView.self), @@ -85,6 +86,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", @@ -123,6 +125,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName(target: Namespace1.Namespace2.Namespace3.TestableView.self), @@ -157,6 +160,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", @@ -195,6 +199,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName(UIView.self), @@ -232,6 +237,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", @@ -273,6 +279,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: _typeName( @@ -311,6 +318,7 @@ final class StorybookPreviewTests: XCTestCase { #Preview { { enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider { + @MainActor static var bookBody: BookPage { .init( title: "Path1.Path2.Title", From b50f8d41a3ae4d26a867af7e0affe800f75e8078 Mon Sep 17 00:00:00 2001 From: Hiroshi Kimura Date: Thu, 14 Nov 2024 16:53:17 +0900 Subject: [PATCH 3/4] make public (#47) --- Sources/StorybookKit/Internals/TargetViewControllerKey.swift | 2 +- Sources/StorybookKit/Primitives/BookAction.swift | 2 +- Sources/StorybookKit/Primitives/BookPresent.swift | 2 +- Sources/StorybookKit/Primitives/BookPush.swift | 4 ++-- Sources/StorybookKit/StorybookDisplayRootView.swift | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/StorybookKit/Internals/TargetViewControllerKey.swift b/Sources/StorybookKit/Internals/TargetViewControllerKey.swift index de66384..e563833 100644 --- a/Sources/StorybookKit/Internals/TargetViewControllerKey.swift +++ b/Sources/StorybookKit/Internals/TargetViewControllerKey.swift @@ -1,5 +1,5 @@ import SwiftUI extension EnvironmentValues { - @Entry var _targetViewController: UIViewController? + @Entry public var storybook_targetViewController: UIViewController? } diff --git a/Sources/StorybookKit/Primitives/BookAction.swift b/Sources/StorybookKit/Primitives/BookAction.swift index 4b080da..8e779d4 100644 --- a/Sources/StorybookKit/Primitives/BookAction.swift +++ b/Sources/StorybookKit/Primitives/BookAction.swift @@ -23,7 +23,7 @@ import SwiftUI public struct BookAction: BookView { - @Environment(\._targetViewController) var targetViewController + @Environment(\.storybook_targetViewController) var targetViewController public let declarationIdentifier: DeclarationIdentifier diff --git a/Sources/StorybookKit/Primitives/BookPresent.swift b/Sources/StorybookKit/Primitives/BookPresent.swift index c4c32e4..2194cea 100644 --- a/Sources/StorybookKit/Primitives/BookPresent.swift +++ b/Sources/StorybookKit/Primitives/BookPresent.swift @@ -24,7 +24,7 @@ import SwiftUI /// A component descriptor that just displays UI-Component public struct BookPresent: BookView { - @Environment(\._targetViewController) private var targetViewController + @Environment(\.storybook_targetViewController) private var targetViewController public let declarationIdentifier: DeclarationIdentifier public let presentedViewControllerBlock: @MainActor () -> UIViewController diff --git a/Sources/StorybookKit/Primitives/BookPush.swift b/Sources/StorybookKit/Primitives/BookPush.swift index 632ae98..622305d 100644 --- a/Sources/StorybookKit/Primitives/BookPush.swift +++ b/Sources/StorybookKit/Primitives/BookPush.swift @@ -24,7 +24,7 @@ import SwiftUI /// A component descriptor that previewing with push presentation. public struct BookPush: BookView { - @Environment(\._targetViewController) var targetViewController + @Environment(\.storybook_targetViewController) var targetViewController public let pushingViewControllerBlock: @MainActor () -> UIViewController public let declarationIdentifier: DeclarationIdentifier @@ -44,7 +44,7 @@ public struct BookPush: BookView { NavigationLink(title, destination: { _ViewControllerHost(instantiate: pushingViewControllerBlock) - .environment(\._targetViewController, targetViewController) + .environment(\.storybook_targetViewController, targetViewController) }) } diff --git a/Sources/StorybookKit/StorybookDisplayRootView.swift b/Sources/StorybookKit/StorybookDisplayRootView.swift index b6bb6b4..c44edbe 100644 --- a/Sources/StorybookKit/StorybookDisplayRootView.swift +++ b/Sources/StorybookKit/StorybookDisplayRootView.swift @@ -163,7 +163,7 @@ final class _ViewController: UIViewController { let hosting = UIHostingController( rootView: content - .environment(\._targetViewController, self) + .environment(\.storybook_targetViewController, self) ) addChild(hosting) From b0538f7ede392b2c6448ce1ac6e128ba1a748ac9 Mon Sep 17 00:00:00 2001 From: Marandon Antoine Date: Fri, 15 Nov 2024 16:05:49 +0900 Subject: [PATCH 4/4] Swift 6 compatibility (#48) --- Sources/StorybookKit/Primitives/BookPage.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StorybookKit/Primitives/BookPage.swift b/Sources/StorybookKit/Primitives/BookPage.swift index 281d564..2ed9210 100644 --- a/Sources/StorybookKit/Primitives/BookPage.swift +++ b/Sources/StorybookKit/Primitives/BookPage.swift @@ -66,7 +66,7 @@ public struct BookPage: BookView, Identifiable { _ file: StaticString = #fileID, _ line: UInt = #line, title: String, - @ViewBuilder destination: () -> Destination + @ViewBuilder destination: @MainActor () -> Destination ) { self.file = file self.line = line