From dd53bcf6c4d46bb09b2096e00d6eb627e63f6f5d Mon Sep 17 00:00:00 2001 From: KOTARO SUTO Date: Fri, 25 Feb 2022 16:48:50 -0800 Subject: [PATCH 1/6] Update tests --- Tests/HondanaTests/HondanaTests.swift | 45 +++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/Tests/HondanaTests/HondanaTests.swift b/Tests/HondanaTests/HondanaTests.swift index 2261c45..5e9c898 100644 --- a/Tests/HondanaTests/HondanaTests.swift +++ b/Tests/HondanaTests/HondanaTests.swift @@ -20,14 +20,14 @@ final class HondanaTests: XCTestCase { try AssertExecuteCommand(command: "hondana --version", expected: "0.0.6-d") } - func testListWithoutJSFiles() throws { + // swiftlint:disable line_length + func testList() throws { + // No Bookmarklets try AssertExecuteCommand(command: "hondana list", expected: """ No bookmarklet exist """) - } - - // swiftlint:disable line_length - func testListWithJSFiles() throws { + + // 1 Bookmarklet let bookmarksHtmlPath = try File(path: #file).parent!.parent!.url.appendingPathComponent("Fixtures/+test.js") try Folder(path: "~/.Hondana/Bookmarklets/").createFile(at: "+test.js", contents: try Data(contentsOf: bookmarksHtmlPath)) try AssertExecuteCommand(command: "hondana list", expected: """ @@ -39,9 +39,42 @@ final class HondanaTests: XCTestCase { | test | (alert(%22testing%22))()%3B%0A... | +-------+-----------------------------------+ """) - + try File(path: "~/.Hondana/Bookmarklets/+test.js").delete() } + + func testHelp() throws { + #if canImport(AppKit) + try AssertExecuteCommand(command: "hondana list --help", expected: """ + OVERVIEW: `hondana list` lists every bookmarklet present in + `~/.Hondana/Bookmarklets/` + + `hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it, + and outputs the filtered result in the table. + + USAGE: hondana list [--on-safari] + + OPTIONS: + --on-safari List the bookmarklets on Safari browser + --version Show the version. + -h, --help Show help information. + """) + #else + try AssertExecuteCommand(command: "hondana list --help", expected: """ + OVERVIEW: `hondana list` lists every bookmarklet present in + `~/.Hondana/Bookmarklets/` + + `hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it, + and outputs the filtered result in the table. + + USAGE: hondana list [--on-safari] + + OPTIONS: + --version Show the version. + -h, --help Show help information. + """) + #endif + } /// Returns path to the built products directory. var productsDirectory: URL { From aad7a5ef31754b7e6b96dd16231b7a64547c9163 Mon Sep 17 00:00:00 2001 From: KOTARO SUTO Date: Fri, 25 Feb 2022 16:49:04 -0800 Subject: [PATCH 2/6] Add new Utils.swift --- Sources/Commands/List.swift | 34 +++++++++++++++++++++++------- Sources/Commands/Utils/Utils.swift | 28 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 Sources/Commands/Utils/Utils.swift diff --git a/Sources/Commands/List.swift b/Sources/Commands/List.swift index aba49d7..e5f085c 100644 --- a/Sources/Commands/List.swift +++ b/Sources/Commands/List.swift @@ -5,28 +5,46 @@ import SwiftyTextTable import Rainbow import Models +#if canImport(AppKit) +import AppKit +#endif + struct List: ParsableCommand { static let configuration = CommandConfiguration(commandName: Constants.commandName, abstract: Constants.abstract, discussion: Constants.discussion) + +#if canImport(AppKit) + @Flag(help: "List the bookmarklets on Safari browser") + var onSafari = false +#endif } extension List { func run() throws { - var w = winsize() - let bookmarklets: [(uuid: String, title: String, url: String)] = - try Folder(path: Constants.hondanaDirURL).createSubfolderIfNeeded(at: "Bookmarklets/") + let jsFiles = try Folder(path: Constants.hondanaDirURL) + .createSubfolderIfNeeded(at: "Bookmarklets/") .files .filter { $0.extension == "js" } + guard !jsFiles.isEmpty else { + print("No bookmarklet exist") + return + } + #if canImport(AppKit) + guard !onSafari else { + let htmlFile = try Utils.generateHTML(from: jsFiles) + try NSWorkspace.shared.open([htmlFile.url], withApplicationAt: URL(string: "file://~/Applications/Safari.app")!, configuration: [:]) + return + } + #endif + var w = winsize() + let bookmarklets: [(uuid: String, title: String, url: String)] = + try jsFiles .map { (uuid: $0.nameExcludingExtension.components(separatedBy: "+").first!, title: $0.nameExcludingExtension.components(separatedBy: "+")[1], url: String(try $0.readAsString(encodedAs: .utf8) .withoutJSPrefix.minified.prefix( ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w) == 0 ? - Int(w.ws_col) - 30 : 30))) + Int(w.ws_col) - 30 : 30))) } - guard !bookmarklets.isEmpty else { - print("No bookmarklet exist") - return - } let titleCol = TextTableColumn(header: "Title".bold) let urlCol = TextTableColumn(header: "URL".bold) var table = TextTable(columns: [titleCol, urlCol], header: "Bookmarklets".bold) diff --git a/Sources/Commands/Utils/Utils.swift b/Sources/Commands/Utils/Utils.swift new file mode 100644 index 0000000..ef3dd8f --- /dev/null +++ b/Sources/Commands/Utils/Utils.swift @@ -0,0 +1,28 @@ +import Files +import Foundation +enum Utils { + static func generateHTML(from jsFiles: [File]) throws -> File { + let rawHTMLstring = """ + + Bookmarklets +

Bookmarklets

+ """ + + (try jsFiles + .map { jsFile in + return aTag(url: try jsFile.readAsString(encodedAs: .utf8).withJSPrefix.minified, + title: jsFile.nameExcludingExtension.components(separatedBy: "+")[1]) + } + .joined(separator: "\n")) + + """ + + """ + return try Folder(path: "~") + .createSubfolderIfNeeded(at: ".Hondana") + .createFile(at: "bookmarklets.html", contents: rawHTMLstring.data(using: .utf8)) + } + + private static func aTag(url: String, title: String) -> String { + return "\(title)" + } +} From c94486b114b630b8b438bf7bb06c5972e4b75674 Mon Sep 17 00:00:00 2001 From: KOTARO SUTO Date: Fri, 25 Feb 2022 16:52:54 -0800 Subject: [PATCH 3/6] Correct version definition --- Sources/Commands/Utils/Version.swift | 2 +- Tests/HondanaTests/HondanaTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Commands/Utils/Version.swift b/Sources/Commands/Utils/Version.swift index 5af55d3..af211f3 100644 --- a/Sources/Commands/Utils/Version.swift +++ b/Sources/Commands/Utils/Version.swift @@ -1 +1 @@ -let version = "0.0.6-d" +let version = "0.0.6" diff --git a/Tests/HondanaTests/HondanaTests.swift b/Tests/HondanaTests/HondanaTests.swift index 5e9c898..e4ff2cc 100644 --- a/Tests/HondanaTests/HondanaTests.swift +++ b/Tests/HondanaTests/HondanaTests.swift @@ -17,7 +17,7 @@ final class HondanaTests: XCTestCase { } func testVersionFlag() throws { - try AssertExecuteCommand(command: "hondana --version", expected: "0.0.6-d") + try AssertExecuteCommand(command: "hondana --version", expected: "0.0.6") } // swiftlint:disable line_length From 16c1d1bd921bc954d03f0000c0e662bd1e21b66c Mon Sep 17 00:00:00 2001 From: KOTARO SUTO Date: Fri, 25 Feb 2022 16:54:02 -0800 Subject: [PATCH 4/6] Fix variable name --- Sources/Commands/List.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Commands/List.swift b/Sources/Commands/List.swift index e5f085c..30407c8 100644 --- a/Sources/Commands/List.swift +++ b/Sources/Commands/List.swift @@ -35,15 +35,15 @@ extension List { return } #endif - var w = winsize() + var winsize = winsize() let bookmarklets: [(uuid: String, title: String, url: String)] = try jsFiles .map { (uuid: $0.nameExcludingExtension.components(separatedBy: "+").first!, title: $0.nameExcludingExtension.components(separatedBy: "+")[1], url: String(try $0.readAsString(encodedAs: .utf8) .withoutJSPrefix.minified.prefix( - ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w) == 0 ? - Int(w.ws_col) - 30 : 30))) + ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &winsize) == 0 ? + Int(winsize.ws_col) - 30 : 30))) } let titleCol = TextTableColumn(header: "Title".bold) let urlCol = TextTableColumn(header: "URL".bold) From 2339d171fcd1d85b88b547550626aa9feb716fcd Mon Sep 17 00:00:00 2001 From: KOTARO SUTO Date: Fri, 25 Feb 2022 16:56:13 -0800 Subject: [PATCH 5/6] Fix test --- Tests/HondanaTests/HondanaTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/HondanaTests/HondanaTests.swift b/Tests/HondanaTests/HondanaTests.swift index e4ff2cc..fed4cab 100644 --- a/Tests/HondanaTests/HondanaTests.swift +++ b/Tests/HondanaTests/HondanaTests.swift @@ -67,7 +67,7 @@ final class HondanaTests: XCTestCase { `hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it, and outputs the filtered result in the table. - USAGE: hondana list [--on-safari] + USAGE: hondana list OPTIONS: --version Show the version. From 17878714fcf84ddb17dddb94a3383e58986bffe7 Mon Sep 17 00:00:00 2001 From: KOTARO SUTO Date: Fri, 25 Feb 2022 16:57:13 -0800 Subject: [PATCH 6/6] Run `swiftlint --fix` --- Tests/HondanaTests/HondanaTests.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Tests/HondanaTests/HondanaTests.swift b/Tests/HondanaTests/HondanaTests.swift index fed4cab..8212fe9 100644 --- a/Tests/HondanaTests/HondanaTests.swift +++ b/Tests/HondanaTests/HondanaTests.swift @@ -26,7 +26,7 @@ final class HondanaTests: XCTestCase { try AssertExecuteCommand(command: "hondana list", expected: """ No bookmarklet exist """) - + // 1 Bookmarklet let bookmarksHtmlPath = try File(path: #file).parent!.parent!.url.appendingPathComponent("Fixtures/+test.js") try Folder(path: "~/.Hondana/Bookmarklets/").createFile(at: "+test.js", contents: try Data(contentsOf: bookmarksHtmlPath)) @@ -39,21 +39,21 @@ final class HondanaTests: XCTestCase { | test | (alert(%22testing%22))()%3B%0A... | +-------+-----------------------------------+ """) - + try File(path: "~/.Hondana/Bookmarklets/+test.js").delete() } - + func testHelp() throws { #if canImport(AppKit) try AssertExecuteCommand(command: "hondana list --help", expected: """ OVERVIEW: `hondana list` lists every bookmarklet present in `~/.Hondana/Bookmarklets/` - + `hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it, and outputs the filtered result in the table. - + USAGE: hondana list [--on-safari] - + OPTIONS: --on-safari List the bookmarklets on Safari browser --version Show the version. @@ -63,12 +63,12 @@ final class HondanaTests: XCTestCase { try AssertExecuteCommand(command: "hondana list --help", expected: """ OVERVIEW: `hondana list` lists every bookmarklet present in `~/.Hondana/Bookmarklets/` - + `hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it, and outputs the filtered result in the table. - + USAGE: hondana list - + OPTIONS: --version Show the version. -h, --help Show help information.