Skip to content

Commit 39f1852

Browse files
committed
refactor: re-add template
1 parent f81f5a6 commit 39f1852

File tree

5 files changed

+133
-158
lines changed

5 files changed

+133
-158
lines changed

.github/actions/setvars/action.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: 'Set environment variables'
2+
description: 'Configures environment variables for a workflow'
3+
inputs:
4+
varFilePath:
5+
description: 'File path to variable file or directory. Defaults to ./.github/variables/* if none specified and runs against each file in that directory.'
6+
required: false
7+
default: ./.github/variables/*
8+
runs:
9+
using: "composite"
10+
steps:
11+
- run: |
12+
sed "" ${{ inputs.varFilePath }} >> $GITHUB_ENV
13+
shell: bash

.github/workflows/auto-update.yml

Lines changed: 15 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,39 @@
1-
name: Auto update generated client
2-
on:
3-
workflow_dispatch:
4-
schedule:
5-
- cron: '0 0 * * *'
61

7-
# Allows external webhook trigger
82
repository_dispatch:
93
types:
104
- webhook
11-
env:
12-
library_last_version: 0.0.0
13-
template_last_version: 0.0.0
14-
template_current_version: 0.0.0
15-
document_last_version: 0.0.0
16-
document_current_version: 0.0.0
17-
major_template_last_version: 0
18-
minor_template_last_version: 0
19-
patch_template_last_version: 0
20-
major_template_current_version: 0
21-
minor_template_current_version: 0
22-
patch_template_current_version: 0
23-
major_version_change: false
24-
minor_version_change: false
25-
patch_version_change: false
26-
commit_message: ""
275
jobs:
28-
get-template-version:
6+
update-code:
297
runs-on: ubuntu-latest
308
steps:
31-
- uses: actions/checkout@v2
32-
# Find all relevant semver versions needed to figure out how to update the client
33-
- name: Fetch the last AsyncAPI document version that generated the client
34-
run: echo "document_last_version=$(cat ./configs.json | jq -r '.document_last_version')" >> $GITHUB_ENV
35-
- name: Fetch the last template version that generated the client
36-
run: echo "template_last_version=$(cat ./configs.json | jq -r '.template_last_version')" >> $GITHUB_ENV
37-
- name: Fetch the last library version
38-
run: echo "library_last_version=$(cat ./package.json | jq -r '.version':=0.0.0)" >> $GITHUB_ENV
39-
- name: Fetch current template version
40-
run: echo "template_current_version=$(curl -sL https://api.github.com/repos/asyncapi/ts-nats-template/releases/latest | jq -r '.tag_name' | sed 's/v//')" >> $GITHUB_ENV
41-
- name: Get all the application AsyncAPI documents
42-
uses: actions/checkout@v2
9+
- uses: actions/checkout@v3
10+
11+
- uses: actions/setup-node@v3
4312
with:
44-
repository: GamingAPI/definitions
45-
path: "./definitions"
46-
- name: Get the version of the API
47-
run: echo "document_current_version=$(cat ./definitions/bundled/rust.asyncapi.json | jq -r '.info.version' | sed 's/v//')" >> $GITHUB_ENV
13+
node-version: '14'
4814

49-
# Figure out how the template version have changed
50-
- run: semver_template_last_version=( ${template_last_version//./ } ) && major_template_last_version=${semver_template_last_version[0]} && echo "major_template_last_version=$major_template_last_version" >> $GITHUB_ENV
51-
- run: semver_template_last_version=( ${template_last_version//./ } ) && minor_template_last_version=${semver_template_last_version[1]} && echo "minor_template_last_version=$minor_template_last_version" >> $GITHUB_ENV
52-
- run: semver_template_last_version=( ${template_last_version//./ } ) && patch_template_last_version=${semver_template_last_version[2]} && echo "patch_template_last_version=$patch_template_last_version" >> $GITHUB_ENV
53-
- run: semver_template_current_version=( ${template_current_version//./ } ) && major_template_current_version=${semver_template_current_version[0]} && echo "major_template_current_version=$major_template_current_version" >> $GITHUB_ENV
54-
- run: semver_template_current_version=( ${template_current_version//./ } ) && minor_template_current_version=${semver_template_current_version[1]} && echo "minor_template_current_version=$minor_template_current_version" >> $GITHUB_ENV
55-
- run: semver_template_current_version=( ${template_current_version//./ } ) && patch_template_current_version=${semver_template_current_version[2]} && echo "patch_template_current_version=$patch_template_current_version" >> $GITHUB_ENV
56-
- if: ${{ env.major_template_current_version > env.major_template_last_version }}
57-
run: echo "major_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}Template have changed to a new major version." >> $GITHUB_ENV
58-
- if: ${{ env.minor_template_current_version > env.minor_template_last_version }}
59-
run: echo "minor_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}Template have changed to a new minor version." >> $GITHUB_ENV
60-
- if: ${{ env.patch_template_current_version > env.patch_template_last_version }}
61-
run: echo "patch_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}Template have changed to a new patch version." >> $GITHUB_ENV
62-
63-
# Figure out how the AsyncAPI document have changed
64-
- run: semver_document_last_version=( ${document_last_version//./ } ) && major_document_last_version=${semver_document_last_version[0]} && echo "major_document_last_version=$major_document_last_version" >> $GITHUB_ENV
65-
- run: semver_document_last_version=( ${document_last_version//./ } ) && minor_document_last_version=${semver_document_last_version[1]} && echo "minor_document_last_version=$minor_document_last_version" >> $GITHUB_ENV
66-
- run: semver_document_last_version=( ${document_last_version//./ } ) && patch_document_last_version=${semver_document_last_version[2]} && echo "patch_document_last_version=$patch_document_last_version" >> $GITHUB_ENV
67-
- run: semver_document_current_version=( ${document_current_version//./ } ) && major_document_current_version=${semver_document_current_version[0]} && echo "major_document_current_version=$major_document_current_version" >> $GITHUB_ENV
68-
- run: semver_document_current_version=( ${document_current_version//./ } ) && minor_document_current_version=${semver_document_current_version[1]} && echo "minor_document_current_version=$minor_document_current_version" >> $GITHUB_ENV
69-
- run: semver_document_current_version=( ${document_current_version//./ } ) && patch_document_current_version=${semver_document_current_version[2]} && echo "patch_document_current_version=$patch_document_current_version" >> $GITHUB_ENV
70-
- if: ${{ env.major_document_current_version > env.major_document_last_version }}
71-
run: echo "major_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}AsyncAPI document have changed to a new major version. " >> $GITHUB_ENV
72-
- if: ${{ env.minor_document_current_version > env.minor_document_last_version && env.major_document_current_version < env.major_document_last_version }}
73-
run: echo "minor_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}AsyncAPI document have changed to a new minor version. " >> $GITHUB_ENV
74-
- if: ${{ env.patch_document_current_version > env.patch_document_last_version && env.minor_document_current_version < env.minor_document_last_version && env.major_document_current_version < env.major_document_last_version }}
75-
run: echo "patch_version_change=true" >> $GITHUB_ENV && echo "commit_message=${commit_message}AsyncAPI document have changed to a new patch template version. " >> $GITHUB_ENV
15+
- name: Generate code
16+
run: chmod +x ./generate.sh && ./generate.sh
7617

77-
# Make changes to the client code if needed
78-
- name: Remove previous files to ensure clean slate
79-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
80-
run: find . -not \( -name configs.json -or -name .gitignore -or -name custom_package.json -or -iwholename *.github* -or -iwholename *./definitions* -or -iwholename *.git* -or -name . \) -exec echo {} +
81-
- name: Generating client from the AsyncAPI document
82-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
83-
uses: docker://asyncapi/github-action-for-generator
18+
- name: Set Environment Variables from generator file
19+
uses: ./.github/actions/setvars
8420
with:
85-
template: '@asyncapi/ts-nats-template'
86-
filepath: ./definitions/bundled/rust.asyncapi.json
87-
output: ./
88-
- name: Write new config file to ensure we keep the new state for next time
89-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
90-
run: |
91-
contents="$(jq ".template_last_version = \"$template_current_version\" | .document_last_version = \"$document_current_version\"" configs.json)" && echo "${contents}" > configs.json
92-
- name: Write old version to package.json file as it was cleared by the generator
93-
if: ${{ env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
94-
run: contents="$(jq ".version = \"$library_last_version\"" package.json)" && echo "${contents}" > package.json
95-
- name: Merge custom package file with template generated
96-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
97-
run: jq -s '.[0] * .[1]' ./package.json ./custom_package.json > ./package_tmp.json
98-
- name: Remove old package file
99-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
100-
run: rm ./package.json
101-
- name: Take new package file in use
102-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
103-
run: mv ./package_tmp.json ./package.json
104-
105-
# Clean up from code generation
106-
- name: Remove excess files
107-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
108-
run: rm -rf ./definitions
109-
110-
# Build the library
111-
- name: Install dependencies
112-
if: ${{env.major_version_change == 'true' || env.minor_version_change == 'true' || env.patch_version_change == 'true'}}
113-
run: npm i
114-
21+
varFilePath: ./.github/variables/generator.env
11522
# Follow conventional commit for PR's to ensure accurate updates
11623
- name: Create pull request for major version if needed
11724
if: ${{env.major_version_change == 'true'}}
118-
uses: peter-evans/create-pull-request@v3
25+
uses: peter-evans/create-pull-request@v4
11926
with:
12027
title: "feat!: major version change"
121-
committer: GamingEventapiBot <gamingeventapi@gmail.com>
122-
author: GamingEventapiBot <gamingeventapi@gmail.com>
123-
delete-branch: true
124-
commit-message: ${{env.commit_message}}. Library require a new major version change.
125-
body: ${{env.commit_message}}. Library require a new major version change.
28+
12629
- name: Create pull request for minor version if needed
12730
if: ${{env.minor_version_change == 'true'}}
128-
uses: peter-evans/create-pull-request@v3
31+
uses: peter-evans/create-pull-request@v4
12932
with:
13033
title: "feat: minor version change"
131-
committer: GamingEventapiBot <gamingeventapi@gmail.com>
132-
author: GamingEventapiBot <gamingeventapi@gmail.com>
133-
delete-branch: true
134-
commit-message: ${{env.commit_message}}. Library require a new minor version change.
135-
body: ${{env.commit_message}}. Library require a new minor version change.
34+
13635
- name: Create pull request for patch version if needed
13736
if: ${{env.patch_version_change == 'true'}}
138-
uses: peter-evans/create-pull-request@v3
37+
uses: peter-evans/create-pull-request@v4
13938
with:
14039
title: "fix: patch version change"
141-
committer: GamingEventapiBot <gamingeventapi@gmail.com>
142-
author: GamingEventapiBot <gamingeventapi@gmail.com>
143-
delete-branch: true
144-
commit-message: ${{env.commit_message}}. Library require a new patch version change.
145-
body: ${{env.commit_message}}. Library require a new patch version change.

.gitignore

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,2 @@
1-
*.swp
2-
*.*~
3-
project.lock.json
4-
.DS_Store
5-
*.pyc
6-
nupkg/
7-
8-
# Visual Studio Code
9-
.vscode
10-
11-
# Rider
12-
.idea
13-
14-
# User-specific files
15-
*.suo
16-
*.user
17-
*.userosscache
18-
*.sln.docstates
19-
20-
# Build results
21-
[Dd]ebug/
22-
[Dd]ebugPublic/
23-
[Rr]elease/
24-
[Rr]eleases/
25-
x64/
26-
x86/
27-
build/
28-
bld/
29-
[Bb]in/
30-
[Oo]bj/
31-
[Oo]ut/
32-
msbuild.log
33-
msbuild.err
34-
msbuild.wrn
35-
36-
# Visual Studio 2015
37-
.vs/
1+
node_modules
2+
.github/variables

generate.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
set -e
3+
4+
library_last_version="0.0.0"
5+
template_last_version="0.0.0"
6+
template_current_version="0.0.0"
7+
document_last_version="0.0.0"
8+
document_current_version="0.0.0"
9+
major_template_last_version=0
10+
minor_template_last_version=0
11+
patch_template_last_version=0
12+
major_template_current_version=0
13+
minor_template_current_version=0
14+
patch_template_current_version=0
15+
major_version_change="false"
16+
minor_version_change="false"
17+
patch_version_change="false"
18+
commit_message=""
19+
20+
document_last_version=$(cat ./configs.json | jq -r '.document_last_version')
21+
template_last_version=$(cat ./configs.json | jq -r '.template_last_version')
22+
library_last_version=$(cat ./package.json | jq -r '.version')
23+
24+
[ -d "./definitions" ] && rm -rf ./definitions
25+
26+
template_current_version=$(curl -sL https://api.github.com/repos/asyncapi/ts-nats-template/releases/latest | jq -r '.tag_name' | sed 's/v//')
27+
28+
git clone https://github.com/GamingAPI/definitions.git definitions
29+
document_current_version=$(cat ./definitions/bundled/rust.asyncapi.json | jq -r '.info.version' | sed 's/v//')
30+
31+
semver_template_last_version=( ${template_last_version//./ } )
32+
major_template_last_version=${semver_template_last_version[0]}
33+
minor_template_last_version=${semver_template_last_version[1]}
34+
patch_template_last_version=${semver_template_last_version[2]}
35+
semver_template_current_version=( ${template_current_version//./ } )
36+
major_template_current_version=${semver_template_current_version[0]}
37+
minor_template_current_version=${semver_template_current_version[1]}
38+
patch_template_current_version=${semver_template_current_version[2]}
39+
40+
if (($major_template_current_version > $major_template_last_version)); then
41+
major_version_change="true"
42+
commit_message="${commit_message}Template have changed to a new major version."
43+
elif (($minor_template_current_version > $minor_template_last_version)); then
44+
minor_version_change="true"
45+
commit_message="${commit_message}Template have changed to a new minor version."
46+
elif (($patch_template_current_version > $patch_template_last_version)) && (($minor_template_current_version < $minor_template_last_version)) && (($major_template_current_version < $major_template_last_version)); then
47+
patch_version_change="true"
48+
commit_message="${commit_message}Template have changed to a new patch version."
49+
fi
50+
51+
semver_document_last_version=( ${document_last_version//./ } )
52+
major_document_last_version=${semver_document_last_version[0]}
53+
minor_document_last_version=${semver_document_last_version[1]}
54+
patch_document_last_version=${semver_document_last_version[2]}
55+
semver_document_current_version=( ${document_current_version//./ } )
56+
major_document_current_version=${semver_document_current_version[0]}
57+
minor_document_current_version=${semver_document_current_version[1]}
58+
patch_document_current_version=${semver_document_current_version[2]}
59+
60+
if (($major_document_current_version > $major_document_last_version)); then
61+
major_version_change="true"
62+
commit_message="${commit_message}AsyncAPI document have changed to a new major version."
63+
elif (($minor_document_current_version > $minor_document_last_version)); then
64+
minor_version_change="true"
65+
commit_message="${commit_message}AsyncAPI document have changed to a new minor version."
66+
elif (($patch_document_current_version > $patch_document_last_version && $minor_document_current_version < $minor_document_last_version && $major_document_current_version < $major_document_last_version)); then
67+
patch_version_change="true"
68+
commit_message="${commit_message}AsyncAPI document have changed to a new patch version."
69+
fi
70+
71+
if $major_version_change == 'true' || $minor_version_change == 'true' || $patch_version_change == 'true'; then
72+
# Remove previous files to ensure clean slate
73+
find . -not \( -name configs.json -or -name .gitignore -or -name LICENSE -or -name custom_package.json -or -name generate.sh -or -iwholename *.github* -or -iwholename *./definitions* -or -iwholename *.git* -or -name . \) -exec rm -rf {} +
74+
75+
# Generating client from the AsyncAPI document
76+
if ! command -v ag &> /dev/null
77+
then
78+
npm install -g @asyncapi/generator
79+
fi
80+
81+
ag --force-write --output ./ ./definitions/bundled/rust.asyncapi.json @asyncapi/ts-nats-template
82+
83+
# Write new config file to ensure we keep the new state for next time
84+
contents="$(jq ".template_last_version = \"$template_current_version\" | .document_last_version = \"$document_current_version\"" configs.json)" && echo "${contents}" > configs.json
85+
# Write old version to package.json file as it was cleared by the generator
86+
contents="$(jq ".version = \"$library_last_version\"" package.json)" && echo "${contents}" > package.json
87+
# Merge custom package file with template generated
88+
jq -s '.[0] * .[1]' ./package.json ./custom_package.json > ./package_tmp.json
89+
rm ./package.json
90+
mv ./package_tmp.json ./package.json
91+
rm -rf ./definitions
92+
npm i
93+
fi
94+
mkdir -p ./.github/variables
95+
echo "
96+
major_version_change="$major_version_change"
97+
minor_version_change="$minor_version_change"
98+
patch_version_change="$patch_version_change"
99+
" > ./.github/variables/generator.env
100+
rm -rf ./definitions

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "0.0.0"
3+
}

0 commit comments

Comments
 (0)