Is there a way to include a file in project from command line in vs2012 ?
The reason why I'm asking is because is very frustrating to include any new file I add to the project folder whenever I use some other IDE ( like ST3 ) or when I save a file from Photoshop, etc.
I'm using Grunt for doing a lot of minifying, concatenation, running ngmin on my angular scripts, etc. There's a grunt-shell plugin that allows grunt tasks to run shell commands ( I'm already using it for unlocking locked files by TFS ). So I was thinking that I could create a task that would do the include in project for me for any new file I add ( by watching a certain folder with grunt-watch ).
You can manualy edit project file to add files.
Project file has xml based format which you can edit symply. Try use grunt-text-replace for edit.
You can replace
with
I know this is not the complete solution and I haven't tested yet, but it may help.
First I searched for a Powershell solution and found this questions:
Add an existing project to solution folder using PowerShell.
Visual Studio Macro: Find files that aren't included
Add existing item in VS2010 in project programmatically
They use EnvDTE the COM library containing the objects and members for Visual Studio core automation.
ProjectItem Interface
ProjectItems.AddFromFile Method
I couldn't find any resources how to use it, unless this from CodeProject Exporing EnvDTE.
Since you want to use Grunt for automating your tasks. You could take a look at Edge - .NET for Node.js.
You can also run powershell script against your project file. We are using this for a long time.
AddContentToProject.ps1
Then run this with powershell
Or this from command prompt
Here is a solution using PowerShell. It is a little long, but I promise it works. I tested quite a bit.
First, the easy part. Here's how you run the script from the command prompt.
Now the scary part, AddExistingItem.ps1:
95% of the code is only there to let you run from the command prompt. If you are writing and running code directly in PowerShell you can leave it out and go straight to
$IDE = New-Object -ComObject VisualStudio.DTE
.Here is a blog post explaining why that scary stuff is needed.
And here is another one on the same thing but in C#.
Another thing worth noting. I tried using the EnvDTE assemblies, like you would in .net, but I kept getting a COM registration error. Once I started using the COM objects directly everything worked. I don't know enough about COM to really venture a guess as to why this is.
EDIT
If you receive this error when trying to run the script from the command prompt:
Then you need to run this first (you should only need to run it once ever.)
powershell -command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned"
Here is a good, in-depth explanation of what that command is doing.
You could just create a small exe to handle this for you:
Structure of C:\Projects\Test:
Usage:
AddTfsItem.exe C:\Projects\Test\Test.csproj SomeFolder\Tester.cs
Notice that the file has to be relative to the .csproj file.