From 44bc07c9dcce3f06de3f904893471b78a3bd8fcb Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 20 Jan 2023 16:36:56 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20WIP:=20Parsing=20.valetrc=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Integrations/Valet/Sites/ValetSite.swift | 47 +++++++++++++++---- phpmon/Localizable.strings | 1 + 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift index ae0e3e6..d0742e7 100644 --- a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift +++ b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift @@ -64,6 +64,7 @@ class ValetSite: ValetListable { case require case platform case valetphprc + case valetrc } init( @@ -209,21 +210,49 @@ class ValetSite: ValetListable { Checks the contents of the .valetphprc file and determine the version, if possible. */ private func determineValetPhpFileInfo() { - let path = "\(absolutePath)/.valetphprc" + let files = [ + (".valetphprc", VersionSource.valetphprc), + (".valetrc", VersionSource.valetrc) + ] - do { - if FileSystem.fileExists(path) { - let contents = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8) - if let version = VersionExtractor.from(contents) { - self.composerPhp = version - self.composerPhpSource = .valetphprc + for (suffix, source) in files { + do { + let path = "\(absolutePath)/\(suffix)" + if FileSystem.fileExists(path) { + try self.handleValetFile(path, source) } + } catch { + Log.err("Something went wrong parsing the '\(suffix)' file") } - } catch { - Log.err("Something went wrong parsing the .valetphprc file") } } + /** + Parse a Valet file (either .valetphprc or .valetrc). + */ + private func handleValetFile(_ path: String, _ source: VersionSource) throws { + let contents = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8) + switch source { + case .valetphprc: + if let version = VersionExtractor.from(contents) { + self.composerPhp = version + self.composerPhpSource = source + } + case .valetrc: + self.parseValetRcFile(contents) + default: + return + } + } + + /** + Specifically extract PHP information from a .valetrc file. + */ + private func parseValetRcFile(_ text: String) { + // TODO: Implement this + fatalError("A .valetrc file was found, needs to be parsed!") + } + // MARK: - File Parsing public static func isolatedVersion(_ filePath: String) -> String? { diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index 67ccaa0..c15ad2b 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -385,6 +385,7 @@ problem manually, using your own Terminal app (this just shows you the output)." "alert.composer_php_requirement.type.require" = "This required PHP version was determined by checking the `require` field in the `composer.json` file when the site list was last refreshed."; "alert.composer_php_requirement.type.platform" = "This required PHP version was determined by checking the `platform` field in the `composer.json` file when the site list was last refreshed."; "alert.composer_php_requirement.type.valetphprc" = "This required PHP version was determined by checking the .valetphprc file in your project's directory."; +"alert.composer_php_requirement.type.valetrc" = "This required PHP version was determined by checking the .valetrc file in your project's directory."; "alert.unable_to_determine_is_fine" = "If you have a simple project, there may not be a specified PHP version set as a requirement. In that case, you are free to ignore this warning."; "alert.php_version_ideal" = "The currently active PHP version is ideal for this site."; "alert.php_version_incorrect" = "The currently active PHP version does not match the required constraint set for this site.";