1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-04-05 18:50:08 +02:00

Added prototype binary to switch quickly

This commit is contained in:
2021-12-21 17:52:13 +01:00
parent e76c6e14e4
commit 63f4f8b078
5 changed files with 83 additions and 36 deletions

View File

@@ -10,6 +10,8 @@ import Foundation
enum AllowedArguments: String, CaseIterable { enum AllowedArguments: String, CaseIterable {
case use = "use" case use = "use"
case performSwitch = "switch"
case help = "help"
static func has(_ string: String) -> Bool { static func has(_ string: String) -> Bool {
return Self.allCases.contains { arg in return Self.allCases.contains { arg in

View File

@@ -8,29 +8,21 @@
import Foundation import Foundation
// First, let's read the initial command line argument let toolver = "1.0"
// REFACTOR REQUIRED let log = Log.shared
// Information about the Homebrew linked alias
// Information about the PHP versions
// etc.: needs to be stored in a separate object we can instantiate here and in PHP Monitor.
var logger = Log.shared
logger.verbosity = .warning
if CommandLine.arguments.count < 3 {
Log.err("You must enter at least two additional arguments.")
exit(1)
}
if CommandLine.arguments.contains("-v") || CommandLine.arguments.contains("--verbose") { if CommandLine.arguments.contains("-v") || CommandLine.arguments.contains("--verbose") {
logger.verbosity = .info Log.shared.verbosity = .info
} }
if CommandLine.arguments.contains("-p") || CommandLine.arguments.contains("--performance") { if CommandLine.arguments.contains("-p") || CommandLine.arguments.contains("--performance") {
logger.verbosity = .performance Log.shared.verbosity = .performance
} }
let argument = CommandLine.arguments[1] var argument = "help"
if CommandLine.arguments.count > 1 {
argument = CommandLine.arguments[1]
}
if !AllowedArguments.has(argument) { if !AllowedArguments.has(argument) {
Log.err("The supported arguments are: \(AllowedArguments.rawValues)") Log.err("The supported arguments are: \(AllowedArguments.rawValues)")
@@ -39,15 +31,66 @@ if !AllowedArguments.has(argument) {
let action = AllowedArguments.init(rawValue: argument) let action = AllowedArguments.init(rawValue: argument)
let switcher = PhpSwitcher.shared
PhpSwitcher.detectPhpVersions()
switch action { switch action {
case .use: case .use, .performSwitch:
let version = CommandLine.arguments[2] if !Shell.fileExists("\(Paths.binPath)/php") {
Log.err("PHP is currently not linked. Attempting to link `php` at least...")
_ = Shell.user.executeSynchronously("brew link php", requiresPath: true)
}
let switcher = PhpSwitcher.shared
PhpSwitcher.detectPhpVersions()
if CommandLine.arguments.count < 3 {
Log.err("You must enter at least two additional arguments when using this command.")
exit(1)
}
let version = CommandLine.arguments[2].replacingOccurrences(of: "php@", with: "")
if switcher.availablePhpVersions.contains(version) {
Log.info("Switching to PHP \(version)...") Log.info("Switching to PHP \(version)...")
break Actions.switchToPhpVersion(
version: version,
availableVersions: switcher.availablePhpVersions,
completed: {
Log.info("The switch has been completed.")
exit(0)
}
)
} else {
Log.err("A PHP installation with version \(version) is not installed.")
Log.err("The installed versions are: \(switcher.availablePhpVersions.joined(separator: ", ")).")
Log.err("If this version is available, you may be able to install it by using `brew install php@\(version)`.")
exit(1)
}
case .help:
print("""
===============================================================
PHP MONITOR CLI \(toolver)
by Nico Verbruggen
===============================================================
Gives access to the quick version switcher from PHP Monitor,
but without the GUI and 100% of the speed!
SUPPORTED COMMANDS
* use {version}: Switch to a specific version of PHP.
(e.g. `phpmon-cli use 8.0`)
* switch {version}: Alias for the `use` command.
* help: Show this help.
SUPPORTED FLAGS
* `-v / --verbose`: Enables verbose mode.
* `-p / --perf`: Enables performance mode.
""")
exit(0)
case .none: case .none:
Log.err("Action not recognized!") Log.err("Action not recognized!")
exit(1) exit(1)
} }
RunLoop.main.run()

View File

@@ -75,6 +75,8 @@ class Actions {
brew("unlink \(formula)") brew("unlink \(formula)")
brew("services stop \(formula)", sudo: true) brew("services stop \(formula)", sudo: true)
Log.perf("Unlinked and stopped services for \(formula)")
group.leave() group.leave()
} }
} }

View File

@@ -10,10 +10,12 @@ import Foundation
class Log { class Log {
static var shared = Log()
enum Verbosity: Int { enum Verbosity: Int {
case error = 1, case error = 1,
info = 2, warning = 2,
warning = 3, info = 3,
performance = 4 performance = 4
public func isApplicable() -> Bool { public func isApplicable() -> Bool {
@@ -21,15 +23,7 @@ class Log {
} }
} }
static var shared = Log() var verbosity: Verbosity = .warning
var verbosity: Verbosity = .info
static func info(_ item: Any) {
if Verbosity.info.isApplicable() {
print(item)
}
}
static func err(_ item: Any) { static func err(_ item: Any) {
if Verbosity.error.isApplicable() { if Verbosity.error.isApplicable() {
@@ -43,6 +37,12 @@ class Log {
} }
} }
static func info(_ item: Any) {
if Verbosity.info.isApplicable() {
print(item)
}
}
static func perf(_ item: Any) { static func perf(_ item: Any) {
if Verbosity.performance.isApplicable() { if Verbosity.performance.isApplicable() {
print(item) print(item)

View File

@@ -51,7 +51,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
*/ */
var switcher: PhpSwitcher! = nil var switcher: PhpSwitcher! = nil
var logger: Log = Log.shared var logger = Log.shared
// MARK: - Initializer // MARK: - Initializer
@@ -59,7 +59,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
When the application initializes, create all singletons. When the application initializes, create all singletons.
*/ */
override init() { override init() {
Log.shared.verbosity = .info logger.verbosity = .info
Log.info("==================================") Log.info("==================================")
Log.info("PHP MONITOR by Nico Verbruggen") Log.info("PHP MONITOR by Nico Verbruggen")
Log.info("Version \(App.version)") Log.info("Version \(App.version)")