I'm having a bit of trouble here.
I want to be able to generate a C# project (and solution) (let's say a C# Console Application) using a template similar to the ones VS2010 uses.
Basically I want to have a method GenerateConsoleApplication which does just that. But since I will need to generate several of these, I want to use a template to populate the Main method of the program.cs class which will be generated alongside the .csproj and app.xml files.
public void GenerateConsoleApplication()
{
var projectName = "MyConsoleApplication";
var projectLocation = "C:\temp";
// what goes here so that it creates a solution Visual Studio that I could use ?
}
I looked up the project template used by VS2010 to generate a C# Console Application and am thinking I could modify it to suit my needs. But I have no idea what I would need to write (code wise) to use the said template and generate all the files of the new solution.
Does anyone know how to do it ? Or if it is even possible ?
I know I could just write the csproj file and the others but I think a template would allow changes more easily.
Just found what I'll need to do.
The problem boils down to the parsing of the templates files.
Once I get the parser, then I can replace the tags with the appropriate values. I wanted to use Visual Studio's parser to do that, and slightly modify the templates it already uses, but it's not possible.
So one has to write its own parser and use it to generate the files.
Then Regex can be use to replace the tags of the template file with the Regex.Replace(String, String, MatchEvaluator) method.
Related
I've been working on learning Roslyn and have made great progress with using the CSharpSyntaxRewriter mechanism to work on code. My goal in the end is to have a customizable coding standards enforcer. As such, I started with Microsoft's code formatter project from https://github.com/dotnet/codeformatter/releases. Right now what I'm working on is formatting white space.
The goal here is to learn Roslyn, so I want to write the code to format all of the whitespace instead of using the Formatter built into Visual Studio and Roslyn.
Formatter.FormatAsync(document, cancellationToken: cancellationToken);
I'm able to parse syntax trees and know I can implement the code necessary to do this using the CSharpSyntaxRewriter, but what I'd like to do is somehow simply get the raw source text for the code, do whatever manipulations are necessary character by character in the source file, and then put the new text back as the document.
However, I cannot figure out if it's even possible to do what I'm trying to do with Roslyn/Visual Studio. I would have thought so, since the source is simply a text file that's already loaded into Visual Studio, and the 'document' object can be acquired for it.
Edit
The optimum solution would be a drop down (or context) menu for C# source files that then ran all modifications on the file. I was unable to link MEF/MPF to any sort of hook that would allow whole-scale modifications of a source file. As such, I went to a console application.
From what I understand, MEF/MPF can provide single entry points to the source file, whereas the Roslyn interface allows simultaneous access to the entire source file at one time. I believe that's why Microsoft used Roslyn in order to implement their coding standards enforcer.
As I said, I've only been looking at Roslyn for about a month, so I may be missing something obvious.
At the moment I am using C# T4 code generation, but as much as I know it is limited to a class file or other file type generation. I need to generate multiple projects in one solution is it possible to accomplish with T4 templates or there is other technique do to this?
Templates in Visual Studio allow for multiple projects / files templates so you can definitely create this.
The way to create them is simply to setup the skeleton you need for the project, then replace the content of the files in the directory with specific values that will be replaced when the template has the information needed.
I had similar experience.
I developed a whole set of T4 templates to generate a solution. But once you want change code in multiple files, some code may mess up.
So I wrote a code generator based on database schema to generate a VS solution with DAL(Linq To SQL, Entity Framework), BLL, WCF, and UI Applications MVC, WPF, Windows PHone, Windows App Store, Silverlight.
You can goto https://github.com/ntierontime/Log4Net to see whether it meets your architecture requirements, or not. And send me an email with the .dbml(Linq To Sql Classes).
I am currently developing a solution that has two projects on it: one is some kind of API where I put all the functions that I want to use in the second.
So, at some point in the project, I need to create a file programatically. This is ok, but I need to include that file in my second project to use the functions from the first projects! Any idea on how to do this?
I have tried the EnvDTE but I think it only works if you are creating a new solution and a new project.
Really need help on this! Thanks in advance! :)
Are you literally trying to recompile the second project after creating these files or is this all happening at runtime?
To me it sounds like what you're trying to do is really not create a file but to emit a class in memory during runtime to make new functions available to the second executable. You should check out Reflection.Emit. That is how you define classes, even assemblies, that didn't exist when you started running the program. Check out MSDN and this code project article.
Does anyone know of a batch processor or a VS 2010 plugin/script that would let me translate comments and region names from Chinese into English?
The only ones I've found either process all strings or only one string at a time.
I have two large C# projects that I am trying to read through.
Thanks.
Use PrepTags to prepare your file for translation. It will allow you to select the text to be translated based on regex.
www.preptags.com
You can work file by file for free, or process the files as batch using the pro version (€39)
In your case, it's pretty simple to prepare. You just mark everything as protected, then unprotect the content of the comments & region names.
Disclosure: I develop PrepTags.
As was noted, you can use Google Translate API or alternatively Bing Translator API. You can detect comments and regions in your files using System.CodeDom.
I'm not too sure if this is possible. What you can do to help would be the following:
1) Make sure that both C# projects have the Properties > Build > Xml document file check box checked.
2.1) Write an application that reads in the generated xml file.
2.2) Parse the file, and for each value make a call to Google Translate to get the translated value.
2.3) Place the translated value within another xml file that has the same structure as the one created from building the project.
This wouldn't solve the your desire to translate the region names, but its a start. At least you would have intelligence when using the two projects.
This is actually a good idea for a small open source project. I may decide to pick it up. If I do, I'll let you know.
C# unfortunately does not allow for extra user-defined syntax. But I was wondering whether it was possible to surpass this limitation by tapping into the visual studio onbuild-event.
Suppose I have some syntactic sugar which could be easily translated into actual C# code.
If I were to automatically translate a cs document containing this new syntax into a valid cs document, right before a C#-project is built, then the project could build succesfully. Overall this would function as if I had extended the C# language, because I started with an invalid cs document containing unoffical syntax, but it compiled anyway.
I realize that this has a few problems, such as that this translation is permanent. This could perhaps be circumvented by restoring the original cs(which should be restored after the debugging has ended, otherwise some IDE functionality would be lost). But these are secondary problems.
Please let me know what you think of this idea. Is this possible, and if so, could someone direct me to some useful tutorials so achieve this? In specific the tapping-into-a-onbuild-event.
I've searched MSDN, but the topic(http://msdn.microsoft.com/en-us/library/hthab0h8.aspx) didn't help me.
I won't say whether this is a good idea or not, since I don't know enough about what you're trying to do. I would suggest this, though: What you're proposing is to have some kind of "extended C#" source code file that gets translated into regular cs during the build process.
Personally, I would clarify this by first breaking away from the idea that you are "extending" the C# language; I would instead think of it as defining a new language that happens to be syntactically similar to C# (I assume). Use a different file extension so that Visual Studio does not try to compile your language as C#. (Maybe .csx? People love adding the letter x, right?)
Visual Studio already does this sort of thing in other ways that might not be quite so obvious. If you add a resource file to a project, Visual Studio will typically also include a dynamically generated "designer.cs" with code generated based on the content of your .resx file. If you look at the properties of the .resx file, you'll note that the "Custom Tool" property has a value of "ResXFileCodeGenerator". In theory you should be able to implement your own generator to perform the translation step that you mentioned. In fact, this translation does not have to be a one-time thing as you said. The translation process should generate a new file, but leave the original file intact. Any changes to the original file causes Visual Studio to regenerate the auto-generated file.
I've not tried to implement a custom generator myself, but I think these articles are relevant: Implementing Single File Generators, and Registering Single File Generators
Your .csproj file would contain something such as the following:
<Content Include="Example.csx">
<Generator>ExtendedCSharpCodeGenerator</Generator>
<LastGenOutput>Example.cs</LastGenOutput>
</Content>
<Compile Include="Example.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Example.csx</DependentUpon>
</Compile>
Where Example.csx is the source code file containing your extended syntax and Example.cs is the resulting output of translating Example.csx into normal C# code.
What you are talking about doing seems like a perfect task for T4 templates in Visual Studio.
http://msdn.microsoft.com/en-us/library/bb126445.aspx
You can define anything you'd like; text files with a certain format, UML models, a database; and your T4 template can transform it into code in what ever way you wish.
I'm not sure it's a good idea, but I just had an idea: maybe you can have a look at Extending Visual Studio, download the SDK and check the doc. Maybe it would be possible to do what you are trying to achieve.