qleany 1.7.3

Architecture generator for Rust and C++/Qt applications.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use crate::app_context::AppContext;
use crate::cli_handlers;
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Parser)]
#[command(author, version)]
#[command(about = "Architecture generator for C++/Qt6 and Rust applications")]
#[command(before_help = concat!("Qleany v", env!("CARGO_PKG_VERSION"), " - made by FernTech"))]
#[command(propagate_version = true)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// Path to qleany.yaml manifest (searches current directory if not specified)
    #[arg(short, long, global = true)]
    pub manifest: Option<PathBuf>,

    /// Enable verbose output
    #[arg(short, long, global = true)]
    pub verbose: bool,

    /// Suppress non-error output
    #[arg(short, long, global = true)]
    pub quiet: bool,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Create a new qleany.yaml manifest
    New(NewArgs),

    /// Validate the manifest without generating files
    Check(CheckArgs),

    /// List files that would be generated
    List(ListArgs),

    /// Generate scaffolding code
    #[command(visible_alias = "gen")]
    Generate(GenerateArgs),

    /// Generate a project from a demo manifest
    Demo(DemoArgs),

    /// Display manifest information
    Show(ShowArgs),

    /// Export entity diagram
    Export(ExportArgs),

    /// Embedded documentation
    Docs(DocsArgs),

    /// Upgrade manifest to the latest schema version
    Upgrade,

    /// LLM Prompt
    Prompt(PromptArgs),

    /// Show unified diff between generated and on-disk file
    Diff(DiffArgs),

    /// Run Slint GUI to edit manifests
    Gui,
}

// ─────────────────────────────────────────────────────────────
// NEW
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct NewArgs {
    /// Directory where qleany.yaml will be created
    #[arg(default_value = ".")]
    pub path: PathBuf,

    /// Target language for the project
    #[arg(short, long, value_enum)]
    pub language: Option<LanguageOption>,

    /// Application name (PascalCase, e.g. MyApp)
    #[arg(short, long)]
    pub name: Option<String>,

    /// Organisation name (e.g. FernTech)
    #[arg(long)]
    pub org_name: Option<String>,

    /// Manifest template to use
    #[arg(short, long, value_enum)]
    pub template: Option<ManifestTemplateOption>,

    /// UI options: rust_cli, rust_slint, cpp_qt_qtquick, cpp_qt_qtwidgets, rust_ios, rust_android
    #[arg(short, long)]
    pub options: Vec<String>,

    /// Overwrite existing manifest without prompting
    #[arg(long)]
    pub force: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum ManifestTemplateOption {
    Blank,
    Minimal,
    DocumentEditor,
    DataManagement,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum LanguageOption {
    Rust,
    #[value(alias = "cpp-qt")]
    CppQt,
}

// ─────────────────────────────────────────────────────────────
// DEMO
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct DemoArgs {
    /// Directory where qleany.yaml will be created
    #[arg(default_value = ".")]
    pub path: PathBuf,

    /// Target Rust language for the demo project
    #[arg(long)]
    pub rust: bool,

    /// Target C++ Qt language for the demo project
    #[arg(long)]
    pub cpp_qt: bool,

    /// force overwrite the existing qleany-demo folder
    #[arg(long)]
    pub force: bool,
}

// ─────────────────────────────────────────────────────────────
// CHECK
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct CheckArgs {
    /// List all checked rules instead of running validation
    #[arg(long)]
    pub rules: bool,
}

// ─────────────────────────────────────────────────────────────
// LIST
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct ListArgs {
    /// What to list: files, entities, features, groups [default: files]
    #[arg(value_enum, default_value = "files")]
    pub target: ListTarget,

    /// Show all files (all statuses + all natures)
    #[arg(long)]
    pub all: bool,

    // Status filters (default: Modified + New)
    /// Include modified files
    #[arg(long, short = 'M')]
    pub modified: bool,

    /// Include new files
    #[arg(long, short = 'N')]
    pub new: bool,

    /// Include unchanged files
    #[arg(long, short = 'U')]
    pub unchanged: bool,

    /// Include all statuses (modified + new + unchanged)
    #[arg(long)]
    pub all_status: bool,

    // Nature filters (default: all natures)
    /// Include infrastructure files
    #[arg(long, short = 'i')]
    pub infra: bool,

    /// Include aggregate files
    #[arg(long, short = 'g')]
    pub aggregates: bool,

    /// Include scaffold files
    #[arg(long, short = 's')]
    pub scaffolds: bool,

    /// Include all natures (infrastructure + aggregate + scaffold)
    #[arg(long)]
    pub all_natures: bool,

    /// Plain text output without colors
    #[arg(long)]
    pub text: bool,

    /// Output format
    #[arg(short, long, value_enum, default_value = "plain")]
    pub format: OutputFormat,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum ListTarget {
    /// List all generated files (default)
    Files,

    /// List entities defined in manifest
    Entities,

    /// List features and their use cases
    Features,

    /// List file groups (for selective generation)
    Groups,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
    Plain,
    Json,
    Tree,
}

// ─────────────────────────────────────────────────────────────
// GENERATE
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct GenerateArgs {
    /// What to generate: all, feature, entity, file, group [default: all]
    #[arg(value_enum, default_value = "all")]
    pub target: GenerateTarget,

    /// Target name(s) — feature name, entity name, file path/ID, or group name
    #[arg(value_name = "NAME")]
    pub target_names: Vec<String>,

    /// Output directory (defaults to manifest's prefix_path)
    #[arg(short, long)]
    pub output: Option<PathBuf>,

    /// Generate to ./temp/ subdirectory (safe for comparison)
    #[arg(long)]
    pub temp: bool,

    /// Show what would be generated without writing files
    #[arg(long)]
    pub dry_run: bool,

    /// Write all files (all statuses + all natures)
    #[arg(long)]
    pub all: bool,

    // Status filters (default: Modified + New)
    /// Include modified files
    #[arg(long, short = 'M')]
    pub modified: bool,

    /// Include new files
    #[arg(long, short = 'N')]
    pub new: bool,

    /// Include unchanged files
    #[arg(long, short = 'U')]
    pub unchanged: bool,

    /// Include all statuses (modified + new + unchanged)
    #[arg(long)]
    pub all_status: bool,

    // Nature filters (default: all natures)
    /// Include infrastructure files
    #[arg(long, short = 'i')]
    pub infra: bool,

    /// Include aggregate files
    #[arg(long, short = 'g')]
    pub aggregates: bool,

    /// Include scaffold files
    #[arg(long, short = 's')]
    pub scaffolds: bool,

    /// Include all natures (infrastructure + aggregate + scaffold)
    #[arg(long)]
    pub all_natures: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum GenerateTarget {
    /// Generate all files (default)
    All,

    /// Generate files for a specific feature
    Feature,

    /// Generate entity-related files
    Entity,

    /// Generate specific file(s) by path or ID
    File,

    /// Generate files matching a group
    Group,
}

// ─────────────────────────────────────────────────────────────
// SHOW
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct ShowArgs {
    /// What to display
    #[command(subcommand)]
    pub target: Option<ShowTarget>,

    /// Output format
    #[arg(short, long, value_enum, default_value = "plain")]
    pub format: OutputFormat,
}

#[derive(Subcommand)]
pub enum ShowTarget {
    /// Show full manifest (default)
    Manifest,

    /// Show project configuration (global section)
    Config,

    /// Show details for a specific entity
    Entity { name: String },

    /// Show details for a specific feature
    Feature { name: String },
}

// ─────────────────────────────────────────────────────────────
// EXPORT
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct ExportArgs {
    /// Export format
    #[command(subcommand)]
    pub format: ExportFormat,

    /// Output file (stdout if not specified)
    #[arg(short, long)]
    pub output: Option<PathBuf>,
}

#[derive(Subcommand)]
pub enum ExportFormat {
    /// Export entity relationships as Mermaid diagram
    Mermaid,

    /// Export manifest as JSON
    Json,
}

// ─────────────────────────────────────────────────────────────
// DOC
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct DocsArgs {
    /// Which documentation to show
    #[command(subcommand)]
    pub target: Option<DocsTarget>,

    /// Output raw Markdown instead of terminal-formatted text
    #[arg(long, global = true)]
    pub md: bool,
}

#[derive(Subcommand, Clone)]
pub enum DocsTarget {
    /// Show all documentations
    All,

    /// Show introduction documentation
    #[command(visible_alias = "intro")]
    Introduction,

    /// Show manifest reference documentation
    #[command(visible_alias = "manifest")]
    ManifestReference,

    /// Show architecture design documentation
    #[command(visible_alias = "design")]
    DesignPhilosophy,

    /// Show how operations flow documentation
    #[command(visible_alias = "flow")]
    HowOperationsFlow,

    /// Show undo/redo architecture documentation
    #[command(visible_alias = "undo")]
    UndoRedoArchitecture,

    /// Show generated code documentation for C++/Qt
    #[command(visible_alias = "cpp")]
    GeneratedCodeCppQt,

    /// Show generated code documentation for Rust
    #[command(visible_alias = "rust")]
    GeneratedCodeRust,

    /// Show API reference for C++/Qt
    #[command(visible_alias = "api-cpp")]
    ApiReferenceCppQt,

    /// Show API reference for Rust
    #[command(visible_alias = "api-rust")]
    ApiReferenceRust,

    /// Show quick start guide for C++/Qt
    #[command(visible_alias = "start-cpp")]
    QuickStartCppQt,

    /// Show quick start guide for Rust
    #[command(visible_alias = "start-rust")]
    QuickStartRust,

    /// Show QML integration documentation
    #[command(visible_alias = "qml")]
    QmlIntegration,

    /// Show migration guide documentation
    #[command(visible_alias = "mig")]
    MigrationGuide,

    /// Show troubleshooting documentation
    #[command(visible_alias = "trouble")]
    Troubleshooting,

    /// Show regeneration workflow documentation
    #[command(visible_alias = "regen")]
    RegenerationWorkflow,

    /// Show mobile bridge development documentation
    #[command(visible_alias = "mobile")]
    MobileBridgeDevelopment,
}

// ─────────────────────────────────────────────────────────────
// Prompt
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct PromptArgs {
    /// List all use cases grouped by feature
    #[arg(short, long)]
    pub list: bool,

    /// Generate a project context
    #[arg(short, long)]
    pub context: bool,

    /// Generate a prompt for a specific use case. Write "feature:use_case".
    #[arg(short, long)]
    pub use_case: Option<String>,
}

// ─────────────────────────────────────────────────────────────
// DIFF
// ─────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct DiffArgs {
    /// File path (relative to output) or numeric file ID from `list files`
    pub target: String,
}

/// Run the CLI with the given application context.
/// Returns `Some(())` if the application should continue running as GUI, `None` otherwise.
pub fn run_cli(app_context: &Arc<AppContext>) -> Option<()> {
    let cli = Cli::parse();

    // No command provided → launch GUI
    let command = cli.command;

    let command = match command {
        Some(command) => command,
        None => return Some(()),
    };

    // Resolve manifest path for commands that need it
    let manifest_path = resolve_manifest_path(&cli.manifest, &command);

    // Create output context for consistent messaging
    let output = OutputContext {
        verbose: cli.verbose,
        quiet: cli.quiet,
    };

    let result = match command {
        Commands::New(args) => cli_handlers::new::execute(app_context, &args, &output),
        Commands::Check(args) => {
            if args.rules {
                cli_handlers::check::list_rules(&output);
                return None;
            }
            let path = manifest_path.expect("Check requires a manifest");
            cli_handlers::check::execute(app_context, &path, &output)
        }
        Commands::List(args) => {
            let path = manifest_path.expect("List requires a manifest");
            cli_handlers::list::execute(app_context, &path, &args, &output)
        }
        Commands::Generate(args) => {
            let path = manifest_path.expect("Generate requires a manifest");
            cli_handlers::generate::execute(app_context, &path, &args, &output)
        }
        Commands::Show(args) => {
            let path = manifest_path.expect("Show requires a manifest");
            cli_handlers::show::execute(app_context, &path, &args, &output)
        }
        Commands::Export(args) => {
            let path = manifest_path.expect("Export requires a manifest");
            cli_handlers::export::execute(app_context, &path, &args, &output)
        }
        Commands::Docs(args) => cli_handlers::docs::execute(app_context, &args, &output),
        Commands::Upgrade => {
            let path = manifest_path.expect("Upgrade requires a manifest");
            cli_handlers::upgrade::execute(app_context, &path, &output)
        }
        Commands::Prompt(args) => {
            let path = manifest_path.expect("Prompt requires a manifest");
            cli_handlers::prompt::execute(app_context, &path, &args, &output)
        }
        Commands::Diff(args) => {
            let path = manifest_path.expect("Diff requires a manifest");
            cli_handlers::diff::execute(app_context, &path, &args, &output)
        }
        Commands::Demo(args) => cli_handlers::demo::execute(app_context, &args, &output),
        Commands::Gui => return Some(()),
    };

    if let Err(e) = result {
        if !output.quiet {
            eprintln!("Error: {}", e);
        }
        std::process::exit(1);
    }

    None
}

/// Resolves the manifest path from CLI arguments or discovers it in the current directory.
fn resolve_manifest_path(explicit: &Option<PathBuf>, command: &Commands) -> Option<PathBuf> {
    // New, Demo, and Doc commands don't need an existing manifest
    if matches!(
        command,
        Commands::New(_) | Commands::Demo(_) | Commands::Docs(_)
    ) {
        return None;
    }

    // Use explicit path if provided
    if let Some(path) = explicit {
        if path.is_file() {
            return Some(path.clone());
        }
        if path.is_dir() {
            let manifest = path.join("qleany.yaml");
            if manifest.exists() {
                return Some(manifest);
            }
        }
        eprintln!("Manifest not found: {}", path.display());
        std::process::exit(1);
    }

    // Search current directory
    let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let candidates = ["qleany.yaml", "qleany.yml"];

    for candidate in candidates {
        let path = current_dir.join(candidate);
        if path.exists() {
            return Some(path);
        }
    }

    eprintln!("No qleany.yaml found in current directory. Use --manifest to specify location.");
    std::process::exit(1);
}

/// Context for controlling CLI output behavior.
#[derive(Clone, Copy)]
pub struct OutputContext {
    pub verbose: bool,
    pub quiet: bool,
}

impl OutputContext {
    pub fn info(&self, msg: &str) {
        if !self.quiet {
            println!("{}", msg);
        }
    }

    pub fn verbose(&self, msg: &str) {
        if self.verbose && !self.quiet {
            println!("{}", msg);
        }
    }

    pub fn success(&self, msg: &str) {
        if !self.quiet {
            println!("{}", msg);
        }
    }

    pub fn warn(&self, msg: &str) {
        if !self.quiet {
            eprintln!("{}", msg);
        }
    }
}