From 04db3f50edf9adae5e80f19d548e6e50d4ccc1fc Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 3 Dec 2021 01:46:25 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20WIP:=20Detect=20linked=20and=20p?= =?UTF-8?q?arked=20sites=20(#58)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Domain/Integrations/Valet/Valet.swift | 70 ++++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/phpmon/Domain/Integrations/Valet/Valet.swift b/phpmon/Domain/Integrations/Valet/Valet.swift index 20b9cac..ade9a70 100644 --- a/phpmon/Domain/Integrations/Valet/Valet.swift +++ b/phpmon/Domain/Integrations/Valet/Valet.swift @@ -12,7 +12,8 @@ class Valet { var version: String var config: Valet.Configuration - var detectedSites: [String] + var parkedSites: [Site] = [] + var linkedSites: [Site] = [] init() { self.version = Actions.valet("--version") @@ -27,16 +28,77 @@ class Valet { from: try! String(contentsOf: file, encoding: .utf8).data(using: .utf8)! ) - /* print("PHP Monitor should scan the following paths:") print(self.config.paths) - */ - self.detectedSites = [] + resolvePaths() + } + + private func resolvePaths() { + self.linkedSites = [] + self.parkedSites = [] + + for path in self.config.paths { + let entries = try! FileManager.default.contentsOfDirectory(atPath: path) + for entry in entries { + self.resolvePath(entry, forPath: path) + } + } + } + + private func resolvePath(_ entry: String, forPath path: String) { + let siteDir = path + "/" + entry + + // See if the file is a symlink, if so, resolve it + let attrs = try! FileManager.default.attributesOfItem(atPath: siteDir) + + // We can also determine whether the thing at the path is a directory, too + let type = attrs[FileAttributeKey.type] as! FileAttributeType + + if type == FileAttributeType.typeSymbolicLink { + self.linkedSites.append(Site(aliasPath: siteDir)) + } else if type == FileAttributeType.typeDirectory { + self.parkedSites.append(Site(absolutePath: siteDir)) + } else { + print("The item at: `\(siteDir)` was neither a symlink nor a directory. Skipping.") + } } // MARK: - Structs + class Site { + var name: String + + var absolutePath: String + var aliasPath: String? + + init(absolutePath: String) { + self.absolutePath = absolutePath + self.aliasPath = nil + self.name = URL(string: absolutePath)!.lastPathComponent + self.detectSiteProperties() + } + + convenience init(aliasPath: String) { + // Resolve the symlink + let absolutePath = try! FileManager.default + .destinationOfSymbolicLink(atPath: aliasPath) + self.init(absolutePath: absolutePath) + + // TODO: Make sure the destination is a valid directory! + + // The name should be identical to the alias' name + self.name = URL(string: aliasPath)!.lastPathComponent + + // Update the alias' path + self.aliasPath = aliasPath + } + + private func detectSiteProperties() { + // TODO: Determine additional information, like Composer status and PHP version? + } + } + struct Configuration: Decodable { let tld: String let paths: [String]