Skip to content

Commit

Permalink
Python instead of shell for updating citation
Browse files Browse the repository at this point in the history
  • Loading branch information
JeltevanBoheemen committed Jul 23, 2024
1 parent 472f067 commit 053f1e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"patch": "yarn version --patch --no-commit-hooks --no-git-tag-version && yarn update-citation && yarn fyarn prebuild",
"minor": "yarn version --minor --no-commit-hooks --no-git-tag-version && yarn update-citation && yarn fyarn prebuild",
"major": "yarn version --major --no-commit-hooks --no-git-tag-version && yarn update-citation && yarn fyarn prebuild",
"update-citation": "$PWD/update-citation.sh"
"update-citation": "python $PWD/update_citation.py"
},
"private": true,
"devDependencies": {},
Expand Down
4 changes: 0 additions & 4 deletions update-citation.sh

This file was deleted.

38 changes: 38 additions & 0 deletions update_citation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''Updates the CITATION.cff file:
- Sets the date-released to the current date
- Sets the version from toplevel package.json
'''

from datetime import datetime
import json
import re

CITATION_FILE = 'CITATION.CFF'
PACKAGE_FILE = 'package.json'
VERSION_PATTERN = r'^version:\s+.*$'
DATE_RELEASED_PATTERN = r'^date-released:.*$'
VERSION = None
TODAY = datetime.today().strftime('%Y-%m-%d')

with open(PACKAGE_FILE, 'r') as package_file:
package_json = json.load(package_file)
try:
VERSION = package_json.get('version')
except KeyError:
print('No version found in root package.json')
VERSION = '0.0.0'

with open(CITATION_FILE) as citation_file:
citation_in = citation_file.readlines()
citation_out = []

for line in citation_in:
if re.match(VERSION_PATTERN, line):
citation_out.append(f'version: {VERSION}\n')
elif re.match(DATE_RELEASED_PATTERN, line):
citation_out.append(f"date-released: '{TODAY}'\n")
else:
citation_out.append(line)

with open(CITATION_FILE, 'w') as citation_file:
citation_file.writelines(citation_out)

0 comments on commit 053f1e4

Please sign in to comment.