Skip to content

Commit

Permalink
Add option to specify VisualStudioVersion in header (#314)
Browse files Browse the repository at this point in the history
Adds command-line argument `--vsversion`. When present, the version of Visual Studio used to launch the solution is included in the solution file header. If a value is specified, that version is used instead.

Fixes #308
  • Loading branch information
jeffkl authored Nov 22, 2021
1 parent a224cf8 commit e9b8452
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 3,965 deletions.
1 change: 0 additions & 1 deletion Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@
<GlobalPackageReference Include="Microsoft.VisualStudioEng.MicroBuild.Core" Version="1.0.0" />
<GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.4.244" />
<GlobalPackageReference Include="StyleCop.Analyzers" Version="1.1.118" />
<GlobalPackageReference Include="MSBuildSummaryFiles" Version="1.0.23-preview-gb3e57ec836" Condition="'$(EnableBuildSummaryFile)' != 'false'" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Options:
-v|--verbosity Display this amount of information in the event log. The available verbosity levels are:
  q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
--version Display version information only.
--vsversion Specifies that a version of Visual Studio should be included in the solution file. When specified with no value, the value will be set to the version of Visual Studio that is used to open the solution.
-?|-h|--help Show help information.
```

Expand Down
525 changes: 0 additions & 525 deletions src/Microsoft.VisualStudio.SlnGen.Extension/build.summary.yml

This file was deleted.

302 changes: 0 additions & 302 deletions src/Microsoft.VisualStudio.SlnGen.Tool/build.summary.yml

This file was deleted.

34 changes: 34 additions & 0 deletions src/Microsoft.VisualStudio.SlnGen.UnitTests/SlnFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,40 @@ public void WithFoldersIgnoreMainProject()
}
}

[Fact]
public void VisualStudioVersionIsWritten()
{
string solutionFilePath = GetTempFileName();

SlnFile slnFile = new SlnFile
{
VisualStudioVersion = new Version(1, 2, 3, 4),
SolutionGuid = new Guid("{6370DE27-36B7-44AE-B47A-1ECF4A6D740A}"),
};

slnFile.Save(solutionFilePath, useFolders: false);

File.ReadAllText(solutionFilePath).ShouldBe(
@"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 1
VisualStudioVersion = 1.2.3.4
MinimumVisualStudioVersion = 10.0.40219.1
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6370DE27-36B7-44AE-B47A-1ECF4A6D740A}
EndGlobalSection
EndGlobal
",
StringCompareShould.IgnoreLineEndings);
}

private void ValidateProjectInSolution(Action<SlnProject, ProjectInSolution> customValidator, SlnProject[] projects, bool folders)
{
string solutionFilePath = GetTempFileName();
Expand Down
1,906 changes: 0 additions & 1,906 deletions src/Microsoft.VisualStudio.SlnGen.UnitTests/build.summary.yml

This file was deleted.

26 changes: 26 additions & 0 deletions src/Microsoft.VisualStudio.SlnGen/ProgramArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ public sealed class ProgramArguments
Description = @"Display version information only.")]
public bool Version { get; set; }

/// <summary>
/// Gets or sets the Visual Studio version to include in the solution file header.
/// </summary>
[Option(
"--vsversion",
CommandOptionType.SingleOrNoValue,
Description = "Specifies that a version of Visual Studio should be included in the solution file. When specified with no value, the value will be set to the version of Visual Studio that is used to open the solution.")]
public (bool HasValue, string Version) VisualStudioVersion { get; set; }

/// <summary>
/// Gets or sets a <see cref="Func{ProgramArguments,IConsole,Int32}" /> to execute.
/// </summary>
Expand Down Expand Up @@ -308,6 +317,23 @@ public IDictionary<string, string> GetGlobalProperties()
/// <returns>An <see cref="IReadOnlyCollection{T}" /> containing the unique values for Platform.</returns>
public IReadOnlyCollection<string> GetPlatforms() => Platform.SplitValues();

/// <summary>
/// Gets the full path to devenv.exe based on the specified arguments.
/// </summary>
/// <param name="visualStudioInstance">An optional <see cref="VisualStudioInstance" />.</param>
/// <returns>The full path to devenv.exe if one is available, otherwise <c>null</c>.</returns>
public string GetDevEnvFullPath(VisualStudioInstance visualStudioInstance)
{
string devEnvFullPath = DevEnvFullPath?.LastOrDefault();

if (devEnvFullPath.IsNullOrWhiteSpace() && visualStudioInstance != null)
{
devEnvFullPath = Path.Combine(visualStudioInstance.InstallationPath, "Common7", "IDE", "devenv.exe");
}

return devEnvFullPath;
}

/// <summary>
/// Executes the current program.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions src/Microsoft.VisualStudio.SlnGen/SlnFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Build.Evaluation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -99,6 +100,11 @@ public SlnFile()
/// </summary>
public IReadOnlyDictionary<string, Guid> ExistingProjectGuids { get; set; }

/// <summary>
/// Gets or sets an optional minumal Visual Studio version for the solution file.
/// </summary>
public string MinumumVisualStudioVersion { get; set; } = "10.0.40219.1";

/// <summary>
/// Gets or sets a <see cref="IReadOnlyCollection{String}" /> of Platform values to use.
/// </summary>
Expand All @@ -114,6 +120,11 @@ public SlnFile()
/// </summary>
public IReadOnlyCollection<string> SolutionItems => _solutionItems;

/// <summary>
/// Gets or sets an optional Visual Studio version for the solution file.
/// </summary>
public Version VisualStudioVersion { get; set; }

/// <summary>
/// Generates a solution file.
/// </summary>
Expand Down Expand Up @@ -164,6 +175,26 @@ public static (string solutionFileFullPath, int customProjectTypeGuidCount, int
Configurations = arguments.GetConfigurations(),
};

if (arguments.VisualStudioVersion.HasValue)
{
if (arguments.VisualStudioVersion.Version != null && Version.TryParse(arguments.VisualStudioVersion.Version, out Version version))
{
solution.VisualStudioVersion = version;
}

if (solution.VisualStudioVersion == null)
{
string devEnvFullPath = arguments.GetDevEnvFullPath(Program.CurrentDevelopmentEnvironment.VisualStudio);

if (!devEnvFullPath.IsNullOrWhiteSpace() && File.Exists(devEnvFullPath))
{
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(devEnvFullPath);

solution.VisualStudioVersion = new Version(fileVersionInfo.ProductMajorPart, fileVersionInfo.ProductMinorPart, fileVersionInfo.ProductBuildPart, fileVersionInfo.FilePrivatePart);
}
}
}

if (SlnFile.TryParseExistingSolution(solutionFileFullPath, out Guid solutionGuid, out IReadOnlyDictionary<string, Guid> projectGuidsByPath))
{
logger.LogMessageNormal("Updating existing solution file and reusing Visual Studio cache");
Expand Down Expand Up @@ -356,6 +387,13 @@ internal void Save(string rootPath, TextWriter writer, bool useFolders, bool col
{
writer.WriteLine(Header, _fileFormatVersion);

if (VisualStudioVersion != null)
{
writer.WriteLine($"# Visual Studio Version {VisualStudioVersion.Major}");
writer.WriteLine($"VisualStudioVersion = {VisualStudioVersion}");
writer.WriteLine($"MinimumVisualStudioVersion = {MinumumVisualStudioVersion}");
}

if (SolutionItems.Count > 0)
{
writer.WriteLine($@"Project(""{SlnFolder.FolderProjectTypeGuidString}"") = ""Solution Items"", ""Solution Items"", ""{{B283EBC2-E01F-412D-9339-FD56EF114549}}"" ");
Expand Down
4 changes: 1 addition & 3 deletions src/Microsoft.VisualStudio.SlnGen/VisualStudioLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static bool TryLaunch(ProgramArguments arguments, VisualStudioInstance vi
return true;
}

string devEnvFullPath = arguments.DevEnvFullPath?.LastOrDefault();
string devEnvFullPath = arguments.GetDevEnvFullPath(visualStudioInstance);

if (!devEnvFullPath.IsNullOrWhiteSpace())
{
Expand All @@ -64,8 +64,6 @@ public static bool TryLaunch(ProgramArguments arguments, VisualStudioInstance vi
return false;
}

devEnvFullPath = Path.Combine(visualStudioInstance.InstallationPath, "Common7", "IDE", "devenv.exe");

if (!File.Exists(devEnvFullPath))
{
logger.LogError($"The specified path to Visual Studio ({devEnvFullPath}) does not exist or is inaccessible.");
Expand Down
Loading

0 comments on commit e9b8452

Please sign in to comment.