Equivalent to VCPostBuildEventTool for C# project in visual studio 2010 - c#

I am writing an add-in for visual studio, which creates a test project for a production code project.
It should support C, C++ and C# projects.
for C/C++ projects I can progarmatically set the post and pre-build events using VCPostBuildEventTool.
this how I get it from the Project object:
VCProject vcTestProj = (VCProject)testProj.Object;
IVCCollection testProjCfgs = (IVCCollection)vcTestProj.Configurations;
VCConfiguration testProjDebugCfg = (VCConfiguration)testProjCfgs.Item("Debug");
VCLinkerTool testProjLinkerTool = (VCLinkerTool)((IVCCollection)testProjDebugCfg.Tools).Item("VCLinkerTool");
VCCLCompilerTool testProjectCompileTool = (VCCLCompilerTool)((IVCCollection)testProjDebugCfg.Tools).Item("C/C++ Compiler Tool");
VCPreBuildEventTool testProjPreBuildTool = (VCPreBuildEventTool)((IVCCollection)testProjDebugCfg.Tools).Item("Pre-Build Event Tool");
VCPostBuildEventTool testProjPostBuildTool = (VCPostBuildEventTool)((IVCCollection)testProjDebugCfg.Tools).Item("Post-Build Event Tool");
But a C# project can not be used as a VCProject, and I can't find a way to access it's pre and post build events progarmatically.
Anyway to do that?

C#/VB.NET projects are treated as the same, so they are part of ProjectProperties3 (or ProjectProperties2) interface,
http://msdn.microsoft.com/en-us/library/vslangproj2.projectproperties2.postbuildevent.aspx
How to get to that level is mentioned in
http://msdn.microsoft.com/en-us/library/ms228958.aspx

Related

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: WorkspaceChangeKind.DocumentRemoved never raised

I'm working on Roslyn plugin to Visual Studio. I am trying to subscribe to an event that would be raised after a file is renamed. I am using Workspace.WorkspaceChanged but it doesn't raise DocumentRemoved.
Shouldn't WorkspaceChanged (with Kind=DocumentRemoved) be raised after an item is renamed?
Is there any other way to get notifications about solution item renames? I was trying to subscribe to DTE events but no luck either.
This is the way how I get a workspace:
var componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel));
_myWorkspace = componentModel.GetService<VisualStudioWorkspace>();
It has been a couple of years i did something with this. I once wrote a VS plugin that searched through files in a solution. It worked with rename as well if i remember(unfortunate i can't check because it was for VS 2015). I did it with DTE events back than. I created an DteEventHandler and added to the DocumentEvents.DocumentSaved my function.
dte = Package.GetGlobalService(typeof(DTE)) as DTE2;
events = dte.Events;
docEv = events.DocumentEvents;
docEv.DocumentSaved += ScanDocumentForFunction;
The other way is probably to go through the solution file. The solutionfile contains all csproj files and you can get all files from there.
dte = FillIndexListCommandPackage.GetGlobalService(typeof(DTE)) as DTE2;
var solutionnamearr = dte.Solution.FullName.Split('\\');
...
if you use git it would probably the easiest way to just call a git status in a command windows and pipe the result through to your plugin logic.
i hope i was some help or pointed you at least in the right direction.

Call method from interface or abstract class within static void

I'm usually a javascript developer, but for my company I just started learning c# in order to use the CimatronE 13 API to develop custom command line PDM tools for this 3D modelling software.
As I'm making progress understanding the programming language, there's this frustrating situation where I want to use an API endpoint method but I can't manage to get it working.
The Cimatron documentation says the following:
IPdm::GetRelatedDocuments
Syntax: RelatedDocuments = GetRelatedDocuments ( DocumentPath );
This method allows you to get related files from compound types of files, for example Assembly or Drawing.
Input: (String) DocumentPath,
Path to file. For example \Documents\Location\Folder\Document. The file must be Assembly or Drawing.
Return: (Variant) RelatedDocuments,
Variant type array each element of which contain two dimensioned string type array of files related to selected one.
This looks pretty straight forward to me, so I tried calling it in multiple ways from within the static void Main() method, but I keep getting errors:
var RelatedDocuments = interop.CimBaseAPI.IPdm.GetRelatedDocuments("path");
CS0120: An object reference is required for the non-static field, method, or property 'IPdm.GetRelatedDocuments(string)'
interop.CimBaseAPI.IPdm pdm = new interop.CimBaseAPI.IPdm();
var RelatedDocuments = pdm.GetRelatedDocuments("path");
CS0144: Cannot create an instance of the abstract class or interface 'IPdm'
Any ideas? It's probably simple but I'm still a noob with c# :p
EDIT:
Cimatron documentation about the interface interop.CimBaseAPI.IPdm:
Properties:
Get
Query (String, DocumentEnumType, DocumentEnumUnit )
Variant
Methods:
A lot, including Variant GetRelatedDocuments ( String )
As how I see it now... interop.CimatronE.IPdm is an interface and in order to use it's methods, we first need access to the Cimatron application. Using the application object, we can use it's methods to get the desired interfaces such as IPdm and use their methods.
The following code gives no errors from the compiler but does when executing. This seems to be related to version 13 of CimatronE, since the application object works just fine using version 12. A lot has changed between these versions which I think is the reason the API is not functioning properly, outdated.
interop.CimAppAccess.AppAccess AppAcc = new interop.CimAppAccess.AppAccess();
interop.CimatronE.IApplication CimApp = /*(interop.CimatronE.IApplication)*/AppAcc.GetApplication();
interop.CimatronE.IPdm pdm = CimApp.GetPdm();
var RelatedDocuments = pdm.GetRelatedDocuments("path");
Console.WriteLine(RelatedDocuments);
Please correct me if I'm wrong! (since I just started and still learning c#)
I ran into this same issue with Cimatron 14.
I needed to make some changes in Visual Studio for things run properly with Cimatron.
Run Visual Studio in administrator mode
Set your Debug & Release Solution Platform to 'x64'
It was also recommended to point the build path for release & debug to the same folder as the Cimatron references. In my case 'C:\Program Files\3D Systems\Cimatron\14.0\Program'. However my code appears to run fine without this.
I created the Cimatron Application with this code (VB.Net):
Dim gAppAccess As New CIMAPPACCESSLib.AppAccess 'Define an AppAccess object to get running active application
Dim gApp As CIMAPPACCESSLib.Application 'Define an Application object
gApp = gAppAccess.GetApplication 'Getting running active application
If gApp Is Nothing Then
gApp = New CIMAPPACCESSLib.Application 'Creating a new instance of a Cimatron application
End If
References: Interop.CIMAPPACCESSLib.dll & interop.CimServicesAPI.dll
It is my understanding that Cimatron 15 may also requires some manifest changes.
There is some help information in the Cimatron program under Cimatrom Modules > Cimaton SDK that may be mildly helpful.

Change VS option by using the Settings Store: finding the right CollectionPath?

I need to set the value of a Visual Studio option found in Visual Studio -> Tools -> Options -> Text Editor -> JavaScript/TypeScript -> EsLint but I can't seem to find the CollectionPath for this option.
GetSubCollectionNames("Text Editor"); yield a number of results, while GetSubCollectionNames("Text Editor\\JavaScript"); yield 0 results.
TL;DR
How would one go about finding the right CollectionPath for the option pictured in the image below?
This is what I'm using currently.
[ImportingConstructor]
internal VSOptions([Import] SVsServiceProvider serviceProvider)
{
var settingsManager = new ShellSettingsManager(serviceProvider);
_writableSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings)
?? throw new Exception(nameof(settingsManager));
var textEditorSubCollections = _writableSettingsStore.GetSubCollectionNames("Text Editor");
var javaScriptSubCollections = _writableSettingsStore.GetSubCollectionNames("Text Editor\\JavaScript");
// TODO: set option value when we have the right CollectionPath
}
The WritabelSettingsStore class used to extend Visual Studio common settings in Visual Studio. You could use GetPropertyNames("Text Editor\JavaScript") to list all writabel settings for JavaScript, where you will find not all Properties under JavaScript sub collections are listed.
The EsLint is not common Visual Studio Settings. It is third part tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
So we could not change it directly with WritableSettingsStore class. You need to know how the EsLint added in Visual Studio and then modify its configuration file for Visual Studio.

How do I retrieve text from the Visual Studio editor for use with Roslyn SyntaxTree?

I am attempting to write a Visual Studio extension that will analyze the C# code displayed in the editor and possibly update the code based on what I find. This would be on demand (via a menu item), and not using an analyzer and code fix.
There are a number of examples and samples on the Internet, but they all start either with the source code hard-coded in the samples, or create a new document, or look at each file in the VS solution that is open. How do I access the source code from the active editor window?
In a comment to my original question, #SJP gave a link to #Frank Bakker's answer to the question at Calling Roslyn from VSIX Command. This does work as outlined.
#JoshVarty provided a hint of the direction to go in his answer above. I combined that with code provided by #user1912383 for how to get an IWpfTextView answering the question Find an IVsTextView or IWpfTextView for a given ProjectItem, in 2010 RC extension. Here is the code I came up with:
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
IVsTextView activeView = null;
ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out activeView));
var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var textView = editorAdapter.GetWpfTextView(activeView);
var document = (textView.TextBuffer.ContentType.TypeName.Equals("CSharp"))
? textView : null;
In a comment after #user1912383's code mentioned above, #kman mentioned that this does not work for document types such as .sql files. It does, however, work for .cs files which is what I will be using it with.
First, you need to install the Microsoft.CodeAnalysis.EditorFeatures.Text package.
Then you need to add the appropriate using statement:
using Microsoft.CodeAnalysis.Text;
Now you can map between Visual Studio concepts (ITextSnapshot, ITextBuffer etc.) and Roslyn concepts (Document, SourceText etc.) with the extension methods found here: https://github.com/dotnet/roslyn/blob/master/src/EditorFeatures/Text/Extensions.cs
For example:
ITextSnapshot snapshot = ... //Get this from Visual Studio
var documents = snapshot.GetRelatedDocuments(); //There may be more than one

Categories