Skip to content

Commit 4bddb03

Browse files
committed
Skip non-nugetized projects when nugetizer runs for solution
Rather than failing ungracefully, just skip those project where we can safely detect nugetizer isn't installed. Fixes #107
1 parent 181af9a commit 4bddb03

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
static class EmbeddedResource
7+
{
8+
static readonly string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
9+
10+
public static string GetContent(string relativePath)
11+
{
12+
var filePath = Path.Combine(baseDir, Path.GetFileName(relativePath));
13+
if (File.Exists(filePath))
14+
return File.ReadAllText(filePath);
15+
16+
var baseName = Assembly.GetExecutingAssembly().GetName().Name;
17+
var resourceName = relativePath
18+
.TrimStart('.')
19+
.Replace(Path.DirectorySeparatorChar, '.')
20+
.Replace(Path.AltDirectorySeparatorChar, '.');
21+
22+
var manifestResourceName = Assembly.GetExecutingAssembly()
23+
.GetManifestResourceNames().FirstOrDefault(x => x.EndsWith(resourceName));
24+
25+
if (string.IsNullOrEmpty(manifestResourceName))
26+
throw new InvalidOperationException($"Did not find required resource ending in '{resourceName}' in assembly '{baseName}'.");
27+
28+
using var stream = Assembly.GetExecutingAssembly()
29+
.GetManifestResourceStream(manifestResourceName);
30+
31+
if (stream == null)
32+
throw new InvalidOperationException($"Did not find required resource '{manifestResourceName}' in assembly '{baseName}'.");
33+
34+
using var reader = new StreamReader(stream);
35+
return reader.ReadToEnd();
36+
}
37+
}

src/dotnet-nugetize/Program.cs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ int Run(string[] args)
4242
.Add(" [msbuild args] Examples: ")
4343
.Add(" - Automatically restore: -r")
4444
.Add(" - Set normal MSBuild verbosity: -v:n")
45+
.Add(" - Set minimal MSBuild verbosity: -v:m")
4546
.Add(" - Build configuration: -p:Configuration=Release");
4647

4748
extra = options.Parse(args);
@@ -116,18 +117,53 @@ int Execute()
116117
if (File.Exists(file))
117118
File.Delete(file);
118119

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

124-
if (!File.Exists(file))
124+
try
125125
{
126-
// Re-run requesting contents only.
127-
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize-contents=true -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents\""))
126+
if (project.EndsWith(".sln") && !File.Exists(slnTargets))
127+
{
128+
File.WriteAllText(slnTargets, EmbeddedResource.GetContent("after.sln.targets"), Encoding.UTF8);
129+
deleteSlnTargets = true;
130+
}
131+
132+
// Optimize the "build" so that it doesn't actually do a full compile if possible.
133+
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents;Pack\""))
134+
{
135+
// The error might have been caused by us not being able to write our slnTargets
136+
if (project.EndsWith(".sln") && !deleteSlnTargets)
137+
ColorConsole.WriteLine($"Solution targets '{slnTargets}' already exists. NuGetizing all projects in the solution is therefore required.".Yellow());
138+
128139
return -1;
140+
}
141+
142+
if (!File.Exists(file))
143+
{
144+
// Re-run requesting contents only.
145+
if (!Execute(DotnetMuxer.Path.FullName, $"msbuild {project} {string.Join(' ', extra)} -p:dotnet-nugetize-contents=true -p:dotnet-nugetize=\"{file}\" -t:\"GetPackageContents\""))
146+
{
147+
// The error might have been caused by us not being able to write our slnTargets
148+
if (project.EndsWith(".sln") && !deleteSlnTargets)
149+
ColorConsole.WriteLine($"Solution targets '{slnTargets}' already exists. NuGetizing all projects in the solution is therefore required.".Yellow());
129150

130-
contentsOnly = true;
151+
return -1;
152+
}
153+
154+
contentsOnly = true;
155+
}
156+
}
157+
finally
158+
{
159+
if (File.Exists(slnTargets) && deleteSlnTargets)
160+
File.Delete(slnTargets);
161+
}
162+
163+
if (!File.Exists(file))
164+
{
165+
ColorConsole.WriteLine("Failed to discover nugetized content.".Red());
166+
return -1;
131167
}
132168

133169
var doc = XDocument.Load(file);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Target Name="RemoveNonNuGetized" BeforeTargets="GetPackageContents">
4+
<MSBuild Properties="BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)"
5+
BuildInParallel="True"
6+
SkipNonexistentProjects="%(ProjectReference.SkipNonexistentProjects)"
7+
Targets="GetTargetPath"
8+
Projects="@(ProjectReference)">
9+
<Output TaskParameter="TargetOutputs" ItemName="ReferencedProjectTargetPath" />
10+
</MSBuild>
11+
<ItemGroup>
12+
<NuGetizedTargetPath Include="@(ReferencedProjectTargetPath -> WithMetadataValue('IsNuGetized', 'true'))" />
13+
<NonNuGetizedTargetPath Include="@(ReferencedProjectTargetPath)" Exclude="@(NuGetizedTargetPath)" />
14+
<ProjectReference Remove="@(NonNuGetizedTargetPath -> '%(MSBuildSourceProjectFile)')" />
15+
</ItemGroup>
16+
</Target>
17+
</Project>

src/dotnet-nugetize/dotnet-nugetize.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
<ItemGroup>
3030
<None Update="NuGetize.Build.targets" CopyToOutputDirectory="PreserveNewest" />
3131
<Compile Remove="Range.cs" Condition="'$(TargetFramework)' != 'netcoreapp2.1'" />
32+
<None Remove="after.sln.targets" />
33+
<EmbeddedResource Include="after.sln.targets" CopyToOutputDirectory="PreserveNewest" />
3234
<ProjectProperty Include="Version" />
3335
<ProjectProperty Include="ToolCommandName" />
3436
<ProjectProperty Include="Product" />

0 commit comments

Comments
 (0)