run-script
The Declarative Hook run-script
is used to run Python scripts, in a plugin.yaml
or template.taml
file. See an example of a Declarative Hook of type run-script
:
hooks:
- type: run-script
trigger: before-render
script: path/to/script.py
The script
attribute defines the path where the script to be executed is in the Template/Plugin file structure.
The script that will be executed must be outside the templates
folder of the Plugin/Template to not be interpolated.
It is common to expect that in this script, a function named run
is defined. This function receives as a parameter an object of the Metadata class (class Metadata
) from the template framework and which returns a Metadata in its return, which can be the same one received or another created by the function.
Metadata
When run-script
is run and exported using a Metadata class, you can see a structure like the one below:
import shutil
import os
from templateframework.metadata import Metadata
def run(metadata: Metadata = None):
inputs = metadata.all_inputs()
inputs_local = metadata.inputs
inputs_global = metadata.global_inputs
inputs_computed_global = metadata.global_computed_inputs
target_path = metadata.target_path
component_path = metadata.component_path
stack_path = metadata.stack_path
See the description of each item in the class:
inputs = metadata.all_inputs()
: Returns all inputs processed from the Plugin/Template;inputs_local = metadata.inputs
: It is the input dictionary;inputs_global = metadata.global_inputs
: Dictionary of global inputs;inputs_computed_global = metadata.global_computed_inputs
: Dictionary of global computed inputs;target_path = metadata.target_path
: It is the path where the Plugin/Template is being executed.component_path = metadata.component_path
: It is the Plugin/Template path(~/.stk/stacks/<stack_name>/<component_name>)
.stack_path = metadata.stack_path
: It is the Stack path(~/.stk/stacks/<stack_name>)
.
See a simple example of defining a script, which can execute as a Declarative Hook:
from templateframework.metadata import Metadata
def run(metadata: Metadata = None):
print("Hello from script.py!")
return metadata
In the script, you can use the Python 3.8 standard library and the extra libraries that are dependencies of STK CLI. Check some included below:
Available Actions
trigger:
Field to set triggers to tell you when a Python script execution should occur.
before-input
:
Executes the script before receiving the input parameters from the user.
trigger: before-input
before-render
:
Runs the script before the Template generates files in the project.
trigger: before-render
after-render
:
Runs the script after the Template generates files in the project.
trigger: after-render
script:
Defines the path of the Python script that will execute.
script: path/to/script.py
You need to execute the script outside the templates
folder of the Plugin/Template, so it won't be interpolated.
To run-script
execute a Python script, the script must be defined with:
- A function named
run
.. - The
run
function has to receive as a parameter an object from the Metadata class of the templateframework. - The function should return metadata. It can be the same metadata received or another one created by the function.
run
function in Python script example
from templateframework.metadata import Metadata
def run(metadata: Metadata = None):
print("Hello from script.py!")
return metadata
In the script the Python 3.8 standard library and the Requests and Questionary libraries are available as dependencies in STK CLI.
Declarative Hook run-script
example
hooks:
- type: run-script
trigger: before-render
script: path/to/script.py
Read more
Was this page helpful?