unity-cli 0.16.0

Rust CLI for Unity Editor automation over the Unity TCP protocol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::path::PathBuf;

use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};

#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum OutputFormat {
    #[default]
    Text,
    Json,
}

#[derive(Debug, Parser)]
#[command(
    name = "unity-cli",
    version,
    about = "Rust CLI for Unity Editor automation over Unity TCP protocol",
    arg_required_else_help = true
)]
pub struct Cli {
    #[arg(long, global = true, value_enum, default_value_t = OutputFormat::Text)]
    pub output: OutputFormat,

    #[arg(long, global = true)]
    pub host: Option<String>,

    #[arg(long, global = true)]
    pub port: Option<u16>,

    /// Target the Unity Editor that has this project open (default: the
    /// project containing the current directory). Also read from UNITY_PROJECT_ROOT.
    #[arg(long, global = true, value_name = "PATH")]
    pub project_path: Option<PathBuf>,

    #[arg(long, global = true, value_name = "MS")]
    pub timeout_ms: Option<u64>,

    #[arg(short, long, global = true, action = ArgAction::Count)]
    pub verbose: u8,

    #[arg(long, global = true)]
    pub dry_run: bool,

    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    Raw(RawArgs),
    Editor {
        #[command(subcommand)]
        command: EditorCommand,
    },
    Tool {
        #[command(subcommand)]
        command: ToolCommand,
    },
    System {
        #[command(subcommand)]
        command: SystemCommand,
    },
    Scene {
        #[command(subcommand)]
        command: SceneCommand,
    },
    Instances {
        #[command(subcommand)]
        command: InstancesCommand,
    },
    Lsp {
        #[command(subcommand)]
        command: LspCommand,
    },
    Cli {
        #[command(subcommand)]
        command: CliCommand,
    },
    Lspd {
        #[command(subcommand)]
        command: LspdCommand,
    },
    Unityd {
        #[command(subcommand)]
        command: UnitydCommand,
    },
    Skills {
        #[command(subcommand)]
        command: SkillsCommand,
    },
    Reference {
        #[command(subcommand)]
        command: ReferenceCommand,
    },
    /// Install the bridge into a Unity project and confirm the Editor connection.
    Setup {
        /// Unity project root (default: nearest ancestor with Packages/manifest.json).
        #[arg(long, value_name = "PATH")]
        project_path: Option<PathBuf>,
        /// Launch the project's Unity Editor (Unity Hub install path) when it is not reachable.
        #[arg(long, default_value_t = false)]
        launch_editor: bool,
        /// Seconds to wait for the Editor bridge (default: 0, or 900 with --launch-editor).
        #[arg(long, value_name = "SECS")]
        wait_secs: Option<u64>,
    },
    /// Manage the com.akiojin.unity-cli-bridge package in a Unity project.
    Bridge {
        #[command(subcommand)]
        command: BridgeCommand,
    },
    /// Diagnose why the Unity bridge is unreachable (Safe Mode, missing bridge, port conflict, ...).
    Doctor(DoctorArgs),
    Batch {
        #[arg(long, value_name = "JSON")]
        json: Option<String>,
        #[arg(long)]
        stdin: bool,
    },
}

#[derive(Debug, Args)]
pub struct DoctorArgs {
    /// Unity project root. Defaults to UNITY_PROJECT_ROOT or the nearest Unity project above the current directory.
    #[arg(long, value_name = "DIR")]
    pub project_path: Option<PathBuf>,
}

#[derive(Debug, Subcommand)]
pub enum ToolCommand {
    List(ToolListArgs),
    Schema {
        tool_name: Option<String>,
    },
    Call(RawArgs),
    #[command(external_subcommand)]
    External(Vec<String>),
}

#[derive(Debug, Args, Default)]
pub struct ToolListArgs {
    /// Case-insensitive substring matched against tool names and one-line descriptions.
    #[arg(long, value_name = "TEXT")]
    pub query: Option<String>,

    /// docs/tools.md category heading or slug (e.g. `scenes`, `playback-testing`).
    #[arg(long, value_name = "NAME")]
    pub category: Option<String>,

    /// Emit `{name, description}` entries instead of bare tool names.
    #[arg(long)]
    pub compact: bool,

    /// Maximum number of tools to return after filtering.
    #[arg(long, value_name = "N")]
    pub limit: Option<usize>,

    /// Number of filtered tools to skip before `--limit` applies.
    #[arg(long, value_name = "N", default_value_t = 0)]
    pub offset: usize,
}

#[derive(Debug, Args)]
pub struct RawArgs {
    pub tool_name: String,

    #[arg(long, value_name = "JSON")]
    pub json: Option<String>,

    #[arg(long, value_name = "FILE")]
    pub params_file: Option<PathBuf>,
}

#[derive(Debug, Subcommand)]
pub enum SystemCommand {
    Ping {
        #[arg(long)]
        message: Option<String>,
    },
}

#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum EvalMode {
    #[default]
    Expression,
    Statements,
}

#[derive(Debug, Subcommand)]
pub enum EditorCommand {
    /// Evaluate synchronous C# in the Editor. Timeout does not cancel execution.
    Eval {
        code: String,
        #[arg(long, value_enum, default_value_t = EvalMode::Expression)]
        mode: EvalMode,
        /// Reuse this ID to query the result after a timeout. Never retry with a new ID.
        #[arg(long)]
        request_id: Option<String>,
    },
    /// Retrieve a result from the current Editor domain (unknown after reload).
    EvalStatus { request_id: String },
    /// Show eval counters of the current Editor domain: loaded assemblies, caches and memory.
    EvalStats {
        /// Run a managed garbage collection before reading memory.
        #[arg(long)]
        collect: bool,
    },
}

#[derive(Debug, Subcommand)]
pub enum SceneCommand {
    Create {
        scene_name: String,

        #[arg(long)]
        path: Option<String>,

        #[arg(long, default_value_t = true)]
        load_scene: bool,

        #[arg(long, default_value_t = false)]
        add_to_build_settings: bool,
    },
}

#[derive(Debug, Subcommand)]
pub enum InstancesCommand {
    List {
        #[arg(long, value_name = "CSV")]
        ports: Option<String>,

        #[arg(long, default_value = crate::config::DEFAULT_HOST)]
        host: String,

        #[arg(long, value_name = "MS", default_value_t = 1000)]
        timeout_ms: u64,
    },
    SetActive {
        id: String,

        #[arg(long, value_name = "MS", default_value_t = 1000)]
        timeout_ms: u64,
    },
}

#[derive(Debug, Subcommand)]
pub enum BridgeCommand {
    /// Add the OpenUPM registry and the bridge (CLI version) to Packages/manifest.json. Idempotent.
    Install {
        #[arg(long, value_name = "PATH")]
        project_path: Option<PathBuf>,
    },
    /// Pin the bridge dependency to the CLI version.
    Upgrade {
        #[arg(long, value_name = "PATH")]
        project_path: Option<PathBuf>,
    },
    /// Show the declared / resolved bridge version and the CLI version check.
    Status {
        #[arg(long, value_name = "PATH")]
        project_path: Option<PathBuf>,
    },
}

#[derive(Debug, Subcommand)]
pub enum LspCommand {
    Install,
    Doctor,
}

#[derive(Debug, Subcommand)]
pub enum CliCommand {
    Install {
        #[arg(long, default_value_t = false)]
        force: bool,
    },
    Doctor,
}

#[derive(Debug, Subcommand)]
pub enum LspdCommand {
    Start,
    Stop,
    Status,
    #[command(hide = true)]
    Serve,
}

#[derive(Debug, Subcommand)]
pub enum UnitydCommand {
    Start,
    Stop,
    Status,
    #[command(hide = true)]
    Serve,
}

#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum SkillSeverity {
    #[default]
    Warning,
    Error,
}

#[derive(Debug, Clone, Copy, ValueEnum, Default)]
pub enum SkillFormat {
    #[default]
    Text,
    Json,
}

#[derive(Debug, Subcommand)]
pub enum SkillsCommand {
    /// Validate Claude Code / Codex skill directories against Skill Contract v1.
    Lint {
        /// Skills root (default: auto-detect `.claude-plugin/plugins/unity-cli/skills`).
        #[arg(long, value_name = "PATH")]
        root: Option<PathBuf>,

        /// Output format.
        #[arg(long, value_enum, default_value_t = SkillFormat::Text)]
        format: SkillFormat,

        /// Severity gate; `error` exits non-zero on any violation.
        #[arg(long, value_enum, default_value_t = SkillSeverity::Warning)]
        severity: SkillSeverity,
    },
}

#[derive(Debug, Subcommand)]
pub enum ReferenceCommand {
    /// Fetch UnityCsReference for the active Unity version into the local cache.
    Fetch {
        #[arg(long)]
        version: Option<String>,
        #[arg(long)]
        branch: Option<String>,
        #[arg(long, default_value_t = false)]
        force: bool,
        #[arg(long, default_value_t = false)]
        accept_license: bool,
    },
    /// Show cached UnityCsReference versions and disk usage.
    Status {
        #[arg(long)]
        version: Option<String>,
    },
    /// Search the cached reference source for a pattern (file-level hits).
    Search {
        pattern: String,
        #[arg(long)]
        version: Option<String>,
        #[arg(long)]
        path: Option<String>,
        #[arg(long)]
        max_results: Option<u64>,
        #[arg(long, default_value_t = false)]
        regex: bool,
    },
    /// Grep the cached reference source line-by-line with optional context.
    Grep {
        pattern: String,
        #[arg(long)]
        version: Option<String>,
        #[arg(long)]
        file_glob: Option<String>,
        #[arg(long, default_value_t = 0)]
        context: u32,
    },
    /// View a file from the cached reference source with an optional line range.
    View {
        path: String,
        #[arg(long)]
        version: Option<String>,
        #[arg(long)]
        start_line: Option<u32>,
        #[arg(long)]
        max_lines: Option<u32>,
    },
    /// Find a symbol (class/interface/struct/enum) in the cached reference index.
    FindSymbol {
        #[arg(long)]
        name: String,
        #[arg(long)]
        kind: Option<String>,
        #[arg(long)]
        namespace: Option<String>,
        #[arg(long)]
        version: Option<String>,
    },
    /// Diff a symbol or path range between two cached Unity versions.
    Diff {
        #[arg(long)]
        from: String,
        #[arg(long)]
        to: String,
        #[arg(long)]
        symbol: Option<String>,
        #[arg(long)]
        path: Option<String>,
        #[arg(long)]
        max_symbols: Option<u64>,
    },
    /// Resolve the C# token at a cursor position to candidate reference cache entries.
    ResolveSymbolAt {
        path: String,
        #[arg(long)]
        line: u32,
        #[arg(long)]
        column: u32,
        #[arg(long)]
        version: Option<String>,
    },
    /// Build the embedding index for a cached Unity version (uses fastembed).
    EmbedBuild {
        #[arg(long)]
        version: Option<String>,
    },
    /// Semantic search over the embedding index for a cached Unity version.
    EmbedSearch {
        #[arg(long)]
        query: String,
        #[arg(long)]
        version: Option<String>,
        #[arg(long)]
        top_k: Option<u64>,
    },
    /// Remove old UnityCsReference snapshots, keeping the newest entries.
    Clean {
        #[arg(long, default_value_t = 1)]
        keep: u64,
        #[arg(long)]
        version: Option<String>,
        #[arg(long, default_value_t = false)]
        dry_run: bool,
    },
}