Skip to content

Commit

Permalink
feat: support combining templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mohnoor94 committed Aug 20, 2024
1 parent b34fa56 commit cdb755b
Show file tree
Hide file tree
Showing 34 changed files with 122 additions and 9 deletions.
37 changes: 37 additions & 0 deletions .github/scripts/combine_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import shutil


def copy_files(src_dir, dst_dir, base_dir):
for root, _, files in os.walk(src_dir):
for file in files:
src = os.path.join(root, file)
dst = os.path.join(dst_dir, os.path.relpath(src, base_dir))
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
print(f"Copied {src} to {dst}")


def handle_versions(base_templates, version, output_dir):
for i in range(1, version + 1):
version_dir = os.path.join(base_templates, f"v{i}")
if not os.path.exists(version_dir):
print(f"Version {i} does not exist - skipping")
continue
print(f"Copying version v{i} from {version_dir}")
copy_files(version_dir, output_dir, version_dir)


if __name__ == "__main__":
base_templates = os.getenv('INPUT_BASE_TEMPLATES')
custom_templates = os.getenv('INPUT_CUSTOM_TEMPLATES')
version = int(os.getenv('INPUT_VERSION'))
output_dir = 'tmp/templates'

print(f"Base templates: {base_templates}")
print(f"Custom templates: {custom_templates}")
print(f"Version: {version}")
print(f"Output templates: {output_dir}")

handle_versions(base_templates, version, output_dir)
copy_files(custom_templates, output_dir, custom_templates)
47 changes: 47 additions & 0 deletions .github/workflows/selfserve-combine-templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Combine Templates

on:
workflow_dispatch:
inputs:
base_templates:
description: 'Base SDK templates path (without the versioned directories)'
type: string
default: 'sdk-repo/generator/openapi/src/main/resources/templates/expediagroup-sdk'
version:
description: 'Base templates version'
type: number
default: 1
custom_templates:
description: 'Custom templates path to merge with base templates'
type: string
default: ''
combined_templates_key:
description: 'Key to the combined templates artifact'
type: string
default: 'templates'

jobs:
combine-templates:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Combine templates
run: python .github/scripts/combine_templates.py
env:
INPUT_BASE_TEMPLATES: ${{ github.event.inputs.base_templates }}
INPUT_CUSTOM_TEMPLATES: ${{ github.event.inputs.custom_templates }}
INPUT_VERSION: ${{ github.event.inputs.version }}
INPUT_OUTPUT_DIR: 'tmp/templates'

- name: Upload combined templates
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.combined_templates_key }}
path: tmp/templates
32 changes: 28 additions & 4 deletions .github/workflows/selfserve-full-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,27 @@ on:
E.g., 1.0.0, 1.0.1, 1.0.0-SNAPSHOT, etc.
required: true
type: string
templates:
custom_templates:
description: |
Path to the templates directory.
Path to the custom templates directory.
Make sure to checkout the custom templates directory before setting this input.
type: string
default: '-NA-'
base_templates:
description: |
Path to the base templates directory (without the version number).
Use the default path if you are using the default templates.
Use `sdk-repo` to refer to the `expediagroup-sdk` repo root path if needed.
type: string
default: 'sdk-repo/generator/openapi/src/main/resources/templates/expediagroup-sdk'
templates_version:
description: 'Base templates version'
type: number
default: 1
combined_templates_key:
description: 'Key to the combined templates artifact'
type: string
default: 'templates'
repository:
description: |
Repository to generate the SDK in.
Expand Down Expand Up @@ -76,7 +90,9 @@ jobs:
run: |
echo "SDK Name: ${{ inputs.name }}"
echo "SDK Version: ${{ inputs.version }}"
echo "Templates: ${{ inputs.templates }}"
echo "Base Templates: ${{ inputs.base_templates }}"
echo "Base Templates Version: v${{ inputs.templates_version }}"
echo "Custom Templates: ${{ inputs.custom_templates }}"
echo "Repository: ${{ inputs.repository }}"
echo "Ref: ${{ inputs.ref }}"
echo "Specs Key: ${{ inputs.specs_key }}"
Expand All @@ -102,14 +118,22 @@ jobs:
transformations: ${{ inputs.transformations }}
transformed_specs_key: ${{ inputs.transformed_specs_key }}

combine-templates:
uses: ./.github/workflows/selfserve-combine-templates.yaml
with:
base_templates: ${{ inputs.base_templates }}
version: ${{ inputs.templates_version }}
custom_templates: ${{ inputs.custom_templates }}
combined_templates_key: ${{ inputs.combined_templates_key }}

generate-sdk:
uses: ./.github/workflows/selfserve-generate.yaml
needs: [ transform-specs ]
with:
name: ${{ inputs.name }}
version: ${{ inputs.version }}
templates: ${{ inputs.templates }}
specs_key: ${{ inputs.transformed_specs_key }}
templates_key: ${{ inputs.combined_templates_key }}
sdk_key: ${{ inputs.sdk_key }}

publish-sources:
Expand Down
15 changes: 10 additions & 5 deletions .github/workflows/selfserve-generate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ on:
description: 'SDK Version'
required: true
type: string
templates:
description: 'Path to the templates directory'
templates_key:
description: 'Key to the combined templates artifact'
required: true
type: string
specs_key:
Expand Down Expand Up @@ -45,6 +45,11 @@ jobs:
with:
name: ${{ inputs.specs_key }}

- name: Download Templates
uses: actions/download-artifact@v4
with:
name: ${{ inputs.templates_key }}

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
Expand All @@ -53,12 +58,11 @@ jobs:

- name: Generate Product SDK
run: |
mvn -f sdk-repo/generator/openapi clean install exec:java "-Dnamespace=${{ inputs.name }}" -DsdkVersion=${{ inputs.version }} "-Dspec=./${{ inputs.specs_key }}.yaml" -DtemplatesDir=./${{ inputs.templates }}
mvn -f sdk-repo/generator/openapi clean install exec:java "-Dnamespace=${{ inputs.name }}" -DsdkVersion=${{ inputs.version }} "-Dspec=./${{ inputs.specs_key }}.yaml" -DtemplatesDir=./${{ inputs.templates_key }}
- name: Install SDK
working-directory: sdk-repo/generator/openapi/target/sdk
run: |
mvn clean install
mvn clean install -f sdk-repo/generator/openapi/target/sdk
- name: Persist SDK Artifact
uses: actions/upload-artifact@v4
Expand All @@ -67,6 +71,7 @@ jobs:
path: |
sdk-repo/generator/openapi/target/sdk
!sdk-repo/generator/openapi/target/sdk/target
- name: Persist JAR Artifact # TODO: Remove when product config is proposed
uses: actions/upload-artifact@v4
with:
Expand Down

0 comments on commit cdb755b

Please sign in to comment.