r2x 0.1.14

The r2x-cli plugin manager for model interoperability.
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
use clap::{Parser, Subcommand};
use r2x::commands::{
    cache,
    config::{self, ConfigAction, PythonAction},
    init,
    log::{self, LogAction},
    plugins, read, run, self_update, venv,
};
use r2x::common::GlobalOpts;
use r2x_config as config_manager;
use r2x_logger as logger;
use r2x_python::python_bridge::process_exit;
use std::ffi::OsString;

#[derive(Parser)]
#[command(name = "r2x")]
#[command(version = env!("CARGO_PKG_VERSION"))]
#[command(
    about = "model interoperability CLI",
    long_about = "r2x-cli is the command-line interface for model interoperability."
)]
struct Cli {
    #[command(flatten)]
    global: GlobalOpts,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Configure r2x-cli
    #[command(subcommand_required = false, arg_required_else_help = false)]
    Config {
        /// Output in JSON format
        #[arg(long)]
        json: bool,
        #[command(subcommand)]
        action: Option<ConfigAction>,
    },
    /// Python runtime management
    Python {
        #[command(subcommand)]
        action: PythonAction,
    },
    /// Logging configuration
    #[command(subcommand_required = false, arg_required_else_help = false)]
    Log {
        /// Output in JSON format
        #[arg(long)]
        json: bool,
        #[command(subcommand)]
        action: Option<LogAction>,
    },
    /// List installed plugins
    List {
        /// Optional plugin name to filter by (e.g., r2x-reeds)
        plugin: Option<String>,
        /// Optional module/function name to filter by (e.g., break_gens)
        module: Option<String>,
        /// Output in JSON format
        #[arg(long)]
        json: bool,
    },
    /// Install a plugin
    Install {
        plugin: Option<String>,
        /// Install in editable mode (-e)
        #[arg(short, long)]
        editable: bool,
        /// Force re-discovery of plugins (ignore cached metadata)
        #[arg(long)]
        no_cache: bool,
        /// Git host (default: github.com). Use with gh:owner/repo or full URLs.
        #[arg(long)]
        host: Option<String>,
        /// Install from a git branch
        #[arg(long, conflicts_with_all = ["tag", "commit"])]
        branch: Option<String>,
        /// Install from a git tag
        #[arg(long, conflicts_with_all = ["branch", "commit"])]
        tag: Option<String>,
        /// Install from a git commit hash
        #[arg(long, conflicts_with_all = ["branch", "tag"])]
        commit: Option<String>,
        /// Install a package from a repository subdirectory
        #[arg(long)]
        subdirectory: Option<String>,
    },
    /// Remove a plugin
    Remove { plugin: String },
    /// Sync plugin manifest (re-run plugin discovery for all installed packages)
    Sync {
        /// Upgrade installed plugin packages before syncing metadata
        #[arg(long)]
        upgrade: bool,
    },
    /// Clean plugins and cache (removes installed plugins and cleans cache folder)
    Clean {
        /// Skip confirmation prompt
        #[arg(short = 'y', long)]
        yes: bool,
    },
    /// Virtual environment management
    Venv {
        #[command(subcommand)]
        command: venv::VenvCommand,
    },
    /// Cache management
    Cache {
        #[command(subcommand)]
        command: cache::CacheCommand,
    },
    /// Initialize a new pipeline file
    Init {
        /// Optional filename for the pipeline (default: pipeline.yaml)
        file: Option<String>,
    },

    /// Run pipelines or plugins
    Run(run::RunCommand),
    /// Read a system from JSON (stdin or file) and open an interactive IPython session
    Read(read::ReadCommand),
    /// Manage the r2x executable
    Self_(self_update::SelfNamespace),
}

/// Move root global flags ahead of `run` before Clap parses its trailing target.
///
/// `RunCommand` captures the target and all following values verbatim so it
/// can distinguish a pipeline path from a plugin reference. Without this
/// normalization, root-global flags written after `run` would be mistaken for
/// plugin or pipeline arguments and would miss logger initialization.
fn normalize_run_global_args(args: Vec<OsString>) -> Vec<OsString> {
    let Some(command_index) = args.iter().enumerate().skip(1).find_map(|(index, arg)| {
        let arg = arg.to_string_lossy();
        (!is_global_run_option(&arg) && arg != "--").then_some(index)
    }) else {
        return args;
    };

    if args[command_index].to_string_lossy() != "run" {
        return args;
    }

    let mut normalized = Vec::with_capacity(args.len());
    normalized.extend(args[..command_index].iter().cloned());

    let mut run_args = Vec::new();
    let mut moved_globals = Vec::new();
    let mut parse_options = true;
    for arg in args[command_index + 1..].iter().cloned() {
        let arg_text = arg.to_string_lossy();
        if parse_options && arg_text == "--" {
            parse_options = false;
            run_args.push(arg);
        } else if parse_options && is_global_run_option(&arg_text) {
            moved_globals.push(arg);
        } else {
            run_args.push(arg);
        }
    }

    normalized.extend(moved_globals);
    normalized.push(OsString::from("run"));
    normalized.extend(run_args);
    normalized
}

fn is_global_run_option(arg: &str) -> bool {
    matches!(
        arg,
        "--quiet" | "--verbose" | "--log-python" | "--python-log" | "--no-stdout"
    ) || is_global_short_flag(arg)
}

fn is_global_short_flag(arg: &str) -> bool {
    arg.starts_with('-')
        && !arg.starts_with("--")
        && arg.len() > 1
        && arg[1..]
            .chars()
            .all(|character| matches!(character, 'q' | 'v'))
}

fn with_plugin_context<F>(action: F) -> Result<(), r2x::plugins::error::PluginError>
where
    F: FnOnce(&mut plugins::context::PluginContext) -> Result<(), r2x::plugins::error::PluginError>,
{
    let mut ctx = plugins::context::PluginContext::load()?;
    action(&mut ctx)
}

fn exit_on_plugin_error(result: Result<(), r2x::plugins::error::PluginError>) {
    if let Err(e) = result {
        logger::error(&e.to_string());
        std::process::exit(1);
    }
}

fn requires_python_environment(command: &Commands) -> bool {
    matches!(
        command,
        Commands::List { .. }
            | Commands::Install { .. }
            | Commands::Remove { .. }
            | Commands::Sync { .. }
            | Commands::Clean { .. }
            | Commands::Run(_)
            | Commands::Read(_)
    )
}

fn main() {
    // Respect NO_COLOR and TERM=dumb for accessibility and automation
    if std::env::var_os("NO_COLOR").is_some()
        || std::env::var("TERM").ok().as_deref() == Some("dumb")
    {
        colored::control::set_override(false);
    }

    let mut args = std::env::args_os().collect::<Vec<_>>();
    if let Some(program) = args.first_mut() {
        *program = OsString::from("r2x");
    }
    let cli = Cli::parse_from(normalize_run_global_args(args));

    let mut startup_config = match config_manager::Config::load() {
        Ok(cfg) => Some(cfg),
        Err(e) => {
            eprintln!("Warning: Failed to load config: {}", e);
            None
        }
    };

    let (saved_log_python, saved_no_stdout, saved_log_path, saved_log_max_size) =
        match startup_config.as_ref() {
            Some(cfg) => (
                cfg.log_python.unwrap_or(false),
                cfg.no_stdout.unwrap_or(false),
                cfg.log_path.as_deref(),
                cfg.log_max_size,
            ),
            None => (false, false, None, None),
        };
    let effective_log_python = cli.global.log_python || saved_log_python;
    let effective_no_stdout = cli.global.no_stdout || saved_no_stdout;

    // Initialize logger with verbosity level, log_python flag, and no_stdout flag
    if let Err(e) = logger::init_with_config(
        cli.global.verbosity_level(),
        cli.global.quiet,
        effective_log_python,
        effective_no_stdout,
        saved_log_path,
        saved_log_max_size,
    ) {
        eprintln!("Warning: Failed to initialize logger: {}", e);
    }

    if requires_python_environment(&cli.command) {
        if let Some(cfg) = startup_config.as_mut() {
            if let Err(e) = cfg.ensure_uv_path().and_then(|_| cfg.ensure_cache_path()) {
                logger::warn(&format!("Failed to setup CLI: {}", e));
            }
        }
    }

    match cli.command {
        Commands::Config { json, action } => {
            config::handle_config(action, json, cli.global);
        }
        Commands::Python { action } => {
            config::handle_python(action, cli.global);
        }
        Commands::Log { json, action } => {
            log::handle_log(action, json);
        }
        Commands::List {
            plugin,
            module,
            json,
        } => {
            exit_on_plugin_error(with_plugin_context(|ctx| {
                plugins::list::list_plugins(&cli.global, plugin, module, json, ctx)
            }));
        }
        Commands::Install {
            plugin,
            editable,
            no_cache,
            host,
            branch,
            tag,
            commit,
            subdirectory,
        } => match plugin {
            Some(pkg) => {
                exit_on_plugin_error(with_plugin_context(|ctx| {
                    plugins::install::install_plugin(
                        &pkg,
                        editable,
                        no_cache,
                        plugins::install::GitOptions {
                            host,
                            branch,
                            tag,
                            commit,
                            subdirectory,
                        },
                        ctx,
                    )
                }));
            }
            None => {
                if let Err(e) = plugins::install::show_install_help() {
                    logger::error(&e.to_string());
                    std::process::exit(1);
                }
            }
        },
        Commands::Remove { plugin } => {
            exit_on_plugin_error(with_plugin_context(|ctx| {
                plugins::remove::remove_plugin(&plugin, ctx)
            }));
        }
        Commands::Sync { upgrade } => {
            exit_on_plugin_error(with_plugin_context(|ctx| {
                plugins::sync::sync_manifest(ctx, upgrade)
            }));
        }
        Commands::Clean { yes } => {
            exit_on_plugin_error(with_plugin_context(|ctx| {
                plugins::clean::clean_manifest(yes, ctx)
            }));
        }
        Commands::Venv { command } => {
            venv::handle_venv(command, cli.global);
        }
        Commands::Cache { command } => {
            cache::handle_cache(command, cli.global);
        }
        Commands::Init { file } => {
            init::handle_init(file, cli.global);
        }

        Commands::Run(cmd) => {
            if let Err(e) = run::handle_run(cmd, cli.global) {
                logger::error(&format!("Run command failed: {}", e));
                std::process::exit(1);
            }
            // Re-acquire the GIL on the main thread before exit so that
            // PyO3's atexit handler can call PyEval_SaveThread() without
            // crashing. See python_bridge::process_exit for details.
            process_exit(0);
        }
        Commands::Read(cmd) => {
            if let Err(e) = read::handle_read(cmd, cli.global) {
                logger::error(&format!("Read command failed: {}", e));
                std::process::exit(1);
            }
        }
        Commands::Self_(args) => match self_update::handle_self_command(args) {
            Ok(code) => std::process::exit(code),
            Err(e) => {
                logger::error(&e.to_string());
                std::process::exit(1);
            }
        },
    }
}

#[cfg(test)]
mod tests {
    use super::normalize_run_global_args;
    use std::ffi::OsString;

    fn args(values: &[&str]) -> Vec<OsString> {
        values.iter().map(OsString::from).collect()
    }

    #[test]
    fn moves_global_options_before_run_for_clap() {
        assert_eq!(
            normalize_run_global_args(args(&[
                "r2x",
                "run",
                "r2x-reeds.reeds-parser",
                "-qv",
                "--python-log",
                "--no-stdout",
            ])),
            args(&[
                "r2x",
                "-qv",
                "--python-log",
                "--no-stdout",
                "run",
                "r2x-reeds.reeds-parser",
            ])
        );
    }

    #[test]
    fn keeps_options_after_double_dash_with_the_plugin() {
        assert_eq!(
            normalize_run_global_args(args(&[
                "r2x",
                "run",
                "r2x-reeds.reeds-parser",
                "--",
                "--quiet",
            ])),
            args(&["r2x", "run", "r2x-reeds.reeds-parser", "--", "--quiet",])
        );
    }

    #[test]
    fn leaves_non_run_commands_unchanged() {
        let input = args(&["r2x", "install", "--no-stdout", "r2x-reeds"]);
        assert_eq!(normalize_run_global_args(input.clone()), input);
    }
}