mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-07 21:20:07 +01:00
👌 Cleanup writing helper files
This commit is contained in:
@@ -153,42 +153,7 @@ class PhpEnv {
|
||||
|
||||
private func writeHelpers(with versions: [String]) {
|
||||
for version in versions {
|
||||
do {
|
||||
let resolve = "\(Paths.optPath)/php@\(version)/bin"
|
||||
let path = URL(fileURLWithPath: resolve).resolvingSymlinksInPath().path
|
||||
let dotless = version.replacingOccurrences(of: ".", with: "")
|
||||
try! FileManager.default.createDirectory(
|
||||
at: URL(fileURLWithPath: "/Users/\(Paths.whoami)/.local/phpmon"),
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
let sourcePath = "/Users/\(Paths.whoami)/.local/phpmon/source_php\(dotless)"
|
||||
let source = """
|
||||
export PATH=\(path):$PATH
|
||||
"""
|
||||
try source.write(
|
||||
to: URL(fileURLWithPath: sourcePath),
|
||||
atomically: true,
|
||||
encoding: String.Encoding.utf8
|
||||
)
|
||||
let script = """
|
||||
#!/bin/zsh
|
||||
[[ $ZSH_EVAL_CONTEXT =~ :file$ ]] \\
|
||||
&& echo "PHP Monitor has enabled this terminal to use PHP \(version)." \\
|
||||
|| echo "You must run '. pm\(dotless)' (or 'source pm\(dotless)') instead!";
|
||||
source ~/.local/phpmon/source_php\(dotless);
|
||||
"""
|
||||
let alias = "/usr/local/bin/pm\(dotless)"
|
||||
try script.write(
|
||||
to: URL(fileURLWithPath: alias),
|
||||
atomically: true,
|
||||
encoding: String.Encoding.utf8
|
||||
)
|
||||
Shell.run("chmod +x \(sourcePath)")
|
||||
Shell.run("chmod +x \(path)")
|
||||
} catch {
|
||||
print(error)
|
||||
Log.err("Could not write PHP Monitor helper for PHP \(version) to /usr/local/bin")
|
||||
}
|
||||
PhpHelper.generate(for: version)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
60
phpmon/Common/PHP/PHP Version/PhpHelper.swift
Normal file
60
phpmon/Common/PHP/PHP Version/PhpHelper.swift
Normal file
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// PhpHelper.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 17/03/2022.
|
||||
// Copyright © 2022 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class PhpHelper {
|
||||
|
||||
static let keyPhrase = "This file was automatically generated by PHP Monitor."
|
||||
|
||||
public static func generate(for version: String) {
|
||||
// Take the PHP version (e.g. "7.2") and generate a dotless version
|
||||
let dotless = version.replacingOccurrences(of: ".", with: "")
|
||||
|
||||
do {
|
||||
let destination = "/usr/local/bin/pm\(dotless)"
|
||||
if FileManager.default.fileExists(atPath: destination) {
|
||||
let contents = try String(contentsOfFile: destination)
|
||||
if !contents.contains(keyPhrase) {
|
||||
Log.info("The file at '\(destination)' already exists and was not generated by PHP Monitor (or is unreadable). Not updating this file.")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Let's follow the symlink to the PHP binary folder
|
||||
let path = URL(fileURLWithPath: "\(Paths.optPath)/php@\(version)/bin")
|
||||
.resolvingSymlinksInPath().path
|
||||
|
||||
// The contents of the script!
|
||||
let script = """
|
||||
#!/bin/zsh
|
||||
# \(keyPhrase)
|
||||
# It reflects the location of PHP \(version)'s binaries on your system.
|
||||
# Usage: . pm\(dotless)
|
||||
[[ $ZSH_EVAL_CONTEXT =~ :file$ ]] \\
|
||||
&& echo "PHP Monitor has enabled this terminal to use PHP \(version)." \\
|
||||
|| echo "You must run '. pm\(dotless)' (or 'source pm\(dotless)') instead!";
|
||||
export PATH=\(path):$PATH
|
||||
"""
|
||||
|
||||
// Write to the destination
|
||||
try script.write(
|
||||
to: URL(fileURLWithPath: destination),
|
||||
atomically: true,
|
||||
encoding: String.Encoding.utf8
|
||||
)
|
||||
|
||||
// Make sure the file is executable
|
||||
Shell.run("chmod +x \(destination)")
|
||||
} catch {
|
||||
print(error)
|
||||
Log.err("Could not write PHP Monitor helper for PHP \(version) to /usr/local/bin/pm\(dotless)")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user