ggen-domain 5.1.3

Domain logic layer for ggen - pure business logic without CLI dependencies
Documentation
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
//! Production-grade pack installer with marketplace integration
//!
//! This module provides real package installation by integrating with
//! the marketplace domain layer and ggen-core infrastructure.

use crate::marketplace;
use crate::packs::dependency_graph::DependencyGraph;
use crate::packs::repository::{FileSystemRepository, PackRepository};
use crate::packs::types::Pack;
use ggen_utils::error::{Error, Result};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Instant;
use tracing::{error, info, warn};

/// Pack installer with dependency resolution and conflict detection
pub struct PackInstaller {
    repository: Box<dyn PackRepository>,
}

impl PackInstaller {
    /// Create new installer with custom repository
    pub fn new(repository: Box<dyn PackRepository>) -> Self {
        Self { repository }
    }

    /// Create installer with default filesystem repository
    pub fn with_default_repo() -> Result<Self> {
        let repo = FileSystemRepository::discover()?;
        Ok(Self::new(Box::new(repo)))
    }

    /// Install a pack with full dependency resolution
    ///
    /// # Features
    /// - Resolves dependencies recursively
    /// - Detects circular dependencies
    /// - Topological sort for correct install order
    /// - Conflict detection between packs
    /// - Rollback on failure (when force=false)
    ///
    /// # Arguments
    /// * `pack_id` - ID of the pack to install
    /// * `options` - Installation options
    pub async fn install(&self, pack_id: &str, options: &InstallOptions) -> Result<InstallReport> {
        let start = Instant::now();

        info!("Starting pack installation: {}", pack_id);

        // Load pack metadata
        let pack = self.repository.load(pack_id).await?;

        info!("Loaded pack: {} v{}", pack.name, pack.version);

        // For dry-run, return early without resolving dependencies or reaching marketplace
        if options.dry_run {
            let duration = start.elapsed();
            let install_order = vec![pack_id.to_string()];
            return Ok(InstallReport {
                pack_id: pack_id.to_string(),
                pack_name: pack.name,
                pack_version: pack.version,
                packages_installed: pack.packages.clone(),
                templates_available: pack.templates.iter().map(|t| t.name.clone()).collect(),
                install_path: options
                    .target_dir
                    .clone()
                    .unwrap_or_else(|| PathBuf::from(".")),
                dependencies_resolved: vec![pack_id.to_string()],
                install_order,
                conflicts: vec![],
                duration,
                success: true,
            });
        }

        // Resolve dependencies (only for real installation)
        let all_packs = if options.skip_dependencies {
            vec![pack.clone()]
        } else {
            self.resolve_dependencies(&pack).await?
        };

        info!(
            "Resolved {} pack(s) including dependencies",
            all_packs.len()
        );

        // Build dependency graph
        let graph = DependencyGraph::from_packs(&all_packs)?;
        let install_order = graph.topological_sort()?;

        info!("Installation order: {:?}", install_order);

        // Detect conflicts
        let conflicts = self.detect_conflicts(&all_packs);
        if !conflicts.is_empty() {
            warn!("Detected {} conflict(s)", conflicts.len());
            for conflict in &conflicts {
                warn!("  - {}", conflict);
            }

            if !options.force {
                return Err(Error::new(&format!(
                    "Conflicts detected. Use --force to override:\n{}",
                    conflicts.join("\n")
                )));
            }
        }

        // Determine install path
        let install_path = options.target_dir.clone().unwrap_or_else(|| {
            dirs::home_dir()
                .unwrap_or_else(|| PathBuf::from("."))
                .join(".ggen")
                .join("packs")
        });

        // Create install directory
        tokio::fs::create_dir_all(&install_path).await?;

        // Install packages in topological order
        let mut packages_installed = Vec::new();
        let mut failed_packages = Vec::new();

        for pack_id_to_install in &install_order {
            let pack_to_install = all_packs
                .iter()
                .find(|p| p.id == *pack_id_to_install)
                .unwrap();

            info!("Installing packages from pack: {}", pack_to_install.name);

            for package_name in &pack_to_install.packages {
                match self
                    .install_package(package_name, &install_path, options)
                    .await
                {
                    Ok(_) => {
                        packages_installed.push(package_name.clone());
                        info!("✓ Installed package: {}", package_name);
                    }
                    Err(e) => {
                        error!("✗ Failed to install package {}: {}", package_name, e);
                        failed_packages.push(package_name.clone());

                        if !options.force {
                            return Err(Error::new(&format!(
                                "Failed to install package '{}': {}",
                                package_name, e
                            )));
                        }
                    }
                }
            }
        }

        let duration = start.elapsed();
        let success = failed_packages.is_empty();

        if success {
            info!(
                "✓ Pack installation completed successfully in {:?}",
                duration
            );
        } else {
            warn!(
                "⚠ Pack installation completed with {} failures in {:?}",
                failed_packages.len(),
                duration
            );
        }

        Ok(InstallReport {
            pack_id: pack_id.to_string(),
            pack_name: pack.name,
            pack_version: pack.version,
            packages_installed,
            templates_available: pack.templates.iter().map(|t| t.name.clone()).collect(),
            install_path,
            dependencies_resolved: all_packs.iter().map(|p| p.id.clone()).collect(),
            install_order,
            conflicts,
            duration,
            success,
        })
    }

    /// Resolve pack dependencies recursively
    async fn resolve_dependencies(&self, pack: &Pack) -> Result<Vec<Pack>> {
        let mut resolved = Vec::new();
        let mut visited = std::collections::HashSet::new();
        let mut queue = std::collections::VecDeque::new();

        queue.push_back(pack.clone());

        while let Some(current) = queue.pop_front() {
            if visited.contains(&current.id) {
                continue;
            }

            visited.insert(current.id.clone());
            resolved.push(current.clone());

            // Add dependencies to queue
            for dep in &current.dependencies {
                if !dep.optional && !visited.contains(&dep.pack_id) {
                    match self.repository.load(&dep.pack_id).await {
                        Ok(dep_pack) => {
                            queue.push_back(dep_pack);
                        }
                        Err(e) => {
                            return Err(Error::new(&format!(
                                "Failed to load dependency '{}': {}",
                                dep.pack_id, e
                            )));
                        }
                    }
                }
            }
        }

        Ok(resolved)
    }

    /// Detect conflicts between packs
    ///
    /// Conflicts occur when multiple packs provide the same package
    fn detect_conflicts(&self, packs: &[Pack]) -> Vec<String> {
        let mut conflicts = Vec::new();
        let mut package_sources: HashMap<String, Vec<String>> = HashMap::new();

        for pack in packs {
            for package in &pack.packages {
                package_sources
                    .entry(package.clone())
                    .or_insert_with(Vec::new)
                    .push(pack.id.clone());
            }
        }

        for (package, sources) in package_sources {
            if sources.len() > 1 {
                conflicts.push(format!(
                    "Package '{}' provided by multiple packs: {}",
                    package,
                    sources.join(", ")
                ));
            }
        }

        conflicts
    }

    /// Install a single package using marketplace domain functions
    async fn install_package(
        &self, package_name: &str, target_dir: &PathBuf, options: &InstallOptions,
    ) -> Result<()> {
        // Parse package name and version
        let (name, version) = if let Some(idx) = package_name.find('@') {
            let (n, v) = package_name.split_at(idx);
            (n.to_string(), Some(v[1..].to_string()))
        } else {
            (package_name.to_string(), None)
        };

        // Build marketplace install input
        // Format: package@version or just package
        let package_spec = if let Some(ver) = version {
            format!("{}@{}", name, ver)
        } else {
            name
        };

        let marketplace_input = marketplace::InstallInput {
            package: package_spec,
            target: Some(target_dir.display().to_string()),
            force: options.force,
            no_dependencies: options.skip_dependencies,
            dry_run: false, // We handle dry-run at pack level
        };

        // Call marketplace domain function
        marketplace::execute_install(marketplace_input).await?;

        Ok(())
    }
}

/// Installation options
#[derive(Debug, Clone)]
pub struct InstallOptions {
    /// Target directory for installation
    pub target_dir: Option<PathBuf>,
    /// Force installation even if conflicts exist
    pub force: bool,
    /// Dry run mode - don't actually install
    pub dry_run: bool,
    /// Skip dependency resolution
    pub skip_dependencies: bool,
}

impl Default for InstallOptions {
    fn default() -> Self {
        Self {
            target_dir: None,
            force: false,
            dry_run: false,
            skip_dependencies: false,
        }
    }
}

/// Installation report
#[derive(Debug, Clone)]
pub struct InstallReport {
    /// Pack ID that was installed
    pub pack_id: String,
    /// Pack name
    pub pack_name: String,
    /// Pack version
    pub pack_version: String,
    /// List of packages successfully installed
    pub packages_installed: Vec<String>,
    /// List of templates available in the pack
    pub templates_available: Vec<String>,
    /// Installation path
    pub install_path: PathBuf,
    /// Dependencies that were resolved
    pub dependencies_resolved: Vec<String>,
    /// Installation order (topologically sorted)
    pub install_order: Vec<String>,
    /// Conflicts detected (if any)
    pub conflicts: Vec<String>,
    /// Time taken for installation
    pub duration: std::time::Duration,
    /// Whether installation was successful
    pub success: bool,
}

impl InstallReport {
    /// Get summary string
    pub fn summary(&self) -> String {
        format!(
            "Installed pack '{}' v{} with {} packages in {:?}",
            self.pack_name,
            self.pack_version,
            self.packages_installed.len(),
            self.duration
        )
    }

    /// Get detailed report
    pub fn detailed_report(&self) -> String {
        let mut report = Vec::new();

        report.push(format!("Pack Installation Report"));
        report.push(format!("========================"));
        report.push(format!("Pack: {} v{}", self.pack_name, self.pack_version));
        report.push(format!("Pack ID: {}", self.pack_id));
        report.push(format!("Install Path: {}", self.install_path.display()));
        report.push(format!("Duration: {:?}", self.duration));
        report.push(format!(
            "Status: {}",
            if self.success { "SUCCESS" } else { "PARTIAL" }
        ));
        report.push(String::new());

        report.push(format!(
            "Packages Installed: {}",
            self.packages_installed.len()
        ));
        for pkg in &self.packages_installed {
            report.push(format!("{}", pkg));
        }
        report.push(String::new());

        report.push(format!(
            "Templates Available: {}",
            self.templates_available.len()
        ));
        for tmpl in &self.templates_available {
            report.push(format!("{}", tmpl));
        }
        report.push(String::new());

        if !self.dependencies_resolved.is_empty() {
            report.push(format!(
                "Dependencies Resolved: {}",
                self.dependencies_resolved.len()
            ));
            for dep in &self.dependencies_resolved {
                report.push(format!("{}", dep));
            }
            report.push(String::new());
        }

        if !self.conflicts.is_empty() {
            report.push(format!("⚠ Conflicts Detected: {}", self.conflicts.len()));
            for conflict in &self.conflicts {
                report.push(format!("  ! {}", conflict));
            }
        }

        report.join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_installer_with_default_repo() {
        // Just test creation doesn't panic
        let _result = PackInstaller::with_default_repo();
    }

    #[test]
    fn test_install_options_default() {
        let opts = InstallOptions::default();
        assert!(!opts.force);
        assert!(!opts.dry_run);
        assert!(!opts.skip_dependencies);
        assert!(opts.target_dir.is_none());
    }

    #[test]
    fn test_install_report_summary() {
        let report = InstallReport {
            pack_id: "test-pack".to_string(),
            pack_name: "Test Pack".to_string(),
            pack_version: "1.0.0".to_string(),
            packages_installed: vec!["pkg1".to_string(), "pkg2".to_string()],
            templates_available: vec![],
            install_path: PathBuf::from("/tmp/test"),
            dependencies_resolved: vec![],
            install_order: vec![],
            conflicts: vec![],
            duration: std::time::Duration::from_millis(100),
            success: true,
        };

        let summary = report.summary();
        assert!(summary.contains("Test Pack"));
        assert!(summary.contains("1.0.0"));
        assert!(summary.contains("2 packages"));
    }

    #[test]
    fn test_install_report_detailed() {
        let report = InstallReport {
            pack_id: "test-pack".to_string(),
            pack_name: "Test Pack".to_string(),
            pack_version: "1.0.0".to_string(),
            packages_installed: vec!["pkg1".to_string()],
            templates_available: vec!["template1".to_string()],
            install_path: PathBuf::from("/tmp/test"),
            dependencies_resolved: vec!["dep1".to_string()],
            install_order: vec![],
            conflicts: vec!["conflict1".to_string()],
            duration: std::time::Duration::from_millis(100),
            success: true,
        };

        let detailed = report.detailed_report();
        assert!(detailed.contains("Pack Installation Report"));
        assert!(detailed.contains("Test Pack"));
        assert!(detailed.contains("pkg1"));
        assert!(detailed.contains("template1"));
        assert!(detailed.contains("dep1"));
        assert!(detailed.contains("conflict1"));
    }
}