包括命令行中的项目文件 [英] include file in project from command line
问题描述
有没有一种方法,包括命令行中vs2012在项目中的文件?
Is there a way to include a file in project from command line in vs2012 ?
为什么我问的原因是因为很沮丧包括任何新的文件,我将添加到每当我使用一些其他的IDE(如ST3)或项目文件夹当我保存从Photoshop等文件。
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.
我使用咕噜做了很多缩小文件,连接的,在我的角度脚本等运行ngmin有一个咕噜壳插件,使繁重的任务,运行shell命令(我已经在使用它的解除锁定通过TFS文件)。所以我想,我可以创建一个能够做的项目包括:对我来说任何新的文件,我想补充(通过观察与咕噜手表特定的文件夹)的任务。
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 ).
推荐答案
下面是一个使用PowerShell的一个解决方案。这是一个有点长,但我保证它的工作原理。我测试了不少。
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.
powershell -File C:\AddExistingItem.ps1 -solutionPath "C:\Test.sln" -projectName "TestAddItem" -item "C:\Test.txt"
现在害怕的是,AddExistingItem.ps1:
Now the scary part, 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
在code的95%只是有让你从命令提示符下运行。如果你正在写,直接在PowerShell中运行code你可以离开出来,并直接进入 $ IDE =新对象-ComObject VisualStudio.DTE
。
这里是一篇博客文章中解释为什么需要可怕的东西。结果
和这里是同样的事情又一个但在C#。
Here is a blog post explaining why that scary stuff is needed.
And here is another one on the same thing but in C#.
另外一件事值得一提。我尝试使用EnvDTE组件,就像您在.NET中,但我一直得到一个COM注册错误。一旦我开始使用COM对象直接一切正常。我不知道有足够的了解COM真正一个大胆的猜测,这是为什么。
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.
修改
如果您试图运行从命令提示符脚本时收到此错误:
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设置ExecutionPolicy -ExecutionPolicy下RemoteSigned
<一个href=\"http://www.howtogeek.com/106273/how-to-allow-the-execution-of-powershell-scripts-on-windows-7/\"相对=nofollow>这里是一个什么样的命令做的不错,在深入的解释。
Here is a good, in-depth explanation of what that command is doing.
这篇关于包括命令行中的项目文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!