Skip to content

Commit e28a5ae

Browse files
feat: improve rollback - don't save project file until all operations succeed
1 parent b6d0985 commit e28a5ae

File tree

8 files changed

+46
-43
lines changed

8 files changed

+46
-43
lines changed

Sources/XcodeProjectCLI/Subcommands/Files/DeleteFileCommand.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ struct DeleteFileCommand: ParsableCommand {
2222
}
2323

2424
try project.files.removeFile(filePath)
25-
try project.save()
26-
27-
guard !options.projectOnly else { return }
2825

29-
try FileManager.default.removeItem(atPath: filePath.absolutePath)
26+
if !options.projectOnly {
27+
try FileManager.default.removeItem(atPath: filePath.absolutePath)
28+
}
29+
try project.save()
3030
}
3131
}

Sources/XcodeProjectCLI/Subcommands/Files/MoveFileCommand.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ struct MoveFileCommand: ParsableCommand {
3333
}
3434

3535
let targets = try project.files.moveFile(filePath, to: destination)
36-
try project.save()
3736

3837
if printTargets {
3938
targets.forEach { print($0) }
4039
}
4140

42-
guard !options.projectOnly else { return }
43-
44-
try FileManager.default.moveItem(
45-
atPath: filePath.absolutePath,
46-
toPath: destination.absolutePath
47-
)
41+
if !options.projectOnly {
42+
try FileManager.default.moveItem(
43+
atPath: filePath.absolutePath,
44+
toPath: destination.absolutePath
45+
)
46+
}
47+
try project.save()
4848
}
4949
}

Sources/XcodeProjectCLI/Subcommands/Files/RenameFileCommand.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ struct RenameFileCommand: ParsableCommand {
2525
}
2626

2727
try project.files.renameFile(filePath, newName: name)
28-
try project.save()
29-
30-
guard !options.projectOnly else { return }
3128

32-
try FileManager.default.moveItem(
33-
atPath: filePath.absolutePath,
34-
toPath: filePath.changeLastComponent(to: name).absolutePath
35-
)
29+
if !options.projectOnly {
30+
try FileManager.default.moveItem(
31+
atPath: filePath.absolutePath,
32+
toPath: filePath.changeLastComponent(to: name).absolutePath
33+
)
34+
}
35+
try project.save()
3636
}
3737
}

Sources/XcodeProjectCLI/Subcommands/Groups/AddGroupCommand.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ struct AddGroupCommand: ParsableCommand {
2020
let project = try Project(projectPath: options.projectPath)
2121
let groupPath = groupPath.asInputPath
2222

23-
if !groupPath.exists {
24-
if createGroups, !options.projectOnly {
25-
try FileManager.default.createDirectory(
26-
atPath: groupPath.absolutePath,
27-
withIntermediateDirectories: true
28-
)
29-
} else if !createGroups {
30-
throw CLIError.groupNotFoundOnDisk(groupPath)
31-
}
23+
try project.groups.addGroup(groupPath)
24+
25+
guard !options.projectOnly, !groupPath.exists else {
26+
return try project.save()
3227
}
3328

34-
try project.groups.addGroup(groupPath)
29+
guard createGroups else {
30+
throw CLIError.groupNotFoundOnDisk(groupPath)
31+
}
32+
33+
try FileManager.default.createDirectory(
34+
atPath: groupPath.absolutePath,
35+
withIntermediateDirectories: true
36+
)
3537
try project.save()
3638
}
3739
}

Sources/XcodeProjectCLI/Subcommands/Groups/DeleteGroupCommand.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ struct DeleteGroupCommand: ParsableCommand {
2222
}
2323

2424
try project.groups.deleteGroup(groupPath)
25-
try project.save()
26-
27-
guard !options.projectOnly else { return }
2825

29-
try FileManager.default.removeItem(atPath: groupPath.absolutePath)
26+
if !options.projectOnly {
27+
try FileManager.default.removeItem(atPath: groupPath.absolutePath)
28+
}
29+
try project.save()
3030
}
3131
}

Sources/XcodeProjectCLI/Subcommands/Groups/MoveGroupCommand.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ struct MoveGroupCommand: ParsableCommand {
3030
}
3131

3232
try project.groups.moveGroup(groupPath, to: destination)
33-
try project.save()
3433

35-
guard !options.projectOnly else { return }
34+
guard !options.projectOnly else { return try project.save() }
3635

3736
let newGroupPath = destination.appending(groupPath.lastComponent)
3837

@@ -50,6 +49,8 @@ struct MoveGroupCommand: ParsableCommand {
5049
toPath: newGroupPath.absolutePath
5150
)
5251
}
52+
53+
try project.save()
5354
}
5455

5556
private func mergeDirectories(from source: String, to destination: String) throws {

Sources/XcodeProjectCLI/Subcommands/Groups/RenameGroupCommand.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct RenameGroupCommand: ParsableCommand {
1010
@OptionGroup
1111
var options: ProjectWriteOptions
1212

13-
@Option(name: .customLong("group"), help: "Path to group.")
13+
@Option(name: .customLong("group"), help: .init("Group path.", valueName: "group-path"))
1414
var groupPath: String
1515

1616
@Option(name: .customLong("name"), help: "New name for the group.")
@@ -25,13 +25,13 @@ struct RenameGroupCommand: ParsableCommand {
2525
}
2626

2727
try project.groups.renameGroup(groupPath, newName: name)
28-
try project.save()
29-
30-
guard !options.projectOnly else { return }
3128

32-
try FileManager.default.moveItem(
33-
atPath: groupPath.absolutePath,
34-
toPath: groupPath.changeLastComponent(to: name).absolutePath
35-
)
29+
if !options.projectOnly {
30+
try FileManager.default.moveItem(
31+
atPath: groupPath.absolutePath,
32+
toPath: groupPath.changeLastComponent(to: name).absolutePath
33+
)
34+
}
35+
try project.save()
3636
}
3737
}

Sources/XcodeProjectCLI/XcodeProjectCLI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct XcodeProjectCLI: ParsableCommand {
3434
static let configuration: CommandConfiguration = .init(
3535
commandName: "xcp",
3636
abstract: "XcodeProjectCLI",
37-
version: "0.9.2",
37+
version: "0.9.3",
3838
groupedSubcommands: [
3939
.init(
4040
name: "Target",

0 commit comments

Comments
 (0)