diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 7cc6615..f76893b 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -31,6 +31,8 @@ C476FF9822B0DD830098105B /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C476FF9722B0DD830098105B /* Alert.swift */; }; C4811D2422D70A4700B5F6B3 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4811D2322D70A4700B5F6B3 /* App.swift */; }; C4811D2A22D70F9A00B5F6B3 /* MainMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4811D2922D70F9A00B5F6B3 /* MainMenu.swift */; }; + C481F79726164A78004FBCFF /* PrefsVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5420395826135DC100FB00FA /* PrefsVC.swift */; }; + C481F79A26164A7C004FBCFF /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5420395E2613607600FB00FA /* Preferences.swift */; }; C48D0C9025CC7FD000CC7490 /* StatsView.xib in Resources */ = {isa = PBXBuildFile; fileRef = C48D0C8F25CC7FD000CC7490 /* StatsView.xib */; }; C48D0C9325CC804200CC7490 /* XibLoadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C48D0C9225CC804200CC7490 /* XibLoadable.swift */; }; C48D0C9625CC80B100CC7490 /* HeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C48D0C9525CC80B100CC7490 /* HeaderView.swift */; }; @@ -65,6 +67,7 @@ C4F780CD25D80B75000DBC97 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = C476FF9722B0DD830098105B /* Alert.swift */; }; C4F780CE25D80B75000DBC97 /* LocalNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = C474B00524C0E98C00066A22 /* LocalNotification.swift */; }; C4F8C0A422D4F12C002EFE61 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8C0A322D4F12C002EFE61 /* DateExtension.swift */; }; + C4FBFC532616485F00CDB8E1 /* PhpVersionDetectionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4FBFC512616485F00CDB8E1 /* PhpVersionDetectionTest.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -124,6 +127,7 @@ C4F780AD25D80B37000DBC97 /* ExtensionParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionParserTest.swift; sourceTree = ""; }; C4F8C0A322D4F12C002EFE61 /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = ""; }; C4F8C0A522D4FA41002EFE61 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + C4FBFC512616485F00CDB8E1 /* PhpVersionDetectionTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhpVersionDetectionTest.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -257,6 +261,7 @@ C4F7809B25D80344000DBC97 /* CommandTest.swift */, C4F780AD25D80B37000DBC97 /* ExtensionParserTest.swift */, C43A8A2325D9D20D00591B77 /* BrewJsonParserTest.swift */, + C4FBFC512616485F00CDB8E1 /* PhpVersionDetectionTest.swift */, C43A8A1925D9CD1000591B77 /* Utility.swift */, ); path = "phpmon-tests"; @@ -427,12 +432,14 @@ C4F780CC25D80B75000DBC97 /* PhpInstallation.swift in Sources */, C4F780B125D80B4D000DBC97 /* PhpExtension.swift in Sources */, C4F780CE25D80B75000DBC97 /* LocalNotification.swift in Sources */, + C4FBFC532616485F00CDB8E1 /* PhpVersionDetectionTest.swift in Sources */, C43A8A2425D9D20D00591B77 /* BrewJsonParserTest.swift in Sources */, C4F780CA25D80B75000DBC97 /* HomebrewPackage.swift in Sources */, C4F780C025D80B6E000DBC97 /* Startup.swift in Sources */, C4F780AE25D80B37000DBC97 /* ExtensionParserTest.swift in Sources */, C4F780C725D80B75000DBC97 /* StatusMenu.swift in Sources */, C4F780CD25D80B75000DBC97 /* Alert.swift in Sources */, + C481F79726164A78004FBCFF /* PrefsVC.swift in Sources */, C4F7809C25D80344000DBC97 /* CommandTest.swift in Sources */, C4F780BA25D80B62000DBC97 /* AppDelegate.swift in Sources */, C4F780A225D804AA000DBC97 /* Paths.swift in Sources */, @@ -442,6 +449,7 @@ C4F780C525D80B75000DBC97 /* MenuBarImageGenerator.swift in Sources */, C4F780B725D80B5D000DBC97 /* App.swift in Sources */, C4F780C925D80B75000DBC97 /* StringExtension.swift in Sources */, + C481F79A26164A7C004FBCFF /* Preferences.swift in Sources */, C4F780CB25D80B75000DBC97 /* StatsView.swift in Sources */, C43A8A1A25D9CD1000591B77 /* Utility.swift in Sources */, C4F780C625D80B75000DBC97 /* XibLoadable.swift in Sources */, diff --git a/phpmon-tests/PhpVersionDetectionTest.swift b/phpmon-tests/PhpVersionDetectionTest.swift new file mode 100644 index 0000000..ea2ec36 --- /dev/null +++ b/phpmon-tests/PhpVersionDetectionTest.swift @@ -0,0 +1,28 @@ +// +// PhpVersionDetectionTest.swift +// phpmon-tests +// +// Created by Nico Verbruggen on 14/02/2021. +// Copyright © 2021 Nico Verbruggen. All rights reserved. +// + +import XCTest + +class PhpVersionDetectionTest: XCTestCase { + + func testCanExtractData() throws { + let outcome = Actions.extractPhpVersions(from: [ + "", // empty lines should be omitted + "php@8.0", + "php@8.0", // should only be detected once + "meta-php@8.0", // should be omitted, invalid + "php@8.0-coolio", // should be omitted, invalid + "php@7.0", + "php@5.6", + "php@5.4" // should be omitted, not supported + ], checkBinaries: false) + + XCTAssertEqual(outcome, ["8.0", "7.0", "5.6"]) + } + +} diff --git a/phpmon/Domain/Terminal/Actions.swift b/phpmon/Domain/Terminal/Actions.swift index 2ba7be3..c917478 100644 --- a/phpmon/Domain/Terminal/Actions.swift +++ b/phpmon/Domain/Terminal/Actions.swift @@ -15,30 +15,7 @@ class Actions { public static func detectPhpVersions() -> [String] { let files = Shell.pipe("ls \(Paths.optPath) | grep php@") - var versions = files.components(separatedBy: "\n") - - // Remove all empty strings - versions.removeAll { (string) -> Bool in - return (string == "") - } - - // Get a list of versions only - var versionsOnly : [String] = [] - versions.filter { (version) -> Bool in - // Omit everything that doesn't start with php@ - // (e.g. something-php@8.0 won't be detected) - return version.starts(with: "php@") - }.forEach { (string) in - let version = string.components(separatedBy: "php@")[1] - // Only append the version if it doesn't already exist (avoid dupes), - // is supported and where the binary exists (avoids broken installs) - if !versionsOnly.contains(version) - && Constants.SupportedPhpVersions.contains(version) - && Shell.fileExists("\(Paths.optPath)/php@\(version)/bin/php") - { - versionsOnly.append(version) - } - } + var versionsOnly = Self.extractPhpVersions(from: files.components(separatedBy: "\n")) // Make sure the aliased version is detected // The user may have `php` installed, but not e.g. `php@8.0` @@ -55,6 +32,35 @@ class Actions { return versionsOnly } + /** + Extracts valid PHP versions from an array of strings. + This array of strings is usually retrieved from `grep`. + */ + public static func extractPhpVersions( + from versions: [String], + checkBinaries: Bool = true + ) -> [String] { + var output : [String] = [] + + versions.filter { (version) -> Bool in + // Omit everything that doesn't start with php@ + // (e.g. something-php@8.0 won't be detected) + return version.starts(with: "php@") + }.forEach { (string) in + let version = string.components(separatedBy: "php@")[1] + // Only append the version if it doesn't already exist (avoid dupes), + // is supported and where the binary exists (avoids broken installs) + if !output.contains(version) + && Constants.SupportedPhpVersions.contains(version) + && (checkBinaries ? Shell.fileExists("\(Paths.optPath)/php@\(version)/bin/php") : true) + { + output.append(version) + } + } + + return output + } + // MARK: - Services public static func restartPhpFpm()