Skip to content

Commit c87b7b9

Browse files
authored
[feat]: add commands (#13)
- open: to open a file/folder or the app - version: get version numbers for cli and app ## Usage ```sh # open CodeEdit.app codeedit-cli # open specific file codeedit-cli index.html # open file with line number and optionally column number codeedit-cli index.html -l 43 codeedit-cli index.html -line 43 codeedit-cli index.html -l 43 -c 4 codeedit-cli index.html -line 43 -column 4 # show version information codeedit-cli version ``` ### Output of `version` ``` CodeEdit.app: 1.0 CodeEditCLI: 0.0.3 ``` ## Screenshot <img width="597" alt="Help section" src="https://user-images.githubusercontent.com/9460130/205972530-8ca6db6c-f50f-4353-8652-ccd54544b7b0.png">
2 parents 56ddd86 + 3eb077e commit c87b7b9

File tree

8 files changed

+182
-49
lines changed

8 files changed

+182
-49
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: Build
22
on:
33
workflow_dispatch:
44
workflow_call:
5+
push:
6+
branches:
7+
- main
58
pull_request:
69
branches:
710
- main
@@ -15,4 +18,4 @@ jobs:
1518
uses: actions/checkout@v2
1619

1720
- name: Building
18-
run: swift build -c release --arch arm64 --arch x86_64
21+
run: swift build -c release --arch arm64 --arch x86_64

.github/workflows/swiftlint.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: SwiftLint
2+
on:
3+
workflow_call:
4+
push:
5+
branches:
6+
- 'main'
7+
pull_request:
8+
branches:
9+
- 'main'
10+
jobs:
11+
SwiftLint:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v1
15+
- name: GitHub Action for SwiftLint with --strict
16+
uses: norio-nomura/[email protected]
17+
with:
18+
args: --strict

.swiftlint.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
identifier_name:
3+
allowed_symbols: ['_']
4+
5+
excluded:
6+
- .build
7+
- .git
8+
- .swiftpm

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ let package = Package(
1616
name: "CodeEditCLI",
1717
dependencies: [
1818
.product(name: "ArgumentParser", package: "swift-argument-parser")
19-
]),
19+
])
2020
]
2121
)

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@
1414

1515
`codeedit` is a set of command line tools that ship with CodeEdit which allow users to open and interact with editor via the command line.
1616

17+
## Installation
18+
19+
### Build locally
20+
21+
```sh
22+
swift build -c release --arch arm64 --arch x86_64
23+
sudo cp -f .build/apple/Products/Release/codeedit-cli /usr/local/bin/codeedit-cli
24+
```
25+
26+
> Note that you must have `CodeEdit` installed in a `Release` configuration. A `Debug` build of `CodeEdit` will not work.
27+
1728
## Documentation
1829

19-
### `open` (not available yet)
30+
### `open`
2031

2132
Opens CodeEdit.
2233

@@ -44,18 +55,12 @@ From an optional line and column
4455
codeedit index.html -l 50 -c 50
4556
```
4657

47-
### `--version` (not available yet)
58+
### `version`
4859

4960
Outputs the version of CodeEdit and CodeEdit CLI Tools.
5061

5162
```sh
52-
codeedit --version
53-
```
54-
55-
or
56-
57-
```sh
58-
codeedit -v
63+
codeedit version
5964
```
6065

6166
### `new-window` (not available yet)

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)