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

Replace SwiftCLI Task with Bash command #20

Closed
Closed
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
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@
"revision": "3d79b2b5a2e5af52c14e462044702ea7728f5770",
"version": "0.1.0"
}
},
{
"package": "SwiftCLI",
"repositoryURL": "https://github.com/jakeheis/SwiftCLI",
"state": {
"branch": null,
"revision": "c72c4564f8c0a24700a59824880536aca45a4cae",
"version": "6.0.1"
}
}
]
},
Expand Down
5 changes: 0 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ let package = Package(
.executable(name: "badgy", targets: ["Badgy"])
],
dependencies: [
.package(
url: "https://github.com/jakeheis/SwiftCLI",
from: "6.0.0"
),
.package(
url: "https://github.com/kylef/PathKit",
from: "1.0.0"
Expand All @@ -28,7 +24,6 @@ let package = Package(
.target(
name: "Badgy",
dependencies: [
"SwiftCLI",
"PathKit",
.product(name: "ArgumentParser", package: "swift-argument-parser")
]),
Expand Down
46 changes: 46 additions & 0 deletions Sources/Badgy/Commands/Bash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Badgy
//

import Foundation
import ArgumentParser

struct Bash {
var command: String
var arguments: [String]

init(_ command: String, _ arguments: String...) {
self.command = command
self.arguments = arguments
}

@discardableResult
func run() throws -> String? {
guard var bashCommand = try execute(command: "/bin/bash", arguments: ["-l", "-c", "which \(command)"]) else {
throw ValidationError("\(command) not found")
}
bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
return try execute(command: bashCommand, arguments: arguments)
}

// MARK: - Private

private func execute(command: String, arguments: [String] = []) throws -> String? {
let process = Process()
let pipe = Pipe()
process.arguments = arguments
process.standardOutput = pipe

if #available(OSX 10.13, *) {
process.executableURL = URL(fileURLWithPath: command)
try process.run()
} else {
process.launchPath = command
process.launch()
}

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)
return output
}
}
13 changes: 6 additions & 7 deletions Sources/Badgy/Helpers/Factory+Resizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

import Foundation
import SwiftCLI
import PathKit

struct ImageSize: Codable {
Expand All @@ -25,11 +24,11 @@ extension Factory {
let size = "\($0)x\($0)"
Logger.shared.logInfo("Resizing to: ", item: size, color: .purple)
do {
try Task.run(
try Bash(
"convert", filename.absolute().description,
"-resize", size,
"\(bareFilename)_\($0).png"
)
).run()
} catch {
Logger.shared.logError("❌ ", item: "Failed to resize icon to \(size)", color: .red)
}
Expand All @@ -41,11 +40,11 @@ extension Factory {
.forEach { (info) in
Logger.shared.logInfo("Replacing: ", item: info.image, color: .purple)
do {
try Task.run(
try Bash(
"convert", newBadgeFile.absolute().description,
"-resize", info.size.description(),
info.image.absolute().description
)
).run()
} catch {
Logger.shared.logError("❌ ",
item: "Failed to replace \(info.image)")
Expand All @@ -58,11 +57,11 @@ extension Factory {
.forEach { (info) in
Logger.shared.logInfo("Replacing: ", item: info.image, color: .purple)
do {
try Task.run(
try Bash(
"convert", newBadgeFile.absolute().description,
"-resize", info.size.description(),
info.image.absolute().description
)
).run()
} catch {
Logger.shared.logError("❌ ",
item: "Failed to replace \(info.image)")
Expand Down
15 changes: 7 additions & 8 deletions Sources/Badgy/Helpers/Factory+Small.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

import Foundation
import SwiftCLI
import PathKit

extension Factory {
Expand All @@ -20,10 +19,10 @@ extension Factory {
do {
let folderBase = folder.absolute().description
if !folder.isDirectory {
try Task.run("mkdir", folderBase)
try Bash("mkdir", folderBase).run()
}

try Task.run(
try Bash(
"convert", "-size", "260x",
"-background", color,
"-gravity", "Center",
Expand All @@ -32,9 +31,9 @@ extension Factory {
"-fill", color,
"caption:'-'",
"\(folderBase)/top.png"
)
).run()

try Task.run(
try Bash(
"convert", "-size", "260x",
"-background", color,
"-gravity", "Center",
Expand All @@ -43,12 +42,12 @@ extension Factory {
"-fill", "\(tintColor)",
"caption:\(label)",
"\(folderBase)/bottom.png"
)
).run()

try Task.run(
try Bash(
"convert", "\(folderBase)/top.png", "\(folderBase)/bottom.png",
"-append", "\(folderBase)/badge.png"
)
).run()

return "\(folderBase)/badge.png"
} catch {
Expand Down
67 changes: 32 additions & 35 deletions Sources/Badgy/Helpers/Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Created by Arthur Alves on 30/05/2020.
//

import SwiftCLI
import AppKit
import PathKit

Expand All @@ -25,43 +24,43 @@ struct Factory {

let folderBase = folder.absolute().description
if !folder.isDirectory {
try Task.run("mkdir", folderBase)
try Bash("mkdir", folderBase).run()
}

try Task.run(
"convert", "-size", "1520x",
try Bash(
"convert",
"-size", "1520x",
"-background", color,
"-gravity", "Center",
"-weight","700",
"-weight", "700",
"-pointsize", "50",
"-fill", color,
"caption:'-'",
"\(folderBase)/top.png"
)
).run()

try Task.run(
"convert", "-size", "1520x",
try Bash(
"convert",
"-size", "1520x",
"-background", color,
"-gravity", "Center",
"-weight","700",
"-pointsize", "180",
"-fill", "\(tintColor)",
"caption:\(label)",
"\(folderBase)/bottom.png"
)
).run()

try Task.run(
"convert", "\(folderBase)/top.png", "\(folderBase)/bottom.png",
"-append", "\(folderBase)/badge.png"
)
try Bash("convert", "\(folderBase)/top.png", "\(folderBase)/bottom.png",
"-append", "\(folderBase)/badge.png"
).run()

if let angle = angle {
try Task.run(
"convert", "\(folderBase)/badge.png",
"-background", "transparent",
"-rotate", "\(angle)",
"\(folderBase)/badge.png"
)
try Bash("convert", "\(folderBase)/badge.png",
"-background", "transparent",
"-rotate", "\(angle)",
"\(folderBase)/badge.png"
).run()
}

return "\(folderBase)/badge.png"
Expand All @@ -80,18 +79,16 @@ struct Factory {
let folderBase = folder.absolute().description
let finalFilename = "\(folderBase)/\(label).png"

try Task.run(
"convert", baseIcon,
"-resize", "1024x",
finalFilename
)
try Bash("convert", baseIcon,
"-resize", "1024x",
finalFilename
).run()

try Task.run(
"convert", "-composite",
"-gravity", "\(position.cardinal)",
finalFilename, "\(folderBase)/badge.png",
finalFilename
)
try Bash("convert", "-composite",
"-gravity", "\(position.cardinal)",
finalFilename, "\(folderBase)/badge.png",
finalFilename
).run()

return finalFilename
}
Expand All @@ -100,12 +97,12 @@ struct Factory {
do {
let folderBase = folder.absolute().description

try Task.run("rm", "-rf",
"\(folderBase)/top.png",
"\(folderBase)/bottom.png",
"\(folderBase)/badge.png")
try Bash("rm", "-rf",
"\(folderBase)/top.png",
"\(folderBase)/bottom.png",
"\(folderBase)/badge.png").run()
} catch {
throw CLI.Error(message: "Failed to clean up temporary files")
throw RuntimeError("Failed to clean up temporary files")
}
}
}
16 changes: 7 additions & 9 deletions Sources/Badgy/Helpers/IconSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import Foundation
import PathKit
import SwiftCLI

struct IconSet {
typealias ImageInfo = (image: Path, size: ImageSize)
Expand Down Expand Up @@ -47,15 +46,14 @@ private extension ImageSize {
}

do {
let result = try Task.capture(
"identify", arguments: [
"-format", "{\"width\":%[fx:w],\"height\":%[fx:h]}",
imagePath.absolute().description
]
)
let result = try Bash(
"identify",
"-format", "{\"width\":%[fx:w],\"height\":%[fx:h]}",
imagePath.absolute().description
).run()

guard let data = result.stdout.data(using: .utf8) else {
throw CLI.Error(message: "Failed to get image size")
guard let data = result?.data(using: .utf8) else {
throw RuntimeError("Failed to get image size")
}

return try JSONDecoder().decode(ImageSize.self, from: data)
Expand Down
13 changes: 13 additions & 0 deletions Sources/Badgy/Helpers/RuntimeError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Badgy
//

import Foundation

struct RuntimeError: Error, CustomStringConvertible {
var description: String

init(_ description: String) {
self.description = description
}
}
7 changes: 3 additions & 4 deletions Sources/Badgy/IconSetDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Foundation
import PathKit
import SwiftCLI

typealias ImageInfo = (image: Path, size: ImageSize)
typealias IconSetImages = (largest: ImageInfo, remaining: [ImageInfo])
Expand Down Expand Up @@ -60,9 +59,9 @@ extension IconSetDelegate {

private func imageSize(from imagePath: Path) -> ImageSize? {
do {
let result = try Task.capture("identify", arguments: ["-format", "{\"width\":%[fx:w],\"height\":%[fx:h]}", imagePath.absolute().description])
guard let data = result.stdout.data(using: .utf8) else { return nil }
let result = try Bash("identify", "-format", "{\"width\":%[fx:w],\"height\":%[fx:h]}", imagePath.absolute().description).run()

guard let data = result?.data(using: .utf8) else { return nil }
let jsonDecoder = JSONDecoder()
let imageSize: ImageSize = try jsonDecoder.decode(ImageSize.self, from: data)
return imageSize
Expand Down
3 changes: 1 addition & 2 deletions Sources/Badgy/Loggers/VerboseLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Created by Arthur Alves on 05/05/2020.
//

import SwiftCLI
import Foundation

public enum ShellColor: String {
Expand Down Expand Up @@ -58,7 +57,7 @@ extension VerboseLogger {
"\(color.rawValue)\(item)\(ShellColor.neutral.rawValue)"
]
arguments.forEach { command.append($0) }
try? Task.run("printf", command+"\n")
_ = try? Bash("printf", command+"\n").run()
}

public func logBack(_ prefix: Any = "", item: Any, indentationLevel: Int = 0) -> String {
Expand Down