Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

TaskExtensionMethods

refractalize edited this page · 10 revisions

Task Extension Methods

ITask.WhenBuilt

ITask.WhenBuilt() returns a new Task<T> that is dependent on the task, who's value is the result of a lambda. In this example, we create a Task, who's value will be c:\deployments and is dependent on both task1 and task2:

Task<string> localDirectory = new All (task1, task2).WhenBuilt(() => "c:\deployments");

By the time the lambda is executed, task1 and task2 will have been executed, so if they're Task<T> you can access their .Value property:

Task<string> environmentConfig = new All (binaryPath, envConfigName)
    .WhenBuilt(() => binaryPath.Value + "." + envConfigName.Value + ".config");

Task<T>.WhenBuilt

For Task<T>, like the ITask.WhenBuilt but the .Value of the Task<T> is passed into the lambda.

Task<string> config = binaryPath.WhenBuilt(path => path + ".config");

Which is equivalent to:

Task<string> config = binaryPath.WhenBuilt(() => binaryPath.Value + ".config");

Task<string>.SubPath

Since dealing with file system paths is such a common use case in build scripts, we have .SubPath:

var subPath = path.SubPath("SubPath");

Is equivalent to:

var subPath = path.WhenBuilt(p => Path.Combine(p, "SubPath"));

Task<T>.WithDependencyOn

Often you'll have a task that you want to make dependent on another task, this can be done with .WithDependencyOn:

Task<string> a = ...;
ITask b = ...;
Task<string> c = a.WithDependencyOn(b);

This returns a copy of a that has the same value, but depends on b. Note: the task a is unchanged here, only c is dependent upon b (and, crucially a too) so just doing this won't work:

a.WithDependencyOn(b);

var dependsOnA = a;

Task<IEnumerable<T>>.SelectTasks

Map an array of items into an array of tasks. Here we take a source directory and copy it to several destination directories:

Task<string> sourceDirectory = ...;
Task<IEnumerable<string>> destinationDirectories = ...;
Task<IEnumerable<Copy>> copiedDirectories = destinationDirectories.SelectTasks(
    dest => new Copy { FromPath = sourceDirectory, ToPath = dest }
);

It takes a lambda that maps each item of the enumeration into an ITask.

Task<IEnumerable<T>>.SelectManyTasks

Like .SelectTasks, but allows you to return zero or more tasks per item in the input array. Useful if you want to skip certain items, or return more than one task each:

Task<string> sourceDirectory = ...;
Task<IEnumerable<string>> destinationDirectories = ...;
Task<IEnumerable<Copy>> copiedDirectories = destinationDirectories.SelectManyTasks(
    dest => {
        if (dest.StartsWith(@"C:\")) {
            return new [] {new Copy { FromPath = sourceDirectory, ToPath = dest }};
        } else {
            return new ITask [0];
        }
    }
);

This only copies to destinations on the C drive.

Task<IEnumerable<T>>.Select

Exactly the same as IEnumerable<T>.Select(), only it works for Task<IEnumerable<T>>s.

Task<IEnumerable<T>>.Where

Exactly the same as IEnumerable<T>.Where(), only it works for Task<IEnumerable<T>>s.

Task<T>.SelectTask

Returns an ITask as a result of a lambda based on the value of the Task<T>:

Task<string> compileBeforeDeploy = parameter.Default("compileBeforeDeploy", true);
ITask binaries = compileBeforeDeploy.SelectTask(compile => {
    if (compile) {
        return new Copy {FromPath = project.OutputDirectory, ToPath = "output"};
    } else {
        return (Task<string>) "output";
    }
});

Task<bool>.IfTrue and Task<bool>.IfFalse

Conditionally depend on a task if the Task<bool> is either true of false:

Task<bool> runUnitTests = parameters.Default("runUnitTests", true);
NUnitTests unitTests = new NUnitTests {...};
ITask optionalUnitTests = runUnitTests.IfTrue(unitTests);

Or, if you prefer it the other way round:

Task<bool> runUnitTests = parameters.Default("skipUnitTests", false);
NUnitTests unitTests = new NUnitTests {...};
ITask optionalUnitTests = runUnitTests.IfFalse(unitTests);

Task<T>.Switch()

Switch() is akin to the switch statement in C#. The condition is a Task<T>, usually taken from a command line parameter, and different tasks can be queued depending on the value of that condition:

Task<string> env = parameters.Default("env", "local");
var deploy = env.Switch();

var liveServer = "\\liveserver\deployments";
var outputDirectory = solution.Projects["MyApp"].OutputDirectory;
deploy["local"] = outputDirectory;
deploy["live"] = new Copy { FromPath = outputDirectory, ToPath = liveServer };

return {
    Deploy = deploy
};

A short hand form is also available, and accepts up to 3 cases:

var copiedOutputDirectory = new Copy { FromPath = outputDirectory, ToPath = liveServer };
var deploy = env.Switch("local", outputDirectory, "live", copiedOutputDirectory);

Task<T>.LogDebug and Task<T>.LogInfo

It's useful to see what values are when the task graph executes. LogDebug and LogInfo can be used to output the value of a Task<T>, along with a message that will help you identify it on the output.

Task<string> path = ...
var loggedPath = path.LogInfo("path");
Something went wrong with that request. Please try again.