-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgramCli.cs
More file actions
91 lines (81 loc) · 3.52 KB
/
ProgramCli.cs
File metadata and controls
91 lines (81 loc) · 3.52 KB
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
using System.Diagnostics;
using System.Text;
using CodeContext.Configuration;
using CodeContext.Services;
using CodeContext.Utils;
namespace CodeContext;
/// <summary>
/// CLI mode implementation for CodeContext.
/// </summary>
public static class ProgramCli
{
public static void RunCli(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
try
{
// Initialize dependencies using functional composition
var console = new ConsoleWriter();
var configLoader = new ConfigLoader(console);
var pathResolver = new PathResolver(console);
var filterConfig = new FilterConfiguration();
var statsCalculator = new StatsCalculator();
// Load configuration
var config = configLoader.Load();
// Get and validate input path
var defaultInputPath = args.FirstOrDefault() ?? config.DefaultInputPath;
console.Write($"Enter the path to index (default: {defaultInputPath}): ");
var projectPath = pathResolver.GetInputPath(defaultInputPath);
// Load GitIgnore patterns for the project (I/O boundary clearly defined)
var gitIgnoreParser = GitHelper.FindRepositoryRoot(projectPath) switch
{
null => GitIgnoreParser.Empty,
var gitRoot => GitIgnoreParser.FromFile(Path.Combine(gitRoot, ".gitignore"))
};
// Initialize file checker with immutable GitIgnore parser
var fileChecker = new FileFilterService(filterConfig, gitIgnoreParser);
var scanner = new ProjectScanner(fileChecker, console);
var contentBuilder = new ContentBuilder(scanner);
var outputFormatter = new OutputFormatter(console);
// Determine output path
var folderName = PathResolver.GetFolderName(projectPath);
var defaultFileName = $"{folderName}_{config.DefaultOutputFileName}";
var defaultOutputPath = Path.Combine(projectPath, defaultFileName);
var outputArg = args.ElementAtOrDefault(1);
console.Write($"Enter output file/directory (default: {defaultOutputPath}): ");
var outputPath = pathResolver.GetOutputPath(outputArg, defaultOutputPath);
// Build content
var stopwatch = Stopwatch.StartNew();
var content = contentBuilder.Build(projectPath, config);
var stats = statsCalculator.Calculate(projectPath, content, stopwatch.Elapsed);
// Write output
var actualOutputPath = outputFormatter.WriteToFile(outputPath, content, config.OutputFormat, defaultFileName);
console.WriteLine($"\n✅ Output written to {actualOutputPath}");
console.WriteLine(stats);
}
catch (DirectoryNotFoundException ex)
{
Console.WriteLine($"❌ Directory Error: {ex.Message}");
Environment.Exit(1);
}
catch (IOException ex)
{
Console.WriteLine($"❌ I/O Error: {ex.Message}");
Environment.Exit(2);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine($"❌ Access Denied: {ex.Message}");
Environment.Exit(3);
}
catch (Exception ex)
{
Console.WriteLine($"❌ Unexpected Error: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($" Details: {ex.InnerException.Message}");
}
Environment.Exit(4);
}
}
}