spm_swift_package/domain/file/
project_templates.rs

1/// Provides template generators for all project files
2/// Each method returns the content of a specific file as a String
3pub struct ProjectTemplates;
4
5impl ProjectTemplates {
6	/// Returns the default Swift source file template
7	/// Used to generate the main `{name}.swift` file inside Sources
8	pub fn project_swift_content() -> String {
9		r#"// The Swift Programming Language
10// https://docs.swift.org/swift-book/
11"#
12		.to_string()
13	}
14
15	/// Returns the default XCTest file template
16	/// Includes boilerplate test structure for `{project_name}Tests.swift`
17	pub fn test_content(project_name: &str) -> String {
18		format!(
19			r#"import XCTest
20@testable import {}
21
22final class {}Tests: XCTestCase {{
23    func testExample() throws {{
24        // XCTest Documentation
25        // https://developer.apple.com/documentation/xctest
26
27        // Defining Test Cases and Test Methods
28        // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
29    }}
30}}
31"#,
32			project_name, project_name,
33		)
34	}
35
36	/// Returns the Package.swift template
37	/// Supports two variants: plugin-enabled or standard package
38	///
39	/// * `project_name` - name of the Swift package
40	/// * `platform` - selected platform such as iOS or macOS
41	/// * `version` - minimum deployment version
42	/// * `is_plugin` - adds SwiftLint plugin configuration when true
43	pub fn package_swift_content(
44		project_name: &str,
45		platform: &str,
46		version: &str,
47		is_plugin: bool,
48	) -> String {
49		if is_plugin {
50			format!(
51				r#"// swift-tools-version: 6.2
52// The swift-tools-version declares the minimum version of Swift required to build this package.
53
54import PackageDescription
55
56let package = Package(
57    name: "{}",
58    platforms: [
59        .{}(.v{})
60    ],
61    products: [
62        // Products define the executables and libraries a package produces, making them visible to other packages.
63        .library(
64            name: "{}",
65            targets: ["{}"]),
66    ],
67    dependencies: [
68        .package(url: "https://github.com/lukepistrol/SwiftLintPlugin", exact: "0.62.2")
69    ],
70    targets: [
71        // Targets are the basic building blocks of a package, defining a module or a test suite.
72        // Targets can depend on other targets in this package and products from dependencies.
73        .target(
74            name: "{}",
75            plugins: [
76                .plugin(name: "SwiftLint", package: "SwiftLintPlugin")
77            ]
78        ),
79        .testTarget(
80            name: "{}Tests",
81            dependencies: ["{}"]
82        ),
83    ]
84)
85"#,
86				project_name,
87				platform,
88				version,
89				project_name,
90				project_name,
91				project_name,
92				project_name,
93				project_name,
94			)
95		} else {
96			format!(
97				r#"// swift-tools-version: 6.2
98// The swift-tools-version declares the minimum version of Swift required to build this package.
99
100import PackageDescription
101
102let package = Package(
103    name: "{}",
104    platforms: [
105        .{}(.v{})
106    ],
107    products: [
108        // Products define the executables and libraries a package produces, making them visible to other packages.
109        .library(
110            name: "{}",
111            targets: ["{}"]),
112    ],
113    targets: [
114        // Targets are the basic building blocks of a package, defining a module or a test suite.
115        // Targets can depend on other targets in this package and products from dependencies.
116        .target(
117            name: "{}"
118        ),
119        .testTarget(
120            name: "{}Tests",
121            dependencies: ["{}"]
122        ),
123    ]
124)
125"#,
126				project_name,
127				platform,
128				version,
129				project_name,
130				project_name,
131				project_name,
132				project_name,
133				project_name,
134			)
135		}
136	}
137
138	/// Returns the default CHANGELOG.md template
139	/// Used when the Changelog option is selected
140	pub fn changelog_content() -> String {
141		r#"# CHANGELOG
142
143## Version 1.0.0
144**2024-01-18**
145
146- First release
147"#
148		.to_string()
149	}
150
151	/// Returns the README.md template
152	/// Includes only the project title by default
153	pub fn readme_content(project_name: &str) -> String {
154		format!(
155			r#"# {}
156"#,
157			project_name
158		)
159	}
160
161	/// Returns the .spi.yml template for Swift Package Index configuration
162	/// Defines documentation targets and scheme attributes
163	pub fn spi_content(project_name: &str) -> String {
164		format!(
165			r#"version: 1
166builder:
167  configs:
168    - documentation_targets: [{}]
169      scheme: {}
170"#,
171			project_name, project_name
172		)
173	}
174
175	/// Returns a default SwiftLint configuration template
176	/// Includes baseline rules, opt-in rules, and excluded directories
177	pub fn swiftlint_content() -> String {
178		r#"disabled_rules:
179  - trailing_whitespace
180
181vertical_whitespace:
182    severity: error
183
184opt_in_rules:
185  - empty_count
186  - comma
187
188excluded:
189  - Pods
190  - Carthage
191  - Fastlane
192"#
193		.to_string()
194	}
195}