Skip to content

Commit 9388825

Browse files
authored
Merge pull request #242 from olehermanse/replace
Added new replace_version build step
2 parents fe972b8 + 2b67f35 commit 9388825

6 files changed

Lines changed: 87 additions & 0 deletions

File tree

JSON.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ In `cfbs.json`'s `"steps"`, the build step name must be separated from the rest
264264
- Converts the input data for a module into the augments format and merges it with the target augments file.
265265
- Source is relative to module directory and target is relative to `out/masterfiles`.
266266
- In most cases, the build step should be: `input ./input.json def.json`
267+
- `replace_version <to_replace> <filename>`
268+
- Replace the string inside the file with the version number of that module.
269+
- The module must have a version and the string must occur exactly once in the file.
267270

268271
When `def.json` is modified during a `json`, `input`, `directory`, `bundles`, or `policy_files` build step, the values of some lists of strings are deduplicated, when this does not make any difference in behavior.
269272
These cases are:

cfbs/build.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"input": 2,
3131
"policy_files": "1+",
3232
"bundles": "1+",
33+
"replace_version": 2, # string to replace and filename
3334
}
3435

3536

@@ -259,6 +260,41 @@ def _perform_build_step(module, step, max_length):
259260
merged = augment
260261
log.debug("Merged def.json: %s", pretty(merged))
261262
write_json(path, merged)
263+
elif operation == "replace_version":
264+
assert len(args) == 2
265+
print("%s replace_version '%s'" % (prefix, "' '".join(args)))
266+
file = os.path.join(destination, args[1])
267+
if not os.path.isfile(file):
268+
user_error(
269+
"No such file '%s' in replace_version for module '%s"
270+
% (file, module["name"])
271+
)
272+
try:
273+
with open(file, "r") as f:
274+
content = f.read()
275+
except:
276+
user_error(
277+
"Could not open/read '%s' in replace_version for module '%s"
278+
% (file, module["name"])
279+
)
280+
to_replace = args[0]
281+
version = module["version"]
282+
new_content = content.replace(to_replace, version, 1)
283+
if new_content == content:
284+
user_error(
285+
"replace_version requires that '%s' has exactly 1 occurence of '%s' - 0 found"
286+
% (file, to_replace)
287+
)
288+
if to_replace in new_content:
289+
user_error(
290+
"replace_version requires that '%s' has exactly 1 occurence of '%s' - more than 1 found"
291+
% (file, to_replace)
292+
)
293+
try:
294+
with open(file, "w") as f:
295+
f.write(new_content)
296+
except:
297+
user_error("Failed to write to '%s'" % (file,))
262298

263299

264300
def perform_build(config) -> int:

tests/shell/043_replace_version.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
set -e
2+
set -x
3+
cd tests/
4+
mkdir -p ./tmp/
5+
cd ./tmp/
6+
7+
# Set up the project we will build:
8+
cp ../shell/043_replace_version/example-cfbs.json ./cfbs.json
9+
mkdir -p subdir
10+
cp ../shell/043_replace_version/subdir/example.py ./subdir/example.py
11+
12+
# Before building, version number is 0.0.0:
13+
grep 'print("Version: 0.0.0")' ./subdir/example.py
14+
! grep 'print("Version: 1.2.3")' ./subdir/example.py
15+
16+
cfbs build
17+
18+
# After building, input and output should be different:
19+
! diff ./subdir/example.py ./out/masterfiles/services/cfbs/subdir/example.py
20+
21+
# Check that version number is correct in output:
22+
grep 'print("Version: 1.2.3")' ./out/masterfiles/services/cfbs/subdir/example.py
23+
! grep 'print("Version: 0.0.0")' ./out/masterfiles/services/cfbs/subdir/example.py
24+
25+
# Also check that the input was not modified:
26+
grep 'print("Version: 0.0.0")' ./subdir/example.py
27+
! grep 'print("Version: 1.2.3")' ./subdir/example.py
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "Example project",
3+
"description": "Example description",
4+
"type": "policy-set",
5+
"git": true,
6+
"build": [
7+
{
8+
"name": "./subdir/",
9+
"description": "Local subdirectory added using cfbs command line",
10+
"added_by": "cfbs add",
11+
"version": "1.2.3",
12+
"steps": [
13+
"copy example.py services/cfbs/subdir/example.py",
14+
"replace_version 0.0.0 services/cfbs/subdir/example.py"
15+
]
16+
}
17+
]
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if __name__ == "__main__":
2+
print("Version: 0.0.0")

tests/shell/all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ bash tests/shell/039_add_added_by_field_update_1.sh
4646
bash tests/shell/040_add_added_by_field_update_2.sh
4747
bash tests/shell/041_add_multidep.sh
4848
bash tests/shell/042_update_from_url.sh
49+
bash tests/shell/043_replace_version.sh
4950

5051
echo "All cfbs shell tests completed successfully!"

0 commit comments

Comments
 (0)