-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
188 lines (164 loc) · 6.34 KB
/
Copy pathProgram.cs
File metadata and controls
188 lines (164 loc) · 6.34 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
public class Config
{
public string name { get; set; }
public string path { get; set; }
public string[] ext { get; set; }
public string[] exclude { get; set; }
}
public class SourceCodeAggregator
{
public static void Main(string[] args)
{
try
{
// config.json 파일 경로 결정
string configPath = "config.json";
if (args.Length > 0)
{
configPath = args[0];
}
if (!File.Exists(configPath))
{
Console.WriteLine($"설정 파일을 찾을 수 없습니다: {configPath}");
return;
}
string configJson = File.ReadAllText(configPath, Encoding.UTF8);
Config config = JsonSerializer.Deserialize<Config>(configJson);
if (config == null)
{
Console.WriteLine($"설정 파일을 파싱할 수 없습니다: {configPath}");
return;
}
// 설정 값 검증
if (string.IsNullOrEmpty(config.path) || !Directory.Exists(config.path))
{
Console.WriteLine($"경로가 존재하지 않습니다: {config.path}");
return;
}
if (config.ext == null || config.ext.Length == 0)
{
Console.WriteLine("확장자가 지정되지 않았습니다.");
return;
}
// 제외할 폴더 목록 정규화
HashSet<string> excludeFolders = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (config.exclude != null)
{
foreach (string exclude in config.exclude)
{
string normalizedExclude = Path.GetFullPath(Path.Combine(config.path, exclude.TrimStart('.', '/', '\\')));
excludeFolders.Add(normalizedExclude);
}
}
// 소스 파일 수집
List<string> sourceFiles = CollectSourceFiles(config.path, config.ext, excludeFolders);
if (sourceFiles.Count == 0)
{
Console.WriteLine("조건에 맞는 파일을 찾을 수 없습니다.");
return;
}
// 결과 파일 생성
string outputPath = string.IsNullOrEmpty(config.name)
? "aggregated_source.txt"
: $"{config.name}.txt";
using (StreamWriter writer = new StreamWriter(outputPath, false, Encoding.UTF8))
{
foreach (string filePath in sourceFiles.OrderBy(f => f))
{
try
{
string fileName = Path.GetFileName(filePath);
string fileContent = File.ReadAllText(filePath, Encoding.UTF8);
writer.WriteLine($"[{fileName}]");
writer.WriteLine("```");
writer.WriteLine(fileContent);
writer.WriteLine("```");
writer.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine($"파일 읽기 실패: {filePath} - {ex.Message}");
}
}
}
Console.WriteLine($"작업 완료! {sourceFiles.Count}개 파일이 {outputPath}에 저장되었습니다.");
}
catch (Exception ex)
{
Console.WriteLine($"오류 발생: {ex.Message}");
}
}
private static List<string> CollectSourceFiles(string rootPath, string[] extensions, HashSet<string> excludeFolders)
{
List<string> sourceFiles = new List<string>();
try
{
CollectSourceFilesRecursive(rootPath, extensions, excludeFolders, sourceFiles);
}
catch (Exception ex)
{
Console.WriteLine($"파일 수집 중 오류: {ex.Message}");
}
return sourceFiles;
}
private static void CollectSourceFilesRecursive(string currentPath, string[] extensions, HashSet<string> excludeFolders, List<string> sourceFiles)
{
try
{
string fullCurrentPath = Path.GetFullPath(currentPath);
// 현재 경로가 제외 목록에 있는지 확인
if (excludeFolders.Contains(fullCurrentPath))
{
return;
}
// 현재 디렉터리의 파일들 처리
foreach (string file in Directory.GetFiles(currentPath))
{
string extension = Path.GetExtension(file);
if (extensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
sourceFiles.Add(file);
}
}
// 하위 디렉터리 재귀 탐색
foreach (string directory in Directory.GetDirectories(currentPath))
{
CollectSourceFilesRecursive(directory, extensions, excludeFolders, sourceFiles);
}
}
catch (UnauthorizedAccessException)
{
Console.WriteLine($"접근 권한이 없습니다: {currentPath}");
}
catch (Exception ex)
{
Console.WriteLine($"디렉터리 탐색 중 오류: {currentPath} - {ex.Message}");
}
}
}
// 예제 config.json 생성용 클래스
public class ConfigGenerator
{
public static void GenerateExampleConfig()
{
var exampleConfig = new Config
{
name = "2dframework",
path = @"C:\Users\YourName\Projects\MyProject",
ext = new[] { ".cpp", ".h", ".c", ".hpp" },
exclude = new[] { "./Bin", "./Debug", "./Release", "./obj" }
};
string json = JsonSerializer.Serialize(exampleConfig, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText("example_config.json", json);
Console.WriteLine("example_config.json 파일이 생성되었습니다.");
}
}