Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 27, 2024
1 parent 558b1b3 commit 426e75b
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 375 deletions.
42 changes: 20 additions & 22 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
{
"object": {
"pins": [
{
"package": "Aperture",
"repositoryURL": "https://github.com/wulkano/Aperture",
"state": {
"branch": null,
"revision": "ddfb0fc1b3c789339dd5fd9296ba8076d292611c",
"version": "2.0.1"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "82905286cc3f0fa8adc4674bf49437cab65a8373",
"version": "1.1.1"
}
"pins" : [
{
"identity" : "aperture",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wulkano/Aperture",
"state" : {
"revision" : "ddfb0fc1b3c789339dd5fd9296ba8076d292611c",
"version" : "2.0.1"
}
]
},
"version": 1
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "46989693916f56d1186bd59ac15124caef896560",
"version" : "1.3.1"
}
}
],
"version" : 2
}
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.9
import PackageDescription

let package = Package(
Expand All @@ -16,7 +16,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/wulkano/Aperture", from: "2.0.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.1.1")
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.1")
],
targets: [
.executableTarget(
Expand Down
294 changes: 147 additions & 147 deletions Sources/ApertureCLI/ApertureCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,170 +3,170 @@ import Aperture
import ArgumentParser

enum OutEvent: String, CaseIterable, ExpressibleByArgument {
case onStart
case onFileReady
case onPause
case onResume
case onFinish
case onStart
case onFileReady
case onPause
case onResume
case onFinish
}

enum InEvent: String, CaseIterable, ExpressibleByArgument {
case pause
case resume
case isPaused
case onPause
case pause
case resume
case isPaused
case onPause
}

extension CaseIterable {
static var asCommaSeparatedList: String {
allCases.map { "\($0)" }.joined(separator: ", ")
}
static var asCommaSeparatedList: String {
allCases.map { "\($0)" }.joined(separator: ", ")
}
}

@main
struct ApertureCLI: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "aperture",
subcommands: [
List.self,
Record.self,
Events.self
]
)
static let configuration = CommandConfiguration(
commandName: "aperture",
subcommands: [
List.self,
Record.self,
Events.self
]
)
}

extension ApertureCLI {
struct List: ParsableCommand {
static let configuration = CommandConfiguration(
subcommands: [
Screens.self,
AudioDevices.self
]
)
}

struct Record: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Start a recording with the given options.")

@Option(name: .shortAndLong, help: "The ID to use for this process")
var processId = "main"

@Argument(help: "Stringified JSON object with options passed to Aperture")
var options: String

mutating func run() throws {
try record(options, processId: processId)
}
}

struct Events: ParsableCommand {
static let configuration = CommandConfiguration(
subcommands: [
Send.self,
Listen.self,
ListenAll.self
]
)
}
struct List: ParsableCommand {
static let configuration = CommandConfiguration(
subcommands: [
Screens.self,
AudioDevices.self
]
)
}

struct Record: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Start a recording with the given options.")

@Option(name: .shortAndLong, help: "The ID to use for this process")
var processId = "main"

@Argument(help: "Stringified JSON object with options passed to Aperture")
var options: String

mutating func run() throws {
try record(options, processId: processId)
}
}

struct Events: ParsableCommand {
static let configuration = CommandConfiguration(
subcommands: [
Send.self,
Listen.self,
ListenAll.self
]
)
}
}

extension ApertureCLI.List {
struct Screens: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "List available screens.")

mutating func run() throws {
// Uses stderr because of unrelated stuff being outputted on stdout.
print(try toJson(Aperture.Devices.screen().map { ["id": $0.id, "name": $0.name] }), to: .standardError)
}
}

struct AudioDevices: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "List available audio devices.")

mutating func run() throws {
// Uses stderr because of unrelated stuff being outputted on stdout.
print(try toJson(Aperture.Devices.audio().map { ["id": $0.id, "name": $0.name] }), to: .standardError)
}
}
struct Screens: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "List available screens.")

mutating func run() throws {
// Uses stderr because of unrelated stuff being outputted on stdout.
print(try toJson(Aperture.Devices.screen().map { ["id": $0.id, "name": $0.name] }), to: .standardError)
}
}

struct AudioDevices: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "List available audio devices.")

mutating func run() throws {
// Uses stderr because of unrelated stuff being outputted on stdout.
print(try toJson(Aperture.Devices.audio().map { ["id": $0.id, "name": $0.name] }), to: .standardError)
}
}
}

extension ApertureCLI.Events {
struct Send: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Send an event to the given process.")

@Flag(inversion: .prefixedNo, help: "Wait for event to be received")
var wait = true

@Option(name: .shortAndLong, help: "The ID of the target process")
var processId = "main"

@Argument(help: "Name of the event to send. Can be one of:\n\(InEvent.asCommaSeparatedList)")
var event: InEvent

@Argument(help: "Data to pass to the event")
var data: String?

mutating func run() {
ApertureEvents.sendEvent(processId: processId, event: event.rawValue, data: data) { notification in
if let data = notification.data {
print(data)
}

Foundation.exit(0)
}

if wait {
RunLoop.main.run()
}
}
}

struct Listen: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Listen to an outcoming event for the given process.")

@Flag(help: "Exit after receiving the event once")
var exit = false

@Option(name: .shortAndLong, help: "The ID of the target process")
var processId = "main"

@Argument(help: "Name of the event to listen for. Can be one of:\n\(OutEvent.asCommaSeparatedList)")
var event: OutEvent

func run() {
_ = ApertureEvents.answerEvent(processId: processId, event: event.rawValue) { notification in
if let data = notification.data {
print(data)
}

if exit {
notification.answer()
Foundation.exit(0)
}
}

RunLoop.main.run()
}
}

struct ListenAll: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Listen to all outcoming events for the given process.")

@Option(name: .shortAndLong, help: "The ID of the target process")
var processId = "main"

func run() {
for event in OutEvent.allCases {
_ = ApertureEvents.answerEvent(processId: processId, event: event.rawValue) { notification in
if let data = notification.data {
print("\(event) \(data)")
} else {
print(event)
}
}
}

RunLoop.main.run()
}
}
struct Send: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Send an event to the given process.")

@Flag(inversion: .prefixedNo, help: "Wait for event to be received")
var wait = true

@Option(name: .shortAndLong, help: "The ID of the target process")
var processId = "main"

@Argument(help: "Name of the event to send. Can be one of:\n\(InEvent.asCommaSeparatedList)")
var event: InEvent

@Argument(help: "Data to pass to the event")
var data: String?

mutating func run() {
ApertureEvents.sendEvent(processId: processId, event: event.rawValue, data: data) { notification in
if let data = notification.data {
print(data)
}

Foundation.exit(0)
}

if wait {
RunLoop.main.run()
}
}
}

struct Listen: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Listen to an outcoming event for the given process.")

@Flag(help: "Exit after receiving the event once")
var exit = false

@Option(name: .shortAndLong, help: "The ID of the target process")
var processId = "main"

@Argument(help: "Name of the event to listen for. Can be one of:\n\(OutEvent.asCommaSeparatedList)")
var event: OutEvent

func run() {
_ = ApertureEvents.answerEvent(processId: processId, event: event.rawValue) { notification in
if let data = notification.data {
print(data)
}

if exit {
notification.answer()
Foundation.exit(0)
}
}

RunLoop.main.run()
}
}

struct ListenAll: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Listen to all outcoming events for the given process.")

@Option(name: .shortAndLong, help: "The ID of the target process")
var processId = "main"

func run() {
for event in OutEvent.allCases {
_ = ApertureEvents.answerEvent(processId: processId, event: event.rawValue) { notification in
if let data = notification.data {
print("\(event) \(data)")
} else {
print(event)
}
}
}

RunLoop.main.run()
}
}
}
Loading

0 comments on commit 426e75b

Please sign in to comment.