|
9 | 9 |
|
10 | 10 | namespace ProgrammerAL.Tools.CodeUpdater;
|
11 | 11 |
|
12 |
| -public record UpdateWork(ImmutableArray<string> CsProjectFiles, ImmutableArray<string> NpmDirectories); |
| 12 | +public record UpdateWork(ImmutableArray<string> ValidDirectories, ImmutableArray<string> CsProjectFiles, ImmutableArray<string> NpmDirectories); |
13 | 13 |
|
14 | 14 | public class WorkLocator(ILogger Logger, UpdateOptions UpdateOptions)
|
15 | 15 | {
|
16 |
| - public ImmutableArray<string> DetermineSkipPaths(IEnumerable<string> additionalSkipPaths) |
| 16 | + public UpdateWork DetermineUpdateWork(string rootDirectory) |
17 | 17 | {
|
18 |
| - var skipPaths = new[] |
19 |
| - { |
20 |
| - //Ignore all obj and bin folders |
21 |
| - @"/obj/Debug/", |
22 |
| - @"/obj/Release/", |
23 |
| - @"/bin/Debug/", |
24 |
| - @"/bin/Release/", |
| 18 | + var validDirectories = DetermineValidDirectories(rootDirectory); |
| 19 | + var csProjFiles = FindCsProjFiles(validDirectories); |
| 20 | + var npmDirectories = FindNpmDirectories(validDirectories); |
25 | 21 |
|
26 |
| - //Ignore packages inside node_modules |
27 |
| - @"/node_modules/" |
28 |
| - } |
29 |
| - .ToImmutableArray(); |
30 |
| - |
31 |
| - //Include the same paths, but with backslashes so this is cross-platform |
32 |
| - skipPaths = skipPaths.AddRange(skipPaths.Select(x => x.Replace("/", "\\"))); |
33 |
| - |
34 |
| - skipPaths = skipPaths.AddRange(additionalSkipPaths); |
35 |
| - |
36 |
| - return skipPaths; |
| 22 | + return new UpdateWork(validDirectories, csProjFiles, npmDirectories); |
37 | 23 | }
|
38 | 24 |
|
39 |
| - public UpdateWork DetermineUpdateWork(string rootDirectory, ImmutableArray<string> skipPaths) |
| 25 | + private ImmutableArray<string> DetermineValidDirectories(string rootDirectory) |
40 | 26 | {
|
41 |
| - var csProjFiles = FindCsProjFiles(rootDirectory, skipPaths); |
42 |
| - var npmDirectories = FindNpmDirectories(rootDirectory, skipPaths); |
| 27 | + var skipPaths = DetermineSkipPaths(UpdateOptions.UpdatePathOptions.IgnorePatterns); |
43 | 28 |
|
44 |
| - return new UpdateWork(csProjFiles, npmDirectories); |
45 |
| - } |
46 |
| - |
47 |
| - public ImmutableArray<string> FindCsProjFiles(string rootDirectory, ImmutableArray<string> skipPaths) |
48 |
| - { |
49 |
| - var allCsProjFilesPaths = Directory.GetFiles(rootDirectory, "*.csproj", SearchOption.AllDirectories); |
50 |
| - var validCsProjFilesPaths = new List<string>(); |
| 29 | + //Get all directories, including subdirectories |
| 30 | + // Make sure the directory path string includes a trailing slash so we can compare it to the skip paths |
| 31 | + var allDirectories = Directory.GetDirectories(rootDirectory, "*", SearchOption.AllDirectories) |
| 32 | + .Select(x => $"{x}{Path.DirectorySeparatorChar}"); |
| 33 | + var validDirectories = new List<string>(); |
51 | 34 |
|
52 |
| - foreach (var csProjFilePath in allCsProjFilesPaths) |
| 35 | + foreach (var directoryPath in allDirectories) |
53 | 36 | {
|
54 |
| - var skipPath = skipPaths.FirstOrDefault(x => csProjFilePath.Contains(x, StringComparison.OrdinalIgnoreCase)); |
| 37 | + var skipPath = skipPaths.FirstOrDefault(x => directoryPath.Contains(x, StringComparison.OrdinalIgnoreCase)); |
55 | 38 | if (skipPath is object)
|
56 | 39 | {
|
57 |
| - Logger.Debug($"Skipping '{csProjFilePath}' file because it's path should be ignored by rule: {skipPath}"); |
| 40 | + Logger.Debug($"Skipping directory '{directoryPath}' because it's path should be ignored by rule: {skipPath}"); |
58 | 41 | }
|
59 | 42 | else
|
60 | 43 | {
|
61 |
| - validCsProjFilesPaths.Add(csProjFilePath); |
| 44 | + validDirectories.Add(directoryPath); |
62 | 45 | }
|
63 | 46 | }
|
64 | 47 |
|
| 48 | + return validDirectories.ToImmutableArray(); |
| 49 | + } |
| 50 | + |
| 51 | + public ImmutableArray<string> FindCsProjFiles(ImmutableArray<string> validDirectories) |
| 52 | + { |
| 53 | + if (UpdateOptions.CSharpOptions is null) |
| 54 | + { |
| 55 | + Logger.Information("No CSharpOptions config set, will not attempt to update any C# code"); |
| 56 | + return ImmutableArray<string>.Empty; |
| 57 | + } |
| 58 | + |
| 59 | + var validCsProjFilesPaths = new List<string>(); |
| 60 | + |
| 61 | + foreach (var dir in validDirectories) |
| 62 | + { |
| 63 | + var allCsProjFilesPaths = Directory.GetFiles(dir, "*.csproj", SearchOption.TopDirectoryOnly); |
| 64 | + validCsProjFilesPaths.AddRange(allCsProjFilesPaths); |
| 65 | + } |
| 66 | + |
65 | 67 | return validCsProjFilesPaths.ToImmutableArray();
|
66 | 68 | }
|
67 | 69 |
|
68 |
| - public ImmutableArray<string> FindNpmDirectories(string rootDirectory, ImmutableArray<string> skipPaths) |
| 70 | + public ImmutableArray<string> FindNpmDirectories(ImmutableArray<string> validDirectories) |
69 | 71 | {
|
70 | 72 | if (UpdateOptions.NpmOptions is null)
|
71 | 73 | {
|
72 | 74 | Logger.Information("No NpmOptions config set, will not attempt to update NPM Packages");
|
73 | 75 | return ImmutableArray<string>.Empty;
|
74 | 76 | }
|
75 | 77 |
|
76 |
| - var allPackageJsonPaths = Directory.GetFiles(rootDirectory, "package.json", SearchOption.AllDirectories); |
77 | 78 | var validPaths = new List<string>();
|
78 | 79 |
|
79 |
| - foreach (var packageJsonPath in allPackageJsonPaths) |
| 80 | + foreach (var dir in validDirectories) |
80 | 81 | {
|
81 |
| - var packagePath = Path.GetDirectoryName(packageJsonPath); |
82 |
| - if (string.IsNullOrWhiteSpace(packagePath)) |
| 82 | + var packageJsonFiles = Directory.GetFiles(dir, "package.json", SearchOption.TopDirectoryOnly); |
| 83 | + |
| 84 | + var packageJsonFile = packageJsonFiles.FirstOrDefault(); |
| 85 | + |
| 86 | + if (string.IsNullOrWhiteSpace(packageJsonFile)) |
83 | 87 | {
|
84 |
| - Logger.Debug($"Skipping '{packageJsonPath}' file because it's path is null or empty"); |
| 88 | + Logger.Debug($"Skipping directory '{dir}' because it doesn't have a package.json file in it"); |
85 | 89 | continue;
|
86 | 90 | }
|
87 | 91 |
|
88 |
| - var skipPath = skipPaths.FirstOrDefault(x => packagePath.Contains(x, StringComparison.OrdinalIgnoreCase)); |
89 |
| - if (skipPath is object) |
90 |
| - { |
91 |
| - Logger.Debug($"Skipping '{packagePath}' because it's path should be ignored by rule: {skipPath}"); |
92 |
| - } |
93 |
| - else |
| 92 | + var packagePath = Path.GetDirectoryName(packageJsonFile); |
| 93 | + if (string.IsNullOrWhiteSpace(packagePath)) |
94 | 94 | {
|
95 |
| - validPaths.Add(packagePath); |
| 95 | + Logger.Debug($"Skipping '{packageJsonFile}' file because it's package.json path is null or empty"); |
| 96 | + continue; |
96 | 97 | }
|
| 98 | + |
| 99 | + validPaths.Add(packagePath); |
97 | 100 | }
|
98 | 101 |
|
99 | 102 | return validPaths.ToImmutableArray();
|
100 | 103 | }
|
| 104 | + |
| 105 | + private ImmutableArray<string> DetermineSkipPaths(IEnumerable<string> additionalSkipPaths) |
| 106 | + { |
| 107 | + var skipPaths = new[] |
| 108 | + { |
| 109 | + //Ignore all obj and bin folders |
| 110 | + @"/obj/", |
| 111 | + @"/obj/Debug/", |
| 112 | + @"/obj/Release/", |
| 113 | + @"/bin/", |
| 114 | + @"/bin/Debug/", |
| 115 | + @"/bin/Release/", |
| 116 | + |
| 117 | + //Ignore packages inside node_modules |
| 118 | + @"/node_modules/", |
| 119 | + |
| 120 | + //Ignore the .git folder |
| 121 | + @"/.git/", |
| 122 | + @"/.vs/", |
| 123 | + } |
| 124 | + .ToImmutableArray(); |
| 125 | + |
| 126 | + //Include the same paths, but with backslashes so this is cross-platform |
| 127 | + skipPaths = skipPaths.AddRange(skipPaths.Select(x => x.Replace("/", "\\"))); |
| 128 | + |
| 129 | + skipPaths = skipPaths.AddRange(additionalSkipPaths); |
| 130 | + |
| 131 | + return skipPaths; |
| 132 | + } |
101 | 133 | }
|
0 commit comments