Skip to content

Commit

Permalink
feat: Specify selective package(s) to update (#85)
Browse files Browse the repository at this point in the history
* feat: Specify package(s) to update
  • Loading branch information
OskarKlintrot authored Jun 1, 2022
1 parent 4d1c02a commit d0e7dad
Show file tree
Hide file tree
Showing 22 changed files with 450 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
with:
dotnet-version: 6.x.x
- name: Run tests
run: dotnet run -- test update-README --parallel
run: dotnet run -- test --parallel
working-directory: tools/Build
22 changes: 22 additions & 0 deletions .github/workflows/update-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: docs

on:
workflow_dispatch:
push:
branches:
- main

jobs:
update-docs:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.x.x
- name: Update README
run: dotnet run -- update-README --parallel
working-directory: tools/Build
3 changes: 2 additions & 1 deletion UpdatR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gh-workflows", "gh-workflow
.github\workflows\post-release.yml = .github\workflows\post-release.yml
.github\workflows\push.yml = .github\workflows\push.yml
.github\workflows\release.yml = .github\workflows\release.yml
.github\workflows\update-docs.yml = .github\workflows\update-docs.yml
.github\workflows\update.yml = .github\workflows\update.yml
EndProjectSection
EndProject
Expand All @@ -45,7 +46,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "images", "images", "{E30AD9
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{1F5D2047-E0F7-40C1-8B53-B72D0FDD0E79}"
ProjectSection(SolutionItems) = preProject
README.source.md = README.source.md
mdsource\README.source.md = mdsource\README.source.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UpdatR.UnitTests", "tests\UpdatR.UnitTests\UpdatR.UnitTests.csproj", "{656203A1-D1BE-4E3D-AA49-68D00A3003CF}"
Expand Down
6 changes: 6 additions & 0 deletions mdsource/cli-readme.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ For larger solutions with multiple packages the console output is not optimal. Y
> update --browser
```

To update only one or more specific packages you can use the `--package` option:

```
> update --package Microsoft.* --package Newtonsoft.*
```

If you don't want to update a package or packages you can exclude them:

```
Expand Down
1 change: 1 addition & 0 deletions src/Build/docs/release-notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Option to update only specified package(s).
20 changes: 12 additions & 8 deletions src/UpdatR/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using NuGet.Credentials;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using UpdatR.Domain;
using UpdatR.Internals;

Expand All @@ -25,13 +24,15 @@ public Updater(ILogger<Updater>? logger = null)
/// </summary>
/// <param name="path">Path to solution or project(s). Leave out if solution or project(s) is in current folder or if project(s) is in subfolders.</param>
/// <param name="excludePackages">Packages to exlude. Supports * as wildcard.</param>
/// <param name="packages">Packages to update. Supports * as wildcard. If <see langword="null"/> or empty then all packages, except <paramref name="excludePackages"/>, will be updated.</param>
/// <param name="dryRun">Do not save any changes.</param>
/// <param name="interactive">Interaction with user is possible.</param>
/// <returns><see cref="Summary"/></returns>
/// <exception cref="ArgumentException"></exception>
public async Task<Summary> UpdateAsync(
string? path = null,
string[]? excludePackages = null,
string[]? packages = null,
bool dryRun = false,
bool interactive = false
)
Expand All @@ -41,15 +42,17 @@ public async Task<Summary> UpdateAsync(
path = Directory.GetCurrentDirectory();
}

var shouldExcludePackage = CreateSearch(excludePackages);
var shouldIncludePackage = CreateSearch(packages, treatNullOrEmptyAs: true);
var shouldExcludePackage = CreateSearch(excludePackages, treatNullOrEmptyAs: false);

var dir = RootDir.Create(path);

var result = new Result(path);

var (packages, unauthorizedSources) = await GetPackageVersions(
var (nugetPackages, unauthorizedSources) = await GetPackageVersions(
dir.Csprojs ?? Array.Empty<Csproj>(),
dir.DotnetTools ?? Array.Empty<DotnetTools>(),
shouldIncludePackage,
shouldExcludePackage,
interactive,
new NuGetLogger(_logger)
Expand All @@ -62,7 +65,7 @@ public async Task<Summary> UpdateAsync(

foreach (var csproj in dir.Csprojs ?? Array.Empty<Csproj>())
{
var project = csproj.UpdatePackages(packages, dryRun, _logger);
var project = csproj.UpdatePackages(nugetPackages, dryRun, _logger);

if (project is not null)
{
Expand All @@ -72,7 +75,7 @@ public async Task<Summary> UpdateAsync(

foreach (var config in dir.DotnetTools ?? Array.Empty<DotnetTools>())
{
var project = await config.UpdatePackagesAsync(packages, dryRun, _logger);
var project = await config.UpdatePackagesAsync(nugetPackages, dryRun, _logger);

if (project is not null)
{
Expand All @@ -83,11 +86,11 @@ public async Task<Summary> UpdateAsync(
return Summary.Create(result);
}

private static Func<string, bool> CreateSearch(string[]? strs)
private static Func<string, bool> CreateSearch(string[]? strs, bool treatNullOrEmptyAs)
{
if (strs is null || strs.Length == 0)
{
return _ => false;
return _ => treatNullOrEmptyAs;
}

var regexes = strs.Select(x => ConvertSearchPatternToRegex(x)).ToList();
Expand All @@ -111,6 +114,7 @@ static Regex ConvertSearchPatternToRegex(string matchAgainst)
> UnauthorizedSources)> GetPackageVersions(
IEnumerable<Csproj> projects,
IEnumerable<DotnetTools> dotnetTools,
Func<string, bool> shouldIncludePackage,
Func<string, bool> shouldExcludePackage,
bool interactive,
NuGet.Common.ILogger nuGetLogger
Expand Down Expand Up @@ -150,7 +154,7 @@ var repo in sourceRepositoryProvider
{
foreach (var packageId in packageIds)
{
if (shouldExcludePackage(packageId))
if (!shouldIncludePackage(packageId) || shouldExcludePackage(packageId))
{
packageSearchMetadata[packageId] = null;

Expand Down
10 changes: 9 additions & 1 deletion src/dotnet-updatr/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal static partial class Program
/// Update all packages in solution or project(s).
/// </summary>
/// <param name="args">Path to solution or project(s). Defaults to current folder. Target can be a specific file or folder. If target is a folder then all *.csproj-files and dontet-config.json-files will be processed.</param>
/// <param name="package">Package to update. Supports * as wildcard. Will update all unless specified.</param>
/// <param name="excludePackage">Package to exlude. Supports * as wildcard.</param>
/// <param name="output">Defaults to "output.md". Explicitly set to fileName.txt to generate plain text instead of markdown.</param>
/// <param name="title">Outputs title to path.</param>
Expand All @@ -29,6 +30,7 @@ internal static partial class Program
/// <exception cref="ArgumentException"></exception>
internal static async Task Main(
string? args = ".",
string[]? package = null,
string[]? excludePackage = null,
string? output = null,
string? title = null,
Expand Down Expand Up @@ -56,7 +58,13 @@ internal static async Task Main(

var update = services.GetRequiredService<Updater>();

var summary = await update.UpdateAsync(args, excludePackage, dryRun, interactive);
var summary = await update.UpdateAsync(
args,
excludePackages: excludePackage,
packages: package,
dryRun,
interactive
);

var outputStr = TextFormatter.PlainText(summary);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
[]

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.1" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.1" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
[
{
PackageId: Dummy.Tool,
Updates: [
{
Item1: 0.0.1,
Item2: 0.0.2,
Item3: src\Dummy.App.csproj
}
]
}
]

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.1" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.2" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
[
{
PackageId: Dummy.Tool,
Updates: [
{
Item1: 0.0.1,
Item2: 0.0.2,
Item3: src\Dummy.App.csproj
}
]
},
{
PackageId: Has.Previews,
Updates: [
{
Item1: 0.0.1,
Item2: 0.0.2,
Item3: src\Dummy.App.csproj
}
]
}
]

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.1" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.2" />
<PackageReference Include="Has.Previews" Version="0.0.2" />
</ItemGroup>
</Project>
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
[
{
PackageId: Dummy.Tool,
Updates: [
{
Item1: 0.0.1,
Item2: 0.0.2,
Item3: src\Dummy.App.csproj
}
]
}
]

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.1" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dummy.Tool" Version="0.0.2" />
<PackageReference Include="Has.Previews" Version="0.0.1" />
</ItemGroup>
</Project>
]
Loading

0 comments on commit d0e7dad

Please sign in to comment.