-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_callable_Build-CodeCombiner.ps1
More file actions
128 lines (99 loc) · 4.6 KB
/
_callable_Build-CodeCombiner.ps1
File metadata and controls
128 lines (99 loc) · 4.6 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
# Create project directory
$projectDir = "CodeCombiner"
New-Item -ItemType Directory -Path $projectDir | Out-Null
Set-Location $projectDir
# Create new console app (creates Program.cs with template content)
dotnet new console -n CodeCombinerApp
# Define the full program code (this would be your complete code from earlier)
$programCode = @"
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class Program {
static void Main(string[] args) {
string currentDirectory = Directory.GetCurrentDirectory();
string targetDirectory = Path.Combine(currentDirectory, "Repos");
CodeFileConverter cfc = new();
List<FileInfoEntry> list = cfc.ConvertFilesToTxt(targetDirectory);
List<FileInfoEntry> fileList = list;
Console.WriteLine($"{cfc.GetType().Name} created with extension list {string.Join(" ", cfc._codeExtensions)}");
CodeCombiner combiner = new();
string outputPath = combiner.CombineFiles(fileList, "combined_output.txt");
Console.WriteLine($"Combined file created at: {outputPath}");
Console.WriteLine("Process completed successfully.");
Console.WriteLine("Press any key to exit. combined_output.txt has been created.");
Console.ReadLine();
}
}
internal class CodeFileConverter {
internal readonly HashSet<string> _codeExtensions =
[
".cs", ".vb", ".js", ".ts", ".py", ".java", ".cpp", ".c", ".h", ".hpp"
];
internal List<FileInfoEntry> ConvertFilesToTxt(string rootDirectory) {
List<FileInfoEntry> fileList = [];
foreach (string filePath in Directory.EnumerateFiles(rootDirectory, "*", SearchOption.AllDirectories)) {
string extension = Path.GetExtension(filePath).ToLower();
if (_codeExtensions.Contains(extension)) {
string convertedPath = Path.ChangeExtension(filePath, ".txt");
File.Copy(filePath, convertedPath, true);
FileInfoEntry entry = new() {
OriginalPath = filePath,
ConvertedPath = convertedPath,
FileExtension = extension.Substring(1)
};
fileList.Add(entry);
}
}
return fileList;
}
}
internal class CodeCombiner {
internal string CombineFiles(List<FileInfoEntry> fileList, string outputPath) {
StringBuilder result = new();
foreach (FileInfoEntry file in fileList) {
if (!File.Exists(file.ConvertedPath))
continue;
string content = File.ReadAllText(file.ConvertedPath);
// Add distinguishing comment between files
if (result.Length > 0) {
_ = result.AppendLine();
_ = result.AppendLine(GetDistinguishingComment(file.FileExtension));
_ = result.AppendLine();
}
_ = result.AppendLine(content);
}
File.WriteAllText(outputPath, result.ToString());
return outputPath;
}
internal string GetDistinguishingComment(string fileType) => fileType.ToLower() switch {
"cs" => "/* ========================================================================================================================= */",
"vb" => "' ========================================================================================================================== ",
"js" or "ts" => "// =========================================================================================================================== ",
"py" => "# ============================================================================================================================ ",
"cpp" => "/* ========================================================================================================================= */",
"c" => "/* ========================================================================================================================= */",
"h" => "/* ========================================================================================================================= */",
"java" => "/** ========================================================================================================================= */",
_ => "// =========================================================================================================================== ",
};
}
internal class FileInfoEntry {
internal required string OriginalPath { get; set; }
internal required string ConvertedPath { get; set; }
internal required string FileExtension { get; set; }
}
"@
# Replace the generated Program.cs with our full code
$cc = "CodeCombiner"
$appPath = "CodeCombinerApp"
$programDirectory = Join-Path -Path $cc $appPath
$programCs = Join-Path -Path $programDirectory "Program.cs"
cd ../
Set-Content -Path $programCs -Value $programCode -Force
$csproj = ".\$programDirectory\CodeCombinerApp.csproj"
# Build the application (this will use the .NET CLI)
dotnet build --configuration Release $csproj
Write-Host $PWD
dotnet run --project $csproj