GithubHelp home page GithubHelp logo

Conditional Task Issue? about invoke-build HOT 3 CLOSED

zloeber avatar zloeber commented on May 27, 2024
Conditional Task Issue?

from invoke-build.

Comments (3)

zloeber avatar zloeber commented on May 27, 2024 1

Ok, so I was 'just missing something' then :)

The difference between () and {} is nuanced but it makes sense. I swear I was trying to do the appropriate amount of RTFM before asking. I'd maybe modify the documentation slightly to show both scenarios.

-If
    Specifies the optional condition to be evaluated. If the condition
    evaluates to false then the task is not invoked. The condition can be
    defined in one of two ways depending on your requirements.

    Using script block notation (curly braces) the condition will be evaluated
    dynamically upon task invocation. This is ideal for tasks that may be
    run multiple times and are skipped at first but invoked later when the 
    script block becomes true. 

    Example:
        task SomeTask -If {$SomeCondition} {...}

    Using standard Boolean notation (parenthesis) the condition will only 
    be evaluated when the task is loaded into the build engine. A use case
    for this notation might be evaluating parameters that are passed into 
    the build.

    Example:
        task SomeTask -If ($SomeCondition) {...}

    On WhatIf a script block is treated as true without invocation using 
    script block notation but not if the condition is defined in Boolean 
    notation.

Or something like that?

Regardless, thanks for clarifying for me and thank you for the excellent project!

from invoke-build.

nightroman avatar nightroman commented on May 27, 2024

Task parameters If, Inputs, and Outputs can be either script blocks
evaluated on task invocation or other values evaluated on task definition.

In your case the value for If may change after adding tasks and you want to
use the current changed value on invocation of the task TestConfigure. So use
the script block:

task TestConfigure -If {$Script:IsConfigured} {
    Write-Host 'configured...for certain!'
}

You should use -If $Script:IsConfigured only if $Script:IsConfigured does
not change or you specifically want to check for the original value, not
changed during the build.


If values for If (or Inputs, Outputs) do not change during the build then
it does not matter what to use, -If {} or -If (). But if the expression is
complex/expensive you may choose -If {} in order to avoid evaluation if a
task is not invoked at all in the build. On the other hand, if you want to
catch potential problems earlier, at a task definition, then you still may
want to use -If ().

Should we improve help perhaps? Here is the current:

0> . Invoke-Build
0> help task -Parameter If

-If
    Specifies the optional condition to be evaluated. If the condition
    evaluates to false then the task is not invoked.

    The value is a script block evaluated on task invocation or an object
    treated as Boolean on definition. On WhatIf a script block is treated
    as true without invocation.

    If the task is called several times then it is possible that the task
    is skipped at first but invoked later when the script block gets true.

from invoke-build.

nightroman avatar nightroman commented on May 27, 2024

Thank you for the suggested improved help. I like it and will use it in vNext.

from invoke-build.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.