Skip to content

Commit

Permalink
Support for ignoring custom directories
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Jun 21, 2023
1 parent eb74790 commit 3a67c6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ Get additional help using the `help` option:

```console
nupu --help
```

### Ignoring directories

Out of the box, NuPU ignores a list of directories to process like `bin`, `.git`, `packages`, and similar. You can override this behavior by creating a file named `.nupuignore` in a root folder you want to run the NuPU command in:

```
.git
.github
.vs
.vscode
bin
obj
packages
node_modules
my_custom_folder
```
34 changes: 32 additions & 2 deletions src/NuPU/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
: updateCommandSettings.Directory;

var rootDir = new DirectoryInfo(rootPath);
var ignoreDirs = ResolveIgnoreDirs(rootPath);

var csProjFiles = rootDir.EnumerateFiles("*.csproj", new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = updateCommandSettings.Recursive });
var ignoreDirs = new[] { ".git", ".github", ".vs", ".vscode", "bin", "obj", "packages", "node_modules" };
foreach (var csProjFile in csProjFiles.Where(f => !ignoreDirs.Contains(f.DirectoryName)))
foreach (var csProjFile in csProjFiles.Where(f => !Ignored(f, ignoreDirs)))
{
var settings = Settings.LoadDefaultSettings(csProjFile.Directory.FullName);
var enabledSources = SettingsUtility.GetEnabledSources(settings);
Expand Down Expand Up @@ -178,6 +179,35 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
return 0;
}

private bool Ignored(FileInfo fileInfo, List<string> ignoreDirs)
{
if (ignoreDirs.Count == 0) return false;

var directory = fileInfo.Directory;
while (directory != null)
{
if (ignoreDirs.Contains(directory.Name)) return true;
directory = directory.Parent;
}

return false;
}

private List<string> ResolveIgnoreDirs(string rootPath)
{
var ignoreDirs = new List<string> { ".git", ".github", ".vs", ".vscode", "bin", "obj", "packages", "node_modules" };

var nupuIgnore = Path.Combine(rootPath, ".nupuignore");
if (File.Exists(nupuIgnore))
{
AnsiConsole.MarkupLine($"Ignore directories in [yellow]{nupuIgnore}[/]");
var lines = File.ReadAllLines(nupuIgnore);
ignoreDirs = lines.Where(l => !string.IsNullOrWhiteSpace(l)).ToList();
}

return ignoreDirs;
}

private static IEnumerable<NuGetVersion> HighestRevision(IEnumerable<NuGetVersion> versions, NuGetVersion nugetVersion)
{
var toReturn = new List<NuGetVersion>();
Expand Down

0 comments on commit 3a67c6e

Please sign in to comment.