Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #26

Merged
merged 9 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ let package = Package(
"SwiftyTextTable",
"Extensions",
]),
.target(name: "Models"),
.target(name: "Extensions"),
.target(name: "Models", dependencies: ["Extensions", "Files"]),
.target(name: "Extensions", dependencies: ["Files"]),
.testTarget(
name: "HondanaTests",
dependencies: ["Commands", "AssertSwiftCLI"]),
Expand Down
4 changes: 3 additions & 1 deletion Sources/Commands/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension List {
#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: [:])
try NSWorkspace.shared.open([htmlFile.url], withApplicationAt: Constants.List.safariAppURL, configuration: [:])
return
}
#endif
Expand Down Expand Up @@ -66,5 +66,7 @@ extension Constants {
static let discussion = "`hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it, and outputs the filtered result in the table."

static let hondanaDirURL = "~/.Hondana/"

static let safariAppURL = URL(string: "file://~/Applications/Safari.app")!
}
}
10 changes: 6 additions & 4 deletions Sources/Commands/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Sync: ParsableCommand {

extension Sync {
func run() throws {
let jsContent: [Utils.Bookmarklet]
let jsContent: [Bookmarklet]
switch from {
case .hondanaDir:
jsContent = try Utils.readJSContents(from: .hondanaDir)
Expand All @@ -33,9 +33,11 @@ extension Sync {
extension Constants {
enum Sync {
static let commandName = "sync"
static let abstract = "`hondana sync` syncs the JavaScript files in `~/.Hondana/Bookmarklets/` with bookmarklets in browsers"
static let discussion = "`hondana sync` first reads the `--from` option to decide which source should be used as origin. After that, it will read the bookmarklets from the orign to sync the bookmarklets"

static let abstract =
"`hondana sync` syncs the JavaScript files in `~/.Hondana/Bookmarklets/` with bookmarklets in browsers"
static let discussion =
"`hondana sync` first reads the `--from` option to decide which source should be used as origin."
+ "After that, it will read the bookmarklets from the orign to sync the bookmarklets"
static let bookmarkletsURL = "Bookmarklets/"
static let hondanaDirURL = "~/.Hondana/"
}
Expand Down
21 changes: 10 additions & 11 deletions Sources/Commands/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ enum Utils {
<h1>Bookmarklets</h1>
""" +
(try jsFiles
.map { jsFile in
return aTag(url: try jsFile.readAsString(encodedAs: .utf8).withJSPrefix.minified,
title: jsFile.nameExcludingExtension.components(separatedBy: "+")[1])
.map {
aTag(url: try $0.readAsString(encodedAs: .utf8).withJSPrefix.minified,
title: $0.nameExcludingExtension.components(separatedBy: "+")[1])
}
.joined(separator: "\n"))
.joined(separator: "\n")
)
+ """
</html>
"""
Expand All @@ -29,16 +30,14 @@ enum Utils {
return "<a href=\"\(url)\">\(title)</a>"
}

typealias Bookmarklet = (uuid: String, title: String, url: String)

static func readJSContents(from: SyncOrigin) throws -> [Bookmarklet] {
switch from {
case .hondanaDir:
let folder = try Folder(path: Constants.hondanaDirURL + Constants.bookmarkletsURL)
let jsFiles = folder.files.filter { $0.extension == "js" }
return try jsFiles
.map {
(uuid: $0.nameExcludingExtension.components(separatedBy: "+").first!, title: $0.nameExcludingExtension.components(separatedBy: "+")[1], url: try $0.readAsString(encodedAs: .utf8).withJSPrefix.minified)
return jsFiles
.compactMap {
Bookmarklet(file: $0)
}
case .plist:
let file = try Folder(path: Constants.hondanaDirURL).file(named: "Bookmarks.plist")
Expand All @@ -58,7 +57,7 @@ enum Utils {
$0.URLString!.hasPrefix("javascript")
}
.map {
(uuid: $0.WebBookmarkUUID, title: $0.URIDictionary!.title, url: $0.URLString!.withoutJSPrefix.unminified)
Bookmarklet(bookmark: $0)
}

return bookmarklets
Expand Down Expand Up @@ -100,7 +99,7 @@ enum Utils {
return arr
}
.map { (arr: [String]) in
(uuid: "", title: arr[2], url: arr[1])
Bookmarklet(uuid: "", title: arr[2], url: arr[1])
}

return bookmarklets
Expand Down
7 changes: 7 additions & 0 deletions Sources/Extensions/Files+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Files

extension File {
var isBookmarklet: Bool {
self.extension == "js" && ((try? self.readAsString()) ?? "").hasPrefix("javascript") && self.nameExcludingExtension.contains("+")
}
}
42 changes: 42 additions & 0 deletions Sources/Models/Bookmarklet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Bookmarklet.swift
//
//
// Created by Kotaro Suto on 2022/02/27.
//
public struct Bookmarklet {
public var uuid: String
public var title: String
public var url: String

public init(uuid: String, title: String, url: String) {
self.uuid = uuid
self.title = title
self.url = url
}

}

extension Bookmarklet {
public init(bookmark: Bookmark) {
self.init(uuid: bookmark.WebBookmarkUUID,
title: bookmark.URIDictionary!.title,
url: bookmark.URLString!.withoutJSPrefix.unminified)
}
}

import Files
import Extensions

extension Bookmarklet {
public init?(file: File) {
guard !file.nameExcludingExtension.components(separatedBy: "+").isEmpty,
let url = try? file.readAsString() else {
return nil
}

self.uuid = file.nameExcludingExtension.components(separatedBy: "+").first!
self.title = file.nameExcludingExtension.components(separatedBy: "+")[1]
self.url = url.withJSPrefix.minified
}
}
14 changes: 13 additions & 1 deletion Sources/Models/Bookmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@
// Created by Kotaro Suto on 2021/10/08.
//

// Using a upper case to follow the format of the plist
// swiftlint:disable identifier_name
import Foundation

/// Codable Representaion for `Bookmarks.plist`
struct BookmarksPlist: Codable {
var Root: [String: Bookmark]
}

public struct Bookmark: Codable {
public init(WebBookmarkUUID: String, WebBookmarkFileVersion: Int? = nil, Children: [Bookmark]? = nil, Sync: Sync? = nil, Title: String? = nil, WebBookmarkType: String, WebBookmarkIdentifier: String? = nil, ShouldOmitFromUI: Bool? = nil, URLString: String? = nil, URIDictionary: URIDictionary? = nil) {
public init(
WebBookmarkUUID: String,
WebBookmarkFileVersion: Int? = nil,
Children: [Bookmark]? = nil,
Sync: Sync? = nil, Title: String? = nil,
WebBookmarkType: String,
WebBookmarkIdentifier: String? = nil,
ShouldOmitFromUI: Bool? = nil,
URLString: String? = nil,
URIDictionary: URIDictionary? = nil) {
self.WebBookmarkUUID = WebBookmarkUUID
self.WebBookmarkFileVersion = WebBookmarkFileVersion
self.Children = Children
Expand Down
4 changes: 1 addition & 3 deletions Tests/HondanaTests/HondanaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ final class HondanaTests: XCTestCase {
// swiftlint:disable line_length
func testList() throws {
// No Bookmarklets
try AssertExecuteCommand(command: "hondana list", expected: """
No bookmarklet exist
""")
try AssertExecuteCommand(command: "hondana list", expected: "No bookmarklet exist")

// 1 Bookmarklet
let bookmarksHtmlPath = try File(path: #file).parent!.parent!.url.appendingPathComponent("Fixtures/+test.js")
Expand Down