将多个 parameters/arguments 传递给目标
Pass multiple parameters/arguments to target
我有一个目标 Release-Prepare
将版本作为参数:
var version = Argument("version", "1.0.0.0");
Task ("Release-Prepare")
.Does (() => {
// Compute the package version
var gitCommitHash = GitLogTip(projectDir).Sha;
var gitVersion = gitCommitHash?.Substring(0, 8) ?? "Unknown";
var currentDateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
var packageVersion = $"{version}-{gitVersion}-{currentDateTime}";
Information(packageVersion);
// Versions having to be modified in *.csproj
var versionXPath = "/Project/PropertyGroup/Version";
var assemblyVersionXPath = "/Project/PropertyGroup/AssemblyVersion";
var fileVersionXPath = "/Project/PropertyGroup/FileVersion";
// Set versions for all projects (excpet testing)
var projectFilePaths = GetFiles("./**/*.csproj");
foreach(var projectFilePath in projectFilePaths) {
if(projectFilePath.FullPath.Contains("Tests")){
// Do not version test projects
continue;
}
XmlPoke(projectFilePath, versionXPath, packageVersion);
XmlPoke(projectFilePath, assemblyVersionXPath, version);
XmlPoke(projectFilePath, fileVersionXPath, version);
}
});
不带参数执行目标工作正常:
PS E:\dev\Sppd.TeamTuner> powershell -ExecutionPolicy ByPass -File build.ps1 -script "e:\dev\Sppd.TeamTuner\build.cake" -target "Release-Prepare" -verbosity normal
Preparing to run build script...
Running build script...
========================================
Release-Prepare
========================================
1.0.0.0-af9d218d-20191125093743
Task Duration
--------------------------------------------------
Release-Prepare 00:00:02.3258569
--------------------------------------------------
Total: 00:00:02.3258569
但我不知道如何使用 PowerShell 命令传递目标和版本。我试过:
powershell -ExecutionPolicy ByPass -File build.ps1 -script "e:\dev\Sppd.TeamTuner\build.cake" -target "Release-Prepare" -version="1.2.3.4" -verbosity normal
-> 错误:参数值不是有效的布尔值。
powershell -ExecutionPolicy ByPass -File build.ps1 -script "e:\dev\Sppd.TeamTuner\build.cake" -ScriptArgs '--target=Release-Prepare','--version=1.3.1.2' -verbosity normal
-> 错误:未找到目标 'Release-Prepare,--version=1.3.1.2'。
我已经尝试了我能想到的 -ScriptArgs '--target=Release-Prepare','--version=1.3.1.2'
的所有排列(单破折号、space/comma、single/double 引号。但是我尝试过的所有方法都导致蛋糕 intzerpreting 作为单个命令。
如何指定 ScriptArgs 才能用于多个参数?
我假设您使用的是最新的引导程序文件,可从此处获得:
https://cakebuild.net/download/bootstrapper/windows
注意:如果不是这种情况,那么参数被解析并发送到 Cake 的方式可能与我在这里展示的不同。
您可以使用以下方式下载:
Invoke-WebRequest https://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1
如前所述here.
有了这些,让我们使用以下 build.cake 文件:
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var version = Argument("applicationVersion", "1.0.0.0");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(ctx =>
{
// Executed BEFORE the first task.
Information("Running tasks...");
});
Teardown(ctx =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.Does(() => {
Information("Configuration: {0}", configuration);
Information("Target: {0}", target);
Information("Version: {0}", version);
});
Task("Release-Prepare")
.Does(() =>
{
Information("Configuration: {0}", configuration);
Information("Target: {0}", target);
Information("Version: {0}", version);
});
RunTarget(target);
如果我们 运行 这个没有参数,我们将得到这个作为输出:
========================================
Default
========================================
Configuration: Release
Target: Default
Version: 1.0.0.0
但是,如果我们 运行 以下内容:
.\build.ps1 --target=Release-Prepare --configuration=Debug --applicationVersion=2.2.2.2
我们将得到:
========================================
Release-Prepare
========================================
Configuration: Debug
Target: Release-Prepare
Version: 2.2.2.2
与您所拥有的相比,要提一件事...
version
参数已经是Cake.exe直接保留的参数,即运行ning:
cake.exe --version
会输出Cake本身的版本号。这就是为什么我转而使用 applicationVersion
作为参数名称,而不是版本。
我有一个目标 Release-Prepare
将版本作为参数:
var version = Argument("version", "1.0.0.0");
Task ("Release-Prepare")
.Does (() => {
// Compute the package version
var gitCommitHash = GitLogTip(projectDir).Sha;
var gitVersion = gitCommitHash?.Substring(0, 8) ?? "Unknown";
var currentDateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
var packageVersion = $"{version}-{gitVersion}-{currentDateTime}";
Information(packageVersion);
// Versions having to be modified in *.csproj
var versionXPath = "/Project/PropertyGroup/Version";
var assemblyVersionXPath = "/Project/PropertyGroup/AssemblyVersion";
var fileVersionXPath = "/Project/PropertyGroup/FileVersion";
// Set versions for all projects (excpet testing)
var projectFilePaths = GetFiles("./**/*.csproj");
foreach(var projectFilePath in projectFilePaths) {
if(projectFilePath.FullPath.Contains("Tests")){
// Do not version test projects
continue;
}
XmlPoke(projectFilePath, versionXPath, packageVersion);
XmlPoke(projectFilePath, assemblyVersionXPath, version);
XmlPoke(projectFilePath, fileVersionXPath, version);
}
});
不带参数执行目标工作正常:
PS E:\dev\Sppd.TeamTuner> powershell -ExecutionPolicy ByPass -File build.ps1 -script "e:\dev\Sppd.TeamTuner\build.cake" -target "Release-Prepare" -verbosity normal
Preparing to run build script...
Running build script...
========================================
Release-Prepare
========================================
1.0.0.0-af9d218d-20191125093743
Task Duration
--------------------------------------------------
Release-Prepare 00:00:02.3258569
--------------------------------------------------
Total: 00:00:02.3258569
但我不知道如何使用 PowerShell 命令传递目标和版本。我试过:
powershell -ExecutionPolicy ByPass -File build.ps1 -script "e:\dev\Sppd.TeamTuner\build.cake" -target "Release-Prepare" -version="1.2.3.4" -verbosity normal
-> 错误:参数值不是有效的布尔值。powershell -ExecutionPolicy ByPass -File build.ps1 -script "e:\dev\Sppd.TeamTuner\build.cake" -ScriptArgs '--target=Release-Prepare','--version=1.3.1.2' -verbosity normal
-> 错误:未找到目标 'Release-Prepare,--version=1.3.1.2'。
我已经尝试了我能想到的 -ScriptArgs '--target=Release-Prepare','--version=1.3.1.2'
的所有排列(单破折号、space/comma、single/double 引号。但是我尝试过的所有方法都导致蛋糕 intzerpreting 作为单个命令。
如何指定 ScriptArgs 才能用于多个参数?
我假设您使用的是最新的引导程序文件,可从此处获得:
https://cakebuild.net/download/bootstrapper/windows
注意:如果不是这种情况,那么参数被解析并发送到 Cake 的方式可能与我在这里展示的不同。
您可以使用以下方式下载:
Invoke-WebRequest https://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1
如前所述here.
有了这些,让我们使用以下 build.cake 文件:
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var version = Argument("applicationVersion", "1.0.0.0");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(ctx =>
{
// Executed BEFORE the first task.
Information("Running tasks...");
});
Teardown(ctx =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.Does(() => {
Information("Configuration: {0}", configuration);
Information("Target: {0}", target);
Information("Version: {0}", version);
});
Task("Release-Prepare")
.Does(() =>
{
Information("Configuration: {0}", configuration);
Information("Target: {0}", target);
Information("Version: {0}", version);
});
RunTarget(target);
如果我们 运行 这个没有参数,我们将得到这个作为输出:
========================================
Default
========================================
Configuration: Release
Target: Default
Version: 1.0.0.0
但是,如果我们 运行 以下内容:
.\build.ps1 --target=Release-Prepare --configuration=Debug --applicationVersion=2.2.2.2
我们将得到:
========================================
Release-Prepare
========================================
Configuration: Debug
Target: Release-Prepare
Version: 2.2.2.2
与您所拥有的相比,要提一件事...
version
参数已经是Cake.exe直接保留的参数,即运行ning:
cake.exe --version
会输出Cake本身的版本号。这就是为什么我转而使用 applicationVersion
作为参数名称,而不是版本。