使用 psake、exec 和 msbuild 时如何正确转义目录?
我可以在 PowerShell 中执行以下命令:
msbuild "c:\some\spaced path\project.sln" /p:MvcBuildViews=False /p:OutDir="c:\\some\\spaced path\\deploy\\Package\\"
路径已更改,但真实路径还包含空格组件。双斜杠是来自例如这个的技巧回答。
如果我直接运行它,msbuild
就会理解该路径。但是,它需要像这样在 psake 中运行:
exec { msbuild $SolutionFile "/p:MvcBuildViews=False;OutDir=$OutputDir" }
如果路径没有空格,那么它就可以工作,但我想调整它以使用空格(对于 sln 路径和 OutDir)。我试过了,但我无法弄清楚逃跑的方法。
编辑:
为了澄清,如果我在 psake 中硬编码完整路径,它也可以工作:
exec { msbuild "c:\some\spaced path\project.sln" /p:MvcBuildViews=False /p:OutDir="c:\\some\\spaced path\\deploy\\Package\\" }
但是,它需要使用 OutputDir 变量(不是双斜杠转义)。因此,我为此添加一个临时变量,然后尝试构建命令行。:
$double_slashed_dir = $OutputDir.Replace('\', '\\');
write $double_slashed_dir;
exec { msbuild $SolutionFile /p:MvcBuildViews=False "/p:OutDir=`"$double_slashed_dir`"" }
这不起作用(我尝试了几个变体)。通过以上内容,我得到“MSB1008:只能指定一个项目。”
I can execute the following command in PowerShell:
msbuild "c:\some\spaced path\project.sln" /p:MvcBuildViews=False /p:OutDir="c:\\some\\spaced path\\deploy\\Package\\"
The paths are changed, but the real ones also contain a spaced component. The double-slash is a trick from e.g. this answer.
If I run that directly, msbuild
understands the path. However, it needs to run in psake like this:
exec { msbuild $SolutionFile "/p:MvcBuildViews=False;OutDir=$OutputDir" }
That works if the path has no spaces, but I want to adapt it to work with spaces (for both the sln path and OutDir). I've tried, but I couldn't figure out the escaping.
EDIT:
To clarify, it also works if I hard-code the full path in psake:
exec { msbuild "c:\some\spaced path\project.sln" /p:MvcBuildViews=False /p:OutDir="c:\\some\\spaced path\\deploy\\Package\\" }
However, it needs to use the OutputDir variable (which is not double-slash escaped). So, I add a temporary variable for that, then try to construct the command line.:
$double_slashed_dir = $OutputDir.Replace('\', '\\');
write $double_slashed_dir;
exec { msbuild $SolutionFile /p:MvcBuildViews=False "/p:OutDir=`"$double_slashed_dir`"" }
This doesn't work (I've tried a couple variations). With the above I get "MSB1008: Only one project can be specified."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这种变化对我有用(
$OutputDir
中的双斜杠和尾部斜杠似乎很重要):This variation worked for me (double slashes and trailing slashes in
$OutputDir
seem to be important):您想在 OutDir 周围添加引号,但是由于参数中存在其他引号,添加单引号会使命令 shell 解析器感到困惑,因此您需要通过双引号来转义这些引号:
You want to put quotes around your OutDir, but putting single quotes will confuse the command shell parser due to the other quotes in your argument, so you need to escape those quotes by double quoting them:
我尝试了以下方法并且成功了。我唯一添加的是 msbuild 命令后的双引号(“)。这是我尝试过的:
如您所见,构建目录包含一个空格('tweet Sharp')。只需尝试将您的所有参数括起来msbuild 任务用双引号括起来并检查它是否有效。
I tried this with the following and it worked. The only thing i added is a double quote(") after the msbuild command. This is what I have tried :
As you can see, the build dir includes a space('tweet sharp'). Just try enclosing all the parameters of your msbuild task within double quotes and check if it works.
我让它与 Invoke-Expression 一起工作:
我认为这有点黑客。它需要字符串连接,据我所知,Invoke-Expression 基本上是 eval (尽管它应该是安全的,因为这里不涉及用户输入)。
所以我暂时保留这个问题,希望能得到更好的答案。它应该清楚地显示变量。
I got it working with
Invoke-Expression
:I consider this a little hackish. It requires string concatenation, and as far as I can tell,
Invoke-Expression
is basically eval (though it should be safe as there is no user input involved here).So I'm leaving the question open for now in the hopes of a better answer. It should show the variables clearly.
我也一直在努力这个问题,我最终想出的答案是:引用整个交换机,而不仅仅是路径。
因此,例如:
此尝试与我所有其他尝试之间的主要区别在于 /Property 开关的引用。
我最初是这样做的:
请注意,我想要传递给 msbuild 的双引号位于开关名称之后和值之前。
MsBuild 会看到:
这不起作用。
我将其更改为:
MsBuild 会看到:
这确实有效!
I've been struggling this this too, and the answer I finally came up with is: quote the entire switch, not just the path.
So, for example:
The key difference between this and all my other attempts is in the quoting of the /Property switch.
I initially did:
Note that the double quote I want to pass to msbuild comes after the switch name and before the value.
MsBuild would see:
This didn't work.
I changed it to:
MsBuild would see:
This did work!