如何使用特定参数指定Task(“ A”)。IsDependantOn(“ B”)

时间:2018-12-29 20:12:18

标签: c# dependencies arguments task cakebuild

我使用Cakebuild定义标准任务依赖项:

def __str__(self):
    m = hashlib.md5()
    m.update(self.image)
    return m.hexdigest()

我希望“ Run-Integration-Tests”特别依赖“ Build”,并带有“ UAT”的“ env”参数(因此,如果未通过,则将其设置为“ UAT”)。可能吗?

3 个答案:

答案 0 :(得分:4)

可以在执行脚本之前操纵Cake使用的有向无环图(DAG)。这可以通过将任务定义为变量,然后根据需要在任务上调用方法来完成。例如:

var IntegrationTask = Task("Run-Integration-Tests")
    .Does(() =>
{
    NUnit("./src/**/bin/release/*.Tests.dll");
});

然后,在脚本的其他位置执行操作:

if(env == "UAT")
{
    IntegrationTask.IsDependentOn("Build");
}

Cake.Recipe中广泛使用此技术来根据所构建项目的类型来操纵DAG。一个例子可以在这里看到:

https://github.com/cake-contrib/Cake.Recipe/blob/develop/Cake.Recipe/Content/build.cake#L549-L590

即:

private static void SetupTasks(bool isDotNetCoreBuild)
{
    var prefix = isDotNetCoreBuild ? "DotNetCore-" : "";
    BuildParameters.Tasks.CreateNuGetPackagesTask.IsDependentOn(prefix + "Build");
    BuildParameters.Tasks.CreateChocolateyPackagesTask.IsDependentOn(prefix + "Build");
    BuildParameters.Tasks.TestTask.IsDependentOn(prefix + "Build");
    BuildParameters.Tasks.DupFinderTask.IsDependentOn(prefix + "Build");
    BuildParameters.Tasks.InspectCodeTask.IsDependentOn(prefix + "Build");
    BuildParameters.Tasks.PackageTask.IsDependentOn("Analyze");
    BuildParameters.Tasks.PackageTask.IsDependentOn("Test");
    BuildParameters.Tasks.PackageTask.IsDependentOn("Create-NuGet-Packages");
    BuildParameters.Tasks.PackageTask.IsDependentOn("Create-Chocolatey-Packages");
    BuildParameters.Tasks.UploadCodecovReportTask.IsDependentOn("Test");
    BuildParameters.Tasks.UploadCoverallsReportTask.IsDependentOn("Test");
    BuildParameters.Tasks.AppVeyorTask.IsDependentOn("Upload-Coverage-Report");
    BuildParameters.Tasks.AppVeyorTask.IsDependentOn("Publish-Chocolatey-Packages");
    BuildParameters.Tasks.InstallReportGeneratorTask.IsDependentOn(prefix + "Build");

    if (!isDotNetCoreBuild)
    {
        if (BuildParameters.TransifexEnabled)
        {
            BuildParameters.Tasks.BuildTask.IsDependentOn("Transifex-Pull-Translations");
        }
        BuildParameters.Tasks.TestTask.IsDependentOn("Test-NUnit");
        BuildParameters.Tasks.TestTask.IsDependentOn("Test-xUnit");
        BuildParameters.Tasks.TestTask.IsDependentOn("Test-MSTest");
        BuildParameters.Tasks.TestTask.IsDependentOn("Test-VSTest");
        BuildParameters.Tasks.TestTask.IsDependentOn("Test-Fixie");
        BuildParameters.Tasks.InstallOpenCoverTask.IsDependentOn("Install-ReportUnit");
    }
    else
    {
        if (BuildParameters.TransifexEnabled)
        {
            BuildParameters.Tasks.DotNetCoreBuildTask.IsDependentOn("Transifex-Pull-Translations");
        }
        BuildParameters.Tasks.TestTask.IsDependentOn(prefix + "Test");
        BuildParameters.Tasks.InstallOpenCoverTask.IsDependentOn("Install-ReportGenerator");
        BuildParameters.Tasks.PackageTask.IsDependentOn(prefix + "Pack");
    }
}

答案 1 :(得分:3)

您可以向任务添加条件:

var env = Argument("env", "DEV");

Task("Run-Integration-Tests")
    .WithCriteria(env == "UAT)
    .IsDependentOn("Build")
    .Does(() => { ... })

仅当Run-Integration-Tests设置为“ UAT”时,才会运行env任务。

答案 2 :(得分:1)

我提出了一种有条件地替换变量的方法。

void ArgumentIfTask<T>(string taskName, ref T argument, string argumentName, T argumentValue)
{
    if(Argument("target", "Default") != taskName)
    {
        return;
    }

    argument = Argument(argumentName, argumentValue);
}

var env = Argument("env", "DEV");

//… many tasks 

ArgumentIfTask("Run-Integration-Tests", ref env, "env", "UAT");
Task("Run-Integration-Tests")
    .IsDependentOn("Build")
    .Does(() =>
        {
            NUnit("./src/**/bin/release/*.Tests.dll");
        });