简体   繁体   English

是否可以在发布后脚本中使用带双引号的命令参数?

[英]Is it possible to use a command argument with double quotes in a postpublish script?

I want to call a command in a postpublish script for my ASP.NET Core project.我想在我的 ASP.NET Core 项目的postpublish脚本中调用一个命令。 The command requires an argument that contains spaces and is wrapped in " . Below is the desired command that I would run, this works when running manually from the command line:该命令需要一个包含空格并包含在" 。下面是我要运行的所需命令,这在从命令行手动运行时有效:

dotnet nswag webapi2swagger /Assembly:./path/to/assembly.dll /Output:./path/to/swagger.json /InfoTitle:"My API Name" /InfoVersion:v1

In my project.json this command is wrapped in a JSON string.在我的project.json此命令包含在 JSON 字符串中。 In order to use quotes in the string I escape them with \\ :为了在字符串中使用引号,我用\\转义它们:

"dotnet nswag webapi2swagger /Assembly:%publish:OutputPath%/assembly.dll /Output:%publish:OutputPath%/docs/swagger.json /InfoTitle:\\"My API\\" /InfoVersion:v1"

Unfortunately, while this creates a valid JSON string, the command that runs at post publish appears to actually resolve the escape characters.不幸的是,虽然这会创建一个有效的 JSON 字符串,但在发布后运行的命令似乎实际上解决了转义字符。 As a result the value passed to InfoTitle is \\"My and not "My API" like I would expect.因此,传递给InfoTitle的值是\\"My而不是我期望的"My API"

Is this a bug?这是一个错误吗? Is it possible to somehow escape quotes differently in a postpublish script in order to get the results I expect?是否有可能以某种方式在postpublish脚本中以不同的方式转义引号以获得我期望的结果?

I had a similar problem where I needed to run a command on postPublish but got the message "No executable found matching command "cmd.exe"" when publishing within VS2015.我有一个类似的问题,我需要在 postPublish 上运行命令,但在 VS2015 中发布时收到消息“找不到匹配命令“cmd.exe”的可执行文件”。

What I ended up doing is to create a postPublish.cmd file in the project root and configuring it to run on postPublish in project.json, passing the publish output folder to the script.我最终做的是在项目根目录中创建一个 postPublish.cmd 文件并将其配置为在 project.json 中的 postPublish 上运行,将发布输出文件夹传递给脚本。

    "postpublish": [
        "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%",
        "postPublish.cmd %publish:OutputPath%",
    ]

You will have more control inside the postPublish.cmd script and do not need to escape "path"'s, eg:您将在 postPublish.cmd 脚本中拥有更多控制权,并且不需要转义“路径”,例如:

:: use %1 to access the temp publish folder:
rmdir /s /q "%1\wwwroot\Uploads"

:: The script will be executed at the root of your project. Testing this:
cd >> test.txt
type "project.json" >> testing2.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM