1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

Add default site to site list (#125)

This commit is contained in:
2022-03-20 14:38:58 +01:00
parent e6b2ddf2ad
commit 1d74e1536c
11 changed files with 97 additions and 32 deletions

Binary file not shown.

View File

@ -32,6 +32,7 @@ class ValetConfigParserTest: XCTestCase {
"/Users/username/.config/valet/Sites",
"/Users/username/Sites"
])
XCTAssertEqual(config.defaultSite, "/Users/username/default-site")
XCTAssertEqual(config.loopback, "127.0.0.1")
}

View File

@ -4,5 +4,6 @@
"/Users/username/.config/valet/Sites",
"/Users/username/Sites"
],
"loopback": "127.0.0.1"
"loopback": "127.0.0.1",
"default": "/Users/username/default-site"
}

View File

@ -0,0 +1,25 @@
{
"images" : [
{
"filename" : "Default.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Default@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"template-rendering-intent" : "template"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 861 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -13,6 +13,8 @@ protocol SiteScanner
func resolveSiteCount(paths: [String]) -> Int
func resolveSitesFrom(paths: [String]) -> [ValetSite]
func resolveSite(path: String) -> ValetSite?
}
class FakeSiteScanner: SiteScanner
@ -41,6 +43,10 @@ class FakeSiteScanner: SiteScanner
func resolveSitesFrom(paths: [String]) -> [ValetSite] {
return fakes
}
func resolveSite(path: String) -> ValetSite? {
return nil
}
}
class ValetSiteScanner: SiteScanner
@ -67,7 +73,7 @@ class ValetSiteScanner: SiteScanner
.contentsOfDirectory(atPath: path)
return entries.forEach {
if let site = self.getSite($0, forPath: path, tld: Valet.shared.config.tld) {
if let site = self.resolveSite(path: "\($0)/\(path))") {
sites.append(site)
}
}
@ -76,6 +82,35 @@ class ValetSiteScanner: SiteScanner
return sites
}
/**
Determines whether the site can be resolved as a symbolic link or as a directory.
Regular files are ignored, and the site is added to Valet's list of sites.
*/
func resolveSite(path: String) -> ValetSite? {
// Get the TLD from the global Valet object
let tld = Valet.shared.config.tld
// See if the file is a symlink, if so, resolve it
let attrs = try! FileManager.default.attributesOfItem(atPath: path)
// We can also determine whether the thing at the path is a directory, too
let type = attrs[FileAttributeKey.type] as! FileAttributeType
// We should also check that we can interpret the path correctly
if URL(fileURLWithPath: path).lastPathComponent == "" {
Log.warn("Could not parse the site: \(path), skipping!")
return nil
}
if type == FileAttributeType.typeSymbolicLink {
return ValetSite(aliasPath: path, tld: tld)
} else if type == FileAttributeType.typeDirectory {
return ValetSite(absolutePath: path, tld: tld)
}
return nil
}
/**
Determines whether the site can be resolved as a symbolic link or as a directory.
Regular files are ignored. Returns true if the path can be parsed.
@ -93,32 +128,4 @@ class ValetSiteScanner: SiteScanner
return false
}
/**
Determines whether the site can be resolved as a symbolic link or as a directory.
Regular files are ignored, and the site is added to Valet's list of sites.
*/
private func getSite(_ entry: String, forPath path: String, tld: String) -> ValetSite? {
let siteDir = path + "/" + entry
// See if the file is a symlink, if so, resolve it
let attrs = try! FileManager.default.attributesOfItem(atPath: siteDir)
// We can also determine whether the thing at the path is a directory, too
let type = attrs[FileAttributeKey.type] as! FileAttributeType
// We should also check that we can interpret the path correctly
if URL(fileURLWithPath: siteDir).lastPathComponent == "" {
Log.warn("Could not parse the site: \(siteDir), skipping!")
return nil
}
if type == FileAttributeType.typeSymbolicLink {
return ValetSite(aliasPath: siteDir, tld: tld)
} else if type == FileAttributeType.typeDirectory {
return ValetSite(absolutePath: siteDir, tld: tld)
}
return nil
}
}

View File

@ -98,6 +98,8 @@ class Valet {
We don't want to do duplicate or parallel work!
*/
public func reloadSites() {
loadConfiguration()
if (isBusy) {
return
}
@ -155,6 +157,11 @@ class Valet {
.resolveSitesFrom(paths: config.paths)
.sorted { $0.absolutePath < $1.absolutePath }
if let defaultPath = Valet.shared.config.defaultSite,
let site = ValetSiteScanner().resolveSite(path: defaultPath) {
sites.insert(site, at: 0)
}
isBusy = false
}

View File

@ -15,6 +15,8 @@ class SiteListKindCell: NSTableCellView, SiteListCellProtocol
func populateCell(with site: ValetSite) {
// If the `aliasPath` is nil, we're dealing with a parked site (otherwise: linked).
imageViewType.image = NSImage(
named: site.aliasPath == nil
@ -22,6 +24,11 @@ class SiteListKindCell: NSTableCellView, SiteListCellProtocol
: "IconLinked"
)
// Unless, of course, this is a default site
if site.absolutePath == Valet.shared.config.defaultSite {
imageViewType.image = NSImage(named: "IconDefault")
}
imageViewType.contentTintColor = NSColor.tertiaryLabelColor
}
}

View File

@ -78,8 +78,21 @@ extension SiteListVC {
}
@objc func isolateSite(sender: PhpMenuItem) {
self.performAction(command: "cd '\(selectedSite!.absolutePath)' && sudo \(Paths.valet) isolate php@\(sender.version) && exit;") {
let command = "cd '\(selectedSite!.absolutePath)' && sudo \(Paths.valet) isolate php@\(sender.version) && exit;"
self.performAction(command: command) {
self.selectedSite!.determineIsolated()
if self.selectedSite!.isolatedPhpVersion == nil {
BetterAlert()
.withInformation(
title: "site_list.alerts_isolation_failed.title".localized,
subtitle: "site_list.alerts_isolation_failed.subtitle".localized,
description: "site_list.alerts_isolation_failed.desc".localized(command)
)
.withPrimary(text: "OK")
.show()
}
}
}

View File

@ -67,7 +67,11 @@
"site_list.title" = "Domains";
"site_list.subtitle" = "Linked & Parked";
"site_list.alerts_status_not_changed.title" = "SSL Status Not Changed";
"site_list.alerts_isolation_failed.title" = "Oops! Site Not Isolated";
"site_list.alerts_isolation_failed.subtitle" = "Something went wrong trying to isolate this site. If this is your default site but it is not linked, I recommend manually linking the site prior to setting up isolation.";
"site_list.alerts_isolation_failed.desc" = "To find out what goes wrong, you can try running the command in your terminal manually: %@";
"site_list.alerts_status_not_changed.title" = "Oops! SSL Status Not Changed";
"site_list.alerts_status_not_changed.desc" = "Something went wrong. Try running the command in your terminal manually: %@";
"site_list.alerts_status_changed.title" = "SSL Status Changed";