Skip to content

Commit 292db91

Browse files
authored
Merge pull request #17 from tryboxx/SNLB-16/bug/uploadSnippets
[SNLB-16] Fix uploading snippets into Xcode directory
2 parents 75a936e + 0d4753c commit 292db91

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

SnippetsLibrary.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
B81B0876270232B600E59F86 /* NSNotification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSNotification+Name.swift"; sourceTree = "<group>"; };
123123
B81B0878270245CE00E59F86 /* AppMenu+HideWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppMenu+HideWindow.swift"; sourceTree = "<group>"; };
124124
B81B087A2702468C00E59F86 /* FileStatusCardType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileStatusCardType.swift; sourceTree = "<group>"; };
125+
B81B087C2702852600E59F86 /* SnippetsLibrary.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SnippetsLibrary.entitlements; sourceTree = "<group>"; };
126+
B81B087D2702852600E59F86 /* SnippetsLibraryDebug.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SnippetsLibraryDebug.entitlements; sourceTree = "<group>"; };
125127
B82561B026E81D570040A67E /* Snippets Library.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Snippets Library.app"; sourceTree = BUILT_PRODUCTS_DIR; };
126128
B82561B326E81D570040A67E /* SnippetsLibraryApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnippetsLibraryApp.swift; sourceTree = "<group>"; };
127129
B82561B726E81D580040A67E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -265,6 +267,8 @@
265267
B82561B226E81D570040A67E /* SnippetsLibrary */ = {
266268
isa = PBXGroup;
267269
children = (
270+
B81B087C2702852600E59F86 /* SnippetsLibrary.entitlements */,
271+
B81B087D2702852600E59F86 /* SnippetsLibraryDebug.entitlements */,
268272
B82561CF26E821660040A67E /* Application */,
269273
B8B6DE7F26EE545800E49C57 /* Dependencies */,
270274
B82561D726E822250040A67E /* Views */,

SnippetsLibrary/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
2525
<key>NSHumanReadableCopyright</key>
2626
<string>Apache 2.0 License. All trademarks are protected.</string>
27+
<key>NSSystemAdministrationUsageDescription</key>
28+
<string>I want to read all your files</string>
2729
</dict>
2830
</plist>

SnippetsLibrary/Modules/SnippetsUpload/SnippetsUploadViewModel.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Krzysztof Łowiec on 26/09/2021.
66
//
77

8-
import Foundation
8+
import AppKit
99
import Combine
1010

1111
final class SnippetsUploadViewModel: ObservableObject {
@@ -38,16 +38,18 @@ final class SnippetsUploadViewModel: ObservableObject {
3838
in: .userDomainMask
3939
)
4040

41-
guard let directoryURL = directoryURLs.first else {
41+
guard
42+
let containerDirectoryURL = directoryURLs.first,
43+
let libraryDirectory = containerDirectoryURL.absoluteString.components(separatedBy: "Containers/").first,
44+
let xcodeUserSnippetsDirectory = xcodeUserSnippetsDirectoryURL(with: libraryDirectory)
45+
else {
4246
uploadingStatus = .error
4347
return
4448
}
4549

46-
let xcodeUserSnippetsDirectory = directoryURL.appendingPathComponent("Developer/Xcode/UserData/CodeSnippets")
47-
4850
for (index, snippet) in snippets.enumerated() {
4951
uploadingStatus = .uploading
50-
52+
5153
DispatchQueue.main.async {
5254
self.progressValue = CGFloat(index + 1) / CGFloat(self.snippets.count)
5355
}
@@ -60,13 +62,32 @@ final class SnippetsUploadViewModel: ObservableObject {
6062
self.uploadingStatus = .error
6163
}
6264
}
63-
65+
6466
guard uploadingStatus != .error else { return }
65-
67+
6668
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
6769
self.progressValue = 1.0
6870
self.uploadingStatus = .done
6971
}
7072
}
7173

74+
private func xcodeUserSnippetsDirectoryURL(with libraryDirectory: String) -> URL? {
75+
let openPanel = NSOpenPanel()
76+
openPanel.message = "Confirm Xcode user snippets directory"
77+
openPanel.prompt = "Confirm"
78+
openPanel.allowedFileTypes = ["none"]
79+
openPanel.allowsOtherFileTypes = false
80+
openPanel.canChooseFiles = false
81+
openPanel.canChooseDirectories = true
82+
openPanel.directoryURL = URL(string: "\(libraryDirectory)Developer/Xcode/UserData/CodeSnippets")
83+
84+
let response = openPanel.runModal()
85+
if response != .OK {
86+
uploadingStatus = .error
87+
return nil
88+
} else {
89+
return openPanel.urls.first
90+
}
91+
}
92+
7293
}

SnippetsLibrary/SnippetsLibrary.entitlements

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7+
<key>com.apple.security.files.user-selected.read-write</key>
8+
<true/>
79
<key>com.apple.security.network.client</key>
810
<true/>
911
<key>com.apple.security.network.server</key>

SnippetsLibrary/SnippetsLibraryDebug.entitlements

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7+
<key>com.apple.security.files.user-selected.read-write</key>
8+
<true/>
79
<key>com.apple.security.network.client</key>
810
<true/>
911
<key>com.apple.security.network.server</key>

0 commit comments

Comments
 (0)