Skip to content

Commit 5b01299

Browse files
committed
initial commit
1 parent 80c6566 commit 5b01299

5 files changed

Lines changed: 81 additions & 25 deletions

File tree

.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

Lines changed: 0 additions & 8 deletions
This file was deleted.

Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@ import PackageDescription
66
let package = Package(
77
name: "CodeEditCLI",
88
products: [
9-
// Products define the executables and libraries a package produces, and make them visible to other packages.
10-
.library(
11-
name: "CodeEditCLI",
12-
targets: ["CodeEditCLI"]),
9+
.executable(name: "codeedit-cli", targets: ["CodeEditCLI"])
1310
],
1411
dependencies: [
15-
// Dependencies declare other packages that this package depends on.
16-
// .package(url: /* package url */, from: "1.0.0"),
12+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0")
1713
],
1814
targets: [
1915
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2016
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21-
.target(
17+
.executableTarget(
2218
name: "CodeEditCLI",
23-
dependencies: []),
24-
.testTarget(
25-
name: "CodeEditCLITests",
26-
dependencies: ["CodeEditCLI"]),
19+
dependencies: [
20+
.product(name: "ArgumentParser", package: "swift-argument-parser")
21+
]),
2722
]
2823
)

Sources/CodeEditCLI/CodeEditCLI.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sources/CodeEditCLI/main.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// main.swift
3+
// CodeEditCLI
4+
//
5+
// Created by Lukas Pistrol on 06.12.22.
6+
//
7+
8+
import ArgumentParser
9+
import Foundation
10+
11+
struct CodeEditCLI: ParsableCommand {
12+
static let configuration = CommandConfiguration(
13+
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()
20+
)
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?
28+
29+
init() {}
30+
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+
56+
enum CLIError: Error {
57+
case invalidWorkingDirectory
58+
}
59+
}
60+
61+
CodeEditCLI.main()

0 commit comments

Comments
 (0)