Skip to content

Commit

Permalink
Skip non-nugetized projects when nugetizer runs for solution
Browse files Browse the repository at this point in the history
Rather than failing ungracefully, just skip those project where we can safely detect nugetizer isn't installed.

Fixes #107
  • Loading branch information
kzu committed Jul 20, 2021
1 parent 181af9a commit 4bddb03
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
37 changes: 37 additions & 0 deletions src/dotnet-nugetize/EmbeddedResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;

static class EmbeddedResource
{
static readonly string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

public static string GetContent(string relativePath)
{
var filePath = Path.Combine(baseDir, Path.GetFileName(relativePath));
if (File.Exists(filePath))
return File.ReadAllText(filePath);

var baseName = Assembly.GetExecutingAssembly().GetName().Name;
var resourceName = relativePath
.TrimStart('.')
.Replace(Path.DirectorySeparatorChar, '.')
.Replace(Path.AltDirectorySeparatorChar, '.');

var manifestResourceName = Assembly.GetExecutingAssembly()
.GetManifestResourceNames().FirstOrDefault(x => x.EndsWith(resourceName));

if (string.IsNullOrEmpty(manifestResourceName))
throw new InvalidOperationException($"Did not find required resource ending in '{resourceName}' in assembly '{baseName}'.");

using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(manifestResourceName);

if (stream == null)
throw new InvalidOperationException($"Did not find required resource '{manifestResourceName}' in assembly '{baseName}'.");

using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
50 changes: 43 additions & 7 deletions src/dotnet-nugetize/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int Run(string[] args)
.Add(" [msbuild args] Examples: ")
.Add(" - Automatically restore: -r")
.Add(" - Set normal MSBuild verbosity: -v:n")
.Add(" - Set minimal MSBuild verbosity: -v:m")
.Add(" - Build configuration: -p:Configuration=Release");

extra = options.Parse(args);
Expand Down Expand Up @@ -116,18 +117,53 @@ int Execute()
if (File.Exists(file))
File.Delete(file);

// Optimize the "build" so that it doesn't actually do a full compile if possible.
var slnTargets = $"after.{Path.GetFileName(project)}.targets";
var deleteSlnTargets = false;
var contentsOnly = false;
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents;Pack\""))
return -1;

if (!File.Exists(file))
try
{
// Re-run requesting contents only.
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize-contents=true -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents\""))
if (project.EndsWith(".sln") && !File.Exists(slnTargets))
{
File.WriteAllText(slnTargets, EmbeddedResource.GetContent("after.sln.targets"), Encoding.UTF8);
deleteSlnTargets = true;
}

// Optimize the "build" so that it doesn't actually do a full compile if possible.
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents;Pack\""))
{
// The error might have been caused by us not being able to write our slnTargets
if (project.EndsWith(".sln") && !deleteSlnTargets)
ColorConsole.WriteLine($"Solution targets '{slnTargets}' already exists. NuGetizing all projects in the solution is therefore required.".Yellow());

return -1;
}

if (!File.Exists(file))
{
// Re-run requesting contents only.
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize-contents=true -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents\""))
{
// The error might have been caused by us not being able to write our slnTargets
if (project.EndsWith(".sln") && !deleteSlnTargets)
ColorConsole.WriteLine($"Solution targets '{slnTargets}' already exists. NuGetizing all projects in the solution is therefore required.".Yellow());

contentsOnly = true;
return -1;
}

contentsOnly = true;
}
}
finally
{
if (File.Exists(slnTargets) && deleteSlnTargets)
File.Delete(slnTargets);
}

if (!File.Exists(file))
{
ColorConsole.WriteLine("Failed to discover nugetized content.".Red());
return -1;
}

var doc = XDocument.Load(file);
Expand Down
17 changes: 17 additions & 0 deletions src/dotnet-nugetize/after.sln.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="RemoveNonNuGetized" BeforeTargets="GetPackageContents">
<MSBuild Properties="BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)"
BuildInParallel="True"
SkipNonexistentProjects="%(ProjectReference.SkipNonexistentProjects)"
Targets="GetTargetPath"
Projects="@(ProjectReference)">
<Output TaskParameter="TargetOutputs" ItemName="ReferencedProjectTargetPath" />
</MSBuild>
<ItemGroup>
<NuGetizedTargetPath Include="@(ReferencedProjectTargetPath -> WithMetadataValue('IsNuGetized', 'true'))" />
<NonNuGetizedTargetPath Include="@(ReferencedProjectTargetPath)" Exclude="@(NuGetizedTargetPath)" />
<ProjectReference Remove="@(NonNuGetizedTargetPath -> '%(MSBuildSourceProjectFile)')" />
</ItemGroup>
</Target>
</Project>
2 changes: 2 additions & 0 deletions src/dotnet-nugetize/dotnet-nugetize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<ItemGroup>
<None Update="NuGetize.Build.targets" CopyToOutputDirectory="PreserveNewest" />
<Compile Remove="Range.cs" Condition="'$(TargetFramework)' != 'netcoreapp2.1'" />
<None Remove="after.sln.targets" />
<EmbeddedResource Include="after.sln.targets" CopyToOutputDirectory="PreserveNewest" />
<ProjectProperty Include="Version" />
<ProjectProperty Include="ToolCommandName" />
<ProjectProperty Include="Product" />
Expand Down

0 comments on commit 4bddb03

Please sign in to comment.