Comments (5)
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.
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
Invoke-Build/Tests/Shared.tasks.ps1
Lines 11 to 19 in 545f37f
(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:
Lines 40 to 42 in 0151787
(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/FarNet/blob/master/FarNet/FarNet.Tools/.build.ps1
- https://github.com/nightroman/FarNet/blob/master/FarNet/FarNet.Works.Config/.build.ps1
- a few more projects
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.
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.
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.
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)
- Amend output on task errors.
- ib.cmd ? ignores a specified file
- vscode outline view: no symbols found in document HOT 2
- Different module and package versions HOT 2
- Parallel execution based on dependency tree HOT 2
- invoke-build discovery fails in powershell container HOT 3
- Please consider adding -Computername and -Session parameter to Task HOT 8
- GitHub action output is not colored
- Color and formatting text in Azure DevOps Pipelines HOT 8
- Use-BuildEnv should make parameters mandatory
- Invoke-BuildExec spitting out service principal secret as current directory HOT 6
- Exec doesn't work well with forward slash arguments HOT 7
- Use `[System.Management.Automation.Cmdlet]::CommonParameters` to get the names of all common parameters HOT 3
- Task condition does not work HOT 8
- Build-Parallel writes only to Output stream HOT 7
- property 'File' cannot be found on this object HOT 17
- Do not wrap internal errors in Resolve-MSBuild
- Fix cryptic errors on unknown parameters
- Invoke-BuildExec should pass the original stderr error to the thrown terminating error HOT 38
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from invoke-build.