-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOutputFiles.cs
More file actions
172 lines (160 loc) · 6.08 KB
/
OutputFiles.cs
File metadata and controls
172 lines (160 loc) · 6.08 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
using System.Xml;
namespace Generator;
public class OutputFiles
{
public string? fullPathToCsproj { get; set; }
public string? fullPathToSln { get; set; }
public string? FolderWithSln
{
get
{
ArgumentNullException.ThrowIfNull(fullPathToSln);
return Path.GetDirectoryName(fullPathToSln);
}
}
public string? ContentCsProj { get;internal set; }
public string[]? csFiles { get; set; }
public FileWithContent[]? contentFiles { get; set; }
public FileWithContent[]? generatedFiles { get; set; }
public int LineInCSproj;
public string ScribanLineInCSproj
{
get
{
return "{" + (LineInCSproj + 1) + "}";
}
}
public string NameCsproj
{
get
{
ArgumentNullException.ThrowIfNull(fullPathToCsproj);
return Path.GetFileName(fullPathToCsproj) ;
}
}
public string[]? excludeDirectoryGenerated { get; internal set; }
public string[]? includeAdditionalFiles { get; internal set; }
public bool HasFilesGenerated() => generatedFiles?.Length > 0;
public async Task GatherData(string nuget)
{
var excludedProjectsWithLine = new[]{
"DemoRegex.csproj",
"DemoSerializeJSON.csproj",
"LibraryImportDemo.csproj"
};
ArgumentNullException.ThrowIfNull(fullPathToCsproj);
ContentCsProj = await File.ReadAllTextAsync(fullPathToCsproj);
if(!string.IsNullOrWhiteSpace(nuget))
if (!excludedProjectsWithLine.Any(it => fullPathToCsproj.Contains(it)))
{
LineInCSproj = ContentCsProj
.Split("\n")
.Select((it, i) => new { it, i })
.Where(it => it.it.Contains(nuget,StringComparison.InvariantCultureIgnoreCase))
.Select(a => a.i)
.FirstOrDefault();
//DemoSerializeJSON has .net core inside
if (LineInCSproj == 0)
{
throw new ArgumentException($"cannot find {nuget} nuget in {fullPathToCsproj}");
}
}
var dir =Path.GetDirectoryName(fullPathToCsproj);
ArgumentNullException.ThrowIfNull(dir);
List <FileWithContent> contents = new();
ArgumentNullException.ThrowIfNull(csFiles);
foreach (var file in csFiles)
{
var fileFound = Directory.GetFiles(dir, file, SearchOption.AllDirectories);
fileFound = fileFound.Where(it => !it.Contains("GX")).ToArray();
if (fileFound.Length == 0)
{
//go a dir upper
var dirParent = new DirectoryInfo(dir).Parent;
ArgumentNullException.ThrowIfNull(dirParent);
fileFound = Directory.GetFiles(dirParent.FullName, file, SearchOption.AllDirectories);
fileFound = fileFound.Where(it => !it.Contains("GX")).ToArray();
}
if(fileFound.Length > 1)
{
var first= fileFound.FirstOrDefault();
var fileInf = new FileInfo(first!);
bool sameFile= true;
foreach (var item in fileFound)
{
var fiLoop = new FileInfo(item!);
if(fiLoop.Length != fileInf.Length)
{
sameFile = false;
break;
}
}
if (sameFile)
{
fileFound = new[] { first! };
}
}
if (fileFound.Length != 1)
{
throw new Exception($"must have 1, but {fileFound.Length} files search {file} in {dir} for GX");
}
string nameFile = Path.GetFileName(fileFound[0]);
FileWithContent f = new(fileFound[0],nameFile, await File.ReadAllTextAsync(fileFound[0]));
contents.Add(f);
}
contentFiles = contents.ToArray();
var AdditionalFiles = Array.Empty<string>();
if (includeAdditionalFiles?.Length > 0)
{
foreach (var item in includeAdditionalFiles)
{
if(item.Length==0)
continue;
var fld=Directory.GetFiles(dir, item, SearchOption.AllDirectories);
if (fld.Length > 0)
{
AdditionalFiles = AdditionalFiles.Concat(fld).ToArray();
}
}
}
var outputGenFolder = Directory.GetDirectories(dir, "GX", SearchOption.AllDirectories);
if (outputGenFolder.Length != 1)
{
throw new Exception($"{outputGenFolder.Length} output folders in {dir}");
}
string folder = outputGenFolder[0];
//var generators = Directory.GetDirectories(folder);
//if (generators.Length > 1)
//{
// generators = generators.Where(it => it != "Microsoft.NET.Sdk.Razor.SourceGenerators").ToArray();
// if (generators.Length > 1) {
// throw new ArgumentException($"more than 1 generated folder for {folder}");
// }
//}
var outputFiles = Directory.GetFiles(folder, "*.cs", SearchOption.AllDirectories);
if(includeAdditionalFiles?.Length > 0)
{
outputFiles = outputFiles.Concat(AdditionalFiles).ToArray();
}
contents = new();
var nr = 0;
foreach (var file in outputFiles)
{
var isInExcluded = false;
if (excludeDirectoryGenerated?.Length > 0)
{
isInExcluded = excludeDirectoryGenerated!.Any(it =>
(!string.IsNullOrWhiteSpace(it)) &&
file.Contains(it, StringComparison.InvariantCultureIgnoreCase));
}
if(isInExcluded)
continue;
var nameFile = Path.GetFileName(file);
string id = nameFile + (++nr).ToString("00#");
FileWithContent f = new(file, nameFile, await File.ReadAllTextAsync(file));
contents.Add(f);
}
generatedFiles = contents.ToArray();
}
}