From 3f7670234e49c715e36d4bc345dfe3597cbdfc2e Mon Sep 17 00:00:00 2001 From: Mihai Codoban Date: Fri, 18 May 2018 17:31:56 -0700 Subject: [PATCH 1/4] register from path --- src/MSBuildLocator/MSBuildLocator.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index ce5504ca..3ed523d6 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -69,6 +69,16 @@ public static void RegisterInstance(VisualStudioInstance instance) if (instance == null) throw new ArgumentNullException(nameof(instance)); + RegisterMSbuildPath(instance.MSBuildPath); + } + + /// + /// Add assembly resolution for Microsoft.Build core dlls in the current AppDomain from the specified + /// path. + /// + /// + public static void RegisterMSbuildPath(string msbuildPath) + { var loadedMSBuildAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(IsMSBuildAssembly); if (loadedMSBuildAssemblies.Any()) { @@ -89,7 +99,7 @@ public static void RegisterInstance(VisualStudioInstance instance) var assemblyName = new AssemblyName(eventArgs.Name); if (IsMSBuildAssembly(assemblyName)) { - var targetAssembly = Path.Combine(instance.MSBuildPath, assemblyName.Name + ".dll"); + var targetAssembly = Path.Combine(msbuildPath, assemblyName.Name + ".dll"); return File.Exists(targetAssembly) ? Assembly.LoadFrom(targetAssembly) : null; } From 1ddca2638c2f87d1e1890dd81fffc358f5e62331 Mon Sep 17 00:00:00 2001 From: Mihai Codoban Date: Mon, 21 May 2018 14:40:23 -0700 Subject: [PATCH 2/4] Feedback --- samples/BuilderApp/BuilderApp.csproj | 2 +- samples/BuilderApp/Program.cs | 51 +++++++++++++++++++++------- src/MSBuildLocator/MSBuildLocator.cs | 12 +++++-- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/samples/BuilderApp/BuilderApp.csproj b/samples/BuilderApp/BuilderApp.csproj index a79ef04d..4750408a 100644 --- a/samples/BuilderApp/BuilderApp.csproj +++ b/samples/BuilderApp/BuilderApp.csproj @@ -2,7 +2,7 @@ Exe - net46 + net47 false false diff --git a/samples/BuilderApp/Program.cs b/samples/BuilderApp/Program.cs index 66ce193e..34ee8a56 100644 --- a/samples/BuilderApp/Program.cs +++ b/samples/BuilderApp/Program.cs @@ -24,14 +24,24 @@ private static void Main(string[] args) // 1) Use defaults and call: MSBuildLocator.RegisterDefaults(); // 2) Do something fancier and ask the user. As an example we'll do that. var instances = MSBuildLocator.QueryVisualStudioInstances().ToList(); - var instanceToUse = AskWhichVisualStudioInstanceToUse(instances); + var msbuildDeploymentToUse = AskWhichMSBuildToUse(instances); - // Calling RegisterInstance will subscribe to AssemblyResolve event. After this we can now + // Calling Register methods will subscribe to AssemblyResolve event. After this we can // safely call code that use MSBuild types (in the Builder class). - MSBuildLocator.RegisterInstance(instanceToUse); + if (msbuildDeploymentToUse.VSInstance != null) + { + Console.WriteLine($"Using MSBuild deployment from VS Instance: {msbuildDeploymentToUse.VSInstance.Name} - {msbuildDeploymentToUse.VSInstance.Version}"); + Console.WriteLine(); - Console.WriteLine($"Using VS Instance: {instanceToUse.Name} - {instanceToUse.Version}"); - Console.WriteLine(); + MSBuildLocator.RegisterInstance(msbuildDeploymentToUse.VSInstance); + } + else + { + Console.WriteLine($"Using MSBuild deployment from path: {msbuildDeploymentToUse.MSBuildPath}"); + Console.WriteLine(); + + MSBuildLocator.RegisterMSBuildPath(msbuildDeploymentToUse.MSBuildPath); + } var result = new Builder().Build(projectFilePath); Console.WriteLine(); @@ -41,14 +51,14 @@ private static void Main(string[] args) Console.ResetColor(); } - private static VisualStudioInstance AskWhichVisualStudioInstanceToUse(List instances) + private static (VisualStudioInstance VSInstance, string MSBuildPath) AskWhichMSBuildToUse(List instances) { if (instances.Count == 0) { - Console.WriteLine("MSBuild not found! Exiting."); - Environment.Exit(-1); + Console.WriteLine("No Visual Studio instances found!"); } + Console.WriteLine($"0) Custom path"); for (var i = 1; i <= instances.Count; i++) { var instance = instances[i - 1]; @@ -65,11 +75,28 @@ private static VisualStudioInstance AskWhichVisualStudioInstanceToUse(List 0 && instanceChoice <= instances.Count) + if (int.TryParse(answer, out int instanceChoice) && instanceChoice >= 0 && instanceChoice <= instances.Count) { - instanceUsed = instances[instanceChoice - 1]; + if (instanceChoice == 0) + { + Console.WriteLine("Input path to MSBuild deployment:"); + var msbuildPath = Console.ReadLine(); + + if (!Directory.Exists(msbuildPath)) + { + Console.WriteLine($"Directory does not exist: {msbuildPath}"); + Environment.Exit(-1); + } + + return (null, msbuildPath); + + } + else + { + var instanceUsed = instances[instanceChoice - 1]; + return (instanceUsed, null); + } } else { @@ -77,7 +104,7 @@ private static VisualStudioInstance AskWhichVisualStudioInstanceToUse(List /// Add assembly resolution for Microsoft.Build core dlls in the current AppDomain from the specified /// path. /// - /// - public static void RegisterMSbuildPath(string msbuildPath) + /// + /// Path to the directory containing a deployment of MSBuild binaries. + /// A minimal MSBuild deployment would be the publish result of the Microsoft.Build.Runtime package. + /// + /// In order to restore and build real projects, one needs a deployment that contains the rest of the toolchain (nuget, compilers, etc.). + /// Such deployments can be found in installations such as Visual Studio or dotnet CLI. + /// + public static void RegisterMSBuildPath(string msbuildPath) { var loadedMSBuildAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(IsMSBuildAssembly); if (loadedMSBuildAssemblies.Any()) From 9b2ef8462ac294ac073209c2e3d02977d0e9c19e Mon Sep 17 00:00:00 2001 From: Mihai Codoban Date: Mon, 21 May 2018 15:12:04 -0700 Subject: [PATCH 3/4] Target 471 to avoid some 47 error --- samples/BuilderApp/BuilderApp.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/BuilderApp/BuilderApp.csproj b/samples/BuilderApp/BuilderApp.csproj index 4750408a..27912763 100644 --- a/samples/BuilderApp/BuilderApp.csproj +++ b/samples/BuilderApp/BuilderApp.csproj @@ -2,7 +2,7 @@ Exe - net47 + net471 false false From 8227282201ab22d49733fe1227b904be8bf19f7e Mon Sep 17 00:00:00 2001 From: Mihai Codoban Date: Tue, 29 May 2018 14:30:44 -0700 Subject: [PATCH 4/4] Feedback --- samples/BuilderApp/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/BuilderApp/Program.cs b/samples/BuilderApp/Program.cs index 34ee8a56..0ec2f0bc 100644 --- a/samples/BuilderApp/Program.cs +++ b/samples/BuilderApp/Program.cs @@ -30,14 +30,14 @@ private static void Main(string[] args) // safely call code that use MSBuild types (in the Builder class). if (msbuildDeploymentToUse.VSInstance != null) { - Console.WriteLine($"Using MSBuild deployment from VS Instance: {msbuildDeploymentToUse.VSInstance.Name} - {msbuildDeploymentToUse.VSInstance.Version}"); + Console.WriteLine($"Using MSBuild from VS Instance: {msbuildDeploymentToUse.VSInstance.Name} - {msbuildDeploymentToUse.VSInstance.Version}"); Console.WriteLine(); MSBuildLocator.RegisterInstance(msbuildDeploymentToUse.VSInstance); } else { - Console.WriteLine($"Using MSBuild deployment from path: {msbuildDeploymentToUse.MSBuildPath}"); + Console.WriteLine($"Using MSBuild from path: {msbuildDeploymentToUse.MSBuildPath}"); Console.WriteLine(); MSBuildLocator.RegisterMSBuildPath(msbuildDeploymentToUse.MSBuildPath);