Skip to content

Commit

Permalink
update yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
yibeichan committed May 10, 2024
1 parent 5ed5e33 commit bc47260
Show file tree
Hide file tree
Showing 33 changed files with 175 additions and 85 deletions.
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-ast
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ You can find a demo protocol [here](https://github.com/ReproNim/reproschema-demo
The following are required and recommended tools for using this cookiecutter and the ReproSchema protocol it generates. This is all a one-time setup, so if you have already done it, skip to the [next section](#creating-a-new-project)!

* **pipx**

pipx is a tool for managing isolated Python-based applications. It is the recommended way to install Poetry and cruft. To install `pipx` follow the instructions here: https://pypa.github.io/pipx/installation/

* **cruft**

cruft is a tool for generating projects based on a cookiecutter (like this one!) and keeping those projects updated if the original cookiecutter changes. Install it with `pipx` by running:
```shell
pipx install cruft
```
You may also choose to not have a persistent installation of cruft, in which case you would replace any calls to the `cruft` command below with `pipx run cruft`.
You may also choose to not have a persistent installation of cruft, in which case you would replace any calls to the `cruft` command below with `pipx run cruft`.

## Creating a new protocol

Expand Down Expand Up @@ -71,21 +71,21 @@ make setup
git branch -M main
git push -u origin main
```
3. Create the gh-pages branch
- Fetch the latest changes from your repository (if any):
```bash
git fetch origin
```
- Create and switch to the new gh-pages branch:
```bash
git checkout -b gh-pages
```
3. Create the gh-pages branch
- Fetch the latest changes from your repository (if any):
```bash
git fetch origin
```
- Create and switch to the new gh-pages branch:
```bash
git checkout -b gh-pages
```
- Push the gh-pages branch to remote:
```bash
git push --set-upstream origin gh-pages
```
This branch allows you to deploy your ReproSchema UI publicly.
This branch allows you to deploy your ReproSchema UI publicly.

## Keeping your project up to date

To be up-to-date with the template, first check if there is a mismatch
Expand Down
86 changes: 61 additions & 25 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,88 @@
import os
import shutil
import requests
import subprocess


def get_pref_label(activity_path):
try:
with open(activity_path, 'r') as file:
with open(activity_path, "r") as file:
activity_schema = json.load(file)
return activity_schema.get("prefLabel", "Unknown Activity")
except (FileNotFoundError, json.JSONDecodeError):
return "Unknown Activity"


def update_json_schema(activities, base_path):
schema_file = os.path.join(base_path, '{{ cookiecutter.__protocol_slug }}/{{ cookiecutter.__protocol_slug }}_schema')
with open(schema_file, 'r') as file:
schema_file = os.path.join(
base_path,
"{{ cookiecutter.__protocol_slug }}/{{ cookiecutter.__protocol_slug }}_schema",
)
with open(schema_file, "r") as file:
schema = json.load(file)

schema['ui']['addProperties'] = []
schema['ui']['order'] = []
schema["ui"]["addProperties"] = []
schema["ui"]["order"] = []

for activity in activities:
activity_schema_path = os.path.join(base_path, f'activities/{activity}/{activity}_schema')
activity_schema_path = os.path.join(
base_path, f"activities/{activity}/{activity}_schema"
)
pref_label = get_pref_label(activity_schema_path)
activity_path = f"../activities/{activity}/{activity}_schema"

schema['ui']['addProperties'].append({
"isAbout": activity_path,
"variableName": f"{activity}_schema",
"prefLabel": pref_label
})
schema['ui']['order'].append(activity_path)
schema["ui"]["addProperties"].append(
{
"isAbout": activity_path,
"variableName": f"{activity}_schema",
"prefLabel": pref_label,
}
)
schema["ui"]["order"].append(activity_path)

with open(schema_file, 'w') as file:
with open(schema_file, "w") as file:
json.dump(schema, file, indent=4)


def fetch_latest_checksum(base_path):
try:
# Using Python requests to fetch and parse JSON
response = requests.get("https://api.github.com/repos/ReproNim/reproschema-ui/commits/master")
response = requests.get(
"https://api.github.com/repos/ReproNim/reproschema-ui/commits/master"
)
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
latest_hash = response.json()['sha']
latest_hash = response.json()["sha"]

config_path = os.path.join(base_path, 'config.env')
with open(config_path, 'w') as file:
config_path = os.path.join(base_path, "config.env")
with open(config_path, "w") as file:
file.write(f"REPROSCHEMA_UI_CHECKSUM={latest_hash}\n")

print("Latest checksum fetched and saved in config.env.")

except Exception as e:
print(f"Error fetching checksum: {e}")


def setup_pre_commit():
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "pre-commit"])
subprocess.check_call(["pre-commit", "install"])
print("Pre-commit hooks set up successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to set up pre-commit hooks: {e}")


def main():
base_path = os.getcwd()
init_flag_path = os.path.join(base_path, '.initialized')
init_flag_path = os.path.join(base_path, ".initialized")

# Check if the project has been initialized before
if not os.path.exists(init_flag_path):
# Project initialization logic
activities_path = os.path.join(base_path, 'activities')
activities_path = os.path.join(base_path, "activities")

try:
with open('selected_activities.json') as f:
with open("selected_activities.json") as f:
selected_activities = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
print("Error reading selected activities.")
Expand All @@ -71,22 +94,35 @@ def main():
if not os.path.exists(activity_dir):
os.makedirs(activity_dir)

all_activities = ['Activity1', 'Activity2', 'Activity3', 'selectActivity', 'voiceActivity']
all_activities = [
"Activity1",
"Activity2",
"Activity3",
"selectActivity",
"voiceActivity",
]
for activity in all_activities:
if activity not in selected_activities:
activity_dir = os.path.join(activities_path, activity)
if os.path.exists(activity_dir):
shutil.rmtree(activity_dir)

activities = [activity for activity in selected_activities if os.path.exists(os.path.join(activities_path, activity))]
activities = [
activity
for activity in selected_activities
if os.path.exists(os.path.join(activities_path, activity))
]
update_json_schema(activities, base_path)

# Mark initialization as complete
with open(init_flag_path, 'w') as f:
f.write('initialized')
with open(init_flag_path, "w") as f:
f.write("initialized")

# Fetch and save the latest checksum
fetch_latest_checksum(base_path)

setup_pre_commit()


if __name__ == "__main__":
main()
main()
17 changes: 13 additions & 4 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
import sys
import json


def select_activities(n):
all_activities = ['Activity1', 'Activity2', 'Activity3', 'selectActivity', 'voiceActivity']
all_activities = [
"Activity1",
"Activity2",
"Activity3",
"selectActivity",
"voiceActivity",
]
return random.sample(all_activities, n)


def main():
# Read the number of activities from the Cookiecutter context
num_activities_str = '{{ cookiecutter.number_of_activities }}'
num_activities_str = "{{ cookiecutter.number_of_activities }}"

try:
num_activities = int(num_activities_str)
Expand All @@ -23,8 +31,9 @@ def main():
print(f"Selected activities: {', '.join(selected_activities)}")

# Write the selected activities to a file
with open('selected_activities.json', 'w') as f:
with open("selected_activities.json", "w") as f:
json.dump(selected_activities, f)


if __name__ == "__main__":
main()
main()
24 changes: 15 additions & 9 deletions update_schema_version.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import os
import re

latest_release = os.environ['LATEST_RELEASE']
latest_release = os.environ["LATEST_RELEASE"]


def update_file(file_path, version):
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
content = file.read()

content = re.sub(r'"@context": "https://raw\.githubusercontent\.com/ReproNim/reproschema/.+?/contexts/generic"',
f'"@context": "https://raw.githubusercontent.com/ReproNim/reproschema/{version}/contexts/generic"',
content)
content = re.sub(r'"schemaVersion": ".+?"', f'"schemaVersion": "{version}"', content)
content = re.sub(
r'"@context": "https://raw\.githubusercontent\.com/ReproNim/reproschema/.+?/contexts/generic"',
f'"@context": "https://raw.githubusercontent.com/ReproNim/reproschema/{version}/contexts/generic"',
content,
)
content = re.sub(
r'"schemaVersion": ".+?"', f'"schemaVersion": "{version}"', content
)

with open(file_path, 'w') as file:
with open(file_path, "w") as file:
file.write(content)

for root, dirs, files in os.walk('.'):

for root, dirs, files in os.walk("."):
for file in files:
if file.endswith('_schema') or file.endswith('_item') or '_item_' in file:
if file.endswith("_schema") or file.endswith("_item") or "_item_" in file:
update_file(os.path.join(root, file), latest_release)
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
- name: Set Environment Variable
run: |
echo "PROTOCOL_SLUG={{cookiecutter.__protocol_slug}}" >> $GITHUB_ENV
- name: Echo Environment Variable
run: |
echo "PROTOCOL_SLUG is set to $PROTOCOL_SLUG"
echo "PROTOCOL_SLUG is set to $PROTOCOL_SLUG"
- name: Check out repository
uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
run: |
python -m pip install --upgrade pip setuptools
pip install reproschema requests_cache pre-commit
- name: Test with pyshacl
run: |
reproschema -l DEBUG validate activities
Expand All @@ -76,6 +76,6 @@ jobs:
if: github.ref == 'refs/heads/main'
uses: JamesIves/github-pages-deploy-action@v4
with:
token: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
token: "{% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}"
branch: gh-pages
folder: ui/dist
2 changes: 1 addition & 1 deletion {{cookiecutter.protocol_name}}/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.DS_Store
*.DS_Store
20 changes: 20 additions & 0 deletions {{cookiecutter.protocol_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-ast
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
2 changes: 1 addition & 1 deletion {{cookiecutter.protocol_name}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ cruft-check:

# Update the project from the Cookiecutter template
cruft-update:
cruft update
cruft update
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"reproschema:AllowExport"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"responseOptions": {
"valueType": "DigitalDocument"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
"reproschema:AllowExport"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"responseOptions": {
"valueType": "xsd:date"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"responseOptions": {
"valueType": "xsd:datetime"
}
}
}
Loading

0 comments on commit bc47260

Please sign in to comment.