From 91ec7dd89e72f517500b9d22110277d53ddbe711 Mon Sep 17 00:00:00 2001 From: eonist <30n1st@gmail.com> Date: Sat, 23 Sep 2023 04:44:26 +0200 Subject: [PATCH] Update comments with LLM --- FileSugarExampleMacOS/AppDelegate.swift | 10 +-- FileSugarExampleMacOS/View.swift | 2 + LICENSE | 17 ++--- Package.swift | 27 +++---- README.md | 7 ++ Sources/FileSugar/FileAsserter.swift | 10 ++- Sources/FileSugar/FileModifier.swift | 1 - Sources/FileSugar/FileUtils.swift | 2 - .../FileSugar/common/String+Extension.swift | 5 +- Sources/FileSugar/path/FilePathAsserter.swift | 12 +-- Sources/FileSugar/path/FilePathParser.swift | 38 +++++----- .../FileSugar/stream/FileStreamReader.swift | 76 +++++++------------ .../FileSugar/stream/FileStreamWriter.swift | 10 +-- 13 files changed, 98 insertions(+), 119 deletions(-) diff --git a/FileSugarExampleMacOS/AppDelegate.swift b/FileSugarExampleMacOS/AppDelegate.swift index 3c2adbb..ffd8ef1 100644 --- a/FileSugarExampleMacOS/AppDelegate.swift +++ b/FileSugarExampleMacOS/AppDelegate.swift @@ -7,11 +7,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { * Creates the view */ lazy var view: NSView = { - let contentRect = window.contentRect(forFrameRect: window.frame)/*size of win sans titlebar*/ - let view: View = .init(frame: contentRect) - window.contentView = view - view.layer?.backgroundColor = NSColor.white.cgColor - return view + let contentRect = window.contentRect(forFrameRect: window.frame) // Get the size of the window without the title bar + let view: View = .init(frame: contentRect) // Create a new view with the same size as the window + window.contentView = view // Set the window's content view to the new view + view.layer?.backgroundColor = NSColor.white.cgColor // Set the background color of the view to white + return view // Return the new view }() func applicationDidFinishLaunching(_ aNotification: Notification) { _ = view diff --git a/FileSugarExampleMacOS/View.swift b/FileSugarExampleMacOS/View.swift index 77fcae7..5d0084b 100644 --- a/FileSugarExampleMacOS/View.swift +++ b/FileSugarExampleMacOS/View.swift @@ -24,7 +24,9 @@ extension View { * Testing getting content from folder */ func test1() { + // Call the content method of the FileParser class with the expanded tilde path let result = FileParser.content(dirPath: NSString(string: "~/Desktop/").expandingTildeInPath) + // Print the count of the result array Swift.print("result.count: \(String(describing: result?.count))") result?.forEach { Swift.print("$0: \($0)") // prints all the files on the desktop folder diff --git a/LICENSE b/LICENSE index 0f8ed3e..e267673 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,16 @@ MIT License -Copyright (c) 2018 André J +(c) 2023 eonist -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +- The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +SOFTWARE. \ No newline at end of file diff --git a/Package.swift b/Package.swift index de2cf01..413fe9a 100644 --- a/Package.swift +++ b/Package.swift @@ -1,21 +1,22 @@ // swift-tools-version:5.6 import PackageDescription +// Define the package with a name and a list of products and dependencies let package = Package( - name: "FileSugar", + name: "FileSugar", // The name of the package products: [ - .library( - name: "FileSugar", - targets: ["FileSugar"]) + .library( // Define a library product + name: "FileSugar", // The name of the library product + targets: ["FileSugar"]) // The targets that make up the product ], - dependencies: [ + dependencies: [ // The list of dependencies for the package ], - targets: [ - .target( - name: "FileSugar", - dependencies: []), - .testTarget( - name: "FileSugarTests", - dependencies: ["FileSugar"]) + targets: [ // The list of targets for the package + .target( // Define a target + name: "FileSugar", // The name of the target + dependencies: []), // The dependencies of the target + .testTarget( // Define a test target + name: "FileSugarTests", // The name of the test target + dependencies: ["FileSugar"]) // The dependencies of the test target ] -) +) \ No newline at end of file diff --git a/README.md b/README.md index 458a3a1..158b1b5 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,15 @@ [![Tests](https://github.com/eonist/FileSugar/actions/workflows/Tests.yml/badge.svg)](https://github.com/eonist/FileSugar/actions/workflows/Tests.yml) [![codebeat badge](https://codebeat.co/badges/b4b79239-d1f9-4c9a-8c46-d6a1b9dcb559)](https://codebeat.co/projects/github-com-eonist-filesugar-master) +### Description FileSugar is a Swift package that provides a simple API for working with files. It allows you to easily open, save, delete, and create files. +## Table of Contents +- [Description](#description) +- [Features](#features) +- [Installation](#installation) +- [Usage](#usage) + ## Features - Open, save, delete, and create files diff --git a/Sources/FileSugar/FileAsserter.swift b/Sources/FileSugar/FileAsserter.swift index b2f145f..1a05c5b 100755 --- a/Sources/FileSugar/FileAsserter.swift +++ b/Sources/FileSugar/FileAsserter.swift @@ -9,7 +9,10 @@ public final class FileAsserter { * - Returns: A boolean value indicating whether the file or folder exists */ public static func exists(path: String) -> Bool { - FileManager().fileExists(atPath: path) + // Create a new instance of the FileManager class + FileManager() + // Check if a file exists at the given path + .fileExists(atPath: path) } /** @@ -20,6 +23,9 @@ public final class FileAsserter { * - Returns: A boolean value indicating whether the directory has files */ public static func hasContent(filePath: String) -> Bool { - FileParser.content(dirPath: filePath)?.isEmpty ?? false + // Call the content method of the FileParser class with the given directory path + FileParser.content(dirPath: filePath)? + // Check if the returned content is empty + .isEmpty ?? false } } \ No newline at end of file diff --git a/Sources/FileSugar/FileModifier.swift b/Sources/FileSugar/FileModifier.swift index 76b7d3a..66bf3e6 100755 --- a/Sources/FileSugar/FileModifier.swift +++ b/Sources/FileSugar/FileModifier.swift @@ -21,7 +21,6 @@ public final class FileModifier { return false // return false if the file move operation failed } } - /** * Copies a file to another location * - Remark: Paths must be created with: URL(fileURLWithPath: directory) and then .path diff --git a/Sources/FileSugar/FileUtils.swift b/Sources/FileSugar/FileUtils.swift index 8a0d9c5..a451726 100755 --- a/Sources/FileSugar/FileUtils.swift +++ b/Sources/FileSugar/FileUtils.swift @@ -1,6 +1,5 @@ #if os(OSX) import Cocoa - /** * A utility class for working with files on macOS */ @@ -14,7 +13,6 @@ class FileUtils { static func showFileInFinder(_ filePath: String) { // Expand the tilde in the file path to the user's home directory let expandedFilePath: String = filePath.tildePath - // Use the shared NSWorkspace instance to select the file or folder in Finder NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: expandedFilePath) } diff --git a/Sources/FileSugar/common/String+Extension.swift b/Sources/FileSugar/common/String+Extension.swift index 18981d6..b5d3560 100644 --- a/Sources/FileSugar/common/String+Extension.swift +++ b/Sources/FileSugar/common/String+Extension.swift @@ -4,5 +4,8 @@ extension String { /** * From user agnostic to absolute URL */ - internal var tildePath: String { NSString(string: self).expandingTildeInPath } + internal var tildePath: String { + // Convert the string to an NSString and expand the tilde in the path + NSString(string: self).expandingTildeInPath + } } diff --git a/Sources/FileSugar/path/FilePathAsserter.swift b/Sources/FileSugar/path/FilePathAsserter.swift index 8f047fd..e8551ca 100755 --- a/Sources/FileSugar/path/FilePathAsserter.swift +++ b/Sources/FileSugar/path/FilePathAsserter.swift @@ -8,8 +8,7 @@ public final class FilePathAsserter { * - Returns: True if the path is absolute, false if it is relative */ public static func isAbsolute(_ path: String, pathSeperator: String = "/") -> Bool { - // Check if the path starts with the path separator - return path.hasPrefix(pathSeperator) + path.hasPrefix(pathSeperator) // Check if the path starts with the path separator } /** @@ -18,10 +17,8 @@ public final class FilePathAsserter { * - Returns: True if the path contains a relative path, false otherwise */ public static func isBacklash(_ path: String) -> Bool { - // Check if the path starts with "../" - return path.hasPrefix("../") + path.hasPrefix("../") // Check if the path starts with "../" } - /** * Tests if a path is a file path (i.e. starts with a path separator or "../") * - Parameter path: The path to test @@ -30,7 +27,7 @@ public final class FilePathAsserter { */ public static func isFilePath(_ path: String, pathSeperator: String = "/") -> Bool { // Check if the path starts with the path separator or "../" - return path.hasPrefix(pathSeperator) || path.hasPrefix(".." + pathSeperator) + path.hasPrefix(pathSeperator) || path.hasPrefix(".." + pathSeperator) } /** @@ -39,7 +36,6 @@ public final class FilePathAsserter { * - Returns: True if the path is a tilde path, false otherwise */ public static func isTildePath(_ path: String) -> Bool { - // Check if the path starts with "~" - return path.hasPrefix("~") + path.hasPrefix("~") // Check if the path starts with "~" } } \ No newline at end of file diff --git a/Sources/FileSugar/path/FilePathParser.swift b/Sources/FileSugar/path/FilePathParser.swift index 83b0bab..37b879b 100755 --- a/Sources/FileSugar/path/FilePathParser.swift +++ b/Sources/FileSugar/path/FilePathParser.swift @@ -10,10 +10,9 @@ public final class FilePathParser { * let appDocPath = FilePathParser.appDocPath() // "/Users/James/Documents" */ public static func appDocPath() -> String? { - let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) - return paths.first + let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) // Get an array of paths to the user's document directory + return paths.first // Return the first path in the array (which should be the document directory) } - /** * Parses a string path into a URL object * - Parameter stringPath: The string path to parse @@ -22,9 +21,8 @@ public final class FilePathParser { * let url = FilePathParser.path("file:///Users/Me/Desktop/Doc.txt") // NSURL object */ public static func path(_ stringPath: String) -> URL? { - URL(string: stringPath) + URL(string: stringPath) // Create a URL from the given string path } - /** * Converts a URL object to a string path * - Parameter url: The URL object to convert @@ -33,9 +31,8 @@ public final class FilePathParser { * let path = FilePathParser.path(NSURL("file:///Users/Me/Desktop/Doc.txt")) // "/Users/Me/Desktop/Doc.txt" */ public static func path(_ url: URL) -> String { - url.path + url.path // Get the path component of the URL as a string } - /** * Converts a URL object to a string path * - Parameter path: The URL object to convert @@ -50,9 +47,8 @@ public final class FilePathParser { * Returns the path to the user's home directory. */ public static func userHomePath() -> String { - NSHomeDirectory() + NSHomeDirectory() // Get the path to the user's home directory } - /** * Returns the file name of the given URL. * @@ -68,9 +64,10 @@ public final class FilePathParser { * ``` */ public static func fileName(_ fileURL: URL, _ withExtension: Bool = true) -> String { + // If withExtension is true, return the last path component of the URL (including the file extension) + // Otherwise, return the last path component of the URL without the file extension withExtension ? fileURL.absoluteURL.lastPathComponent : fileURL.absoluteURL.deletingPathExtension().lastPathComponent } - /** * Returns the file name of the file at the given path. * @@ -85,10 +82,9 @@ public final class FilePathParser { * ``` */ public static func fileName(path filePath: String, withExtension: Bool = true) -> String? { - guard let url: URL = path(filePath) else { return nil } - return fileName(url, withExtension) - } - + guard let url: URL = path(filePath) else { return nil } // Get the URL from the file path + return fileName(url, withExtension) // Get the file name from the URL and return it + } /** * Returns the directory of the given file URL. * @@ -102,9 +98,10 @@ public final class FilePathParser { * ``` */ public static func directory(_ fileURL: URL) -> String { - fileURL.absoluteURL.deletingPathExtension().absoluteString + fileURL.absoluteURL // Get the absolute URL of the file URL + .deletingPathExtension() // Delete the file extension from the URL + .absoluteString // Convert the URL back to a string } - /** * Returns the path to the project's resource folder. * @@ -114,9 +111,8 @@ public final class FilePathParser { * ``` */ public static var resourcePath: String? { - Bundle.main.resourcePath + Bundle.main.resourcePath // Get the path to the main bundle's resource directory } - /** * Returns the file extension of the file at the given path. * @@ -129,7 +125,7 @@ public final class FilePathParser { * ``` */ public static func fileExtension(_ filePath: String) -> String { - NSString(string: filePath).pathExtension + NSString(string: filePath).pathExtension // Get the file extension from the file path as an NSString } } /** @@ -148,7 +144,7 @@ extension FilePathParser { * let fileURL = "file:///Users/username/Documents/example.txt" let fileName = FilePathParser.fileName(fileURL: fileURL) // fileName == "example.txt" */ public static func fileName(fileURL: String, _ withExtension: Bool = true) -> String? { - guard let path = path(fileURL) else { return nil } - return fileName(path, withExtension) + guard let path = path(fileURL) else { return nil } // Get the path from the file URL + return fileName(path, withExtension) // Get the file name from the path and return it } } diff --git a/Sources/FileSugar/stream/FileStreamReader.swift b/Sources/FileSugar/stream/FileStreamReader.swift index 0a5f27d..412f87b 100644 --- a/Sources/FileSugar/stream/FileStreamReader.swift +++ b/Sources/FileSugar/stream/FileStreamReader.swift @@ -19,23 +19,12 @@ public final class FileStreamReader { */ public static func read(url: URL, startIndex: UInt64, endIndex: Int) throws -> Data { do { - // Open the file at the given URL for reading - let file: FileHandle = try .init(forReadingFrom: url) - - // Seek to the starting byte offset - file.seek(toFileOffset: startIndex) - - // Calculate the length of the data to read - let length: Int = endIndex - Int(startIndex) - - // Read the data from the file - let databuffer = file.readData(ofLength: length) - - // Close the file - file.closeFile() - - // Return the data read from the file - return databuffer + let file: FileHandle = try .init(forReadingFrom: url) // Open the file at the given URL for reading + file.seek(toFileOffset: startIndex) // Seek to the starting byte offset + let length: Int = endIndex - Int(startIndex) // Calculate the length of the data to read + let databuffer = file.readData(ofLength: length) // Read the data from the file + file.closeFile() // Close the file + return databuffer // Return the data read from the file } catch { // If an error occurs, throw an NSError with a descriptive message throw NSError(domain: ("Error: \(error) reading \(url.path)"), code: 0) @@ -56,21 +45,16 @@ extension FileStreamReader { public static func getFileSize(filePath: String) throws -> UInt64 { // Create a URL from the file path let fileUrl = URL(fileURLWithPath: filePath) - // Get the default file manager let fileManager = FileManager.default - do { // Get the attributes of the file at the URL let attributes = try fileManager.attributesOfItem(atPath: (fileUrl.path)) - // Get the file size from the attributes dictionary var fileSize = attributes[FileAttributeKey.size] as! UInt64 - // Alternatively, get the file size from the attributes dictionary using the fileSize() method let dict = attributes as NSDictionary fileSize = dict.fileSize() - // Return the file size return fileSize } catch let error as NSError { @@ -81,30 +65,26 @@ extension FileStreamReader { } extension FileStreamReader { /** - * Support for filePath - */ -public static func read(filePath: String, startIndex: UInt64, endIndex: Int) throws -> Data { - // Create a URL from the file path - let url: URL = .init(fileURLWithPath: filePath) - - // Call the read method with the URL - return try read(url: url, startIndex: startIndex, endIndex: endIndex) -} - -/** - * Read string - */ -internal static func read(filePath: String, start: UInt64, end: Int) throws -> String { - // Read the data from the file at the given path - let data: Data = try FileStreamReader.read(filePath: filePath, startIndex: start, endIndex: end) - - // Convert the data to a string using UTF-8 encoding - guard let string = String(data: data, encoding: .utf8) else { - // If the data cannot be converted to a string, throw an NSError with a descriptive message - throw NSError(domain: "FileStreamReader.read() - Unable to get string from data data.count: \(data.count)", code: 0) - } - - // Return the string - return string -} + * Support for filePath + */ + public static func read(filePath: String, startIndex: UInt64, endIndex: Int) throws -> Data { + // Create a URL from the file path + let url: URL = .init(fileURLWithPath: filePath) + // Call the read method with the URL + return try read(url: url, startIndex: startIndex, endIndex: endIndex) + } + /** + * Read string + */ + internal static func read(filePath: String, start: UInt64, end: Int) throws -> String { + // Read the data from the file at the given path + let data: Data = try FileStreamReader.read(filePath: filePath, startIndex: start, endIndex: end) + // Convert the data to a string using UTF-8 encoding + guard let string = String(data: data, encoding: .utf8) else { + // If the data cannot be converted to a string, throw an NSError with a descriptive message + throw NSError(domain: "FileStreamReader.read() - Unable to get string from data data.count: \(data.count)", code: 0) + } + // Return the string + return string + } } diff --git a/Sources/FileSugar/stream/FileStreamWriter.swift b/Sources/FileSugar/stream/FileStreamWriter.swift index 5450c1a..b3c5b6b 100644 --- a/Sources/FileSugar/stream/FileStreamWriter.swift +++ b/Sources/FileSugar/stream/FileStreamWriter.swift @@ -24,10 +24,8 @@ public final class FileStreamWriter { public static func write(url: URL, data: Data, index: UInt64) throws { // Check if the file already exists let fileExists: Bool = FileManager().fileExists(atPath: url.path) - if fileExists == false { - // If the file does not exist, create it and write the data to it - do { + do { // If the file does not exist, create it and write the data to it try data.write(to: url, options: .atomic) } catch { // If an error occurs, throw an NSError with a descriptive message @@ -89,9 +87,7 @@ extension FileStreamWriter { * - Throws: An error if the file cannot be written to */ public static func write(filePath: String, data: Data, index: UInt64) throws { - // Create a URL from the file path - let url: URL = .init(fileURLWithPath: filePath) - // Call the write method with the URL - try write(url: url, data: data, index: index) + let url: URL = .init(fileURLWithPath: filePath) // Create a URL from the file path + try write(url: url, data: data, index: index) // Call the write method with the URL } }