rustisan 0.0.1

Command-line interface for the Rustisan web framework
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
630
//! Make commands for generating application components
//!
//! This module handles all the `rustisan make:*` commands for generating
//! controllers, models, migrations, and other application components.

use anyhow::Result;
use colored::*;

use super::CommandUtils;
use crate::generators::{TemplateManager, GeneratorOptions};
use crate::MakeCommands;

/// Handle make commands
pub async fn handle(component: MakeCommands) -> Result<()> {
    match component {
        MakeCommands::Controller { name, resource, api, model } => {
            make_controller(name, resource, api, model).await
        }
        MakeCommands::Model { name, migration, factory, seeder } => {
            make_model(name, migration, factory, seeder).await
        }
        MakeCommands::Migration { name, create, table } => {
            make_migration(name, create, table).await
        }
        MakeCommands::Middleware { name } => {
            make_middleware(name).await
        }
        MakeCommands::Request { name } => {
            make_request(name).await
        }
        MakeCommands::Resource { name, collection } => {
            make_resource(name, collection).await
        }
        MakeCommands::Seeder { name, model } => {
            make_seeder(name, model).await
        }
        MakeCommands::Factory { name, model } => {
            make_factory(name, model).await
        }
        MakeCommands::Command { name } => {
            make_command(name).await
        }
        MakeCommands::Job { name, sync } => {
            make_job(name, sync).await
        }
        MakeCommands::Event { name } => {
            make_event(name).await
        }
        MakeCommands::Listener { name, event } => {
            make_listener(name, event).await
        }
        MakeCommands::Policy { name, model } => {
            make_policy(name, model).await
        }
        MakeCommands::Trait { name } => {
            make_trait(name).await
        }
        MakeCommands::Test { name, unit, integration } => {
            make_test(name, unit, integration).await
        }
    }
}

/// Generate a controller
async fn make_controller(name: String, resource: bool, api: bool, model: Option<String>) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating controller {}...", name.cyan().bold()));

    // TODO: Implement controller generation
    let class_name = CommandUtils::to_pascal_case(&name);

    // Create template manager
    let template_manager = TemplateManager::new()?;

    // Determine template based on options
    let template_name = if api {
        "controller_api"
    } else if resource {
        "controller_resource"
    } else {
        "controller"
    };

    // Generate template variables
    let template_vars = serde_json::json!({
        "name": name,
        "snake_case": CommandUtils::to_snake_case(&name),
        "pascal_case": CommandUtils::to_pascal_case(&name),
        "resource": resource,
        "api": api,
        "model": model
    });

    // Render template
    let content = template_manager.render(template_name, &template_vars)?;

    // Write file
    let file_path = std::path::Path::new("src/controllers")
        .join(format!("{}.rs", CommandUtils::to_snake_case(&name)));

    CommandUtils::ensure_directory(file_path.parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    // Update mod.rs
    update_module_file("src/controllers", &name)?;

    CommandUtils::success(&format!("Controller {} created successfully!", name.cyan().bold()));

    if resource {
        CommandUtils::info("Resource controller created with methods: index, show, store, update, destroy");
    }

    Ok(())
}

/// Generate a model
async fn make_model(name: String, migration: bool, factory: bool, seeder: bool) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating model {}...", name.cyan().bold()));

    // TODO: Implement model generation

    CommandUtils::success(&format!("Model {} created successfully!", name.cyan().bold()));

    // Generate additional components if requested
    if migration {
        make_migration(
            format!("create_{}_table", pluralize(&CommandUtils::to_snake_case(&name))),
            Some(pluralize(&CommandUtils::to_snake_case(&name))),
            None
        ).await?;
    }

    if factory {
        make_factory(format!("{}Factory", name), Some(name.clone())).await?;
    }

    if seeder {
        make_seeder(format!("{}Seeder", name), Some(name)).await?;
    }

    Ok(())
}

/// Generate a migration
async fn make_migration(name: String, create: Option<String>, table: Option<String>) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating migration {}...", name.cyan().bold()));

    // Generate timestamp
    let timestamp = chrono::Utc::now().format("%Y_%m_%d_%H%M%S");
    let migration_name = format!("{}_{}", timestamp, CommandUtils::to_snake_case(&name));
    let class_name = CommandUtils::to_pascal_case(&name);

    // Generate basic migration content
    let content = format!(
        r#"//! Migration: {}
//! Generated by Rustisan CLI

use rustisan_core::{{Migration, Schema}};
use rustisan_core::database::{{Blueprint, Column}};
use anyhow::Result;

pub struct {} {{}}

impl Migration for {} {{
    fn up(&self, schema: &mut Schema) -> Result<()> {{
        // Add your migration logic here
        Ok(())
    }}

    fn down(&self, schema: &mut Schema) -> Result<()> {{
        // Add your rollback logic here
        Ok(())
    }}
}}
"#,
        name, class_name, class_name
    );

    // Write to file
    let file_path = format!("database/migrations/{}.rs", migration_name);
    CommandUtils::ensure_directory(&std::path::Path::new(&file_path).parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Migration created: {}", file_path));

    Ok(())
}

/// Generate middleware
async fn make_middleware(name: String) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating middleware {}...", name.cyan().bold()));

    // TODO: Implement middleware generation
    CommandUtils::success(&format!("Middleware {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate a request validator
async fn make_request(name: String) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating request {}...", name.cyan().bold()));

    // TODO: Implement request generation
    CommandUtils::success(&format!("Request {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate a resource transformer
async fn make_resource(name: String, collection: bool) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating resource {}...", name.cyan().bold()));

    // TODO: Implement resource generation

    let class_name = CommandUtils::to_pascal_case(&name);
    let snake_case = CommandUtils::to_snake_case(&name);

    let content = if collection {
        format!(
            r#"//! {} Resource Collection

use serde::{{Deserialize, Serialize}};

#[derive(Debug, Serialize, Deserialize)]
pub struct {}Collection {{
    pub data: Vec<{}Resource>,
}}

impl {}Collection {{
    pub fn new(data: Vec<{}Resource>) -> Self {{
        Self {{ data }}
    }}
}}

#[derive(Debug, Serialize, Deserialize)]
pub struct {}Resource {{
    // Add your resource fields here
}}
"#,
            name, class_name, class_name, class_name, class_name, class_name
        )
    } else {
        format!(
            r#"//! {} Resource

use serde::{{Deserialize, Serialize}};

#[derive(Debug, Serialize, Deserialize)]
pub struct {}Resource {{
    // Add your resource fields here
}}

impl {}Resource {{
    pub fn new() -> Self {{
        Self {{
            // Initialize fields
        }}
    }}
}}
"#,
            name, class_name, class_name
        )
    };

    let file_path = format!("src/resources/{}.rs", snake_case);
    CommandUtils::ensure_directory(&std::path::Path::new(&file_path).parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Resource {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate seeder
async fn make_seeder(name: String, model: Option<String>) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating seeder {}...", name.cyan().bold()));

    let class_name = CommandUtils::to_pascal_case(&name);
    let snake_case = CommandUtils::to_snake_case(&name);
    let model_name = model.unwrap_or_else(|| name.clone());

    let content = format!(
        r#"//! {} Seeder

use anyhow::Result;

pub struct {}Seeder {{}}

impl {}Seeder {{
    pub async fn run() -> Result<()> {{
        // Add your seeding logic here
        // Example: Create {} records
        println!("Seeding {} data...");

        Ok(())
    }}
}}
"#,
        name, class_name, class_name, model_name, model_name
    );

    let file_path = format!("database/seeders/{}.rs", snake_case);
    CommandUtils::ensure_directory(&std::path::Path::new(&file_path).parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Seeder {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate factory
async fn make_factory(name: String, model: Option<String>) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating factory {}...", name.cyan().bold()));

    let class_name = CommandUtils::to_pascal_case(&name);
    let snake_case = CommandUtils::to_snake_case(&name);
    let model_name = model.unwrap_or_else(|| name.clone());

    let content = format!(
        r#"//! {} Factory

use anyhow::Result;
use fake::{{Fake, Faker}};

pub struct {}Factory {{}}

impl {}Factory {{
    pub fn create() -> {} {{
        // Add factory logic here using fake data
        // Example factory implementation
        {} {{
            // Generate fake data
        }}
    }}

    pub fn create_many(count: usize) -> Vec<{}> {{
        (0..count).map(|_| Self::create()).collect()
    }}
}}
"#,
        name, class_name, class_name, model_name, model_name, model_name
    );

    let file_path = format!("database/factories/{}.rs", snake_case);
    CommandUtils::ensure_directory(&std::path::Path::new(&file_path).parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Factory {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate command
async fn make_command(name: String) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating command {}...", name.cyan().bold()));

    let class_name = CommandUtils::to_pascal_case(&name);
    let snake_case = CommandUtils::to_snake_case(&name);

    let content = format!(
        r#"//! {} Command

use anyhow::Result;
use clap::Parser;

#[derive(Parser)]
pub struct {}Command {{
    /// Add command arguments here
}}

impl {}Command {{
    pub async fn execute(self) -> Result<()> {{
        // Add command logic here
        println!("Executing {} command...");

        Ok(())
    }}
}}
"#,
        name, class_name, class_name, name
    );

    let file_path = format!("src/commands/{}.rs", snake_case);
    CommandUtils::ensure_directory(&std::path::Path::new(&file_path).parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Command {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate job
async fn make_job(name: String, sync: bool) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating job {}...", name.cyan().bold()));

    let class_name = CommandUtils::to_pascal_case(&name);
    let snake_case = CommandUtils::to_snake_case(&name);

    let content = if sync {
        format!(
            r#"//! {} Synchronous Job

use anyhow::Result;
use serde::{{Deserialize, Serialize}};

#[derive(Debug, Serialize, Deserialize)]
pub struct {}Job {{
    // Add job data fields here
}}

impl {}Job {{
    pub fn new() -> Self {{
        Self {{
            // Initialize fields
        }}
    }}

    pub fn handle(&self) -> Result<()> {{
        // Add synchronous job logic here
        println!("Processing {} job synchronously...");

        Ok(())
    }}
}}
"#,
            name, class_name, class_name, name
        )
    } else {
        format!(
            r#"//! {} Asynchronous Job

use anyhow::Result;
use serde::{{Deserialize, Serialize}};

#[derive(Debug, Serialize, Deserialize)]
pub struct {}Job {{
    // Add job data fields here
}}

impl {}Job {{
    pub fn new() -> Self {{
        Self {{
            // Initialize fields
        }}
    }}

    pub async fn handle(&self) -> Result<()> {{
        // Add asynchronous job logic here
        println!("Processing {} job asynchronously...");

        Ok(())
    }}
}}
"#,
            name, class_name, class_name, name
        )
    };

    let file_path = format!("src/jobs/{}.rs", snake_case);
    CommandUtils::ensure_directory(&std::path::Path::new(&file_path).parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Job {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate event
async fn make_event(name: String) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating event {}...", name.cyan().bold()));

    // TODO: Implement event generation
    CommandUtils::success(&format!("Event {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate a listener
async fn make_listener(name: String, event: Option<String>) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating listener {}...", name.cyan().bold()));

    // TODO: Implement listener generation
    CommandUtils::success(&format!("Listener {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate a policy
async fn make_policy(name: String, model: Option<String>) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating policy {}...", name.cyan().bold()));

    // TODO: Implement policy generation
    CommandUtils::success(&format!("Policy {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate a trait
async fn make_trait(name: String) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating trait {}...", name.cyan().bold()));

    let template_vars = serde_json::json!({
        "name": name,
        "snake_case": CommandUtils::to_snake_case(&name),
        "pascal_case": CommandUtils::to_pascal_case(&name)
    });

    let content = format!(
        r#"//! {} trait
//!
//! This trait defines the interface for {}.

use async_trait::async_trait;
use rustisan_core::Result;

/// {} trait
#[async_trait]
pub trait {} {{
    /// Implementation required
    async fn handle(&self) -> Result<()>;
}}
"#,
        name,
        CommandUtils::to_snake_case(&name),
        name,
        CommandUtils::to_pascal_case(&name)
    );

    let file_path = std::path::Path::new("src/traits")
        .join(format!("{}.rs", CommandUtils::to_snake_case(&name)));

    CommandUtils::ensure_directory(file_path.parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Trait {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Generate a test
async fn make_test(name: String, unit: bool, integration: bool) -> Result<()> {
    CommandUtils::ensure_rustisan_project()?;

    CommandUtils::info(&format!("Creating test {}...", name.cyan().bold()));

    // TODO: Implement test generation

    // Create template manager
    let template_manager = TemplateManager::new()?;

    let template_name = if integration {
        "test_integration"
    } else {
        "test_unit"
    };

    let test_dir = if integration {
        "tests/integration"
    } else {
        "tests/unit"
    };

    let template_vars = serde_json::json!({
        "name": name,
        "snake_case": CommandUtils::to_snake_case(&name),
        "pascal_case": CommandUtils::to_pascal_case(&name),
        "unit": unit,
        "integration": integration
    });

    let content = template_manager.render(template_name, &template_vars)?;

    let file_path = std::path::Path::new(test_dir)
        .join(format!("{}.rs", CommandUtils::to_snake_case(&name)));

    CommandUtils::ensure_directory(file_path.parent().unwrap())?;
    std::fs::write(&file_path, content)?;

    CommandUtils::success(&format!("Test {} created successfully!", name.cyan().bold()));

    Ok(())
}

/// Update module file to include new component
fn update_module_file(module_dir: &str, component_name: &str) -> Result<()> {
    // TODO: Implement module file updates
    Ok(())
}

/// Simple pluralization function
fn pluralize(word: &str) -> String {
    if word.ends_with('y') && word.len() > 1 {
        format!("{}ies", &word[..word.len() - 1])
    } else if word.ends_with('s') || word.ends_with("sh") || word.ends_with("ch") || word.ends_with('x') || word.ends_with('z') {
        format!("{}es", word)
    } else if word.ends_with('f') {
        format!("{}ves", &word[..word.len() - 1])
    } else if word.ends_with("fe") {
        format!("{}ves", &word[..word.len() - 2])
    } else {
        format!("{}s", word)
    }
}