How can I t4 scaffold from powershell.exe? - asp.net-mvc-3

NuGet's PM console is great and all but you have to be in Visual Studio to use it. I have asked this question before without any ansewr and I find it hard to believe there is no documentation on the topic.
I have two users: User A sell shoes, User B sells cars. Each have different property needs.
If I allow them to write there needed properties/datatypes to their respective Product.cs entity, how can I use PowerShell.exe to:
Scaffold the controller, views, dbcontext and repositories?
I know I can do a MSBuild afterwards, but how is it there seems to be no way to use PowerShell from say a bat file to do the scaffolding outside VS 2010?
Any insight would be appreciated.

I finally figured this out after days of no luck. First and foremost, do not use any Microsoft betas, incl Windows 8 Developer Edition, PowerShell 3.0 and VS 2011. Once you have PowerShell 2.0 up and running:
1. in the PS/v1.0 folder add a powershell.exe.config file:
Follow this link for the script:http://connect.microsoft.com/PowerShell/feedback/details/525435/net-4-0-assemblies-and-powershell-v2
restart PowerShell 2.0 and it will now support 4.0 framework.
2. Set the execution policy to require only remote scripts to be sign:
Command line: Set-ExecutionPolicy RemoteSigned
3. Import the required Custom t4 scaffolders (so you can use T4 Scaffold):
Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.NuGetServices.dll
Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.dll
4. Now invoke your powershell file:
Command Line:C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\MvcApplication1\CodeTemplates\Scaffolders\StevceScaffolders.AjaxGrid\StevceScaffolders.AjaxGrid.ps1
(be sure to quote any folder that has spaces, eg. "Visual Studio 2010"
I certainly think it would have been better if MS provided some insight on this in their documentation.

In trying to run it from powershell, you're in hardly-tread territory but its conceivable.
I think the T4 templates are rendered by an external tool. I can't see any reason why you couldn't invoke that EXE from powershell, passing parameters on the command line of the t4 template to be invoked. The resultant file (or files, see the Entity Framework T4 examples for how to write multiple files) would be output by the t4 conversion tool upon completion.
Your various different file types would be handled by different t4 templates, a Controller.t4, a Views.t4, Dbcontext.t4 and respositories.t4.
Hope that helps

Related

Cppcheck doesn't expand Visual Studio project macros

To automate C++ code quality analysis in our company, I've been asked to integrate Cppcheck tool with our SonarQube server (through the C++ Community plugin).
For the C++ part of our main product, in Visual Studio, we have a dozen of solutions, with almost a hundred of projects within each solution.
We use standard inherited property page macros like $(SolutionDir) and $(ProjectDir), and define our own custom (user-defined) macros as well (via Property Manager).
I'm currently writing Windows command line batch scripts to perform static code analysis separately on each solution. The idea is to run Cppcheck with an option like --project="solution_name.sln" and then run SonarQube Scanner to parse the generated XML output.
But there's a problem. It seems that Cppcheck doesn't recognize these $(SolutionDir) macros during include path resolution. (When run with --check-config option, it complains about missing include files.)
Unfortunately it turns out I even cannot supply additional include paths for a solution because the -I and --includes-file options are ignored when --project is specified.
How is Cppcheck supposed to be used with Visual Studio projects in an automated environment? I'm talking about command line interface (to be used on server side), not about interactive plugins (as used locally on developers' machines).
One possible way would be to create a Python script to preprocess the hundreds of project (*.vcxproj) files, replacing all these macros with real paths (each time after checking out from SVN and before executing Cppcheck). Or is there a proper way to make Cppcheck recognize Visual Studio macros?
Yes the --project is supposed to work. It's just that --project is a very new feature.
I would like to have a simple solution so I can reproduce easily.. Could you create some simple solution with such configuration? no real code is needed. I think a cpp file with just a #include and then an empty header might be enough. Configure it so that Visual Studio finds the header but Cppcheck doesn't.
Please attach it to the ticket orbitcowboy created.

VS Single-file generators: how to add registry information? Run code on VS startup?

I'm trying to figure out how to get a single-file generator installed on VS2010. Previously I got it sort-of working on VS2013 after many hours of head-banging; in both cases the primary difficulty is setting up the registry entries. Apparently VSIX files don't allow registry settings:
You can use the VSIX format to package project and item templates,
Visual Studio Integration Packages, Managed Extensibility Framework
(MEF) components, toolbox controls, assemblies, and custom types. The
VSIX format uses strictly file-based deployment and does not support
writing to the Global Assembly Cache (GAC), or to the system registry.
VSIX is the preferred deployment method for the extension types that
it supports.
My VS2013 solution involves the CodeGeneratorRegistrationAttribute and ComVisible(true) on the assembly, but after install, the extension doesn't work until the user runs devenv.exe /setup in Administrator mode. In VS2010, CodeGeneratorRegistrationAttribute does not exist in any of the SDK DLLs and simply adding the source code of CodeGeneratorRegistrationAttribute.cs to the project (as the Single File Generator sample does) doesn't seem to work (and I don't understand why the sample seems to expect it to work; .NET doesn't use structural typing, after all, so how could this attribute possibly have any effect?)
If a VSIX cannot add registry settings directly, I think a reasonable substitute is to include some code that automatically runs on VS startup. That code could find out the path of the registry hive of the running VS version and add the necessary registry settings at that time. So I have three questions:
How can I cause a method written by me, inside my extension, to run when VS starts?
How can I get the path of the current VS registry hive?
Is there any other way to add the registry information?
Eventually I gave up on making a VSIX to hold my single-file generator. I made a CodeProject article about what I did instead.
But back when I was still trying to make a VSIX file, this blog post came in handy.
What is wrong with MSI-deployment? You can do anything from there. WiX has option to install VSIX packages pretty simply, just use the embedded element <VSIXPackage>. It also offers you to create new registry keys + you get registry key unistall for free. Note that you can elevate privileges, if needed.
does your vsix have a class that extends Package?
Add code in your Package's Initialize method that runs when your package is initialized.
To get to VS based registry stuff, see Microsoft.VisualStudio.Shell.VSRegistry

PowerShell module for handling Visual Studio Object Model from within NuGet Install.ps1/Uninstall.ps1 scripts

I'm trying to create a NuGet package that contains both managed (assemblies) and unmanaged (native DLLs) binaries, and also support several platforms (x86, x64).
To have a better understanding of how to handle that properly, I had a look at several packages that share similar requirements, published on the official nuget feed. One of them is Microsoft.SQLServer.Compact.
Seeing what is done in the Install.ps1, Uninstall.ps1 PowerShell scripts and in the VS.psm1 PowerShell module is very helpful.
The VS.psm1 module contains definitions for a few functions allowing to control Visual Studio from within the package installation script (notably, through the $dte object). These functions are based on the Visual Studio Object Model, which I don't know yet and that is (in my opinion) not very well documented.
I was wondering if you know about a PowerShell module that would allow to easily handle the Visual Studio Object model from a NuGet PowerShell script. Alternatively, any pointer to books, blog posts, articles, ... showing a few examples about how to properly use that object model from PowerShell would be very welcome. The only examples I have found on the net usually illustrate very basic functionality.
Also, if you know about other NuGet packages dealing with both managed and unmanaged libraries that I could use as examples, that would be nice.
I could of course start from what I have seen in the files mentioned above and roll my own module, but I would rather concentrate on my package itself if there is already something available.
Thanks for your help.
Take a look at StudioShell. It is a system for bringing PowerShell automation into VS.NET and is available as an installer but also as a NuGet package to enable the sort of scenario you describe. PluralSight course "Everyday PowerShell for Developers" course has an intro to the system and there are some examples on the project site.
This may not be directly related to your post. The Package Manager Console in my opinion should be called the PowerShell Console. It gives you full access to the DTE.
I have a trivial example of using it, pure PowerShell, no t4 or extensions, to add several files to Visual Studio to automate adding Command Pattern classes.
http://github.com/jefflomax/vs-package-manager-console-cmdlets
We could certainly use a more complete library of PowerShell cmdlets to handle simple Visual Studio tasks in this environment.

How update web reference from VS 2008 command line?

We have web references in our VS 2008 solution. We don't check in generated files. I need to update the generated files on our cruise control server -- so I need a command line method to update the web references. Is there a way to force devenv to do this?
There is no command you can pass to Visual Studio from the command line to do this. What you can do though is use the wsdl.exe tool to generate the web references yourself. Under the hood, Visual Studio uses the same API as wsdl.exe to generate the references so there shouldn't be a functional difference between the two pieces of generated code (provided you give wsdl the right parameters)
WSDL.exe
http://msdn.microsoft.com/en-us/library/7h3ystb6(VS.80).aspx

Visual Studio class/file templates: Is there a way to change their content automatically per project/solution?

I've updated my default templates in Visual Studio for classes, interfaces, code files, etc. I removed the default namespaces and added a copyright header blurb.
Is there a way to use a variable or something in the template so I don't have to zip/unzip and re-run the vs installer to change the copyright header? (I'm a consultant, the code-owner isn't always me or my company).
Yes, you can. The documentation for this sort of thing is part of the Visual Studio SDK. There are already many variables you can use.
If you find you want to get fancy, look into the Guidance Automation Toolkit. A template using GAT can accept user input as well as information from the project and environment, can unfold one or more templates, filling in placeholders with the data gathered, and then can execute various actions against the unfolded templates, the project, or whatever.
You can get the complete example implemented here: Multi-Project Templates with Wizard: Visual Studio 2010 Sample

Resources