GithubHelp home page GithubHelp logo

Comments (5)

pwm1234-sri avatar pwm1234-sri commented on June 16, 2024 1

Thank you for your detailed answer!
At the moment as I am just getting started, my needs are rather simple, so I will use imported tasks.
If my needs grow (and my powershell scripting skills improve) I will move to a module.

from invoke-build.

nightroman avatar nightroman commented on June 16, 2024

Here is the most straightforward and idiomatic way (but not always best, see next comment).

(1) Compose your shared tasks in a separate script, example: Shared.tasks.ps1

# Synopsis: Just writes a string.
task SharedTask1 {
'In SharedTask1'
}
# Synopsis: Calls SharedTask1 and writes another string.
task SharedTask2 SharedTask1, {
'In SharedTask2'
}

(2) Import tasks into a build script. Two ways.

(2.1) If the task script contains just tasks with no script scope variables
(e.g. SharedTask1, SharedTask2) then simply invoke the task script from
a build script:

# In order to import more tasks, simply invoke a script with them.
# *.tasks.ps1 files play the same role as MSBuild *.targets files.
.\Shared.tasks.ps1

(2.2) If shared tasks use script scope variables (e.g. Sleep uses
$SleepMilliseconds) then dot-source the task script:

. .\Shared.tasks.ps1 5000

Granted, this is not a typical example, the variable is also a parameter.
Dot-sourcing is needed if a task file declares and assigns script variables.
If you just invoke (&) such a script then variables are gone after the call.
But if you invoke by . then variables get into and stay in the calling scope.

One way or another, by invoking the task script from a build script, you get
the shared tasks added (note that task is just an alias of Add-BuildTask)
to other tasks defined (added) in the build script.

Another existing example

Task script: https://github.com/nightroman/FarNet/blob/master/FarNet/FarNet.tasks.ps1

This task script is imported into (invoked from) several build scripts:

https://github.com/nightroman/Invoke-Build/tree/master/Tasks/Import
Note that in this example actual build scripts are almost "empty", they all
basically consists of the imported tasks. But they use different environments
(script variables).

(Update)

Another example, including tasks in modules

https://github.com/nightroman/Invoke-Build/tree/master/Tasks/Import

from invoke-build.

nightroman avatar nightroman commented on June 16, 2024

Or is the right way to put the reusable logic into a function in a script or module that is imported in a typical powershell way?

This is another absolutely right way. You share the usual PowerShell functions / scripts / modules that run some actions. Then you call them from build script tasks. Tasks are just thin wrappers of that calls.

Which way to use? You decide which is better in a particular case. I use both and kind of prefer imported tasks in relatively simple cases, when tasks do not require parameters / script variables or use just a few of them.

With many parameters / variables, especially different for different tasks, the shared tasks are less attractive, more messy. Then I use shared functions / scripts / modules and call them from thin tasks.

from invoke-build.

nightroman avatar nightroman commented on June 16, 2024

Oh, I completely forgot :) There is the whole article about this, with examples, including shared tasks in modules.
https://github.com/nightroman/Invoke-Build/tree/master/Tasks/Import

from invoke-build.

pwm1234-sri avatar pwm1234-sri commented on June 16, 2024

Thank you! Now that I am understanding invoke-build a little better, I believe there are a number of goodies in the Tasks folder that I should study. Thanks again!!!

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.