Hi I am having a problem, with a custom build task inside of a Visual Studio Extension. I need to identify projects of my custom project type. I can do this fine if they are on the root of the solution, but the problem occurs when it is inside of a solution folder. I can get the solution folder as a EnvDTE.Project, but am not sure how to get projects from within that folder.
I thought I would be able to get it from the projects Collection property but that is null.
Any assistance would be greatly appreciated.
if (Scope == EnvDTE.vsBuildScope.vsBuildScopeSolution)
{
DTE2 dte2 = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;
var sol = dte2.Solution;
EnvDTE.DTE t = dte2.DTE;
var x = t.Solution.Projects;
foreach(var proj in x)
{
try
{
var project = proj as EnvDTE.Project;
var guid = GetProjectTypeGuids(project);
if (guid.Contains("FOLDERGUID"))
{
//here is where I would get the project from the folder
}
I managed to resolve this with a bit more research and some trial and error. In case anybody else comes up with this problem, I changed the main code to
if (Scope == EnvDTE.vsBuildScope.vsBuildScopeSolution)
{
errorListProvider.Tasks.Clear();
DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
var sol = dte2.Solution;
var projs = sol.Projects;
foreach(var proj in sol)
{
var project = proj as Project;
if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
var innerProjects = GetSolutionFolderProjects(project);
foreach(var innerProject in innerProjects)
{
//carry out actions here.
}
}
}
}
The code for the GetSolutionFolderForProjects was
private IEnumerable<Project> GetSolutionFolderProjects(Project project)
{
List<Project> projects = new List<Project>();
var y = (project.ProjectItems as ProjectItems).Count;
for(var i = 1; i <= y; i++)
{
var x = project.ProjectItems.Item(i).SubProject;
var subProject = x as Project;
if (subProject != null)
{
//Carried out work and added projects as appropriate
}
}
return projects;
}
Hope this helps somebody else.
I had a similar question within a T4 template, where I had to find a project by name within the solution, at any level: root, folder, nested folder.
For reference purposes, I'm pasting it here. It's totally based on the solution from #DaveGreen, so credits to him:
<## import namespace="System.Linq" #>
<#
var dte = (DTE)hostServiceProvider.GetService(typeof(DTE));
var project = GetProject(dte.Solution, "ProjectName");
#>
<#+
public static Project GetProject(Solution solution, string name)
{
var project = GetProject(solution.Projects.OfType<Project>(), name);
if (project == null)
{
throw new Exception($"Project {name} not found in solution");
}
return project;
}
public static Project GetProject(IEnumerable<Project> projects, string name)
{
foreach (Project project in projects)
{
var projectName = project.Name;
if (projectName == name)
{
return project;
}
else if (project.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder)
{
var subProjects = project
.ProjectItems
.OfType<ProjectItem>()
.Where(item => item.SubProject != null)
.Select(item => item.SubProject);
var projectInFolder = GetProject(subProjects, name);
if (projectInFolder != null)
{
return projectInFolder;
}
}
}
return null;
}
#>
Related
I have this problem when I'm trying to read JSON file (or any file): It's not able to find that file. I try everything, even the absolute path (error almost same - DirectoryNotFound)
This is structure of mine code:
And this is code:
private void LoadJson()
{
using (var r = new StreamReader("quizQuestions.json"))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<Questions>>(json);
}
}
I I even try to use Directory.GetCurrentDirectory() but it's returning : / -> only this character. I don't know where is a mistake or if I forgot to set something. I try to find answers everywhere but I was not able to find anything with this.
Make sure the Build Action of the file is set as Content or as an Asset and give this a try.
private void LoadJson()
{
AssetManager assets = this.Assets;
using (var r = new StreamReader(assets.Open ("quizQuestions.json")))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<Questions>>(json);
}
}
You can configure the file as Embedded Resource and then access it like this:
public static Stream GetEmbeddedResourceStream(Assembly assembly, string resourceFileName)
{
var resourceNames = assembly.GetManifestResourceNames();
var resourcePaths = resourceNames
.Where(x => x.EndsWith(resourceFileName, StringComparison.CurrentCultureIgnoreCase)).ToArray();
if (resourcePaths.Any() && resourcePaths.Count() == 1)
{
return assembly.GetManifestResourceStream(resourcePaths.Single());
}
return null; // or throw Exception
}
private void LoadJson()
{
Assembly assembly = GetAssemblyContainingTheJson();
using (var r = GetEmbeddedResourceStream(assembly, "quizQuestions.json"))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<Questions>>(json);
}
}
Trying to get the appconfig path from the debug environment for a component but I keep getting nulls errors from this when I build the solution is vs2017. any help appreciated.
public string GetAppConfigPath()
{
var devenv = (DTE)_c.Site.GetService(typeof(DTE));
var projects = (Array)devenv.ActiveSolutionProjects;
var activeProject = (Project)projects.GetValue(0);
foreach (ProjectItem item in activeProject.ProjectItems)
{
if (!item.Name.Equals("app.config")) continue;
var info = new System.IO.FileInfo(activeProject.FullName);
if (info.Directory != null)
return info.Directory.FullName + "\\" + item.Name;
}
return null;
}
I try to create a project automated with DTE this work perfect but i cannot add a nuget package...
Option1 (InstallNuGetPackage code below)
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
//componentModel is always null
I have installed this nuget package
NuGet.VisualStudio 4.0.0
And add following framework references
Microsoft.VisualStudio.ComponentModelHost 15.0.0.0
Microsoft.VisualStudio.Shell.15.0 15.0.0.0
I have found this example but is not work
http://tylerhughes.info/archive/2015/05/06/installing-a-nuget-package-programmatically/
Option2 (Add a own package.config)
I have also try with creating the packages.config xml but then i have no references to this package and must edit the csproj...
public string GetPackagesConfig()
{
var sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.AppendLine("<packages>");
sb.AppendLine("<package id=\"log4net\" version=\"2.0.8\" targetFramework=\"net461\" />");
sb.AppendLine("</packages>");
return sb.ToString();
//Add file to project
}
Visual Studio control
var type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
var obj = Activator.CreateInstance(type, true);
this._applicationObject = (DTE2)obj;
InstallNuGetPackage
public bool InstallNuGetPackage(EnvDTE.Project project, string package)
{
bool installedPkg = true;
try
{
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInstallerServices>();
if (!installerServices.IsPackageInstalled(project, package))
{
var installer = componentModel.GetService<IVsPackageInstaller>();
installer.InstallPackage(null, project, package, (System.Version)null, false);
}
}
catch (Exception ex)
{
installedPkg = false;
}
return installedPkg;
}
Create Project
private void CreateProject(string projectSubFolder, string projectName)
{
Solution2 solution2;
string solutionFileFullName;
string solutionFolderFullName;
string projectFolderFullName;
try
{
solution2 = (Solution2)_applicationObject.Solution;
// Get the full name of the solution file
solutionFileFullName = solution2.FileName;
// Get the full name of the solution folder
solutionFolderFullName = Path.GetDirectoryName(solutionFileFullName);
// Compose the full name of the project folder
projectFolderFullName = Path.Combine(solutionFolderFullName, projectSubFolder);
if (!(projectFolderFullName.EndsWith("\\")))
{
projectFolderFullName += "\\";
}
var programfiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
var template = #"Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates\CSharp\Windows\1033\ClassLibrary\csClassLibrary.vstemplate";
var projectTemplateFileName = Path.Combine(programfiles, template);
// Add the project
solution2.AddFromTemplate(projectTemplateFileName, projectFolderFullName, projectName, false);
//Save
_applicationObject.Solution.SaveAs(_solutionFullFileName);
}
catch (Exception exception)
{
Log.Error(nameof(CreateProject), exception);
}
}
With this example you can open the package manager console window and send a install-package command.
var packageManagerConsoleGuid = "{0AD07096-BBA9-4900-A651-0598D26F6D24}";
var window = this._visualStudioInstance.Windows.Item(packageManagerConsoleGuid);
window.Activate();
var commandName = "View.PackageManagerConsole";
var nugetCommand = "install-package log4net -ProjectName DemoProject";
this._visualStudioInstance.ExecuteCommand(commandName, nugetCommand);
I develop a project to automate create solution with projects you can found it here
Nager.TemplateBuilder
This example create a Windows Desktop Application with two nuget packages
//Configure Project
var demoProject = new ProjectInfo($"DemoProject", ProjectTemplate.WindowsClassicDesktopWindowsFormsApp);
demoProject.NugetPackages = new List<string> { "System.ServiceModel.NetTcp", "System.Runtime.Serialization.Xml" };
//Configure Solution
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var solutionInfo = new SolutionInfo("Test", folder);
solutionInfo.ProjectInfos.Add(demoProject);
//Start building machine
var buildingMachine = new SolutionBuildingMachine();
buildingMachine.Build(solutionInfo);
buildingMachine.Dispose();
I've been following MSDN's Hello World guide to developing Visual Studio extensions (this article specifically deals with creating one as a Visual Studio toolbar command).
I am trying to list all projects contained in the current/active solution.
In the auto generated code for the Command template.
I have tried EnvDTE's Solution's Projects property, but it shows zero projects.
There is a ActiveSolutionProjects property as well, but it also shows an empty array.
How is this achieved ?
P.S.: I tried both DTE and DTE2 interfaces since it is confusing understanding which version to use, from the docs. I get a null service for DTE2, so I am going with DTE.
My Solution Explorer looks like:
Update: Bert Huijben, from gitter/extendvs, suggested the following, found at the VSSDK Extensibility Samples - but this too does not work (returns 0 elements, both within the constructor and within the callback function):
private Hashtable GetLoadedControllableProjectsEnum()
{
Hashtable mapHierarchies = new Hashtable();
IVsSolution sol = (IVsSolution)this.ServiceProvider.GetService(typeof(SVsSolution));
Guid rguidEnumOnlyThisType = new Guid();
IEnumHierarchies ppenum = null;
ErrorHandler.ThrowOnFailure(sol.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION, ref rguidEnumOnlyThisType, out ppenum));
IVsHierarchy[] rgelt = new IVsHierarchy[1];
uint pceltFetched = 0;
while (ppenum.Next(1, rgelt, out pceltFetched) == VSConstants.S_OK &&
pceltFetched == 1)
{
IVsSccProject2 sccProject2 = rgelt[0] as IVsSccProject2;
if (sccProject2 != null)
{
mapHierarchies[rgelt[0]] = true;
}
}
return mapHierarchies;
}
To get the EnvDTE.Project objects:
static private void FindProjectsIn(EnvDTE.ProjectItem item, List<EnvDTE.Project> results)
{
if (item.Object is EnvDTE.Project)
{
var proj = (EnvDTE.Project)item.Object;
if (new Guid(proj.Kind) != Utilities.ProjectTypeGuids.Folder)
{
results.Add((EnvDTE.Project)item.Object);
}
else
{
foreach (EnvDTE.ProjectItem innerItem in proj.ProjectItems)
{
FindProjectsIn(innerItem, results);
}
}
}
if (item.ProjectItems != null)
{
foreach (EnvDTE.ProjectItem innerItem in item.ProjectItems)
{
FindProjectsIn(innerItem, results);
}
}
}
static private void FindProjectsIn(EnvDTE.UIHierarchyItem item, List<EnvDTE.Project> results)
{
if (item.Object is EnvDTE.Project)
{
var proj = (EnvDTE.Project)item.Object;
if (new Guid(proj.Kind) != Utilities.ProjectTypeGuids.Folder)
{
results.Add((EnvDTE.Project)item.Object);
}
else
{
foreach (EnvDTE.ProjectItem innerItem in proj.ProjectItems)
{
FindProjectsIn(innerItem, results);
}
}
}
foreach (EnvDTE.UIHierarchyItem innerItem in item.UIHierarchyItems)
{
FindProjectsIn(innerItem, results);
}
}
static internal IEnumerable<EnvDTE.Project> GetEnvDTEProjectsInSolution()
{
List<EnvDTE.Project> ret = new List<EnvDTE.Project>();
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE));
EnvDTE.UIHierarchy hierarchy = dte.ToolWindows.SolutionExplorer;
foreach (EnvDTE.UIHierarchyItem innerItem in hierarchy.UIHierarchyItems)
{
FindProjectsIn(innerItem, ret);
}
return ret;
}
Notably recursion is necessary to dig into solution folders.
If you just want the file paths you can do this w/o using DTE:
static internal string[] GetProjectFilesInSolution()
{
IVsSolution sol = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution)) as IVsSolution;
uint numProjects;
ErrorHandler.ThrowOnFailure(sol.GetProjectFilesInSolution((uint)__VSGETPROJFILESFLAGS.GPFF_SKIPUNLOADEDPROJECTS, 0, null, out numProjects));
string[] projects = new string[numProjects];
ErrorHandler.ThrowOnFailure(sol.GetProjectFilesInSolution((uint)__VSGETPROJFILESFLAGS.GPFF_SKIPUNLOADEDPROJECTS, numProjects, projects, out numProjects));
//GetProjectFilesInSolution also returns solution folders, so we want to do some filtering
//things that don't exist on disk certainly can't be project files
return projects.Where(p => !string.IsNullOrEmpty(p) && System.IO.File.Exists(p)).ToArray();
}
Unfortunately could not find any working solution here, decided to post my own solution:
/// <summary>
/// Queries for all projects in solution, recursively (without recursion)
/// </summary>
/// <param name="sln">Solution</param>
/// <returns>List of projects</returns>
static List<Project> GetProjects(Solution sln)
{
List<Project> list = new List<Project>();
list.AddRange(sln.Projects.Cast<Project>());
for (int i = 0; i < list.Count; i++)
// OfType will ignore null's.
list.AddRange(list[i].ProjectItems.Cast<ProjectItem>().Select(x => x.SubProject).OfType<Project>());
return list;
}
And if you don't know what references / namespaces to add, you can pick up project with source code from here:
https://github.com/tapika/cppscriptcore/blob/2a73f45474c8b2179774fd4715b8d8e80080f3ae/Tools/vsStart/Program.cs#L478
And check namespaces / references.
Works for me:
Add a field in your package for dte.
Get the DTE service.
Reference the Solution.
using EnvDTE;
using EnvDTE80;
In your constructor:
dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
In your command handler:
Integer count = ((EnvDTE.SolutionClass)dte.Solution).Projects.Count;
I get the correct count from this.
Screenshot (requested)
Code
//------------------------------------------------------------------------------
// <copyright file="Command1.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE;
using EnvDTE80;
namespace SolExpExt
{
internal sealed class Command1
{
public const int CommandId = 0x0100;
public static readonly Guid CommandSet = new Guid("beff5a1a-dff5-4f6a-95c8-fd7ea7411a7b");
private DTE2 dte;
private readonly Package package;
private IVsSolution sol;
private Command1(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
}
public static Command1 Instance
{
get;
private set;
}
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
public static void Initialize(Package package)
{
Instance = new Command1(package);
}
private void MenuItemCallback(object sender, EventArgs e)
{
string message = $"There are {dte.Solution.Projects.Count} projects in this solution.";
string title = "Command1";
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
message,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
}
}
The following, shamelessly taken from AutoFindReplace, works using VS2015 Community:
using EnvDTE;
.
.
protected override void Initialize()
{
base.Initialize();
IServiceContainer serviceContainer = this as IServiceContainer;
dte = serviceContainer.GetService(typeof(SDTE)) as DTE;
var solutionEvents = dte.Events.SolutionEvents;
solutionEvents.Opened += OnSolutionOpened;
var i = dte.Solution.Projects.Count; // Happy days !
}
All the code lines above pre-exist in the solution within VSPackage.cs except for "var i = dte.Solution.Projects.Count;" which I added locally to VSPackage.cs just after line 44. I then open the solution, hit F5, and within the Experimental Instance I opened JoePublic.Sln and hey presto the count was '2' correctly - Bingo ! Happy days !
Currently I'm working on a custom importer for Ironpython, which should add an abstraction layer for writing custom importer. The abstraction layer is an IronPython module, which bases on PEP 302 and the IronPython zipimporter module. The architecture looks like this:
For testing my importer code, I've written a simple test package with modules, which looks like this:
/Math/
__init__.py
/MathImpl/
__init__.py
__Math2__.py
/Math/__init__.py:
print ('Import: /Math/__init__.py')
/Math/MathImpl/__init__.py:
# Sample math package
print ('Begin import /Math/MathImpl/__init__.py')
import Math2
print ('End import /Math/MathImpl/__init__.py: ' + str(Math2.add(1, 2)))
/Math/MathImpl/Math2.py:
# Add two values
def add(x, y):
return x + y
print ('Import Math2.py!')
If i try to import MathImpl like this in a script: import Math.MathImpl
My genericimporter get's called and searchs for some module/package in the find_module method. Which returns an instance of the importer if found, else not:
public object find_module(CodeContext/*!*/ context, string fullname, params object[] args)
{
// Set module
if (fullname.Contains("<module>"))
{
throw new Exception("Why, why does fullname contains <module>?");
}
// Find resolver
foreach (var resolver in Host.Resolver)
{
var res = resolver.GetModuleInformation(fullname);
// If this script could be resolved by some resolver
if (res != ResolvedType.None)
{
this.resolver = resolver;
return this;
}
}
return null;
}
If find_module is called the first time,fullname contains Math, which is ok, because Math should be imported first. The second time find_module is called, Math.MathImpl should be imported, the problem here is, that fullname has now the value <module>.MathImpl, instead of Math.MathImpl.
My idea was, that the module name (__name__) is not set correctly when Math was imported, but i set this in any case when importing the module in load_module:
public object load_module(CodeContext/*!*/ context, string fullname)
{
string code = null;
GenericModuleCodeType moduleType;
bool ispackage = false;
string modpath = null;
PythonModule mod;
PythonDictionary dict = null;
// Go through available import types by search-order
foreach (var order in _search_order)
{
string tempCode = this.resolver.GetScriptSource(fullname + order.Key);
if (tempCode != null)
{
moduleType = order.Value;
code = tempCode;
modpath = fullname + order.Key;
Console.WriteLine(" IMPORT: " + modpath);
if ((order.Value & GenericModuleCodeType.Package) == GenericModuleCodeType.Package)
{
ispackage = true;
}
break;
}
}
// of no code was loaded
if (code == null)
{
return null;
}
var scriptCode = context.ModuleContext.Context.CompileSourceCode
(
new SourceUnit(context.LanguageContext, new SourceStringContentProvider(code), modpath, SourceCodeKind.AutoDetect),
new IronPython.Compiler.PythonCompilerOptions() { },
ErrorSink.Default
);
// initialize module
mod = context.ModuleContext.Context.InitializeModule(modpath, context.ModuleContext, scriptCode, ModuleOptions.None);
dict = mod.Get__dict__();
// Set values before execute script
dict.Add("__name__", fullname);
dict.Add("__loader__", this);
dict.Add("__package__", null);
if (ispackage)
{
// Add path
string subname = GetSubName(fullname);
string fullpath = string.Format(fullname.Replace(".", "/"));
List pkgpath = PythonOps.MakeList(fullpath);
dict.Add("__path__", pkgpath);
}
else
{
StringBuilder packageName = new StringBuilder();
string[] packageParts = fullname.Split(new char[] { '/' });
for (int i = 0; i < packageParts.Length - 1; i++)
{
if (i > 0)
{
packageName.Append(".");
}
packageName.Append(packageParts[i]);
}
dict["__package__"] = packageName.ToString();
}
var scope = context.ModuleContext.GlobalScope;
scriptCode.Run(scope);
return mod;
}
I hope some one has an idea, why this happens. A few line which also may cause the problem are:
var scriptCode = context.ModuleContext.Context.CompileSourceCode
(
new SourceUnit(context.LanguageContext, new SourceStringContentProvider(code), modpath, SourceCodeKind.AutoDetect),
new IronPython.Compiler.PythonCompilerOptions() { },
ErrorSink.Default
);
and
mod = context.ModuleContext.Context.InitializeModule(modpath, context.ModuleContext, scriptCode, ModuleOptions.None);
Because i don't know, whether creating a module this way is completly correct.
The problem can be reproduced downloading this project/branch: https://github.com/simplicbe/Simplic.Dlr/tree/f_res_noid and starting Sample.ImportResolver. An exception in find_module will be raised.
Thank you all!
This problem is solved. Modpath what not allowed to contains /. In general only chars were allowed, which also can be in a file-name.
Maybe this is helpful for someone else...