Skip to content

Commit 8464f63

Browse files
authored
Merge pull request #131 from Rock-Connotation/fix/issue-128
Fix/issue 128
2 parents aab87f3 + 41548e7 commit 8464f63

8 files changed

Lines changed: 286 additions & 58 deletions

File tree

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,25 @@
1-
// swift-tools-version: 5.9
2-
import CompilerPluginSupport
1+
// swift-tools-version: 5.8
32
import PackageDescription
43

54
let package = Package(
65
name: "SmartCodable",
7-
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13), .visionOS(.v1)],
6+
// This manifest is intentionally kept macro-free so older SwiftPM versions
7+
// can still build the runtime library without pulling in swift-syntax.
8+
//
9+
// SwiftPM will automatically pick `Package@swift-5.9.swift` on Swift 5.9+,
10+
// where macro targets and swift-syntax dependencies are defined.
11+
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6)],
812
products: [
913
// Core library without macro support (no swift-syntax dependency)
1014
// Recommended for users who don't need inheritance features
1115
.library(
1216
name: "SmartCodable",
1317
targets: ["SmartCodable"]
14-
),
15-
// Full library with macro support (includes swift-syntax dependency)
16-
// Use this if you need @SmartSubclass and inheritance features
17-
.library(
18-
name: "SmartCodableWithMacros",
19-
targets: ["SmartCodable", "SmartCodableInherit"]
20-
),
21-
// Legacy product name for backward compatibility
22-
// Deprecated: Use SmartCodableWithMacros instead
23-
.library(
24-
name: "SmartCodableInherit",
25-
targets: ["SmartCodableInherit"]
2618
)
2719
],
28-
dependencies: [
29-
// Depend on the latest Swift 5.9 SwiftSyntax
30-
.package(url: "https://github.com/swiftlang/swift-syntax", "600.0.0"..<"700.0.0")
31-
],
3220
targets: [
33-
// Targets are the basic building blocks of a package, defining a module or a test suite.
34-
// Targets can depend on other targets in this package and products from dependencies.
35-
// Macro implementation that performs the source transformation of a macro.
36-
.macro(
37-
name: "SmartCodableMacros",
38-
dependencies: [
39-
.product(name: "SwiftSyntax", package: "swift-syntax"),
40-
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
41-
.product(name: "SwiftOperators", package: "swift-syntax"),
42-
.product(name: "SwiftParser", package: "swift-syntax"),
43-
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
44-
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
45-
]
46-
),
47-
48-
// Library that exposes a macro as part of its API, which is used in client programs.
4921
.target(
5022
name: "SmartCodable",
5123
exclude: ["MacroSupport"]),
52-
53-
.target(
54-
name: "SmartCodableInherit",
55-
dependencies: [
56-
"SmartCodableMacros"
57-
],
58-
path: "Sources/SmartCodable/MacroSupport"),
59-
60-
// A test target used to develop the macro implementation.
61-
.testTarget(
62-
name: "SmartCodableTests",
63-
dependencies: [
64-
"SmartCodable",
65-
"SmartCodableInherit",
66-
"SmartCodableMacros",
67-
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
68-
]
69-
),
7024
]
7125
)

Package@swift-5.9.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// swift-tools-version: 5.9
2+
import CompilerPluginSupport
3+
import PackageDescription
4+
5+
#if compiler(>=6.3)
6+
let swiftSyntaxVersion: Version = "603.0.0"
7+
#elseif compiler(>=6.2)
8+
let swiftSyntaxVersion: Version = "602.0.0"
9+
#elseif compiler(>=6.1)
10+
let swiftSyntaxVersion: Version = "601.0.0"
11+
#elseif compiler(>=6.0)
12+
let swiftSyntaxVersion: Version = "600.0.0"
13+
#elseif compiler(>=5.10)
14+
let swiftSyntaxVersion: Version = "510.0.0"
15+
#else
16+
let swiftSyntaxVersion: Version = "509.0.0"
17+
#endif
18+
19+
let package = Package(
20+
name: "SmartCodable",
21+
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13), .visionOS(.v1)],
22+
products: [
23+
.library(
24+
name: "SmartCodable",
25+
targets: ["SmartCodable"]
26+
),
27+
.library(
28+
name: "SmartCodableInherit",
29+
targets: ["SmartCodableInherit"]
30+
)
31+
],
32+
dependencies: [
33+
// SwiftSyntax major versions track Swift compiler versions (e.g. 602.x for Swift 6.2).
34+
// Pick the matching major so SwiftPM doesn't pull a macro support module built for a
35+
// different compiler version (which then fails to import).
36+
.package(url: "https://github.com/swiftlang/swift-syntax", from: swiftSyntaxVersion)
37+
],
38+
targets: [
39+
.macro(
40+
name: "SmartCodableMacros",
41+
dependencies: [
42+
.product(name: "SwiftSyntax", package: "swift-syntax"),
43+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
44+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
45+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
46+
]
47+
),
48+
49+
.target(
50+
name: "SmartCodable",
51+
exclude: ["MacroSupport"]),
52+
53+
.target(
54+
name: "SmartCodableInherit",
55+
dependencies: [
56+
"SmartCodableMacros"
57+
],
58+
path: "Sources/SmartCodable/MacroSupport"),
59+
60+
.testTarget(
61+
name: "SmartCodableTests",
62+
dependencies: [
63+
"SmartCodable",
64+
"SmartCodableInherit",
65+
"SmartCodableMacros",
66+
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
67+
]
68+
),
69+
]
70+
)
71+

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ dependencies: [
122122
]
123123
```
124124

125+
Notes:
126+
127+
- `SmartCodable` (runtime) works without Swift Macros.
128+
- `SmartCodableInherit` (inheritance + macros) requires **Xcode 15+** and **Swift 5.9+**. Older SwiftPM toolchains will only expose the runtime library.
129+
125130

126131

127132
## Documentation
@@ -757,4 +762,3 @@ SmartCodable is an open-source project dedicated to making Swift data parsing mo
757762
<img src="https://github.com/user-attachments/assets/7b1f8108-968e-4a38-91dd-b99abdd3e500" alt="JoinUs" width="700">
758763
</p>
759764

760-

SmartCodable.podspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Pod::Spec.new do |s|
4040
ss.source_files = 'Sources/SmartCodable/MacroSupport/*{.swift}'
4141

4242
# 这些配置项与宏相关,只放在 MacroSupport subspec 中
43-
ss.preserve_paths = ["Package.swift", "Sources/SmartCodableMacros", "Tests", "Bin"]
43+
ss.preserve_paths = ["Package.swift", "Package@swift-5.9.swift", "Sources/SmartCodableMacros", "Tests", "Bin"]
4444

4545
ss.pod_target_xcconfig = {
4646
"OTHER_SWIFT_FLAGS" => "-Xfrontend -load-plugin-executable -Xfrontend $(PODS_BUILD_DIR)/SmartCodable/release/SmartCodableMacros-tool#SmartCodableMacros",
@@ -66,4 +66,3 @@ Pod::Spec.new do |s|
6666
end
6767
end
6868

69-

Sources/SmartCodable/Core/SmartCodable/SmartCodable.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
public typealias SmartCodableX = SmartDecodable & SmartEncodable
1010

11+
/// Backward-compatible name.
12+
public typealias SmartCodable = SmartCodableX
13+
1114

1215
// 用在泛型解析中
1316
extension Array: SmartCodableX where Element: SmartCodableX { }

Sources/SmartCodableMacros/SmartSubclassMacro.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ import SwiftSyntaxMacros
1414
/// A macro that automatically implements SmartCodable inheritance support
1515
public struct SmartSubclassMacro: MemberMacro {
1616
public static func expansion(
17+
of node: AttributeSyntax,
18+
providingMembersOf declaration: some DeclGroupSyntax,
19+
conformingTo protocols: [TypeSyntax],
20+
in context: some MacroExpansionContext
21+
) throws -> [DeclSyntax] {
22+
try expansionImpl(of: node, providingMembersOf: declaration, in: context)
23+
}
24+
25+
public static func expansion(
26+
of node: AttributeSyntax,
27+
providingMembersOf declaration: some DeclGroupSyntax,
28+
in context: some MacroExpansionContext
29+
) throws -> [DeclSyntax] {
30+
try expansionImpl(of: node, providingMembersOf: declaration, in: context)
31+
}
32+
33+
private static func expansionImpl(
1734
of node: AttributeSyntax,
1835
providingMembersOf declaration: some DeclGroupSyntax,
1936
in context: some MacroExpansionContext

0 commit comments

Comments
 (0)