+ Package size ~ Ventura-y package information

This commit is contained in:
buresdv 2023-02-09 22:42:44 +01:00
parent afc3e0cff1
commit 1c4c62981f
7 changed files with 176 additions and 36 deletions

View File

@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
632FD290299591C10029FC49 /* Get Folder Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = 632FD28F299591C10029FC49 /* Get Folder Size.swift */; };
632FD292299596790029FC49 /* HelpButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 632FD291299596790029FC49 /* HelpButton.swift */; };
634065652871AA42001A2178 /* CorkApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 634065642871AA42001A2178 /* CorkApp.swift */; };
634065672871AA42001A2178 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 634065662871AA42001A2178 /* ContentView.swift */; };
634065692871AA42001A2178 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 634065682871AA42001A2178 /* Assets.xcassets */; };
@ -44,6 +46,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
632FD28F299591C10029FC49 /* Get Folder Size.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Get Folder Size.swift"; sourceTree = "<group>"; };
632FD291299596790029FC49 /* HelpButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpButton.swift; sourceTree = "<group>"; };
634065612871AA42001A2178 /* Cork.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Cork.app; sourceTree = BUILT_PRODUCTS_DIR; };
634065642871AA42001A2178 /* CorkApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CorkApp.swift; sourceTree = "<group>"; };
634065662871AA42001A2178 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@ -150,6 +154,7 @@
637E00632872F3CE005C9890 /* Upgrade Packages.swift */,
637E006728733C1C005C9890 /* Search for Package by ID.swift */,
63D8C954287A15C300483A52 /* Load up Installed Packages.swift */,
632FD28F299591C10029FC49 /* Get Folder Size.swift */,
);
path = Logic;
sourceTree = "<group>";
@ -234,6 +239,7 @@
6371EA1C2871D96800300E1B /* Packages */,
63D8C956287A1D0800483A52 /* Loading View.swift */,
63A874692990127D009F9533 /* PillText.swift */,
632FD291299596790029FC49 /* HelpButton.swift */,
);
path = Views;
sourceTree = "<group>";
@ -359,11 +365,13 @@
6340657F2871AF68001A2178 /* App Constants.swift in Sources */,
634065672871AA42001A2178 /* ContentView.swift in Sources */,
63A8746A2990127D009F9533 /* PillText.swift in Sources */,
632FD292299596790029FC49 /* HelpButton.swift in Sources */,
63A8746E29904A7F009F9533 /* App State.swift in Sources */,
63A87468298FE811009F9533 /* Package Types.swift in Sources */,
6340657D2871AC65001A2178 /* Brew Package.swift in Sources */,
637E00642872F3CE005C9890 /* Upgrade Packages.swift in Sources */,
6371EA2D287202F400300E1B /* Extract from JSON.swift in Sources */,
632FD290299591C10029FC49 /* Get Folder Size.swift in Sources */,
6371EA1E2871D98100300E1B /* Package List Item.swift in Sources */,
637E006B28734041005C9890 /* Install Selected Packages.swift in Sources */,
634EB3972871C1F00009DFC2 /* Get Package Version.swift in Sources */,

View File

@ -44,11 +44,13 @@ func getContentsOfFolder(targetFolder: URL) async -> [BrewPackage]
/// What the fuck?
let installedOn: Date? = (try? FileManager.default.attributesOfItem(atPath: targetFolder.appendingPathComponent(item, conformingTo: .folder).path))?[.creationDate] as? Date
print("Installation date for package \(item) is \(installedOn)")
let folderSizeRaw: Int64? = directorySize(url: targetFolder.appendingPathComponent(item, conformingTo: .directory))
print("\n Installation date for package \(item) at path \(targetFolder.appendingPathComponent(item, conformingTo: .directory)) is \(installedOn) \n")
//let installedOn: Date? = try? URL(string: item)!.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate
contentsOfFolder.append(BrewPackage(name: item, installedOn: installedOn, versions: temporaryVersionStorage))
contentsOfFolder.append(BrewPackage(name: item, installedOn: installedOn, versions: temporaryVersionStorage, sizeInBytes: folderSizeRaw))
temporaryVersionStorage = [String]()
}

View File

@ -0,0 +1,57 @@
//
// Get Folder Size.swift
// Cork
//
// Created by David Bureš on 09.02.2023.
//
import Foundation
func directorySize(url: URL) -> Int64
{
let contents: [URL]
do
{
contents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: [.fileSizeKey, .isDirectoryKey])
}
catch
{
return 0
}
var size: Int64 = 0
for url in contents
{
let isDirectoryResourceValue: URLResourceValues
do
{
isDirectoryResourceValue = try url.resourceValues(forKeys: [.isDirectoryKey])
}
catch
{
continue
}
if isDirectoryResourceValue.isDirectory == true
{
size += directorySize(url: url)
}
else
{
let fileSizeResourceValue: URLResourceValues
do
{
fileSizeResourceValue = try url.resourceValues(forKeys: [.fileSizeKey])
}
catch
{
continue
}
size += Int64(fileSizeResourceValue.fileSize ?? 0)
}
}
return size
}

View File

@ -14,6 +14,8 @@ struct BrewPackage: Identifiable
let installedOn: Date?
let versions: [String]
let sizeInBytes: Int64?
func convertDateToPresentableFormat(date: Date) -> String
{
let dateFormatter = DateFormatter()
@ -21,4 +23,10 @@ struct BrewPackage: Identifiable
return dateFormatter.string(from: date)
}
func convertSizeToPresentableFormat(size: Int64) -> String
{
let byteFormatter = ByteCountFormatter()
return byteFormatter.string(fromByteCount: size)
}
}

View File

@ -0,0 +1,26 @@
//
// HelpButton.swift
// Cork
//
// Created by David Bureš on 09.02.2023.
//
import SwiftUI
struct HelpButton: View {
var action : () -> Void
var body: some View {
Button(action: action, label: {
ZStack {
Circle()
.strokeBorder(Color(NSColor.controlShadowColor), lineWidth: 0.5)
.background(Circle().foregroundColor(Color(NSColor.controlColor)))
.shadow(color: Color(NSColor.controlShadowColor).opacity(0.3), radius: 1)
.frame(width: 20, height: 20)
Text("?").font(.system(size: 15, weight: .medium ))
}
})
.buttonStyle(PlainButtonStyle())
}
}

View File

@ -68,7 +68,7 @@ struct PackageDetailWindow: View
// print(packageInfo.contents)
assembledPackage = BrewPackage(name: package, installedOn: nil, versions: ["\(extractPackageInfo(rawJSON: packageInfo.contents!, whatToExtract: .version))"])
assembledPackage = BrewPackage(name: package, installedOn: nil, versions: ["\(extractPackageInfo(rawJSON: packageInfo.contents!, whatToExtract: .version))"], sizeInBytes: nil)
}
}
else
@ -82,7 +82,7 @@ struct PackageDetailWindow: View
// print(packageInfo.contents)
assembledPackage = BrewPackage(name: package, installedOn: nil, versions: ["\(extractPackageInfo(rawJSON: packageInfo.contents!, whatToExtract: .version))"])
assembledPackage = BrewPackage(name: package, installedOn: nil, versions: ["\(extractPackageInfo(rawJSON: packageInfo.contents!, whatToExtract: .version))"], sizeInBytes: nil)
}
}
}

View File

@ -24,18 +24,28 @@ struct PackageDetailView: View
@State private var description: String = ""
@State private var homepage: String = ""
@State var isShowingPopover: Bool = false
var body: some View
{
VStack(alignment: .leading, spacing: 10)
VStack(alignment: .leading, spacing: 15)
{
VStack(alignment: .leading)
VStack(alignment: .leading, spacing: 5)
{
Text(package.name)
.font(.title)
Text(returnFormattedVersions(package.versions))
.font(.subheadline)
.foregroundColor(.gray)
HStack(alignment: .firstTextBaseline, spacing: 5) {
Text(package.name)
.font(.title)
Text("v. \(returnFormattedVersions(package.versions))")
.font(.subheadline)
.foregroundColor(.gray)
}
if packageInfo.contents != nil
{
Text(description)
.font(.subheadline)
}
}
if packageInfo.contents == nil
@ -44,39 +54,68 @@ struct PackageDetailView: View
}
else
{
GroupBox
VStack(alignment: .leading, spacing: 10)
{
Grid(alignment: .leading)
Text("Package Info")
.font(.title2)
GroupBox
{
GridRow(alignment: .top)
Grid(alignment: .leading)
{
Text("Description")
Text(description)
}
Divider()
GridRow(alignment: .top)
{
Text("Homepage")
Text(.init(homepage))
}
if let installedOnDate = package.installedOn // Only show the "Installed on" date for packages that are actually installed
{
Divider()
GridRow(alignment: .top)
{
Text("Installed On")
Text(package.convertDateToPresentableFormat(date: installedOnDate))
Text("Homepage")
Text(.init(homepage))
}
}
}
if let installedOnDate = package.installedOn // Only show the "Installed on" date for packages that are actually installed
{
GroupBox {
Grid(alignment: .leading)
{
GridRow(alignment: .top)
{
Text("Installed On")
Text(package.convertDateToPresentableFormat(date: installedOnDate))
}
if let packageSize = package.sizeInBytes
{
Divider()
GridRow(alignment: .top)
{
Text("Size")
HStack
{
Text(package.convertSizeToPresentableFormat(size: packageSize))
if isCask {
HelpButton {
isShowingPopover.toggle()
}
.help("Why is the size so small?")
.popover(isPresented: $isShowingPopover) {
VStack(spacing: 10)
{
Text("Casks are not installed into the same installation directory as Formulae. Casks are installed into the Applications directory.")
Text("Since Cork does not have access to your Applications directory, it cannot get the size of the actual app, only of the metadata associated with the Cask.")
}
.padding()
.frame(width: 300, alignment: .center)
}
}
}
}
}
}
}
}
} label: {
Text("Package Info")
.font(.headline)
}
}