Skip to main content
Version: v4.5.0

edit


The Declarative Hook edit defines changes made to existing files. See an example of defining a Declarative Hook of type edit:

hooks: 
- type: edit
trigger: after-render
path: src/some-file.txt
changes:
- insert:
line: 0
value: "# Text inserted in first line\n\n"
when:
not-exists: "# Text inserted in first line"
- search:
string: "# Text inserted in first line"
insert-before:
snippet: snippets/insert-before-first-line.txt
when:
not-exists: "# Text inserted before first line"
- search:
pattern: (Text )inserted in( first line)
replace-by:
value: \1moved from\2
when:
not-exists: print(f"Hello {name}!")

Available Actions

path:

Defines the path to the file it will be edited. The path can have Jinja expressions.

path: "./some/dir/{{file_name}}"

trigger:

Field to set triggers that tell you when file editing should occur.

before-input:
Executes the Declarative Hook before receiving the input parameters from the user.

trigger: before-input

before-render:
Runs the Declarative Hook before the Template generates files in the project.

trigger: before-render

after-render:
Runs the Declarative Hook after the Template generates files in the project.

trigger: after-render

changes:

In this field you define the action type it will be performed on the file, the options are insert and search.

insert:

Action that lets you insert text into an existing file.

line:
Defines the line of the file where the insertion will occur. The first line of the file corresponds to index 0. To make the insertion in the last line of the file, the index is -1.

value:
Defines the text to be inserted in the line.

snippet:
Defines a file that contains the text to be inserted into the file. The insertion of the text will be in the file entered in the path field.

when:

Conditional action that evaluates if the insertion will take place. The available options are not-exists and not-exists-snippet, which receive the text or file to be evaluated by the condition.

not-exists: Checks if the text entered in the value field exists in the file. If exists, the insert action will not take effect.

not-exists-snippet: Checks whether the file content entered in the snippet field exists in the file. If does, the insert action will not take effect.

caution

You can use the two options, not-exists and not-exists-snippet together. That way, both conditions check whether the file contents or text exist in the file entered in value and snippet. Only if in both results the text and file contents do not exist, the insert action will not take effect.

hooks:
- type: edit
/
.
.
.
/
- insert:
line: 0
value: "# Text inserted in first line\n\n"
# or
snippet: path/to/snippet.txt
when:
not-exists: "# Text inserted in first line"
# and/or
not-exists-snippet: path/to/snippet.txt

The editing action searches for the first occurrence of the text in the file.

string:
Defines the text that will be used to search the edited file.

pattern:
Defines the pattern that will be used in the search.

snippet: Defines the file with the content that will be used in the search.

The string, pattern and snippet attributes must not be used together in the search action.

  - search:
string: "# Text inserted in first line"
# or
pattern: (some)\s+(regular)\s+(expression)
# or
snippet: snippets/snippet.txt

replace-by:

Replaces the first match of the text found in the search with the contents of snippet and value.

insert-before:

Inserts the value or snippet on the line before the line where the first match was found.

insert-after:

Inserts the value or snippet on the next line relative to the line where the first match was found.

caution

Only one of the operations must be defined in the search.

replace-by:
snippet: snippets/snippet.txt
# or
insert-before:
value: some string
# or
insert-after:
snippet: snippets/snippet2.txt

snippet:
Defines a file that contains the text to be used when finding the first match in the file.

value:
Sets a string to be used when finding the first match in the file.

caution

The value and snippet attributes should not be used together.

  snippet: snippets/snippet.txt
# or
value: a string

when:

Conditional action that evaluates whether the search will be performed. The available options are not-exists and not-exists-snippet, which receive the text or file to be evaluated by the condition.

not-exists:
Checks if the text entered in the value field exists in the file. If it exists, the search action will not be performed.

not-exists-snippet:
Checks whether the file content entered in the snippet field exists in the archive. If it does, the search action will not be performed.

Example of an edit Declarative Hook with the insert action

hooks:
- type: edit
trigger: after-render
path: src/some-file.txt
changes:
- insert:
line: 0
value: "# Text inserted in first line\n\n"
when:
not-exists: "# Text inserted in first line"

Example of an edit Declarative Hook with the search action options

hooks:
- type: edit
trigger: after-render
path: src/some-file.txt
changes:
- search:
string: "# Text inserted in first line"
# or
pattern: (some)\s+(regular)\s+(expression)
# or
snippet: snippets/snippet.txt
replace-by:
snippet: snippets/snippet.txt
# or
value: \1 \2 \3
# or
insert-before:
snippet: snippets/snippet.txt
# or
value: a string
# or
insert-after:
snippet: snippets/snippet.txt
# or
value: a string
when:
not-exists: "# Text inserted before first line"

Read Also

Was this page helpful?