Skip to content

Commit 9971f9f

Browse files
authored
fix: Modify or create .gitignore files (#9)
1 parent 03b22fa commit 9971f9f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Sources/cap2spm.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct Cap2SPM: ParsableCommand {
5353
try modifySwiftFile(at: swiftFileURL, plugin: capPlugin)
5454
try generatePackageSwiftFile(at: podspecFileURL, plugin: capPlugin)
5555
try modifyPodspec(at: podspecFileURL)
56+
try modifyGitignores(with: capacitorPluginPackage)
5657

5758
var fileList = [mFileURL, hFileURL]
5859
if shouldBackup {
@@ -116,6 +117,31 @@ struct Cap2SPM: ParsableCommand {
116117
try podspecText.write(to: fileURL, atomically: true, encoding: .utf8)
117118
}
118119

120+
private func modifyGitignores(with package: CapacitorPluginPackage) throws {
121+
let baseEntries = ["/Packages", "xcuserdata/" ,"DerivedData/", ".swiftpm/configuration/registries.json", ".swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata", ".netrc"]
122+
var rootEntries = ["Pods", "Podfile.lock", "Package.resolved", "Build", "xcuserdata", "/.build"]
123+
rootEntries.append(contentsOf: baseEntries)
124+
var iosEntries = [".DS_Store", ".build"]
125+
iosEntries.append(contentsOf: baseEntries)
126+
let rootGitignore = package.basePathURL.appending(path: ".gitignore")
127+
let iOSGitignore = package.iosSrcDirectoryURL.appending(path: ".gitignore")
128+
try modifyGitignore(at: rootGitignore, with: rootEntries)
129+
try modifyGitignore(at: iOSGitignore, with: iosEntries)
130+
}
131+
132+
private func modifyGitignore(at fileURL: URL, with content: [String]) throws {
133+
var gitignoreText = ""
134+
if FileManager.default.fileExists(atPath: fileURL.path()) {
135+
gitignoreText = try String(contentsOf: fileURL, encoding: .utf8)
136+
}
137+
content.forEach {
138+
if !gitignoreText.contains($0) {
139+
gitignoreText.append("\n\($0)")
140+
}
141+
}
142+
try gitignoreText.write(to: fileURL, atomically: true, encoding: .utf8)
143+
}
144+
119145
private func moveItemCreatingIntermediaryDirectories(at: URL, to: URL) throws {
120146
print("Moving \(at.path()) to \(to.path())...")
121147
let parentPath = to.deletingLastPathComponent()

0 commit comments

Comments
 (0)