Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call multiple tasks from CLI and pass them to RunTarget #2470

Closed
HofmeisterAn opened this issue Feb 8, 2019 · 9 comments
Closed

Call multiple tasks from CLI and pass them to RunTarget #2470

HofmeisterAn opened this issue Feb 8, 2019 · 9 comments
Labels
Milestone

Comments

@HofmeisterAn
Copy link

I used a lot of Gradle in the past. One of the most features I miss in Cake, is to run multiple tasks from CLI. In Gradle I could simple run .\gradlew clean build. In Cake I’m forced to use IsDependentOn, which does not make sense in every use case (I don’t want to run clean on every build).

It bothers me especial on my build environment (CI). It would be a lot easier to call multiple tasks in one stage. I think this would be a great feature and make Cake much better.

  1. Pass a list of arguments to RunTarget.
  2. Use the argument order and call every task including IsDependentOn tasks.
  3. Consider the tree dependency of the tasks, to order them as well.
@cbittencourt
Copy link

cbittencourt commented Jun 6, 2019

I am also interested in this feature. We have modified build.cake to call RunTarget(target) within a foreach, but I've noticed that we can't run two tasks that are dependent on the same Setup<T>() data.

Setup<MyData>(setupContext => {
  return new MyData()
    {
      DataItem = Argument<string>("command-arg","")
    };
});

Task("Task1").Does<MyData>((myData) => { 
  Information("Task1 runs successfully."); 
});

Task("Task2").Does<MyData>((myData) => { 
  Error("Task2 fails with `Context data of type 'Submission#0+MyData' has already been registered.`"); 
});

foreach(var target in targets) {
	RunTarget(target);
}

Command for example above:
./build.ps1 -targets Task1,Task2 --command-arg=foo

This issue seems to be trying to solve a similar problem, but with a different approach.

@Mrks83
Copy link

Mrks83 commented Jun 28, 2019

Coming from MSBuild I would appreciate this feature, too.

@FrankRay78
Copy link
Contributor

I'd like to have a go at this one @augustoproiete - please can you assign it to me?

@FrankRay78
Copy link
Contributor

I have actually implemented this logic on a local branch to understand how it might work, see: https://github.com/FrankRay78/cake/tree/2470

The following build file with multiple targets now executes correctly:

Setup(context =>
{
    // Executed BEFORE the first task.
    Information($"Setup - {DateTime.Now.ToString()}"); 
});

Task("Clean")
  .Does(context => 
{ 
  Information("Clean runs successfully."); 
});

Task("Default")
  .IsDependentOn("Clean")
  .Does(context => 
{ 
  Information("Default runs successfully."); 
});

Task("Second")
  .IsDependentOn("Clean")
  .Does(context => 
{ 
  Information("Second runs successfully."); 
});

RunTarget("Default,Second");

image

What I am not sure is whether extending the current RunTarget() method to support multiple targets is best, and/or whether to introduce a new method called RunTargets() which does this explicitly. Any preference @augustoproiete, @devlead ?

PS. I haven't implemented any unit tests for this yet on my local branch, but would do so prior to submitting a PR.

@devlead
Copy link
Member

devlead commented Oct 27, 2022

I would expect this to not rely on string splitting but on the fact that Cake allows specifying the same argument multiple times, i.e.

dotnet cake runtargets.cake --target=A --target=B --target=C

and a cake script could look something like this

var targets = Arguments<string>("target", "A");

Task("A");

Task("B");

Task("C");

RunTargets(targets);

a way to simulate this now would be to add a meta task in a Cake script i.e.

CakeReport RunTargets(ICollection<string> targets)
    => RunTarget(GetOrAddTargetsTask(targets).Name);

Task<CakeReport> RunTargetsAsync(ICollection<string> targets)
    => RunTargetAsync(GetOrAddTargetsTask(targets).Name);

private ICakeTaskInfo GetOrAddTargetsTask(ICollection<string> targets)
{
    var targetsTaskName = string.Join(',', targets);
    var targetsTask = Tasks.FirstOrDefault(
        task => task.Name == targetsTaskName
    );
    if (targetsTask == null)
    {
        var task = Task(targetsTaskName);
        foreach(var target in targets)
        {
            task.IsDependentOn(target);
        }
        targetsTask = task.Task;
    }
    return targetsTask;
}

as PoC gist here: https://gist.github.com/devlead/84b21afe5688e638a86f85b940ae57be

For that i.e.

dotnet cake runtargets.cake --target=A

will result in

========================================
A
========================================

Task                          Duration            
--------------------------------------------------
--------------------------------------------------
Total:                        00:00:00.0202365    

and

dotnet cake runtargets.cake --target=A --target=B

will result in


========================================
A
========================================

========================================
B
========================================

========================================
A,B
========================================

Task                          Duration            
--------------------------------------------------
--------------------------------------------------
Total:                        00:00:00.0251612    

and

dotnet cake runtargets.cake --target=A --target=B --target=C

will result in


========================================
A
========================================

========================================
B
========================================

========================================
C
========================================

========================================
A,B,C
========================================

Task                          Duration            
--------------------------------------------------
--------------------------------------------------
Total:                        00:00:00.0560336    

@FrankRay78
Copy link
Contributor

Thanks @devlead, I'll put what you suggest into a PR for review...

@FrankRay78
Copy link
Contributor

I have now raised a PR for this issue, see: #4054

@devlead devlead modified the milestones: Next Minor Candidate, v3.0.0 Nov 7, 2022
@devlead
Copy link
Member

devlead commented Nov 7, 2022

Fixed by #4054

@devlead devlead closed this as completed Nov 7, 2022
@cake-build-bot
Copy link

🎉 This issue has been resolved in version v3.0.0 🎉

The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants