Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ›‘ Prevent overwriting identical workspace data #607

Merged
merged 6 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Sources/XcodeProj/Project/XcodeProj.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ extension XcodeProj: Writable {
/// - Parameter override: if workspace should be overridden. Default is true.
/// If false will throw error if workspace already exists at the given path.
public func writeWorkspace(path: Path, override: Bool = true) throws {
guard let p = path.glob("*.xcworkspacedata").first, workspace.data != (try? XCWorkspaceData(path: p)) else { return }
try workspace.write(path: XcodeProj.workspacePath(path), override: override)
}

Expand Down
9 changes: 9 additions & 0 deletions Sources/XcodeProj/Workspace/XCWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public final class XCWorkspace: Writable, Equatable {
public func write(path: Path, override: Bool = true) throws {
let dataPath = path + "contents.xcworkspacedata"
if override, dataPath.exists {
if let existingContent: String = try? dataPath.read(),
existingContent == data.rawContents() {
// Raw data matches what's on disk
// there's no need to overwrite the contents
// this mitigates Xcode forcing users to
// close and re-open projects/workspaces
// on regneration.
return
}
try dataPath.delete()
}
try dataPath.mkpath()
Expand Down
14 changes: 9 additions & 5 deletions Sources/XcodeProj/Workspace/XCWorkspaceData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ extension XCWorkspaceData: Writable {

self.init(children: children)
}

// MARK: - <Writable>

public func write(path: Path, override: Bool = true) throws {

func rawContents() -> String {
let document = AEXMLDocument()
let workspace = document.addChild(name: "Workspace", value: nil, attributes: ["version": "1.0"])
_ = children
.map { $0.xmlElement() }
.map(workspace.addChild)
return document.xmlXcodeFormat
}

// MARK: - <Writable>

public func write(path: Path, override: Bool = true) throws {
let rawXml = rawContents()
if override, path.exists {
try path.delete()
}
try path.write(document.xmlXcodeFormat)
try path.write(rawXml)
}
}

Expand Down