Roslyn - determine project excluded from build configuration - c#

I am trying to figure out which project is enabled/disabled in respective build configuration/platform setup. Where could I find this "project.BuildsInCurrentConfiguration" information please?
var properties = new Dictionary<string, string>
{
{ "Configuration", "Debug" },
{ "Platform", "x86"}
};
MSBuildWorkspace workspace = MSBuildWorkspace.Create(properties);
workspace.LoadMetadataForReferencedProjects = true;
Solution solution = workspace.OpenSolutionAsync("someSolution.sln").Result;
foreach (Project project in solution.Projects)
Console.Out.WriteLine($"{project.OutputFilePath} is enabled in this build setup: {project.BuildsInCurrentConfiguration}");
workspace.CloseSolution();
I would have thought I wouldn't be offered the projects that are not part of the picked configuration/platform, but solution.Projects shows me all of them regardless build setup.

I don't think Roslyn really has most of that information right now (I'm not sure if it ever would; but I would hope it would). I don't see anything related to a "configuration" for a project with the Roslyn APIs for example. That seems to be delegated to the DTE interfaces. You can get at platform type in a Roslyn project, so conceptually you could only get projects that would apply to a given type of build:
var rolsynProjects = solution.Projects
.Where(p => p.CompilationOptions.Platform == Platform.X86);
but, things like "DEBUG" configuration seem to only be available via DTE--which isn't that hard to get at. e.g.
var project = DTE.Solution.Projects
.Where(p=>p.FullName == rolsynProjects.First().FilePath).FirstOrDefault();
And from that VS project, you can get at its ConfigurationManager

Related

How to get "Include Paths" property of Microsoft Macro Assembler in Visual Studio by a plugin?

I'm workng to get include path resolved by some VS plugin (asm-dude in fact). Include path in microsoft macro assembler looks like this:
includepath
Include file resolve part in asm-dude lies in: https://github.com/HJLebbink/asm-dude/blob/vxix2022-B/VS/CSHARP/asm-dude-vsix/Tools/LabelGraph.cs#L602
Anyway, at the beginning I think I just need to get the value of IncludePath property, and then other things can be done in a minute. But after reading some docs I realized I'm in a mess. It seems that VS prevents me to get names of all properties, but I can only get the value by the name.
Codes I write are like:
DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
Projects projects = dte.Solution.Projects;
if (projects.Count != 0)
{
VCProject project = (VCProject)projects.Item(1).Object;
VCConfiguration cfg = project.ActiveConfiguration;
if (cfg != null)
{
string includePathStr = cfg.GetEvaluatedPropertyValue("IncludePaths");
}
}
but in vein, it gets include path of msvc, not MASM
I cast Project to VCProject because it's a VC project. Although I can iterate properties of a non-VCProject's configuration, but it doesn't seem to work on VCProject, because it doesn't have a (at least not public) member named properties. All these docs tell me that I can only get its value by name, but the problem is I don't know its name. Or I'm completely wrong? I must admit that I'm new to VS plugins.
refs I used so far:
https://learn.microsoft.com/en-us/previous-versions/dn655034(v=vs.140)?redirectedfrom=MSDN
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.vcprojectengine.vcconfiguration?view=visualstudiosdk-2022

Getting the StartArguments from a VSIX plugin for .NET Core projects

EDIT - Looks like the initial problem only applies to .NET Core projects. So the question shifts to "What is the correct way to get the full range of project properties from .NET Core projects?"
I'm writing a Visual Studio 2019 plugin that uses some of the project configuration settings. It seems straight forward enough to get the Project object (C# here, but also C++ & others) and then spelunking the Configuration's for Property objects.
But it appears that accessing most of the properties will throw System.NotImplementedException.
Primary Question: Is there another way to access these settings - like startup arguments and other debugging configuration?
Secondary Question: Are there any good resources on this stuff? The online MS docs are a bit terse for my taste.
void ProcessCSharpProject(VSProject csProj)
{
foreach (var config in csProj.Project.ConfigurationManager.Cast<Configuration>())
{
Property debugInfoProp = config.Properties.Item("DebugInfo");
var debugInfo = debugInfoProp.Value as String; // works
Property startArgsProp = config.Properties.Item("StartArguments");
var startArgs = startArgsProp.Value as String; // NotImplemented
// Another way to access the same thing:
var configProps = config.Object as CSharpProjectConfigurationProperties6;
var startArgs2 = configProps.StartArguments; // Also NotImplemented
}
}
Thanks!

roslyn project configuration

I am using Microsoft.CodeAnalysis and .MSBuild to load up solution, it's projects and retrieve project OutputFilePath. Trouble is Debug and Release have different ones and I can't figure out a way to switch between solution configurations. Any idea how to set which configuration will be used?
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
workspace.LoadMetadataForReferencedProjects = true;
Solution solution = workspace.OpenSolutionAsync("someSolution.sln").Result;
foreach (Project project in solution.Projects)
Console.Out.WriteLine(project.OutputFilePath);
workspace.CloseSolution();
Some MSBuild properties, like typically the output path, depend on the configuration that the project is built with. You have to specify that configuration when you create the workspace.
For example:
var properties = new Dictionary<string, string>
{
{ "Configuration", "Debug" } // Or "Release", or whatever is known to your projects.
// ... more properties that could influence your property,
// e.g. "Platform" ("x86", "AnyCPU", etc.)
};
MSBuildWorkspace workspace = MSBuildWorkspace.Create(properties);
workspace.LoadMetadataForReferencedProjects = true;
Solution solution = workspace.OpenSolutionAsync("someSolution.sln").Result;
foreach (Project project in solution.Projects)
Console.Out.WriteLine(project.OutputFilePath);
workspace.CloseSolution();

Resolving .vcxproj dependencies with c# project class doesn't return included folders

I'm trying to write a tool which will resolve dependencies between c++ and c# projects.
When I try to get folders included in vcxproj, it returns everything but included folders.
I searched, where I would expect to find them:
ItemDefinitions -> ClCompile -> AdditionalIncludeDirectories
But they are not there, or anywhere else.
Project proj = new Project(projectFileName);
ProjectItemDefinition tp = proj.ItemDefinitions.First(pid =>
String.Compare(pid.Key,"ClCompile", true) == 0).Value;
ProjectMetadata tpPMD = tp.Metadata.First(pmd =>
String.Compare(pmd.Name,"AdditionalIncludeDirectories", true) == 0);
As I know, C# project and C++ project have different object. If you want to get the VC++ project you should reference VCProject and VCProjectEngine.
Here is an example about how to get the VC project you can refer to:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.vcproject.aspx

In a MSBuild file how to I get the referenced projects?

I have a MSBuild script file and I want to perform an action for each projects that were imported in the file.
How do I get access to the referenced projects ?
It is not clear what kind of action you want to perform on each project. Assuming you want simply to print out the paths of referenced projects, here is the sample code:
Dictionary<string, string> globalProperties = new Dictionary<string, string>();
globalProperties.Add("Configuraion", "Debug");
globalProperties.Add("Platform", "AnyCPU");
ProjectCollection pc = new ProjectCollection(globalProperties);
Project sln = pc.LoadProject(#"MyProject.csproj", "4.0");
foreach (ProjectItem pi in sln.Items)
{
if (pi.ItemType == "ProjectReference")
{
Console.WriteLine(pi.EvaluatedInclude);
}
}
The code above uses ProjectCollection and Project types from Microsoft.Build.dll, which is part of MSBuild.
Note, that in theory project references depend on build parameters, e.g. you might reference debugging library for Debug configuration, but not for release. Therefore while initializing ProjectCollection you have to pass parameters you want.

Categories