@@ -83,21 +83,6 @@ public final class InitPackage {
83
83
moduleName
84
84
}
85
85
}
86
-
87
- public convenience init ( fileSystem: FileSystem ,
88
- configPath: AbsolutePath ,
89
- destinationPath: AbsolutePath ,
90
- name: String ,
91
- packageType: PackageType
92
- ) throws {
93
- try self . init ( fileSystem: fileSystem,
94
- configPath: configPath,
95
- destinationPath: destinationPath,
96
- mode: . initialize,
97
- packageName: name,
98
- packageType: packageType,
99
- packageTemplateName: nil )
100
- }
101
86
102
87
/// Create an instance that can create a package with given arguments.
103
88
public init ( fileSystem: FileSystem ,
@@ -139,22 +124,22 @@ public final class InitPackage {
139
124
140
125
private func makePackageNew( ) throws {
141
126
let templateHomeDirectory = configPath. appending ( components: " templates " , " new-package " )
142
- var templateToUse : String ?
127
+ var template : String ?
143
128
144
129
if let templateName = packageTemplateName {
145
130
guard fileSystem. exists ( templateHomeDirectory. appending ( component: templateName) ) else {
146
131
throw InternalError ( " Could not find template folder: \( templateHomeDirectory. appending ( component: templateName) ) " )
147
132
}
148
133
149
- templateToUse = templateName
134
+ template = templateName
150
135
} else {
151
136
// Checking if a default template is present
152
137
if fileSystem. exists ( templateHomeDirectory. appending ( components: " default " ) ) {
153
- templateToUse = " default "
138
+ template = " default "
154
139
}
155
140
}
156
141
157
- if let templateName = templateToUse {
142
+ if let templateName = template {
158
143
try fileSystem. getDirectoryContents ( templateHomeDirectory. appending ( component: templateName) ) . forEach {
159
144
progressReporter ? ( " Copying \( $0) " )
160
145
try copyTemplate ( fileSystem: fileSystem,
@@ -177,6 +162,65 @@ public final class InitPackage {
177
162
try writePackageStructure ( template: template)
178
163
}
179
164
165
+ private func getSwiftPMDefaultTemplate( packageType: InitPackage . PackageType ,
166
+ sources: RelativePath = . init( " ./Sources " ) ,
167
+ tests: RelativePath ? = nil ,
168
+ createSubDirectoryForModule: Bool = false ) -> InitPackage . PackageTemplate {
169
+ // Even if we are making a "classic" package that doesn't use a template we should till use templates
170
+ // for consistency within the codebase
171
+ switch InitPackage . createPackageMode {
172
+ case . new:
173
+ return InitPackage . PackageTemplate ( sourcesDirectory: sources,
174
+ testsDirectory: tests,
175
+ createSubDirectoryForModule: createSubDirectoryForModule,
176
+ packageType: packageType)
177
+ case . legacy:
178
+ return InitPackage . PackageTemplate ( sourcesDirectory: RelativePath ( " ./Sources " ) ,
179
+ testsDirectory: RelativePath ( " ./Tests " ) ,
180
+ createSubDirectoryForModule: true ,
181
+ packageType: packageType)
182
+ }
183
+ }
184
+
185
+ private func copyTemplate( fileSystem: FileSystem , sourcePath: AbsolutePath , destinationPath: AbsolutePath , name: String ) throws {
186
+ // Recursively copy the template package
187
+ // Currently only replaces the string literal "$NAME"
188
+ if fileSystem. isDirectory ( sourcePath) {
189
+ if let dirName = sourcePath. pathString. split ( separator: " / " ) . last {
190
+ if !fileSystem. exists ( destinationPath. appending ( component: String ( dirName) ) ) {
191
+ try fileSystem. createDirectory ( destinationPath. appending ( component: String ( dirName) ) )
192
+ }
193
+
194
+ try fileSystem. getDirectoryContents ( sourcePath) . forEach {
195
+ try copyTemplate ( fileSystem: fileSystem,
196
+ sourcePath: sourcePath. appending ( component: $0) ,
197
+ destinationPath: destinationPath. appending ( components: String ( dirName) ) ,
198
+ name: name)
199
+ }
200
+ }
201
+ } else {
202
+ let fileContents = try fileSystem. readFileContents ( sourcePath)
203
+
204
+ if let validDescription = fileContents. validDescription {
205
+ if let fileName = sourcePath. pathString. split ( separator: " / " ) . last {
206
+ if !fileSystem. exists ( destinationPath. appending ( component: String ( fileName) ) ) {
207
+ var renamed = validDescription. replacingOccurrences ( of: " ___NAME___ " , with: name)
208
+ renamed = renamed. replacingOccurrences ( of: " ___NAME_AS_C99___ " , with: name. spm_mangledToC99ExtendedIdentifier ( ) )
209
+
210
+ try fileSystem. writeFileContents ( destinationPath. appending ( component: String ( fileName) ) ) { $0 <<< renamed }
211
+ }
212
+ }
213
+ } else {
214
+ // This else takes care of things such as images
215
+ if let fileName = sourcePath. pathString. split ( separator: " / " ) . last {
216
+ if !fileSystem. exists ( destinationPath. appending ( component: String ( fileName) ) ) {
217
+ try fileSystem. copy ( from: sourcePath, to: destinationPath. appending ( component: String ( fileName) ) )
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+
180
224
/// Actually creates the new package at the destinationPath
181
225
private func writePackageStructure( template: PackageTemplate ) throws {
182
226
progressReporter ? ( " Creating \( template. packageType) package: \( packageName) " )
@@ -521,65 +565,6 @@ public final class InitPackage {
521
565
try writeExecutableTestsFile ( testClassFile)
522
566
}
523
567
}
524
-
525
- private func getSwiftPMDefaultTemplate( packageType: InitPackage . PackageType ,
526
- sources: RelativePath = . init( " ./Sources " ) ,
527
- tests: RelativePath ? = nil ,
528
- createSubDirectoryForModule: Bool = false ) -> InitPackage . PackageTemplate {
529
- // Even if we are making a "classic" package that doesn't use a template we should till use templates
530
- // for consistency within the codebase
531
- switch InitPackage . createPackageMode {
532
- case . new:
533
- return InitPackage . PackageTemplate ( sourcesDirectory: sources,
534
- testsDirectory: tests,
535
- createSubDirectoryForModule: createSubDirectoryForModule,
536
- packageType: packageType)
537
- case . legacy:
538
- return InitPackage . PackageTemplate ( sourcesDirectory: RelativePath ( " ./Sources " ) ,
539
- testsDirectory: RelativePath ( " ./Tests " ) ,
540
- createSubDirectoryForModule: true ,
541
- packageType: packageType)
542
- }
543
- }
544
-
545
- private func copyTemplate( fileSystem: FileSystem , sourcePath: AbsolutePath , destinationPath: AbsolutePath , name: String ) throws {
546
- // Recursively copy the template package
547
- // Currently only replaces the string literal "$NAME"
548
- if fileSystem. isDirectory ( sourcePath) {
549
- if let fileName = sourcePath. pathString. split ( separator: " / " ) . last {
550
- if !fileSystem. exists ( destinationPath. appending ( component: String ( fileName) ) ) {
551
- try fileSystem. createDirectory ( destinationPath. appending ( component: String ( fileName) ) )
552
- }
553
-
554
- try fileSystem. getDirectoryContents ( sourcePath) . forEach {
555
- try copyTemplate ( fileSystem: fileSystem,
556
- sourcePath: sourcePath. appending ( component: $0) ,
557
- destinationPath: destinationPath. appending ( components: String ( fileName) ) ,
558
- name: name)
559
- }
560
- }
561
- } else {
562
- let fileContents = try fileSystem. readFileContents ( sourcePath)
563
-
564
- if let validDescription = fileContents. validDescription {
565
- var renamed = validDescription. replacingOccurrences ( of: " ___NAME___ " , with: name)
566
- renamed = renamed. replacingOccurrences ( of: " ___NAME_AS_C99___ " , with: name. spm_mangledToC99ExtendedIdentifier ( ) )
567
-
568
- if let fileName = sourcePath. pathString. split ( separator: " / " ) . last {
569
- if !fileSystem. exists ( destinationPath. appending ( component: String ( fileName) ) ) {
570
- try fileSystem. writeFileContents ( destinationPath. appending ( component: String ( fileName) ) ) { $0 <<< renamed }
571
- }
572
- }
573
- } else {
574
- // This else takes care of things such as images
575
- if let fileName = sourcePath. pathString. split ( separator: " / " ) . last {
576
- if !fileSystem. exists ( destinationPath. appending ( component: String ( fileName) ) ) {
577
- try fileSystem. copy ( from: sourcePath, to: destinationPath. appending ( component: String ( fileName) ) )
578
- }
579
- }
580
- }
581
- }
582
- }
583
568
584
569
// TEMP
585
570
public enum Mode {
0 commit comments