Skip to content

Commit c81c876

Browse files
committed
If no workspace, but a project, create an empty workspace
1 parent 2f23777 commit c81c876

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Sources/punic/Path+Find.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ extension Path {
6060
func has(extension ext: Extension) -> Bool {
6161
return self.pathExtension == ext.rawValue
6262
}
63+
64+
func of(extension ext: Extension) -> Path {
65+
var path = self.parent + self.fileName
66+
path.pathExtension = ext.rawValue
67+
return path
68+
}
6369
}

Sources/punic/Punic+Workspace.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ extension Punic {
2828
func run(options: Punic.Options) {
2929
let rootPath = options.rootPath(extension: .xcworkspace)
3030
let carthagePath: Path = rootPath + sourceDir
31+
32+
var created = false
33+
if options.filePath(extension: .xcworkspace) == nil,
34+
let projectPath: Path = options.filePath(extension: .xcodeproj) {
35+
// if no workspace but a project we could create it
36+
let xmlString = """
37+
<?xml version="1.0" encoding="UTF-8"?>
38+
<Workspace
39+
version = "1.0">
40+
<FileRef
41+
location = "container:\(projectPath.fileName)">
42+
</FileRef>
43+
</Workspace>
44+
"""
45+
let workspacePath: Path = projectPath.of(extension: .xcworkspace)
46+
if let data = xmlString.data(using: .utf8) {
47+
do {
48+
try workspacePath.createDirectory()
49+
try DataFile(path: workspacePath + "contents.xcworkspacedata").write(data)
50+
created = true
51+
} catch let ioError {
52+
options.error("Cannot save new create workspace \(ioError)")
53+
}
54+
}
55+
}
56+
3157
guard let workspacePath: Path = options.filePath(extension: .xcworkspace) else {
3258
options.error("Cannot find workspace in \(rootPath)") // XXX maybe create an empty one
3359
return
@@ -72,7 +98,7 @@ extension Punic {
7298
if hasChange, let newData = workspaceDocument.xml.data(using: .utf8) {
7399
do {
74100
try workspaceDataFile.write(newData)
75-
options.log("💾 Workspace saved")
101+
options.log("💾 Workspace \(created ? "created": "saved")")
76102
} catch let ioError {
77103
options.error("Cannot save workspace \(ioError)")
78104
}

0 commit comments

Comments
 (0)