-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileAggregator.cs
64 lines (57 loc) · 1.97 KB
/
FileAggregator.cs
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
// Filename: FileAggregator.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class FileAggregator
{
public static void AggregateFiles(string rootFolder, IEnumerable<string> includedFiles, string outputPath)
{
var sb = new StringBuilder();
sb.AppendLine($"Source folder: {rootFolder}");
foreach (var file in includedFiles)
{
bool isTextFile = IsTextFile(file);
Logger.Log($"File: {file}, IsTextFile: {isTextFile}");
if (isTextFile)
{
var content = File.ReadAllText(file);
var relativePath = Path.GetRelativePath(rootFolder, file);
var folder = Path.GetDirectoryName(relativePath);
var fileName = Path.GetFileName(file);
sb.AppendLine($"Folder: {folder}");
sb.AppendLine($"Filename: {fileName}");
sb.AppendLine(content);
sb.AppendLine();
Logger.Log($"First 20 characters of {file}: {content.Substring(0, Math.Min(20, content.Length))}");
}
}
File.WriteAllText(outputPath, sb.ToString());
}
private static bool IsTextFile(string filePath)
{
try
{
using (var stream = new StreamReader(filePath, detectEncodingFromByteOrderMarks: true))
{
char[] buffer = new char[512];
int charsRead = stream.Read(buffer, 0, buffer.Length);
if (charsRead == 0)
return false;
for (int i = 0; i < charsRead; i++)
{
if (char.IsControl(buffer[i]) && buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != '\t')
{
return false;
}
}
return true;
}
}
catch
{
return false;
}
}
}