66//
77
88import ProjectDescription
9+ import Foundation
10+
11+ // MARK: - Helper Functions
12+ private func ensureTestsSourcesDirectoryExists ( for projectName : String ) {
13+ let fileManager = FileManager . default
14+
15+ // Try to find the current module directory
16+ let currentDirectory = fileManager . currentDirectoryPath
17+ let possiblePaths = [
18+ " \( currentDirectory ) /Tests/Sources " ,
19+ " ./Tests/Sources "
20+ ]
21+
22+ for testsSourcesPath in possiblePaths {
23+ let parentDir = URL ( fileURLWithPath : testsSourcesPath ) . deletingLastPathComponent ( ) . path
24+
25+ if fileManager . fileExists ( atPath : parentDir ) || parentDir == " . " {
26+ if ! fileManager . fileExists ( atPath : testsSourcesPath ) {
27+ do {
28+ try fileManager . createDirectory ( atPath : testsSourcesPath , withIntermediateDirectories : true , attributes : nil )
29+ print ( " 📁 Created Tests/Sources directory for \( projectName ) at \( testsSourcesPath ) " )
30+ return
31+ } catch {
32+ print ( " ⚠️ Failed to create Tests/Sources directory for \( projectName ) at \( testsSourcesPath ) : \( error ) " )
33+ }
34+ } else {
35+ return // Directory already exists
36+ }
37+ }
38+ }
39+ }
940
1041public extension Project {
1142 static func makeAppModule (
@@ -23,7 +54,8 @@ public extension Project {
2354 resources : ProjectDescription . ResourceFileElements ? = nil ,
2455 infoPlist : ProjectDescription . InfoPlist = . default ,
2556 entitlements : ProjectDescription . Entitlements ? = nil ,
26- schemes : [ ProjectDescription . Scheme ] = [ ]
57+ schemes : [ ProjectDescription . Scheme ] = [ ] ,
58+ hasTests : Bool = false
2759 ) -> Project {
2860
2961 let appTarget : Target = . target (
@@ -84,18 +116,24 @@ public extension Project {
84116 dependencies : dependencies
85117 )
86118
87- let appTestTarget : Target = . target (
88- name : " \( name ) Tests " ,
89- destinations : destinations ,
90- product : . unitTests ,
91- bundleId : " \( bundleId ) . \( name ) Tests " ,
92- deploymentTargets : deploymentTarget ,
93- infoPlist : . default ,
94- sources : [ " \( name ) Tests/Sources/** " ] ,
95- dependencies : [ . target ( name : name ) ]
96- )
119+ var targets : [ Target ] = [ appTarget , appDevTarget , appStageTarget , appProdTarget ]
120+
121+ if hasTests {
122+ // Ensure Tests/Sources directory exists
123+ ensureTestsSourcesDirectoryExists ( for : name )
97124
98- let targets = [ appTarget , appDevTarget , appStageTarget , appProdTarget , appTestTarget ]
125+ let appTestTarget : Target = . target (
126+ name : " \( name ) Tests " ,
127+ destinations : destinations ,
128+ product : . unitTests ,
129+ bundleId : " \( bundleId ) . \( name ) Tests " ,
130+ deploymentTargets : deploymentTarget ,
131+ infoPlist : . default ,
132+ sources : [ " Tests/Sources/** " ] ,
133+ dependencies : [ . target ( name : name ) ]
134+ )
135+ targets . append ( appTestTarget )
136+ }
99137
100138 return Project (
101139 name : name ,
@@ -125,9 +163,10 @@ public extension Project {
125163 resources : ProjectDescription . ResourceFileElements ? = nil ,
126164 infoPlist : ProjectDescription . InfoPlist = . default ,
127165 entitlements : ProjectDescription . Entitlements ? = nil ,
128- schemes : [ ProjectDescription . Scheme ] = [ ]
166+ schemes : [ ProjectDescription . Scheme ] = [ ] ,
167+ hasTests : Bool = false
129168 ) -> Project {
130-
169+
131170 let appTarget : Target = . target (
132171 name : name ,
133172 destinations : destinations ,
@@ -141,34 +180,26 @@ public extension Project {
141180 scripts : scripts ,
142181 dependencies : dependencies
143182 )
144-
145- let appDevTarget : Target = . target (
146- name : " \( name ) -QA " ,
147- destinations : destinations ,
148- product : product ,
149- bundleId : " \( bundleId ) " ,
150- deploymentTargets : deploymentTarget ,
151- infoPlist : infoPlist ,
152- sources : sources ,
153- resources : resources ,
154- entitlements : entitlements ,
155- scripts : scripts ,
156- dependencies : dependencies
157- )
158-
159- let appTestTarget : Target = . target (
160- name : " \( name ) Tests " ,
161- destinations : destinations ,
162- product : . unitTests ,
163- bundleId : " \( bundleId ) . \( name ) Tests " ,
164- deploymentTargets : deploymentTarget ,
165- infoPlist : . default ,
166- sources : [ " \( name ) Tests/Sources/** " ] ,
167- dependencies : [ . target ( name : name ) ]
168- )
169-
170- let targets = [ appTarget , appDevTarget , appTestTarget ]
171-
183+
184+ var targets : [ Target ] = [ appTarget ]
185+
186+ if hasTests {
187+ // Ensure Tests/Sources directory exists
188+ ensureTestsSourcesDirectoryExists ( for : name )
189+
190+ let appTestTarget : Target = . target (
191+ name : " \( name ) Tests " ,
192+ destinations : destinations ,
193+ product : . unitTests ,
194+ bundleId : " \( bundleId ) . \( name ) Tests " ,
195+ deploymentTargets : deploymentTarget ,
196+ infoPlist : . default ,
197+ sources : [ " Tests/Sources/** " ] ,
198+ dependencies : [ . target ( name : name ) ]
199+ )
200+ targets . append ( appTestTarget )
201+ }
202+
172203 return Project (
173204 name : name ,
174205 packages : packages ,
0 commit comments