1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-08 13:30:06 +01:00

♻️ Rework file checks & replacement into separate methods

This commit is contained in:
2021-01-29 20:32:12 +01:00
parent fbe791902c
commit 3a2cd3bff6

View File

@@ -70,13 +70,13 @@ class Actions {
{ {
availableVersions.forEach { (available) in availableVersions.forEach { (available) in
let formula = (available == App.shared.brewPhpVersion) ? "php" : "php@\(available)" let formula = (available == App.shared.brewPhpVersion) ? "php" : "php@\(available)"
Shell.user.run("\(Paths.brew()) unlink \(formula)") brew("unlink \(formula)")
Shell.user.run("sudo \(Paths.brew()) services stop \(formula)") brew("services stop \(formula)", sudo: true)
} }
let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)" let formula = (version == App.shared.brewPhpVersion) ? "php" : "php@\(version)"
Shell.user.run("\(Paths.brew()) link \(formula) --overwrite --force") brew("link \(formula) --overwrite --force")
Shell.user.run("sudo \(Paths.brew()) services start \(formula)") brew("services start \(formula)", sudo: true)
} }
// MARK: - Finding Config Files // MARK: - Finding Config Files
@@ -103,34 +103,35 @@ class Actions {
public static func didFindXdebug(_ version: String) -> Bool public static func didFindXdebug(_ version: String) -> Bool
{ {
let command = """ return grepContains(
grep -q 'zend_extension="xdebug.so"' \(Paths.etcPath())/php/\(version)/php.ini; [ $? -eq 0 ] && echo "YES" || echo "NO" file: "\(Paths.etcPath())/php/\(version)/php.ini",
""" query: "zend_extension=\"xdebug.so\""
let output = Shell.user.pipe(command).trimmingCharacters(in: .whitespacesAndNewlines) )
return (output == "YES")
} }
public static func didEnableXdebug(_ version: String) -> Bool public static func didEnableXdebug(_ version: String) -> Bool
{ {
let command = """ return !grepContains(
grep -q '; zend_extension="xdebug.so"' \(Paths.etcPath())/php/\(version)/php.ini; [ $? -eq 0 ] && echo "YES" || echo "NO" file: "\(Paths.etcPath())/php/\(version)/php.ini",
""" query: "; zend_extension=\"xdebug.so\""
let output = Shell.user.pipe(command).trimmingCharacters(in: .whitespacesAndNewlines) )
return (output == "NO")
} }
public static func toggleXdebug() public static func toggleXdebug()
{ {
let version = App.phpInstall!.version.short let version = App.phpInstall!.version.short
var command = """
sed -i '' 's/; zend_extension="xdebug.so"/zend_extension="xdebug.so"/g' \(Paths.etcPath())/php/\(version)/php.ini self.didEnableXdebug(version)
""" ? sed(
if (self.didEnableXdebug(version)) { file: "\(Paths.etcPath())/php/\(version)/php.ini",
command = """ original: "zend_extension=\"xdebug.so\"",
sed -i '' 's/zend_extension="xdebug.so"/; zend_extension="xdebug.so"/g' \(Paths.etcPath())/php/\(version)/php.ini replacement: "; zend_extension=\"xdebug.so\""
""" )
} : sed(
Shell.user.run(command) file: "\(Paths.etcPath())/php/\(version)/php.ini",
original: "; zend_extension=\"xdebug.so\"",
replacement: "zend_extension=\"xdebug.so\""
)
} }
// MARK: - Quick Fix // MARK: - Quick Fix
@@ -159,8 +160,35 @@ class Actions {
brew("services stop nginx", sudo: true) brew("services stop nginx", sudo: true)
} }
// MARK: Common Shell Commands
/**
Runs a `brew` command. Can run as superuser.
*/
private static func brew(_ command: String, sudo: Bool = false) private static func brew(_ command: String, sudo: Bool = false)
{ {
Shell.user.run("\(sudo ? "sudo " : "")" + "\(Paths.brew()) \(command)") Shell.user.run("\(sudo ? "sudo " : "")" + "\(Paths.brew()) \(command)")
} }
/**
Runs `sed` in order to replace all occurrences of a string in a specific file with another.
*/
private static func sed(file: String, original: String, replacement: String)
{
Shell.user.run("""
sed -i '' 's/\(original)/\(replacement)/g' \(file)
""")
}
/**
Uses `grep` to determine whether a particular query string can be found in a particular file.
*/
private static func grepContains(file: String, query: String) -> Bool
{
return Shell.user.pipe("""
grep -q '\(query)' \(file); [ $? -eq 0 ] && echo "YES" || echo "NO"
""")
.trimmingCharacters(in: .whitespacesAndNewlines)
.contains("YES")
}
} }