From 422aefe831fc6bf30159c252995508b8b6cb7c3d Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 4 Jan 2022 02:33:53 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Read=20composer.json=20for=20versio?= =?UTF-8?q?n=20requirement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHP Monitor.xcodeproj/project.pbxproj | 12 +++++ phpmon/Domain/Core/Base.lproj/Main.storyboard | 30 ++++++++---- phpmon/Domain/Helpers/Alert.swift | 3 +- .../Integrations/Composer/ComposerJson.swift | 46 +++++++++++++++++++ phpmon/Domain/Integrations/Valet/Valet.swift | 22 +++++++++ phpmon/Domain/SiteList/SiteListCell.swift | 26 ++++++++++- phpmon/Domain/SiteList/SiteListVC.swift | 4 +- phpmon/Localizable.strings | 5 +- 8 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 phpmon/Domain/Integrations/Composer/ComposerJson.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 7cdf41a..4afdae5 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -129,6 +129,7 @@ C4CCBA6C275C567B008C7055 /* PMWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CCBA6B275C567B008C7055 /* PMWindowController.swift */; }; C4CCBA6D275C567B008C7055 /* PMWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CCBA6B275C567B008C7055 /* PMWindowController.swift */; }; C4D8016622B1584700C6DA1B /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D8016522B1584700C6DA1B /* Startup.swift */; }; + C4D89BC62783C99400A02B68 /* ComposerJson.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D89BC52783C99400A02B68 /* ComposerJson.swift */; }; C4D9ADBF277610E1007277F4 /* PhpSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D9ADBE277610E1007277F4 /* PhpSwitcher.swift */; }; C4D9ADC0277610E1007277F4 /* PhpSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D9ADBE277610E1007277F4 /* PhpSwitcher.swift */; }; C4D9ADC1277610E1007277F4 /* PhpSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D9ADBE277610E1007277F4 /* PhpSwitcher.swift */; }; @@ -268,6 +269,7 @@ C4C8E81A276F54E5003AC782 /* PhpConfigWatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhpConfigWatcher.swift; sourceTree = ""; }; C4CCBA6B275C567B008C7055 /* PMWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PMWindowController.swift; sourceTree = ""; }; C4D8016522B1584700C6DA1B /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = ""; }; + C4D89BC52783C99400A02B68 /* ComposerJson.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposerJson.swift; sourceTree = ""; }; C4D9ADBE277610E1007277F4 /* PhpSwitcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhpSwitcher.swift; sourceTree = ""; }; C4D9ADC7277611A0007277F4 /* InternalSwitcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternalSwitcher.swift; sourceTree = ""; }; C4E713562570150F00007428 /* SECURITY.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = SECURITY.md; sourceTree = ""; }; @@ -521,6 +523,7 @@ C4AF9F6B275445D300D44ED0 /* Integrations */ = { isa = PBXGroup; children = ( + C4D89BC42783C98800A02B68 /* Composer */, C4AF9F6C275445D900D44ED0 /* Homebrew */, C4AF9F6A275445C900D44ED0 /* Valet */, ); @@ -576,6 +579,14 @@ name = Frameworks; sourceTree = ""; }; + C4D89BC42783C98800A02B68 /* Composer */ = { + isa = PBXGroup; + children = ( + C4D89BC52783C99400A02B68 /* ComposerJson.swift */, + ); + path = Composer; + sourceTree = ""; + }; C4D9ADBD27761084007277F4 /* PHP */ = { isa = PBXGroup; children = ( @@ -852,6 +863,7 @@ C4B5635E276AB09000F12CCB /* VersionExtractor.swift in Sources */, C47331A2247093B7009A0597 /* StatusMenu.swift in Sources */, C4C3ED412783497000AB15D8 /* MainMenu+Startup.swift in Sources */, + C4D89BC62783C99400A02B68 /* ComposerJson.swift in Sources */, C46FA23F246C358E00944F05 /* StringExtension.swift in Sources */, C4B97B75275CF08C003F3378 /* AppDelegate+MenuOutlets.swift in Sources */, C464ADAC275A7A3F003FCD53 /* SiteListWC.swift in Sources */, diff --git a/phpmon/Domain/Core/Base.lproj/Main.storyboard b/phpmon/Domain/Core/Base.lproj/Main.storyboard index acc1270..446cd12 100644 --- a/phpmon/Domain/Core/Base.lproj/Main.storyboard +++ b/phpmon/Domain/Core/Base.lproj/Main.storyboard @@ -473,7 +473,7 @@ - + @@ -481,7 +481,7 @@ - + @@ -492,7 +492,7 @@ - + @@ -506,22 +506,33 @@ - + - + + - + @@ -534,18 +545,21 @@ - + + + + @@ -608,7 +622,7 @@ - + diff --git a/phpmon/Domain/Helpers/Alert.swift b/phpmon/Domain/Helpers/Alert.swift index d59c2f2..897e310 100644 --- a/phpmon/Domain/Helpers/Alert.swift +++ b/phpmon/Domain/Helpers/Alert.swift @@ -59,6 +59,5 @@ class Alert { secondButtonTitle: "", style: style ) - } - + } } diff --git a/phpmon/Domain/Integrations/Composer/ComposerJson.swift b/phpmon/Domain/Integrations/Composer/ComposerJson.swift new file mode 100644 index 0000000..860b484 --- /dev/null +++ b/phpmon/Domain/Integrations/Composer/ComposerJson.swift @@ -0,0 +1,46 @@ +// +// ComposerJson.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 04/01/2022. +// Copyright © 2022 Nico Verbruggen. All rights reserved. +// + +import Foundation + +struct ComposerJson: Decodable { + let dependencies: Dictionary? + let devDependencies: Dictionary? + let configuration: Config? + + public func getPhpVersion() -> (String, String) { + // Check if in platform + if configuration?.platform?.php != nil { + return (configuration!.platform!.php!, "platform") + } + + // Check if in dependencies + if dependencies?["php"] != nil { + return (dependencies!["php"]!, "dependency list") + } + + // Unknown! + return ("", "unknown") + } + + private enum CodingKeys: String, CodingKey { + case dependencies = "require" + case devDependencies = "require-dev" + case configuration = "config" + } + + struct Config: Decodable { + let platform: Platform? + } + + struct Platform: Decodable { + let php: String? + } +} + + diff --git a/phpmon/Domain/Integrations/Valet/Valet.swift b/phpmon/Domain/Integrations/Valet/Valet.swift index ce8ae55..fcb132e 100644 --- a/phpmon/Domain/Integrations/Valet/Valet.swift +++ b/phpmon/Domain/Integrations/Valet/Valet.swift @@ -160,6 +160,12 @@ class Valet { /// What driver is currently in use. If not detected, defaults to nil. var driver: String? = nil + /// The PHP version as discovered in composer.json + var composerPhp: String = "???" + + /// How the PHP version was determined + var composerPhpSource: String = "unknown" + init() {} convenience init(absolutePath: String, tld: String) { @@ -169,6 +175,7 @@ class Valet { self.aliasPath = nil determineSecured(tld) determineDriver() + determineComposerPhpVersion() } convenience init(aliasPath: String, tld: String) { @@ -178,6 +185,7 @@ class Valet { self.aliasPath = aliasPath determineSecured(tld) determineDriver() + determineComposerPhpVersion() } public func determineSecured(_ tld: String) { @@ -195,6 +203,20 @@ class Valet { self.driver = nil } } + + public func determineComposerPhpVersion() { + let path = "\(absolutePath!)/composer.json" + do { + if Filesystem.fileExists(path) { + (self.composerPhp, self.composerPhpSource) = try JSONDecoder().decode( + ComposerJson.self, + from: String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8).data(using: .utf8)! + ).getPhpVersion() + } + } catch { + Log.err("Something went wrong reading the composer JSON file.") + } + } } struct Configuration: Decodable { diff --git a/phpmon/Domain/SiteList/SiteListCell.swift b/phpmon/Domain/SiteList/SiteListCell.swift index 4af0450..ec640a5 100644 --- a/phpmon/Domain/SiteList/SiteListCell.swift +++ b/phpmon/Domain/SiteList/SiteListCell.swift @@ -11,6 +11,8 @@ import AppKit class SiteListCell: NSTableCellView { + var site: Valet.Site? = nil + @IBOutlet weak var labelSiteName: NSTextField! @IBOutlet weak var labelPathName: NSTextField! @@ -22,11 +24,15 @@ class SiteListCell: NSTableCellView @IBOutlet weak var buttonWarning: NSButton! @IBOutlet weak var labelWarning: NSTextField! + @IBOutlet weak var buttonPhpVersion: NSButton! + override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) } func populateCell(with site: Valet.Site) { + self.site = site + // Make sure to show the TLD labelSiteName.stringValue = "\(site.name!).\(Valet.shared.config.tld)" @@ -54,6 +60,24 @@ class SiteListCell: NSTableCellView : NSColor.init(red: 246/255, green: 71/255, blue: 71/255, alpha: 1.0) // red // Show the current driver - labelDriver.stringValue = site.driver ?? "???" + labelDriver.stringValue = "\(site.driver ?? "???")" + + // Show the PHP version + buttonPhpVersion.title = " PHP \(site.composerPhp) " + buttonPhpVersion.isHidden = (site.composerPhp == "???") + } + + @IBAction func pressedPhpVersion(_ sender: Any) { + guard let site = self.site else { return } + + Alert.confirm( + onWindow: App.shared.siteListWindowController!.window!, + messageText: "alert.composer_php_requirement.title" + .localized("\(site.name!).\(Valet.shared.config.tld)", site.composerPhp), + informativeText: "alert.composer_php_requirement.info" + .localized(site.composerPhpSource), + secondButtonTitle: "", + onFirstButtonPressed: {} + ) } } diff --git a/phpmon/Domain/SiteList/SiteListVC.swift b/phpmon/Domain/SiteList/SiteListVC.swift index 2caeaf5..d416fd7 100644 --- a/phpmon/Domain/SiteList/SiteListVC.swift +++ b/phpmon/Domain/SiteList/SiteListVC.swift @@ -179,8 +179,10 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { sites = Valet.shared.sites.filter({ site in return site.name.lowercased().contains(searchString) }) + DispatchQueue.main.async { + self.tableView.reloadData() + } - tableView.reloadData() } // MARK: - Deinitialization diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index d3bf975..7e6b5b3 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -147,10 +147,13 @@ You can find more information in the terminal output below. You’ll have to fix this problem manually, using your own Terminal app (this just shows you the output)."; - "alert.composer_success.title" = "Composer’s done updating!"; "alert.composer_success.info" = "Your global Composer dependencies have been successfully updated."; +// Composer Version +"alert.composer_php_requirement.title" = "`%@` has the following PHP requirement: \"php\": \"%@\"."; +"alert.composer_php_requirement.info" = "This required PHP version was determined by checking the %@ in `composer.json`.\n\nPlease note that the lockfile is not checked and this reflects the current Composer configuration file as it existed at the time of refreshing the site list."; + // Force Reload Started "alert.force_reload.title" = "PHP Monitor will force reload the latest version of PHP"; "alert.force_reload.info" = "This can take a while. You'll get another alert when the force reload has completed.";