Skip to content

Commit

Permalink
Merge pull request #8 from MaximumFX/dev
Browse files Browse the repository at this point in the history
Added readme generator for configurations
  • Loading branch information
MaximumFX authored Sep 6, 2024
2 parents fbf388d + aa5fc50 commit 34f31f6
Show file tree
Hide file tree
Showing 11 changed files with 683 additions and 248 deletions.
12 changes: 12 additions & 0 deletions .github/workflow-templates/update-config-readme.properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Update README.md",
"description": "CLI README.md generator for ShotGrid configurations.",
"iconName": "octicon book",
"categories": [
"Automation",
"Python"
],
"filePatterns": [
"info.yml"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ jobs:
- uses: MaximumFX/tk-readme-generator@v1.0.4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
mode: config
18 changes: 18 additions & 0 deletions .github/workflow-templates/update-general-readme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Update README.md

on:
push:
paths:
- "info.yml"

permissions:
contents: write

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: MaximumFX/tk-readme-generator@v1.0.4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
mode: general
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@

# ShotGrid Readme Generator

CLI/GitHub Action README.md generator for ShotGrid frameworks/engines/apps.
CLI/GitHub Action README.md generator for ShotGrid frameworks/engines/apps and configurations.

## GitHub Action

Add this GitHub Action to a workflow to automatically update the README.md when a change is committed to info.yml.
See an example workflow [here](.github/workflow-templates/update-readme.yml).
See example workflows: [framework/engine/app](.github/workflow-templates/update-general-readme.yml) and [configuration](.github/workflow-templates/update-config-readme.yml).

## CLI
### Requirements

Requires `pyyaml`
Requires `pyyaml`, `semver`

### Usage

`python tk-readme-generator.py <INFO_FILEPATH>`

#### Options

| Argument | Description |
|---------------------------------|-----------------------------------------------------------------|
| -o, --override | Override the existing readme. |
| -p FILEPATH, --prepend FILEPATH | Prepend an existing readme file after the name and description. |
| -a FILEPATH, --append FILEPATH | Append an existing readme file to the end. |
| Argument | Description |
|---------------------------------|------------------------------------------------------------------------------------------------------------------------|
| -o, --override | Override the existing readme. |
| -p FILEPATH, --prepend FILEPATH | Prepend an existing readme file after the name and description. |
| -a FILEPATH, --append FILEPATH | Append an existing readme file to the end. |
| -m MODE, --mode MODE | Make a readme for a _general_ info file (framework/engine/app) or a _config_ info file. (Options: `general`, `config`) |
25 changes: 20 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,44 @@ inputs:
append:
required: false
description: Append an existing readme file to the end.
mode:
required: false
description: >
Make a readme for a general info file (framework/engine/app) or a config info file.
If none provided the type will be try to be detected automatically.
type: choice
options:
- general
- config

runs:
using: composite
steps:
- name: checkout repo content
- name: Checkout repo content
uses: actions/checkout@v4

- name: setup python
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: install python packages
- name: Install Python packages
shell: bash
run: |
python -m pip install --upgrade pip
pip install -r ${{ github.action_path }}/requirements.txt
- name: execute py script
- name: Execute py script with automatic mode
shell: bash
if: "${{ inputs.mode == '' }}"
run: python ${{ github.action_path }}/tk-readme-generator.py "${{ inputs.infoFile }}" -o -p "${{ inputs.prepend }}" -a "${{ inputs.append }}"

- name: commit files
- name: Execute py script with mode
shell: bash
if: "${{ inputs.mode != '' }}"
run: python ${{ github.action_path }}/tk-readme-generator.py "${{ inputs.infoFile }}" -o -p "${{ inputs.prepend }}" -a "${{ inputs.append }}" -m ${{ inputs.mode }}

- name: Commit files
shell: bash
run: |
git config --local user.email "action@github.com"
Expand Down
211 changes: 211 additions & 0 deletions generator_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
"""
Readme generator for ShotGrid configurations
"""

import subprocess
from pathlib import Path

import semver
import yaml

import utils


def _get_include(base_filepath: Path, filename: str, info_key: str) -> str:
"""
Get the information for an env/include file.
Args:
base_filepath: Filepath to info.yml
filename: Filename to look for
info_key: Key to use for info in file
Returns:
Include section with version status table
"""
filepath = base_filepath.parent / "env" / "includes" / f"{filename}.yml"

if not filepath.is_file():
return ""

with open(str(filepath), "r") as file:
info = yaml.safe_load(file)

if info is None:
return ""

info = utils.yaml_to_nested_dict(info)[info_key]

items = []
rows = []

for key, value in info.items():
item = {**value["location"], "key": key}

if item["type"] == "app_store":
item["repo"] = f"ShotGunSoftware/{item['name']}"
elif item["type"] == "github_release":
item["repo"] = f"{item['organization']}/{item['repository']}"
else:
continue

print(
f"[{utils.capitalize(info_key)}] Getting latest version for {item['repo']}..."
)

repo_url = f"https://github.com/{item['repo']}.git"
output_lines = subprocess.check_output(
[
"git",
"ls-remote",
"--tags",
"--refs",
"--sort=version:refname",
repo_url,
],
encoding="utf-8",
).splitlines()
latest_version = output_lines[-1].rpartition("/")[-1]

source_version = None

if item["type"] == "github_release":
try:
source_lines = subprocess.check_output(
[
"git",
"ls-remote",
"--tags",
"--refs",
"--sort=version:refname",
f"https://github.com/ShotGunSoftware/{item['repository']}.git",
],
encoding="utf-8",
).splitlines()
source_version = source_lines[-1].rpartition("/")[-1]
except:
pass

item["latest"] = latest_version
if source_version is not None:
compare_version = source_version
else:
compare_version = latest_version
version = item["version"]

if version.startswith("v"):
version = version[1:]
if compare_version.startswith("v"):
compare_version = compare_version[1:]

version_count = version.count(".")
compare_count = compare_version.count(".")
if version_count != compare_count:
if version_count > 2:
parts = version.split(".")
version = ".".join(parts[0:3])

if compare_count > 2:
parts = compare_version.split(".")
compare_version = ".".join(parts[0:3])
else:
if version_count > 2:
parts = version.split(".")
version = ".".join(parts[-3:])

parts = compare_version.split(".")
compare_version = ".".join(parts[-3:])

try:
item["compared"] = semver.compare(version, compare_version)
except:
item["compared"] = None

items.append(item)

color = "blue"
if item["compared"] == -1:
if source_version is None:
color = "red"
else:
color = "orange"
if item["compared"] == 1:
color = "green"

if source_version is None:
rows.append(
[
item["key"],
f"![{item['version']}](https://img.shields.io/badge/{item['version']}-{color})",
"",
f"[![{item['repo']}](https://img.shields.io/github/v/tag/{item['repo']})](https://github.com/{item['repo']})",
]
)
else:
rows.append(
[
item["key"],
f"![{item['version']}](https://img.shields.io/badge/{item['version']}-{color})",
f"[![{item['repo']}](https://img.shields.io/github/v/tag/{item['repo']})](https://github.com/{item['repo']})",
f"[![ShotGunSoftware/{item['repository']}](https://img.shields.io/github/v/tag/ShotGunSoftware/{item['repository']})](https://github.com/ShotGunSoftware/{item['repository']})",
]
)

title = utils.capitalize(info_key)

return f"## {title}\n\n" + utils.make_table(
["Name", "Current", "Fork", "Latest"], rows
)


def generate_config_readme(
filepath: Path, prepend: str = None, append: str = None
) -> str:
"""
Generate a readme file for a ShotGrid framework/engine/app
Args:
filepath: File path to info.yml
prepend: File path to Markdown file to prepend in Readme
append: File path to Markdown file to append in Readme
Returns:
Markdown readme
"""
with open(str(filepath), "r") as file:
info = yaml.safe_load(file)

readme = ""

readme += utils.get_header(info, filepath)

# Prepend readme file
if prepend is not None and prepend != "":
prepend_filepath = Path(prepend)
if prepend_filepath.is_file():
with open(prepend_filepath, "r") as prepend_file:
readme += prepend_file.read()
readme += "\n\n"

readme += "## Requirements\n\n"

readme += utils.get_general_requirements(info)

print("Getting framework info...")
readme += _get_include(filepath, "frameworks", "frameworks")
readme += "\n\n"
print("Getting engine info...")
readme += _get_include(filepath, "engine_locations", "engines")
readme += "\n\n"
print("Getting app info...")
readme += _get_include(filepath, "app_locations", "apps")

# Append readme file
if append is not None and append != "":
append_filepath = Path(append)
if append_filepath.is_file():
with open(append_filepath, "r") as append_file:
readme += "\n\n"
readme += append_file.read()

return readme
Loading

0 comments on commit 34f31f6

Please sign in to comment.