|
| 1 | +using System.Diagnostics.CodeAnalysis; |
1 | 2 | using System.IO.Abstractions; |
| 3 | +using Ignore; |
2 | 4 |
|
3 | 5 | namespace CSharpier.Cli; |
4 | 6 |
|
5 | 7 | internal class IgnoreFile |
6 | 8 | { |
7 | | - protected Ignore.Ignore Ignore { get; } |
8 | | - protected string IgnoreBaseDirectoryPath { get; } |
| 9 | + private List<IgnoreWithBasePath> ignores { get; } |
9 | 10 | private static readonly string[] alwaysIgnored = ["**/node_modules", "**/obj", "**/.git"]; |
10 | 11 |
|
11 | | - protected IgnoreFile(Ignore.Ignore ignore, string ignoreBaseDirectoryPath) |
| 12 | + private IgnoreFile(List<IgnoreWithBasePath> ignores) |
12 | 13 | { |
13 | | - this.Ignore = ignore; |
14 | | - this.IgnoreBaseDirectoryPath = ignoreBaseDirectoryPath.Replace('\\', '/'); |
| 14 | + this.ignores = ignores; |
15 | 15 | } |
16 | 16 |
|
17 | 17 | public bool IsIgnored(string filePath) |
18 | 18 | { |
19 | | - var pathRelativeToIgnoreFile = filePath.Replace('\\', '/'); |
20 | | - if ( |
21 | | - !pathRelativeToIgnoreFile.StartsWith( |
22 | | - this.IgnoreBaseDirectoryPath, |
23 | | - StringComparison.Ordinal |
24 | | - ) |
25 | | - ) |
| 19 | + filePath = filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); |
| 20 | + foreach (var ignore in this.ignores) |
26 | 21 | { |
27 | | - // TODO #631 |
28 | | - // the ignore file was created for /test/subfolder |
29 | | - // and we are checking if /test/.editorconfig is ignored, which it CAN'T be |
30 | | - // in main, we did not check if parent folder editorconfigs were ignored, which is probably a bug |
31 | | - // we need to figure out a better way to deal with ignorefiles, which will come in the PR for 631 |
32 | | - |
33 | | - return false; |
34 | | - // throw new Exception( |
35 | | - // $"The filePath of {filePath} does not start with the ignoreBaseDirectoryPath of {this.IgnoreBaseDirectoryPath}" |
36 | | - // ); |
| 22 | + // when using one of the ignore files to determine if a given file is ignored or not |
| 23 | + // we can only consider that file if it actually has a matching rule for the filePath |
| 24 | + var (hasMatchingRule, isIgnored) = ignore.IsIgnored(filePath); |
| 25 | + if (hasMatchingRule) |
| 26 | + { |
| 27 | + return isIgnored; |
| 28 | + } |
37 | 29 | } |
38 | 30 |
|
39 | | - pathRelativeToIgnoreFile = |
40 | | - pathRelativeToIgnoreFile.Length > this.IgnoreBaseDirectoryPath.Length |
41 | | - ? pathRelativeToIgnoreFile[(this.IgnoreBaseDirectoryPath.Length + 1)..] |
42 | | - : string.Empty; |
43 | | - |
44 | | - return this.Ignore.IsIgnored(pathRelativeToIgnoreFile); |
| 31 | + return false; |
45 | 32 | } |
46 | 33 |
|
47 | | - public static async Task<IgnoreFile> Create( |
| 34 | + public static async Task<IgnoreFile?> CreateAsync( |
48 | 35 | string baseDirectoryPath, |
49 | 36 | IFileSystem fileSystem, |
50 | 37 | CancellationToken cancellationToken |
51 | 38 | ) |
52 | 39 | { |
53 | | - var ignore = new Ignore.Ignore(); |
54 | | - |
55 | | - foreach (var name in alwaysIgnored) |
| 40 | + var ignoreFilePaths = FindIgnorePaths(baseDirectoryPath, fileSystem); |
| 41 | + if (ignoreFilePaths.Count == 0) |
56 | 42 | { |
57 | | - ignore.Add(name); |
| 43 | + var ignore = new IgnoreWithBasePath(baseDirectoryPath); |
| 44 | + foreach (var name in alwaysIgnored) |
| 45 | + { |
| 46 | + ignore.Add(name); |
| 47 | + } |
| 48 | + return new IgnoreFile([ignore]); |
58 | 49 | } |
59 | 50 |
|
60 | | - var ignoreFilePath = FindIgnorePath(baseDirectoryPath, fileSystem); |
61 | | - if (ignoreFilePath == null) |
| 51 | + var ignores = new List<IgnoreWithBasePath>(); |
| 52 | + foreach (var ignoreFilePath in ignoreFilePaths) |
62 | 53 | { |
63 | | - return new IgnoreFile(ignore, baseDirectoryPath); |
| 54 | + var ignore = new IgnoreWithBasePath(Path.GetDirectoryName(ignoreFilePath)!); |
| 55 | + ignores.Add(ignore); |
| 56 | + foreach (var name in alwaysIgnored) |
| 57 | + { |
| 58 | + ignore.Add(name); |
| 59 | + } |
| 60 | + foreach ( |
| 61 | + var line in await fileSystem.File.ReadAllLinesAsync( |
| 62 | + ignoreFilePath, |
| 63 | + cancellationToken |
| 64 | + ) |
| 65 | + ) |
| 66 | + { |
| 67 | + if (string.IsNullOrWhiteSpace(line)) |
| 68 | + { |
| 69 | + continue; |
| 70 | + } |
| 71 | + try |
| 72 | + { |
| 73 | + ignore.Add(line); |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + throw new InvalidIgnoreFileException( |
| 78 | + $""" |
| 79 | + The .csharpierignore file at {ignoreFilePath} could not be parsed due to the following line: |
| 80 | + {line} |
| 81 | + """, |
| 82 | + ex |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
64 | 86 | } |
65 | 87 |
|
66 | | - foreach ( |
67 | | - var line in await fileSystem.File.ReadAllLinesAsync(ignoreFilePath, cancellationToken) |
68 | | - ) |
| 88 | + return new IgnoreFile(ignores); |
| 89 | + } |
| 90 | + |
| 91 | + // this will return the ignore paths in order of priority |
| 92 | + // the first csharpierignore it finds at or above the path |
| 93 | + // and then all .gitignores (at or above) it finds in order from closest to further away |
| 94 | + private static List<string> FindIgnorePaths(string baseDirectoryPath, IFileSystem fileSystem) |
| 95 | + { |
| 96 | + var result = new List<string>(); |
| 97 | + string? foundCSharpierIgnoreFilePath = null; |
| 98 | + var directoryInfo = fileSystem.DirectoryInfo.New(baseDirectoryPath); |
| 99 | + while (directoryInfo != null) |
69 | 100 | { |
70 | | - try |
| 101 | + if (foundCSharpierIgnoreFilePath is null) |
71 | 102 | { |
72 | | - ignore.Add(line); |
| 103 | + var csharpierIgnoreFilePath = fileSystem.Path.Combine( |
| 104 | + directoryInfo.FullName, |
| 105 | + ".csharpierignore" |
| 106 | + ); |
| 107 | + if (fileSystem.File.Exists(csharpierIgnoreFilePath)) |
| 108 | + { |
| 109 | + foundCSharpierIgnoreFilePath = csharpierIgnoreFilePath; |
| 110 | + } |
73 | 111 | } |
74 | | - catch (Exception ex) |
| 112 | + |
| 113 | + var gitIgnoreFilePath = fileSystem.Path.Combine(directoryInfo.FullName, ".gitignore"); |
| 114 | + if (fileSystem.File.Exists(gitIgnoreFilePath)) |
75 | 115 | { |
76 | | - throw new InvalidIgnoreFileException( |
77 | | - @$"The .csharpierignore file at {ignoreFilePath} could not be parsed due to the following line: |
78 | | -{line} |
79 | | -", |
80 | | - ex |
81 | | - ); |
| 116 | + result.Add(gitIgnoreFilePath); |
82 | 117 | } |
83 | | - } |
84 | 118 |
|
85 | | - var directoryName = fileSystem.Path.GetDirectoryName(ignoreFilePath); |
| 119 | + directoryInfo = directoryInfo.Parent; |
| 120 | + } |
86 | 121 |
|
87 | | - ArgumentNullException.ThrowIfNull(directoryName); |
| 122 | + if (foundCSharpierIgnoreFilePath is not null) |
| 123 | + { |
| 124 | + result.Insert(0, foundCSharpierIgnoreFilePath); |
| 125 | + } |
88 | 126 |
|
89 | | - return new IgnoreFile(ignore, directoryName); |
| 127 | + return result; |
90 | 128 | } |
91 | 129 |
|
92 | | - private static string? FindIgnorePath(string baseDirectoryPath, IFileSystem fileSystem) |
| 130 | + // modified from the nuget library to include the directory |
| 131 | + // that the ignore file exists at |
| 132 | + // and to return if this ignore file has a rule for a given path |
| 133 | + private class IgnoreWithBasePath(string basePath) |
93 | 134 | { |
94 | | - var directoryInfo = fileSystem.DirectoryInfo.New(baseDirectoryPath); |
95 | | - while (directoryInfo != null) |
| 135 | + private readonly List<IgnoreRule> Rules = new(); |
| 136 | + |
| 137 | + public (bool hasMatchingRule, bool isIgnored) IsIgnored(string path) |
96 | 138 | { |
97 | | - var ignoreFilePath = fileSystem.Path.Combine( |
98 | | - directoryInfo.FullName, |
99 | | - ".csharpierignore" |
100 | | - ); |
101 | | - if (fileSystem.File.Exists(ignoreFilePath)) |
| 139 | + if (!path.StartsWith(basePath, StringComparison.Ordinal)) |
102 | 140 | { |
103 | | - return ignoreFilePath; |
| 141 | + return (false, false); |
104 | 142 | } |
105 | 143 |
|
106 | | - directoryInfo = directoryInfo.Parent; |
| 144 | + var pathRelativeToIgnoreFile = |
| 145 | + path.Length > basePath.Length |
| 146 | + ? path[(basePath.Length + 1)..].Replace('\\', '/') |
| 147 | + : string.Empty; |
| 148 | + |
| 149 | + var isIgnored = false; |
| 150 | + var hasMatchingRule = false; |
| 151 | + foreach (var rule in this.Rules) |
| 152 | + { |
| 153 | + var isMatch = rule.IsMatch(pathRelativeToIgnoreFile); |
| 154 | + if (isMatch) |
| 155 | + { |
| 156 | + hasMatchingRule = true; |
| 157 | + } |
| 158 | + if (rule.Negate) |
| 159 | + { |
| 160 | + if (isIgnored && isMatch) |
| 161 | + { |
| 162 | + isIgnored = false; |
| 163 | + } |
| 164 | + } |
| 165 | + else if (!isIgnored && isMatch) |
| 166 | + { |
| 167 | + isIgnored = true; |
| 168 | + } |
| 169 | + } |
| 170 | + return (hasMatchingRule, isIgnored); |
107 | 171 | } |
108 | 172 |
|
109 | | - return null; |
| 173 | + public void Add(string rule) |
| 174 | + { |
| 175 | + this.Rules.Add(new IgnoreRule(rule)); |
| 176 | + } |
110 | 177 | } |
111 | 178 | } |
112 | 179 |
|
|
0 commit comments