Sources/OrgWorkspace/Discovery.swift

c857dec17c1cf7a45952149fa2e48b69fad2ae25
orgstar/Sources/OrgWorkspace/Discovery.swift history · blame · raw

138 lines · 6013 bytes

  1import Foundation
  2import OrgIndex
  3
  4/// A folder the user added. Identified by its bookmark, so a moved folder is followed.
  5public struct WorkspaceRoot: Sendable, Equatable, Codable {
  6    public let bookmark: Data
  7    /// The path when the bookmark was made or last refreshed, for display.
  8    public var path: String
  9
 10    public init(url: URL) throws {
 11        bookmark = try url.bookmarkData(options: [], includingResourceValuesForKeys: nil, relativeTo: nil)
 12        path = url.standardizedFileURL.path
 13    }
 14
 15    /// The folder's current location. `isStale` means the bookmark should be made again.
 16    public func resolve() throws -> (url: URL, isStale: Bool) {
 17        var isStale = false
 18        let url = try URL(resolvingBookmarkData: bookmark, options: [], relativeTo: nil, bookmarkDataIsStale: &isStale)
 19        _ = url.startAccessingSecurityScopedResource()
 20        return (url.standardizedFileURL, isStale)
 21    }
 22}
 23
 24public struct DiscoveredFile: Sendable, Equatable {
 25    public let url: URL
 26    public let kind: FileKind
 27    public let size: Int
 28    public let mtime: Double
 29}
 30
 31public struct Discovery: Sendable, Equatable {
 32    public var files: [DiscoveredFile] = []
 33    /// iCloud files not downloaded yet, by the name they will have.
 34    public var placeholders: [URL] = []
 35}
 36
 37public enum FileClassification: Sendable, Equatable {
 38    case file(FileKind)
 39    /// An iCloud placeholder (`.name.org.icloud`) for the file at the given name.
 40    case placeholder(String)
 41    case ignored
 42}
 43
 44/// Which files and folders a scan takes in.
 45public struct ScanRules: Sendable, Equatable {
 46    /// Dotfiles and dot folders, other than ignored folders.
 47    public var hiddenFiles: Bool
 48    /// Folder names never looked into: version control, caches, build output.
 49    public var ignoredFolders: Set<String>
 50
 51    public static let defaultIgnoredFolders: Set<String> = [
 52        ".git", ".hg", ".svn", ".jj", ".bzr", ".stfolder", ".stversions", ".Trash", ".Spotlight-V100", ".fseventsd",
 53        ".build", ".venv", ".cache", ".tox", ".mypy_cache", ".pytest_cache", ".gradle", ".next", ".terraform", "node_modules",
 54    ]
 55
 56    public init(hiddenFiles: Bool = true, ignoredFolders: Set<String> = defaultIgnoredFolders) {
 57        self.hiddenFiles = hiddenFiles
 58        self.ignoredFolders = ignoredFolders
 59    }
 60
 61    public func skipsFolder(_ name: String) -> Bool {
 62        ignoredFolders.contains(name) || (!hiddenFiles && name.hasPrefix("."))
 63    }
 64}
 65
 66public enum WorkspaceScanner {
 67    /// What a file name means to the workspace. Syncthing temp files, our own temp and backup
 68    /// files, Emacs backups, auto-saves and lock files, and Finder's files are ignored, and
 69    /// other dotfiles unless `rules` takes them in; files that aren't org files are `.other`.
 70    public static func classify(_ name: String, rules: ScanRules = ScanRules()) -> FileClassification {
 71        if name.hasPrefix(".") {
 72            if name.hasSuffix(".icloud") {
 73                let inner = String(name.dropFirst().dropLast(".icloud".count))
 74                if case .file = classify(inner, rules: rules) { return .placeholder(inner) }
 75                return .ignored
 76            }
 77            if !rules.hiddenFiles || name.hasPrefix(".#") || name.hasPrefix(".syncthing.") || name.contains(".orgstar-")
 78                || name == ".DS_Store" || name == ".localized" {
 79                return .ignored
 80            }
 81        }
 82        if name.hasSuffix("~") || name.hasPrefix("#") { return .ignored }
 83        let conflict = name.contains(".sync-conflict-")
 84        if name.hasSuffix(".org") { return .file(conflict ? .conflict : .org) }
 85        if name.hasSuffix(".org_archive") { return .file(conflict ? .conflict : .archive) }
 86        return .file(.other)
 87    }
 88
 89    /// Every file under `root`, skipping the folders `rules` ignores.
 90    public static func scan(_ root: URL, rules: ScanRules = ScanRules()) throws -> Discovery {
 91        let keys: [URLResourceKey] = [.isDirectoryKey, .fileSizeKey, .contentModificationDateKey]
 92        guard let enumerator = FileManager.default.enumerator(at: root, includingPropertiesForKeys: keys) else {
 93            return Discovery()
 94        }
 95        var discovery = Discovery()
 96        for case let url as URL in enumerator {
 97            let values = try url.resourceValues(forKeys: Set(keys))
 98            let name = url.lastPathComponent
 99            if values.isDirectory == true {
100                if rules.skipsFolder(name) { enumerator.skipDescendants() }
101                continue
102            }
103            switch classify(name, rules: rules) {
104            case .file(let kind):
105                discovery.files.append(DiscoveredFile(
106                    url: url.standardizedFileURL,
107                    kind: kind,
108                    size: values.fileSize ?? 0,
109                    mtime: values.contentModificationDate?.timeIntervalSince1970 ?? 0
110                ))
111            case .placeholder(let inner):
112                discovery.placeholders.append(url.deletingLastPathComponent().appendingPathComponent(inner).standardizedFileURL)
113            case .ignored:
114                break
115            }
116        }
117        discovery.files.sort { $0.url.path < $1.url.path }
118        return discovery
119    }
120}
121
122/// Which files feed the agenda: `.org` files (not archives or conflict copies) matching one of
123/// the globs, relative to their root. No globs means every `.org` file.
124public struct AgendaScope: Sendable, Equatable {
125    public var globs: [String]
126
127    public init(globs: [String] = []) {
128        self.globs = globs
129    }
130
131    public func contains(relativePath: String, kind: FileKind) -> Bool {
132        // `org-agenda-file-regexp` leaves out dotfiles.
133        guard kind == .org, !(relativePath as NSString).lastPathComponent.hasPrefix(".") else { return false }
134        guard !globs.isEmpty else { return true }
135        // Without FNM_PATHNAME, `*` also matches `/`, so `work/*` covers nested folders.
136        return globs.contains { fnmatch($0, relativePath, 0) == 0 }
137    }
138}