From 7f034115cdeff2133e2081bf01e154d9392adf57 Mon Sep 17 00:00:00 2001 From: "Alexander A. Kropotin" Date: Mon, 13 Sep 2021 19:43:12 +0500 Subject: [PATCH] fix(CopyPathAction.swift) add return statement if the paths is empty --- CHANGELOG.md | 10 ++++++++++ README.md | 2 +- commons/link/FileLinkManager.swift | 5 ----- commons/link/action/CopyPathAction.swift | 10 ++++++++-- commons/link/action/CreateLinkAction.swift | 6 ++++++ quick-symlink-tests/ResourcePathTest.swift | 2 +- quick-symlink.xcodeproj/project.pbxproj | 2 ++ 7 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d35e9c0..943ce30 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Refactor application. +## [0.8.1] - 2021-09-13 + +### Fixed + +- Fix a bug that led to fatal error: `Can't remove last element from an empty collection`. + +### Added + +- Added the ability to create links from the `Finder` menu without selected objects. + ## [0.8.0] - 2021-09-02 ### Added diff --git a/README.md b/README.md index 6d58702..ea1b112 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ The Quick Symlink is a `Finder extension` which provides a `contextual menu item` for the symbolic links creation on macOS. -[![status](https://img.shields.io/badge/status-active-active?style=flat-square)](BADGES_GUIDE.md#status) [![version](https://img.shields.io/badge/version-0.8.0-informational?style=flat-square)](BADGES_GUIDE.md#version) [![oss lifecycle](https://img.shields.io/badge/oss_lifecycle-active-important?style=flat-square)](BADGES_GUIDE.md#oss-lifecycle) [![maintenance](https://img.shields.io/badge/maintenance-yes-informational?style=flat-square)](BADGES_GUIDE.md#maintenance) [![last release](https://img.shields.io/badge/last_release-September_02,_2021-informational?style=flat-square)](BADGES_GUIDE.md#release-date) [![last commit](https://img.shields.io/badge/last_commit-September_02,_2021-informational?style=flat-square)](BADGES_GUIDE.md#commit-date) +[![status](https://img.shields.io/badge/status-active-active?style=flat-square)](BADGES_GUIDE.md#status) [![version](https://img.shields.io/badge/version-0.8.1-informational?style=flat-square)](BADGES_GUIDE.md#version) [![oss lifecycle](https://img.shields.io/badge/oss_lifecycle-active-important?style=flat-square)](BADGES_GUIDE.md#oss-lifecycle) [![maintenance](https://img.shields.io/badge/maintenance-yes-informational?style=flat-square)](BADGES_GUIDE.md#maintenance) [![last release](https://img.shields.io/badge/last_release-September_13,_2021-informational?style=flat-square)](BADGES_GUIDE.md#release-date) [![last commit](https://img.shields.io/badge/last_commit-September_13,_2021-informational?style=flat-square)](BADGES_GUIDE.md#commit-date) [![license](https://img.shields.io/badge/license-MIT-informational?style=flat-square)](LICENSE) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=flat-square)](CODE_OF_CONDUCT.md) diff --git a/commons/link/FileLinkManager.swift b/commons/link/FileLinkManager.swift index 359e268..dbda8ec 100644 --- a/commons/link/FileLinkManager.swift +++ b/commons/link/FileLinkManager.swift @@ -66,13 +66,8 @@ public class SoftLinkManager: FileLinkManager { public class HardLinkManager: FileLinkManager { public func linkWith(of: URL!, with: URL!) { - - let pasteboard = NSPasteboard.general; - pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil); - do { try FileManager.default.linkItem(at: of, to: with); - } catch let error as NSError { NSLog("FileManager.createSymbolicLink() failed to create file: %@", error.description as NSString); } diff --git a/commons/link/action/CopyPathAction.swift b/commons/link/action/CopyPathAction.swift index 7cbcb0e..134e5e2 100644 --- a/commons/link/action/CopyPathAction.swift +++ b/commons/link/action/CopyPathAction.swift @@ -19,19 +19,25 @@ public class CopyPathAction: QuickSymlinkAction { public func execute() { //Get all selected path - guard let target = self.finderController.selectedItemURLs() else { + guard var target = self.finderController.selectedItemURLs() else { NSLog("FinderSync() failed to obtain targeted URLs: %@"); return; } + if (target.isEmpty) { + target.append(self.finderController.targetedURL()!); + } + // Append all selected paths to string var paths = "" for path in target { paths.append(contentsOf: path.relativePath); paths.append(";"); } - paths.removeLast(); + if (!paths.isEmpty) { + paths.removeLast(); + } //Copy path list to clipboard let pasteboard = NSPasteboard.init(name: NSPasteboard.Name.init(rawValue: "qs")); diff --git a/commons/link/action/CreateLinkAction.swift b/commons/link/action/CreateLinkAction.swift index 2dcffed..40e53fc 100644 --- a/commons/link/action/CreateLinkAction.swift +++ b/commons/link/action/CreateLinkAction.swift @@ -32,5 +32,11 @@ public class CreateLinkAction: QuickSymlinkAction { let targetPath = self.fileLinkManager.getTargetPath(path, to: path.deletingLastPathComponent()); self.fileLinkManager.linkWith(of: path, with: targetPath); } + + if (target.isEmpty) { + let path = self.finderController.targetedURL()!; + let targetPath = self.fileLinkManager.getTargetPath(path, to: path); + self.fileLinkManager.linkWith(of: path, with: targetPath); + } } } diff --git a/quick-symlink-tests/ResourcePathTest.swift b/quick-symlink-tests/ResourcePathTest.swift index 40f4b7e..3029ddb 100644 --- a/quick-symlink-tests/ResourcePathTest.swift +++ b/quick-symlink-tests/ResourcePathTest.swift @@ -37,7 +37,7 @@ class ResourcePathTest: XCTestCase { XCTAssert(relativePath.toUriString() == "./../..", "The relative path is wrong"); } - func test_relativize_whenCurrentDirectoryAndOtherDirectoryAreNestedToGeneralDirectoryy_thenReturnPathWithJumpsAboveOtherDirectoryAndPartOfCurrentDirectory() { + func test_relativize_whenCurrentDirectoryAndOtherDirectoryAreNestedToGeneralDirectory_thenReturnPathWithJumpsAboveOtherDirectoryAndPartOfCurrentDirectory() { let currentUri: URL = URL.init(string: "/a/b/c1/d1")!; let otherUri: URL = URL.init(string: "/a/b/c2/d2")!; diff --git a/quick-symlink.xcodeproj/project.pbxproj b/quick-symlink.xcodeproj/project.pbxproj index 9853a2e..7796318 100644 --- a/quick-symlink.xcodeproj/project.pbxproj +++ b/quick-symlink.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ A316477826B7B403001DD969 /* CreateLinkAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = A316477626B7B403001DD969 /* CreateLinkAction.swift */; }; A31904A426E104FA00D7F69D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A31904A326E104FA00D7F69D /* Assets.xcassets */; }; A32EE8E0265D4D05008648AA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A32EE8DF265D4D05008648AA /* Assets.xcassets */; }; + A34185F126EF767900224992 /* FileLinkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36F9B0526DFAC27009E95CE /* FileLinkManager.swift */; }; A345C9BF26A0B200004FBF0F /* QuickSymlinkAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = A345C9BE26A0B200004FBF0F /* QuickSymlinkAction.swift */; }; A345C9C226A0B30F004FBF0F /* CopyPathAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = A345C9C126A0B30F004FBF0F /* CopyPathAction.swift */; }; A345C9C526A0B49C004FBF0F /* PasteLinkAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = A345C9C426A0B49C004FBF0F /* PasteLinkAction.swift */; }; @@ -439,6 +440,7 @@ buildActionMask = 2147483647; files = ( A307B43026D257C5002EEF58 /* ResourcePath.swift in Sources */, + A34185F126EF767900224992 /* FileLinkManager.swift in Sources */, A307B42F26D2563B002EEF58 /* ResourcePathTest.swift in Sources */, A307B43126D257CA002EEF58 /* Path.swift in Sources */, );