-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
251 lines (220 loc) · 10.2 KB
/
MainPage.xaml.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
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
244
245
246
247
248
249
250
251
using System.Text;
namespace VSProjectTextExport
{
public partial class MainPage : ContentPage
{
private string _outputFilePath = string.Empty;
private string _selectedPath = string.Empty;
public MainPage()
{
InitializeComponent();
createReportButton.IsEnabled = false;
openTextFileButton.IsVisible = false;
}
private string ListAllFilesAndDirectories(string spath)
{
string rootPath = spath;
var header = "***********************************" + Environment.NewLine;
var firstRelativeFolder = new DirectoryInfo(rootPath).Name;
var allFileTypes = new string[] { "*.cs", "*.xaml", "*.sln", "*.csproj", "*.resx", "*.json" };
var fileListing = new List<string>();
// Add the root folder to the listing
fileListing.Add(firstRelativeFolder);
var allDirectories = Directory.EnumerateDirectories(rootPath, "*", SearchOption.AllDirectories)
.Where(
dir => !dir.Split(Path.DirectorySeparatorChar)
.Any(
part => part.StartsWith(".") ||
part.Equals("bin") ||
part.Equals("obj") ||
part.Equals("Platforms") ||
part.Equals("Resources")))
.OrderBy(dir => dir);
// Process each file and folder
foreach(var dir in allDirectories)
{
var relativeDirPath = Path.GetRelativePath(rootPath, dir);
fileListing.Add("└──Directory: " + relativeDirPath);
// Collect all files in the current directory
var allFilesInDir = allFileTypes
.SelectMany(ext => Directory.EnumerateFiles(dir, ext, SearchOption.TopDirectoryOnly))
.Select(file => Path.GetFileName(file)) // Extract the file names
.OrderBy(fileName => fileName); // Sort the file names
// Add the sorted file names to the list
foreach(var fileName in allFilesInDir)
{
fileListing.Add(" └──File: " + fileName);
}
}
// Collect all files in the root directory
var allFilesInRoot = allFileTypes
.SelectMany(ext => Directory.EnumerateFiles(rootPath, ext, SearchOption.TopDirectoryOnly))
.Select(file => Path.GetFileName(file)) // Extract the file names
.OrderBy(fileName => fileName); // Sort the file names
// Add the sorted file names to the list
foreach(var fileName in allFilesInRoot)
{
fileListing.Add("File: " + fileName);
}
var resultContent = new List<string>();
foreach(var file in fileListing)
{
resultContent.Add(file);
}
var filesListingString = "Files and Folders in Project " +
firstRelativeFolder +
":" +
Environment.NewLine +
string.Join(Environment.NewLine, resultContent) +
Environment.NewLine +
Environment.NewLine;
var singleStr = filesListingString;
Console.WriteLine(singleStr);
return singleStr;
}
private async void OnPathButtonClicked(object sender, EventArgs e)
{
try
{
var options = new PickOptions
{
PickerTitle = "Please choose a folder"
};
var result = await FilePicker.PickAsync(options);
if(result != null)
{
_selectedPath = result.FullPath;
}
}
catch(Exception ex)
{
await DisplayAlert("Error", $"An error has occurred: {ex.Message}", "OK");
}
}
private async void OnPickFolderClicked(object sender, EventArgs e)
{
try
{
var pickOptions = new PickOptions
{
PickerTitle = "Please choose a .sln file",
FileTypes =
new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { "sln" } },
{ DevicePlatform.Android, new[] { "sln" } },
{ DevicePlatform.WinUI, new[] { ".sln" } },
{ DevicePlatform.MacCatalyst, new[] { "sln" } },
{ DevicePlatform.macOS, new[] { "sln" } },
})
};
var result = await FilePicker.PickAsync(pickOptions);
if(result != null)
{
_selectedPath = Path.GetDirectoryName(result.FullPath);
PathLabel.Text = ListAllFilesAndDirectories(_selectedPath);
createReportButton.IsEnabled = true;
}
}
catch(Exception ex)
{
await DisplayAlert("Error", $"An error has occurred: {ex.Message}", "OK");
}
}
private async void OnCreateReportClicked(object sender, EventArgs e)
{
if(string.IsNullOrWhiteSpace(_selectedPath))
{
await DisplayAlert("Error", "Please choose a folder first.", "OK");
return;
}
try
{
var header = "**********************************************************************" +
Environment.NewLine;
// Determine the name of the first relative folder for the output file
var firstRelativeFolder = new DirectoryInfo(_selectedPath).Name;
string outputPath = Path.Combine(_selectedPath + @"\" + firstRelativeFolder + ".txt");
var allFileTypes = new string[] { "*.cs", "*.xaml", "*.sln", "*.csproj", "*.resx", "*.json" };
// Get all directories except those that start with '.', as well as 'bin' and 'obj' folders
var allDirectories = Directory.EnumerateDirectories(_selectedPath, "*", SearchOption.AllDirectories)
.Where(
dir => !dir.Split(Path.DirectorySeparatorChar)
.Any(
part => part.StartsWith(".") ||
part.Equals("bin") ||
part.Equals("obj") ||
part.Equals("Platforms") ||
part.Equals("Resources")))
.OrderBy(dir => dir);
// Search all permissible directories for the specified file types
var filesInDirectories = allDirectories.SelectMany(
dir => allFileTypes.SelectMany(
ext => Directory.EnumerateFiles(dir, ext, SearchOption.TopDirectoryOnly)))
.OrderBy(dir => dir);
// Capture specific files in the root directory in the desired order
var slnFilesInRoot = Directory.EnumerateFiles(_selectedPath, "*.sln", SearchOption.TopDirectoryOnly);
var csprojFilesInRoot = Directory.EnumerateFiles(
_selectedPath,
"*.csproj",
SearchOption.TopDirectoryOnly);
var otherFilesInRoot = allFileTypes.Where(ext => ext != "*.sln" && ext != "*.csproj")
.SelectMany(ext => Directory.EnumerateFiles(_selectedPath, ext, SearchOption.TopDirectoryOnly))
.OrderBy(dir => dir);
// Combine the files from the root directory in the specific order
var filesInRoot = slnFilesInRoot.Concat(csprojFilesInRoot).Concat(otherFilesInRoot);
// Combine the files from the subdirectories and the root directory
var allFiles = filesInRoot.Concat(filesInDirectories);
var resultContent = allFiles.Select(
path => new
{
Name = Path.GetFileName(path),
RelativePath = Path.GetRelativePath(_selectedPath, path),
Contents = File.ReadAllText(path)
})
.Select(
info =>
header +
"Filename: " +
info.Name +
Environment.NewLine +
"Relative Path: " +
firstRelativeFolder +
"\\" +
info.RelativePath +
Environment.NewLine +
header +
info.Contents);
var singleStr = ListAllFilesAndDirectories(_selectedPath);
singleStr += string.Join(Environment.NewLine, resultContent);
Console.WriteLine(singleStr);
File.WriteAllText(outputPath, singleStr, Encoding.UTF8);
OutputPathLabel.Text = outputPath;
_outputFilePath = outputPath;
openTextFileButton.IsVisible = true;
}
catch(Exception ex)
{
await DisplayAlert("Error", $"An error has occurred: {ex.Message}", "OK");
}
}
private async void OnOpenTextFileClicked(object sender, EventArgs e)
{
if(!string.IsNullOrWhiteSpace(_outputFilePath) && File.Exists(_outputFilePath))
{
await Launcher.Default
.OpenAsync(
new OpenFileRequest
{
File = new ReadOnlyFile(_outputFilePath)
});
}
else
{
await DisplayAlert("Error", "File not found.", "OK");
}
}
}
}