Skip to content

Commit 9ce6e16

Browse files
author
Miguel Perez
committed
Refactoring
testPlatforms() was removed because the refactoring also removes InitPackage.InitPackageOptions() It wasn't possible from a user to access, it's dead code and only used by this test.
1 parent 90b3ee0 commit 9ce6e16

File tree

3 files changed

+341
-348
lines changed

3 files changed

+341
-348
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 43 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,30 @@ extension SwiftPackageTool {
222222
var packageName: String?
223223

224224
func run(_ swiftTool: SwiftTool) throws {
225+
guard let cwd = localFileSystem.currentWorkingDirectory else {
226+
throw InternalError("Could not find the current working directroy.")
227+
}
228+
225229
guard let configPath = try swiftTool.getConfigPath() else {
226230
throw InternalError("Could not find config path")
227231
}
228-
229-
try makePackage(
230-
fileSystem: localFileSystem,
231-
configPath: configPath,
232-
packageName: packageName,
233-
mode: .initialize,
234-
packageType: packageType,
235-
packageTemplate: template)
232+
233+
guard !(packageType != nil && template != nil) else {
234+
throw InternalError("Can't use --type in conjunction with --template")
235+
}
236+
237+
let initPackage = try InitPackage(fileSystem: localFileSystem,
238+
configPath: configPath,
239+
destinationPath: cwd,
240+
mode: .initialize,
241+
packageName: packageName ?? cwd.basename,
242+
packageType: packageType,
243+
packageTemplateName: template)
244+
245+
initPackage.progressReporter = { message in
246+
print(message)
247+
}
248+
try initPackage.makePackage()
236249
}
237250
}
238251

@@ -253,17 +266,30 @@ extension SwiftPackageTool {
253266
var packageName: String
254267

255268
func run(_ swiftTool: SwiftTool) throws {
269+
guard let cwd = localFileSystem.currentWorkingDirectory else {
270+
throw InternalError("Could not find the current working directroy.")
271+
}
272+
256273
guard let configPath = try swiftTool.getConfigPath() else {
257274
throw InternalError("Could not find config path")
258275
}
259276

260-
try makePackage(
261-
fileSystem: localFileSystem,
262-
configPath: configPath,
263-
packageName: packageName,
264-
mode: .create,
265-
packageType: packageType,
266-
packageTemplate: template)
277+
guard !(packageType != nil && template != nil) else {
278+
throw InternalError("Can't use --type in conjunction with --template")
279+
}
280+
281+
let initPackage = try InitPackage(fileSystem: localFileSystem,
282+
configPath: configPath,
283+
destinationPath: cwd.appending(component: packageName),
284+
mode: .create,
285+
packageName: packageName,
286+
packageType: packageType,
287+
packageTemplateName: template)
288+
289+
initPackage.progressReporter = { message in
290+
print(message)
291+
}
292+
try initPackage.makePackage()
267293
}
268294
}
269295

@@ -289,8 +315,8 @@ extension SwiftPackageTool {
289315
throw InternalError("Could not get the current working directory")
290316
}
291317

292-
if localFileSystem.exists(cwd.appending(RelativePath(url))) {
293-
let currentTemplateLocation = cwd.appending(RelativePath(url))
318+
if localFileSystem.exists(AbsolutePath(url)) {
319+
let currentTemplateLocation = AbsolutePath(url)
294320
let templateDirectory = configPath.appending(components: "templates", "new-package", url)
295321

296322
guard localFileSystem.exists(currentTemplateLocation) && localFileSystem.isDirectory(currentTemplateLocation) else {
@@ -1198,153 +1224,3 @@ fileprivate func logPackageChanges(changes: [(PackageReference, Workspace.Packag
11981224
}
11991225
stream.flush()
12001226
}
1201-
1202-
extension InitPackage {
1203-
public static var createPackageMode: Mode {
1204-
get {
1205-
switch (ProcessEnv.vars["SWIFTPM_ENABLE_PACKAGE_CREATE"].map { $0.lowercased() }) {
1206-
case "true":
1207-
return .new
1208-
default:
1209-
return .legacy
1210-
}
1211-
}
1212-
}
1213-
1214-
public enum Mode {
1215-
case new
1216-
case legacy
1217-
}
1218-
1219-
public static func getSwiftPMDefaultTemplate(packageType: InitPackage.PackageType,
1220-
sources: RelativePath = .init("./Sources"),
1221-
tests: RelativePath? = nil,
1222-
createSubDirectoryForModule: Bool = false) -> InitPackage.PackageTemplate {
1223-
// Even if we are making a "classic" package that doesn't use a template we should till use templates
1224-
// for consistency within the codebase
1225-
switch InitPackage.createPackageMode {
1226-
case .new:
1227-
return InitPackage.PackageTemplate(sourcesDirectory: sources,
1228-
testsDirectory: tests,
1229-
createSubDirectoryForModule: createSubDirectoryForModule,
1230-
packageType: packageType)
1231-
case .legacy:
1232-
return InitPackage.PackageTemplate(sourcesDirectory: RelativePath("./Sources"),
1233-
testsDirectory: RelativePath("./Tests"),
1234-
createSubDirectoryForModule: true,
1235-
packageType: packageType)
1236-
}
1237-
}
1238-
}
1239-
1240-
fileprivate func makePackage(fileSystem: FileSystem,
1241-
configPath: AbsolutePath,
1242-
packageName: String?,
1243-
mode: MakePackageMode,
1244-
packageType: InitPackage.PackageType?,
1245-
packageTemplate: String?) throws {
1246-
1247-
guard let cwd = fileSystem.currentWorkingDirectory else {
1248-
throw InternalError("Could not find the current working directroy.")
1249-
}
1250-
1251-
guard !(packageType != nil && packageTemplate != nil) else {
1252-
throw InternalError("Can't use --type in conjunction with --template")
1253-
}
1254-
1255-
let name: String
1256-
let destinationPath: AbsolutePath
1257-
let templateHomeDirectory = configPath.appending(components: "templates", "new-package")
1258-
1259-
var foundTemplate = false
1260-
var templateToUse = ""
1261-
1262-
if let templateName = packageTemplate {
1263-
guard fileSystem.exists(templateHomeDirectory.appending(component: templateName)) else {
1264-
throw InternalError("Could not find template folder: \(templateHomeDirectory.appending(component: templateName))")
1265-
}
1266-
1267-
templateToUse = templateName
1268-
foundTemplate = true
1269-
} else {
1270-
// Checking if a default template is present
1271-
if fileSystem.exists(templateHomeDirectory.appending(components: "default")) {
1272-
templateToUse = "default"
1273-
foundTemplate = true
1274-
}
1275-
}
1276-
1277-
switch mode {
1278-
case .initialize:
1279-
name = packageName ?? cwd.basename
1280-
destinationPath = cwd
1281-
case .create:
1282-
name = packageName!
1283-
try localFileSystem.createDirectory(cwd.appending(component: name))
1284-
destinationPath = cwd.appending(component: name)
1285-
}
1286-
1287-
let packageTemplate: InitPackage.PackageTemplate
1288-
if foundTemplate && InitPackage.createPackageMode == .new {
1289-
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: templateToUse)).forEach {
1290-
print("Copying \($0)")
1291-
try copyTemplate(fileSystem: fileSystem, sourcePath: templateHomeDirectory.appending(components: templateToUse, $0), destinationPath: destinationPath, name: name)
1292-
}
1293-
return
1294-
} else {
1295-
// These are only needed in the event that --type was not used when creating a package
1296-
// otherwise if --type is used packageType will not be nill
1297-
let defualtType = mode == .initialize ? InitPackage.PackageType.library : InitPackage.PackageType.executable
1298-
packageTemplate = InitPackage.getSwiftPMDefaultTemplate(packageType: packageType ?? defualtType)
1299-
}
1300-
1301-
let initPackage = try InitPackage(name: name, destinationPath: destinationPath, packageTemplate: packageTemplate)
1302-
initPackage.progressReporter = { message in
1303-
print(message)
1304-
}
1305-
try initPackage.writePackageStructure()
1306-
}
1307-
1308-
fileprivate func copyTemplate(fileSystem: FileSystem, sourcePath: AbsolutePath, destinationPath: AbsolutePath, name: String) throws {
1309-
// Recursively copy the template package
1310-
// Currently only replaces the string literal "$NAME"
1311-
if fileSystem.isDirectory(sourcePath) {
1312-
if let fileName = sourcePath.pathString.split(separator: "/").last {
1313-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
1314-
try fileSystem.createDirectory(destinationPath.appending(component: String(fileName)))
1315-
}
1316-
1317-
try fileSystem.getDirectoryContents(sourcePath).forEach {
1318-
try copyTemplate(fileSystem: fileSystem,
1319-
sourcePath: sourcePath.appending(component: $0),
1320-
destinationPath: destinationPath.appending(components: String(fileName)),
1321-
name: name)
1322-
}
1323-
}
1324-
} else {
1325-
let fileContents = try fileSystem.readFileContents(sourcePath)
1326-
1327-
if let validDescription = fileContents.validDescription {
1328-
var renamed = validDescription.replacingOccurrences(of: "___NAME___", with: name)
1329-
renamed = renamed.replacingOccurrences(of: "___NAME_AS_C99___", with: name.spm_mangledToC99ExtendedIdentifier())
1330-
1331-
if let fileName = sourcePath.pathString.split(separator: "/").last {
1332-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
1333-
try fileSystem.writeFileContents(destinationPath.appending(component: String(fileName))) { $0 <<< renamed }
1334-
}
1335-
}
1336-
} else {
1337-
// This else takes care of things such as images
1338-
if let fileName = sourcePath.pathString.split(separator: "/").last {
1339-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
1340-
try fileSystem.copy(from: sourcePath, to: destinationPath.appending(component: String(fileName)))
1341-
}
1342-
}
1343-
}
1344-
}
1345-
}
1346-
1347-
fileprivate enum MakePackageMode {
1348-
case `initialize`
1349-
case create
1350-
}

0 commit comments

Comments
 (0)