From f596219e0d7389610aef3dac974db679cba7b198 Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Fri, 21 Feb 2020 15:36:27 +0000 Subject: [PATCH] got local scanner working --- Sources/ActionStatusCommon/RepoSet.swift | 46 ++++++++++++++++++- .../Resources/BuildNumber.xcconfig | 4 +- Sources/ActionStatusMobile/AppDelegate.swift | 41 +---------------- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/Sources/ActionStatusCommon/RepoSet.swift b/Sources/ActionStatusCommon/RepoSet.swift index 7accbcf..19fceab 100644 --- a/Sources/ActionStatusCommon/RepoSet.swift +++ b/Sources/ActionStatusCommon/RepoSet.swift @@ -132,19 +132,61 @@ class RepoSet: ObservableObject { DispatchQueue.main.async { print("Completed Refresh") - self.items = reloaded + var included: [Repo] = [] + let remainingIDs = Set(self.items.map({ $0.id.uuidString })) + let reloadedToUse = reloaded.filter({ remainingIDs.contains($0.id.uuidString)}) + self.items = reloadedToUse self.block?() self.scheduleRefresh(after: 10.0) } } } - func addRepo() -> Repo { + @discardableResult func addRepo() -> Repo { let repo = Repo() items.append(repo) return repo } + @discardableResult func addRepo(name: String, owner: String) -> Repo { + let repo = Repo(name, owner: owner, workflow: "Tests") + items.append(repo) + return repo + } + + func add(fromFolders urls: [URL]) { + if let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) { + let fm = FileManager.default + for url in urls { + if let enumerator = fm.enumerator(at: url, includingPropertiesForKeys: []) { + while let url = enumerator.nextObject() as? URL { + if url.lastPathComponent == ".git" { + add(fromGitRepo: url, detector: detector) + } + } + } + } + } + } + + func add(fromGitRepo url: URL, detector: NSDataDetector) { + if let config = try? String(contentsOf: url.appendingPathComponent("config")) { + let tweaked = config.replacingOccurrences(of: "git@github.com:", with: "https://github.com/") + let range = NSRange(location: 0, length: tweaked.count) + for result in detector.matches(in: tweaked, options: [], range: range) { + if let url = result.url, url.scheme == "https", url.host == "github.com" { + let name = url.deletingPathExtension().lastPathComponent + let owner = url.deletingLastPathComponent().lastPathComponent + let existing = items.filter({ $0.name == name && $0.owner == owner }) + if existing.count == 0 { + addRepo(name: name, owner: owner) + } + } + } + } + } + + func remove(repo: Repo) { if let index = items.firstIndex(of: repo) { var updated = items diff --git a/Sources/ActionStatusCommon/Resources/BuildNumber.xcconfig b/Sources/ActionStatusCommon/Resources/BuildNumber.xcconfig index ad1a627..952eb6e 100644 --- a/Sources/ActionStatusCommon/Resources/BuildNumber.xcconfig +++ b/Sources/ActionStatusCommon/Resources/BuildNumber.xcconfig @@ -1,2 +1,2 @@ -BUILD_NUMBER = 83 -BUILD_COMMIT = edca550422e479763147453346af24f05cc4b559 \ No newline at end of file +BUILD_NUMBER = 84 +BUILD_COMMIT = d62a93d35d4ce01d1a109e15e37cacb6f39744b4 \ No newline at end of file diff --git a/Sources/ActionStatusMobile/AppDelegate.swift b/Sources/ActionStatusMobile/AppDelegate.swift index 6614794..6862c91 100644 --- a/Sources/ActionStatusMobile/AppDelegate.swift +++ b/Sources/ActionStatusMobile/AppDelegate.swift @@ -64,49 +64,10 @@ class AppDelegate: AppCommon { @IBAction func addLocalRepos() { pickFilesToOpen(types: ["public.folder"]) { urls in - - - let fm = FileManager.default - for url in urls { - if - let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue), - let items = try? fm.contentsOfDirectory(at: url, includingPropertiesForKeys: [], options: []) { - for item in items { - if item.lastPathComponent == ".git" { - print(item.deletingLastPathComponent()) - if let config = try? String(contentsOf: item.appendingPathComponent("config")) { - let tweaked = config.replacingOccurrences(of: "git@github.com:", with: "https://github.com/") - let range = NSRange(location: 0, length: tweaked.count) - for result in detector.matches(in: tweaked, options: [], range: range) { - if let url = result.url, url.scheme == "https", url.host == "github.com" { - let repo = url.deletingPathExtension().lastPathComponent - let owner = url.deletingLastPathComponent().lastPathComponent - self.addRepo(name: repo, owner: owner) - } - } -// let lines = config.split(separator: "\n").filter({ $0.contains("github.com") }) -// for line in lines { -// if line.contains("git@github.com:") { -// let spec = line.split(separator: ":")[1].split(separator: "/") -// self.addRepo(name: String(spec[0]), owner: String(spec[1])) -// } else if line.contains("https://github.com/") { -// let spec = line.split(separator: "/") -// self.addRepo(name: String(spec[3]), owner: String(spec[4])) -// } -// } - } - } - } - } - } + self.repos.add(fromFolders: urls) } } - func addRepo(name: String, owner: String) { - print(name) - print(owner) - } - class CustomPicker: UIDocumentPickerViewController, UIDocumentPickerDelegate { typealias Completion = ([URL]) -> Void