有没有办法在vs2012的命令行中包含项目中的文件?
我问的原因是因为每当我使用其他IDE(如ST3)或从Photoshop保存文件时,包含我添加到项目文件夹中的任何新文件都非常令人沮丧.
我正在使用Grunt进行大量的缩小,连接,在我的角度脚本上运行ngmin等.有一个grunt-shell插件允许grunt任务运行shell命令(我已经使用它来解锁TFS锁定的文件).所以我想我可以创建一个任务,为我添加的任何新文件(通过观看带有grunt-watch的特定文件夹)为我做项目中的包含.
解决方法
这是使用Power
Shell的解决方案.它有点长,但我保证它有效.我测试了很多.
powershell -File C:\AddExistingItem.ps1 -solutionPath "C:\Test.sln" -projectName "TestAddItem" -item "C:\Test.txt"
现在可怕的部分,AddExistingItem.ps1:
param([String]$solutionPath,[String]$projectName,[String]$item) #BEGIN: section can be removed if executing from within a PowerShell window $source = @" namespace EnvDteUtils { using System; using System.Runtime.InteropServices; public class MessageFilter : IOleMessageFilter { // // Class containing the IOleMessageFilter // thread error-handling functions. // Start the filter. public static void Register() { IOleMessageFilter newFilter = new MessageFilter(); IOleMessageFilter oldFilter = null; CoRegisterMessageFilter(newFilter,out oldFilter); } // Done with the filter,close it. public static void Revoke() { IOleMessageFilter oldFilter = null; CoRegisterMessageFilter(null,out oldFilter); } // // IOleMessageFilter functions. // Handle incoming thread requests. int IOleMessageFilter.HandleInComingCall(int dwCallType,System.IntPtr hTaskCaller,int dwTickCount,System.IntPtr lpInterfaceInfo) { //Return the flag SERVERCALL_ISHANDLED. return 0; } // Thread call was rejected,so try again. int IOleMessageFilter.RetryRejectedCall(System.IntPtr hTaskCallee,int dwRejectType) { if (dwRejectType == 2) // flag = SERVERCALL_RETRYLATER. { // Retry the thread call immediately if return >=0 & // <100. return 99; } // Too busy; cancel call. return -1; } int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee,int dwPendingType) { //Return the flag PENDINGMSG_WAITDEFPROCESS. return 2; } // Implement the IOleMessageFilter interface. [DllImport("Ole32.dll")] private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter,out IOleMessageFilter oldFilter); } [ComImport(),Guid("00000016-0000-0000-C000-000000000046"),InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] interface IOleMessageFilter { [PreserveSig] int HandleInComingCall( int dwCallType,IntPtr hTaskCaller,IntPtr lpInterfaceInfo); [PreserveSig] int RetryRejectedCall( IntPtr hTaskCallee,int dwRejectType); [PreserveSig] int MessagePending( IntPtr hTaskCallee,int dwPendingType); } } "@ Add-Type -TypeDefinition $source [EnvDTEUtils.MessageFilter]::Register() #END: section can be removed if executing from within a PowerShell window $IDE = New-Object -ComObject VisualStudio.DTE $IDE.Solution.Open("$solutionPath") $project = $IDE.Solution.Projects | ? { $_.Name -eq "$projectName" } $project.ProjectItems.AddFromFile("$item") | Out-Null $project.Save() $IDE.Quit() #BEGIN: section can be removed if executing from within a PowerShell window [EnvDTEUtils.MessageFilter]::Revoke() #END: section can be removed if executing from within a PowerShell window
95%的代码只允许您从命令提示符运行.如果您直接在PowerShell中编写和运行代码,则可以将其保留并直接转到$IDE = New-Object -ComObject VisualStudio.DTE.
Here是一篇博客文章,解释了为什么需要这些可怕的东西.
而here是另一个同样的东西,但在C#中.
另一件值得注意的事情.我尝试使用EnvDTE程序集,就像在.net中一样,但我一直收到COM注册错误.一旦我开始使用COM对象直接一切正常.我不太了解COM真的冒险猜测为什么会这样.
编辑
然后你需要先运行它(你应该只需要运行一次.)
powershell -command“Set-ExecutionPolicy -ExecutionPolicy RemoteSigned”
Here是对该命令正在做什么的一个很好的,深入的解释.