What type of extension for VS (and how) to make, to generate C# or C++ code from some text [more so a model]? - visual-studio

I am new to Visual Studio Extensibility and want to make an addin/extension which shall do the following:
It should read all the files with a specific file extension (assume "*.ump").
It should process the text/code/whatever in the files.
It should create new Class/Code file with some code in it. [The code will be produced in step 2, just need to know how to do it?]
Yet, I have been racking my brains through extensibility, saw the single file generators .... and addins which go through ProjectItems and can detect the file extension,
BUT I HAVE NOT BEEN ABLE TO FIND a complete tutorial, guide or explanation as to how or what to do!!
Please help...

You don't want to read all files with a specific file extension in Visual C++ project nor standard Visual C# project. You may do that with hand-made MSBuild project (included in the solution).
In Visual C++ projects, there is a way to define custom tools. They are run as separate processes, so you can implement them in anything you want. Studio will ask you whether you want to define a tool (they are defined in special xml files; studio has dialog for editing them) when you add a file with extension unknown to it. In Visual C# projects, just manually write a MSBuild tasks and insert them into the project.
Do whatever you want. IIRC the generated files will have to be included in the project though. Well, for MSBuild, just tweak the project to your heart's desire, but in Visual C++ they have to.
You can combine MSBuild (csproj,vbproj) and VisualC++ projects in a single solution, so I recommend using separate.
If you ever find out you need to compile for different target where you can't use Visual Studio, you'll be glad that you have stand-alone tool you were just calling from Studio and not something that embeds in it.

Related

Convert Visual Studio exe-project in lib-project

I am developing an application (exe) in c++ with visual studio. I am not that experienced. Now, I came to the conclusion that it might be better to compile the general program functionality into an lib or dll file, which I then would use in a different visual studio project, where I basically implement the functions from the lib files for the more specific purpose of my project. With my current setup, I get the impression that I am starting to mix the gerenal functionality with the specific problem statement.
Basically I am asking for a way to convert my current full visual studio project into two separate projects, one for the gerenal lib files representing the classes and program modules, and one for the specific problem implementation. Is it possible to also keep everything in one visual studio project (edit: solution) for convenience?

Code generation in Visual Studio based on all files with a given extension

I have the following task: Make a visual editor in Visual Studio (not the core of today's question) which results in a text file, on a custom format. This file will then be used as input for code generation resulting in C# code. For this, I've been looking at:
T4
Visual Studio Extensions
Visual Studio Project Templates
Visual Studio Item Templates
I feel the solution is there somewhere, but I can't quite figure out how best to do it. As I see it, the main problem is somehow to automatically generate code for all files with a given extension. Does anyone know of any tutorials or descriptions on how to do this?
Thanks in advance!
To automatically associate a code generator with all files of a given extension, you need to
Create a Visual Studio package
Implement a custom IVsSingleFileGenerator. The easiest option is to subclass the BaseTemplatedCodeGenerator and override its GenerateCode method to supply your own T4 template as the "inputFileContent".
Use the ProvideCodeGeneratorAttribute to register the generator.
Use the ProvideCodeGeneratorExtensionAttribute to associate the generator with a file extension.
Create a VSIX with your package and generator and have your users install it.

Create Visual Studio Solution From msbuild .proj file

Does anyone know if is possible to have a Visual Studio project created automatically from a msbuild .proj file?
Thanks
I've just been trying to do something similar. Basically VS2010 won't let me add a "*.proj" file to a solution. HOWEVER, I found that if I simply renamed it to be a vbproj or a csproj, then VS2010 would let me add it to a solution, and it seemed to invoke it correctly. (The vbproj seemed 'nicest' to me, in that it didn't have a redundant "References" sub-folder.)
It does seem odd that VS2010 seems unable to work with an ad hoc MSBuild project file, demanding instead one that is tied explicitly to a specific languade (C#, VB.Net, SQL, WiX, ...)
In my case, it simlpy had an Exec task in it to go and invoke Doxygen to build developer documentation. That is, it was pretty simple! I hope the same trick might work for you too!
Another option might be for you would be too look at the "Makefile" project type in VS2010 (its under C++); that would allow you to directly invoke nmake and do away with the proj wrapper completely.

Should I include VB macros in source control with my project?

For a C# project, I make use of several Visual Basic macros in Visual Studio. I was just considering that these would be of use to other developers that work on the C# project.
The macros so far include removing trailing whitespace on save, organizing using directives and removing unnecessary ones, and an override for Ctrl-M Ctrl-O that expands regions. Would it be reasonable for me to include this macro code with my C# project in Subversion?
I don't know if it's even possible for macros to be made available/work in Visual Studio just because you open a particular Solution file, and that might be too invasive since some of the macros override existing Visual Studio behavior.
These are more like "useful tools" rather than code that is part of your project. I would not put them in SVN as part of your C# project, but if you think they are useful enough to be used by other team members, you could set up another SVN project just for these types of tools. Then your other team members could use them at their desire.
I think you would have to save the macro code to a file, and then you could just check the file into SVN. Whenever you changed the macro code, you could just keep SVN up to date with the latest macro code.

FSLex example solution?

I've been using C/lex for a long time and would like to use F#/fslex now.
I'm comparably well off in C# and in the process of learning F#.
The only thing is that I can't see any project example or template where fslex is properly included in the Visual Studio build process.
Does anyone know where I can find one?
Lots of Greetings!
Volker
Unfortunately, there is no built-in item template for .fsl files, because FsLex is a part of PowerPack (so Visual Studio cannot expect that it will be installed). It would be definitely useful to have some template that could be installed with PowerPack!
Anyway, if you're looking for a sample project that uses FsLex (and has .fsl files as part of the project), then you can take a look at the F# source code (distribtued with the Visual Studio 2008 MSI/ZIP package). The project that contains .fsl is FSharp.Compiler.fsproj and on my installation, it can be found in C:\Programs Files\FSharp-1.9.9.9\source\fsharp\FSharp.Compiler. Surprisingly, it includes a lexer for the F# language itself :-).
To add FsLex item to the MSBUILD project (which is also Visual Studio project), it uses the following:
<FsLex Include="..\lex.fsl">
<OtherFlags>--lexlib Internal.Utilities.Text.Lexing</OtherFlags>
<Link>lex.fsl</Link>
</FsLex>
<Compile Include="lex.fs" />
In case you also needed FsYacc, here is an example (also from FSharp.Compiler.fsproj):
<FsYacc Include="..\pars.fsy">
<Module>Microsoft.FSharp.Compiler.Parser</Module>
<Open>Microsoft.FSharp.Compiler</Open>
<OtherFlags>--internal --lexlib Internal.Utilities.Text.Lexing
--parslib Internal.Utilities.Text.Parsing</OtherFlags>
<Link>pars.fsy</Link>
</FsYacc>
<Compile Include="pars.fs" />
Note that you need the FsYacc/FsLex command to invoke the custom tool, but also the Compile command which tells the compiler to include the produced fs file when building the project.
I'm using the free VS Shell until I finally threaten myself enough to finally buy the full VS, and unless I'm doing something wrong, (and I probably am) none of the normal solutions seem to work for me. What I do is add the following pre-build events to the project properties:
"C:\Program Files (x86)\FSharpPowerPack-2.0.0.0\bin\fslex.exe" "$(ProjectDir)\Lexer.fsl"
"C:\Program Files (x86)\FSharpPowerPack-2.0.0.0\bin\fsyacc.exe" "$(ProjectDir)\Parser.fsy" --module Parser
That seems to work well enough for me. Errors all go to the output window. If you're on a 32-bit machine, you would remove the (x86) from those paths.
Doing a little research it looks like they need to be separate. I found the following at this blog post ( http://blogs.msdn.com/chrsmith/archive/2008/01/18/fslex-Sample.aspx ) by Chris Smith:
Although you can add an F# Lex Specification file from the Visual Studio Add-In, to run fslex you will need to break to the command line. Fslex.exe is located in your F# distribution’s ‘bin’ directory.
Hope that helps.

Resources