Skip to content

Commit 4b59203

Browse files
committed
add commands
- open: to open a file/folder or the app - version: get version numbers for cli and app
1 parent 56ddd86 commit 4b59203

File tree

3 files changed

+137
-38
lines changed

3 files changed

+137
-38
lines changed

Sources/CodeEditCLI/Open.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Open.swift
3+
// CodeEditCLI
4+
//
5+
// Created by Lukas Pistrol on 06.12.22.
6+
//
7+
8+
import ArgumentParser
9+
import Foundation
10+
11+
extension CodeEditCLI {
12+
struct Open: ParsableCommand {
13+
static var configuration = CommandConfiguration(
14+
commandName: "open",
15+
abstract: "A command-line tool to open files/folders in CodeEdit.app."
16+
)
17+
18+
@Argument(
19+
help: "The path of a file/folder to open.",
20+
completion: .file()
21+
)
22+
private var path: String?
23+
24+
@Option(name: .shortAndLong, help: "The line number to open a file at. Optional.")
25+
private var line: Int?
26+
27+
@Option(name: .shortAndLong, help: "The column to open a file at. Optional.")
28+
private var column: Int?
29+
30+
func run() throws {
31+
let task = Process()
32+
33+
// use the `open` cli as the executable
34+
task.launchPath = "/usr/bin/open"
35+
36+
if let path {
37+
38+
let openURL = try absolutePath(path, for: task)
39+
40+
// open CodeEdit using the url scheme
41+
if let line, !openURL.hasDirectoryPath {
42+
task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"]
43+
} else {
44+
task.arguments = ["-u", "codeedit://\(openURL.path)"]
45+
}
46+
} else {
47+
task.arguments = ["-a", "CodeEdit.app"]
48+
}
49+
50+
try task.run()
51+
}
52+
53+
private func absolutePath(_ path: String, for task: Process) throws -> URL {
54+
guard let workingDirectory = task.currentDirectoryURL,
55+
let url = URL(string: path, relativeTo: workingDirectory) else {
56+
throw CLIError.invalidWorkingDirectory
57+
}
58+
return url
59+
}
60+
}
61+
}

Sources/CodeEditCLI/Version.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Version.swift
3+
// CodeEditCLI
4+
//
5+
// Created by Lukas Pistrol on 06.12.22.
6+
//
7+
8+
import ArgumentParser
9+
import Foundation
10+
11+
extension CodeEditCLI {
12+
struct Version: ParsableCommand {
13+
static var configuration = CommandConfiguration(
14+
commandName: "version",
15+
abstract: "Prints the version of the CLI and CodeEdit.app."
16+
)
17+
18+
func run() throws {
19+
// Run an apple script to find CodeEdit.app
20+
let pathData = try codeEditURLData()
21+
22+
// Check if there is an Info.plist inside CodeEdit.app
23+
// Then get the version number and print it out
24+
//
25+
// This will fail when CodeEdit.app is not installed
26+
if let url = infoPlistUrl(pathData: pathData),
27+
let plist = NSDictionary(contentsOf: url) as? [String: Any],
28+
let version = plist["CFBundleShortVersionString"] as? String {
29+
print("CodeEdit.app: \t\(version)")
30+
} else {
31+
print("CodeEdit.app is not installed.")
32+
}
33+
34+
// Print the cli version
35+
print("CodeEditCLI: \t\(CLI_VERSION)")
36+
}
37+
38+
private func codeEditURLData() throws -> Data {
39+
let task = Process()
40+
let pipe = Pipe()
41+
task.standardOutput = pipe
42+
task.launchPath = "/usr/bin/osascript"
43+
44+
task.arguments = ["-e"]
45+
task.arguments?.append("POSIX path of (path to application \"CodeEdit\")")
46+
47+
try task.run()
48+
49+
return pipe.fileHandleForReading.readDataToEndOfFile()
50+
}
51+
52+
private func infoPlistUrl(pathData: Data) -> URL? {
53+
if let path = String(data: pathData, encoding: .utf8) {
54+
let url = URL(fileURLWithPath: path.trimmingCharacters(in: .whitespacesAndNewlines))
55+
.appendingPathComponent("Contents")
56+
.appendingPathComponent("Info.plist")
57+
return url
58+
} else {
59+
return nil
60+
}
61+
}
62+
}
63+
}

Sources/CodeEditCLI/main.swift

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,26 @@
88
import ArgumentParser
99
import Foundation
1010

11+
// ##################################################
12+
// This needs to be changed prior to every release!
13+
// ##################################################
14+
let CLI_VERSION = "0.0.3"
15+
1116
struct CodeEditCLI: ParsableCommand {
1217
static let configuration = CommandConfiguration(
1318
commandName: "codeedit-cli",
14-
abstract: "A command-line tool to open files/folders in CodeEdit.app."
15-
)
16-
17-
@Argument(
18-
help: "The path of a file/folder to open.",
19-
completion: .file()
19+
abstract: """
20+
A set of command line tools that ship with CodeEdit
21+
which allow users to open and interact with editor via the command line.
22+
23+
Version: \(CLI_VERSION)
24+
""",
25+
subcommands: [Open.self, Version.self],
26+
defaultSubcommand: Open.self
2027
)
21-
private var path: String
22-
23-
@Option(name: .shortAndLong, help: "The line number to open a file at. Optional.")
24-
private var line: Int?
25-
26-
@Option(name: .shortAndLong, help: "The column to open a file at. Optional.")
27-
private var column: Int?
2828

2929
init() {}
3030

31-
func run() throws {
32-
let task = Process()
33-
let openURL = try absolutePath(for: task)
34-
35-
// use the `open` cli as the executable
36-
task.launchPath = "/usr/bin/open"
37-
38-
// open CodeEdit using the url scheme
39-
if let line, !openURL.hasDirectoryPath {
40-
task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"]
41-
} else {
42-
task.arguments = ["-u", "codeedit://\(openURL.path)"]
43-
}
44-
45-
try task.run()
46-
}
47-
48-
private func absolutePath(for task: Process) throws -> URL {
49-
guard let workingDirectory = task.currentDirectoryURL,
50-
let url = URL(string: path, relativeTo: workingDirectory) else {
51-
throw CLIError.invalidWorkingDirectory
52-
}
53-
return url
54-
}
55-
5631
enum CLIError: Error {
5732
case invalidWorkingDirectory
5833
}

0 commit comments

Comments
 (0)