有没有办法在vs2012的命令行中在项目中包含文件?
我问的原因是,无论何时我使用其他IDE(例如ST3)或从Photoshop保存文件时,都很难将添加到项目文件夹中的任何新文件包括在内。
我正在使用Grunt进行大量的缩小,连接,在我的角度脚本上运行ngmin等。有一个grunt-shell插件,允许grunt任务运行shell命令(我已经在使用它来通过TFS解锁锁定文件)。所以我以为我可以创建一个任务,为我添加的任何新文件(通过使用grunt-watch监视某个文件夹)为我完成项目中的包含。
最佳答案
这是使用PowerShell的解决方案。它有点长,但是我保证它会起作用。我测试了很多。
首先,简单的部分。这是从命令提示符下运行脚本的方式。
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 dwTickCount, 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 dwTickCount, 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,
int dwTickCount,
IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(
IntPtr hTaskCallee,
int dwTickCount,
int dwRejectType);
[PreserveSig]
int MessagePending(
IntPtr hTaskCallee,
int dwTickCount,
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#中。
另一件事值得注意。我像在.net中一样尝试使用EnvDTE程序集,但是我一直收到COM注册错误。一旦我开始直接使用COM对象,一切就可以正常工作。我对COM的了解还不足以真正猜测这是为什么。
编辑
如果在尝试从命令提示符运行脚本时收到此错误:
然后,您需要先运行它(您只需要运行一次)。
powershell -command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned"
Here是该命令正在执行的很好的深入解释。
关于asp.net - 从命令行将文件包含在项目中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17382308/