-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
245 lines (216 loc) · 8.69 KB
/
Copy pathProgram.cs
File metadata and controls
245 lines (216 loc) · 8.69 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
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
using pack;
using System.CommandLine;
using System.Net.Http.Headers;
//========define commands==========
var rootCommand = new RootCommand("root command for file bundle CLI");
var bundleCommand = new Command("bundle", "bundle code files to single file");
var createRspCommand = new Command("create-rsp", "create a response file");
//==========adding commands=========
rootCommand.AddCommand(bundleCommand);
rootCommand.AddCommand(createRspCommand);
//========define options===========
var languageOption = new Option<string>(new string[] { "--language", "-l" }, "code language list") { IsRequired = true, AllowMultipleArgumentsPerToken = true };
var outputOption = new Option<FileInfo>(new string[] { "--option", "-o" }, "file path or file name");
var noteOption = new Option<bool>(new string[] { "--note", "-n" }, "write the source as a comment in bundle");
var sortOption = new Option<string>(new string[] { "--sort", "-s" }, "the order of copying the code files");
var removeEmptyLinesOption = new Option<bool>(new string[] { "----remove", "-r" }, "remove empty lines in bundle");
var authorOption = new Option<string>(new string[] { "--author", "-a" }, "adding the author name");
//=========adding option============
bundleCommand.AddOption(languageOption);
bundleCommand.AddOption(outputOption);
bundleCommand.AddOption(noteOption);
bundleCommand.AddOption(sortOption);
bundleCommand.AddOption(removeEmptyLinesOption);
bundleCommand.AddOption(authorOption);
noteOption.SetDefaultValue(true);
sortOption.SetDefaultValue("alphabet");
removeEmptyLinesOption.SetDefaultValue(true);
//languge
Console.WriteLine("jfsvnkmxcl;");
static bool LangugeValidaton(string language)
{
Console.WriteLine("hello LangugeValidaton");
if (string.IsNullOrEmpty(language))
return false;
var languageDict = new Dictionary<string, (string Extension, bool Flag)>
{
{ "java", (".java", false) },
{ "python", (".py", false) },
{ "c", (".c", false) },
{ "c++", (".cpp", false) },
{ "c#", (".cs", false) },
{ "javascript", (".js", false) },
{ "sql", (".sql", false) },
{ "typescript", (".ts", false) },
{ "react", (".tsx", false) },
{ "css", (".css", false) },
{ "html", (".html", false) },
{ "text", (".txt", false) }
};
string[] languages = language.Split(' ');
foreach (var lang in languages)
if (!languageDict.ContainsKey(lang))
return false;
return true;
}
static List<string> FilterDict(string directoryPath, List<string> files, string language)
{
Console.WriteLine("hello FilterDict");
if (language == "all")
language = string.Join(" ", Language.ProgrammingLanguages.Keys);
//get all the extension of the chosen languages
var extension = Language.ProgrammingLanguages.Where(lang => language.Contains(lang.Key))
.Select(lang => lang.Value);
//filter the file include in the list, exclude bin and debug files
var filteredFiles = files.Where(file =>
extension.Contains(Path.GetExtension(file)) ||
!file.Contains(Path.Combine(directoryPath, "bin")) ||
!file.Contains(Path.Combine(directoryPath, "debug"))).ToList();
return filteredFiles;
}
bundleCommand.SetHandler((language, output, note, sort, removeEmptyLines, author) =>
{
Console.WriteLine("hello SetHandler");
try
{
if (!LangugeValidaton(language))
throw new InvalidOperationException("ERROR: invalid language");
var directoryPath = Directory.GetCurrentDirectory();
List<string> files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList();
if (!Directory.Exists(Path.GetDirectoryName(output.FullName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(output.FullName));
}
using (var bFile = new StreamWriter(output.FullName, append: false))
{
var filterFiles = FilterDict(directoryPath, files, language);
if (sort == "alphabet")
{
filterFiles = filterFiles.OrderBy(f => Path.GetFileName(f)).ToList();
}
else
{
filterFiles = filterFiles.OrderBy(f => Path.GetExtension(f)).ToList();
}
if (!string.IsNullOrEmpty(author))
bFile.WriteLine($"author: {author}");
foreach (var file in filterFiles)
{
if (note)
{
bFile.WriteLine($"file name: {Path.GetFileName(file)}: path: {Path.GetRelativePath(directoryPath, file)}");
}
var content = File.ReadAllLines(file);
if (removeEmptyLines)
{
content = content.Where(line => !string.IsNullOrWhiteSpace(line)).ToArray();
}
foreach (string line in content)
{
bFile.WriteLine(line);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: {ex.Message}");
}
},
languageOption,
outputOption,
noteOption,
sortOption,
removeEmptyLinesOption,
authorOption);
// Console.WriteLine(output.FullName);
// try
// {
// using (var bFile = new StreamWriter(output.FullName, append: false))
// {
// if (!LangugeValidaton(language))
// throw new InvalidOperationException("EROR: invalid language");
// var directoryPath = Directory.GetCurrentDirectory();
// List<string> files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList();
// var filterFiles = FilterDict(directoryPath, files, language);
// //sort
// if (sort != "alphabet")
// {
// filterFiles.OrderBy(f => Path.GetExtension(f)).ToList();
// }
// else filterFiles = filterFiles.OrderBy(f => Path.GetFileName(f)).ToList();
// if (!string.IsNullOrEmpty(author))
// bFile.WriteLine($"author: {author}");
// foreach (var file in filterFiles)
// {
// //note
// if (note)
// {
// bFile.WriteLine($"file name: {Path.GetFileName(file)}: path: {Path.GetRelativePath(directoryPath, file)}");
// }
// var content = File.ReadAllLines(file);
// if (removeEmptyLines)
// {
// content = content.Where(line => !string.IsNullOrWhiteSpace(line)).ToArray();
// // כתיבת השורות הלא ריקות חזרה לקובץ
// }
// //using (bFile = new StreamWriter(output.FullName, true)) // true מאפשר הוספה לקובץ קיים
// //{
// // foreach (string line in content)
// // {
// // bFile.WriteLine(line); // כותב כל מחרוזת לקובץ
// // }
// //}
// }
// }
// }
// catch (UnauthorizedAccessException)
// {
// Console.WriteLine("Error: Access denied to the output path.");
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Error: {ex.Message}");
// }
//},
//languageOption,
//outputOption,
//noteOption,
//sortOption,
//removeEmptyLinesOption,
//authorOption
//);
createRspCommand.SetHandler(() =>
{
var quesAns = new Dictionary<string, string> {
{ "language (comma-separated, or 'all')", "" },
{ "include source file paths as comments in the bundle ? (true / false)", "false" },
{ "sort files by ('name' or 'type')", "name" },
{ "remove empty lines from the source files ? (true / false)", "false" },
{ "add Author to the file?(author name)", "" },
{ "output -File path and name", "" }
};
foreach (var key in quesAns.Keys)
{
Console.Write($"Enter {key}: ");
quesAns[key] = Console.ReadLine() ?? quesAns[key];
}
if (string.IsNullOrWhiteSpace(quesAns["language (comma-separated, or 'all'"]) ||
string.IsNullOrWhiteSpace(quesAns["output -File path and name"]))
{
Console.WriteLine("Error: 'language' and 'output' are required fields.");
return;
}
var rspName = "bundle.rsp";
using (FileStream resFile = new FileStream(rspName, FileMode.CreateNew, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(resFile))
{
foreach (var (key, value) in quesAns)
{
var option = key[0];
writer.WriteLine($"--{option} {value}");
}
}
Console.WriteLine($"Response File created successfully. Run it with pack @{rspName}");
});
rootCommand.InvokeAsync(args);