Skip to content

Commit

Permalink
Merge pull request #38 from axone-protocol/refactor/go-codegen
Browse files Browse the repository at this point in the history
[Go] ♻️ Replace `quicktype` by `go-codegen`
  • Loading branch information
bdeneux authored Aug 7, 2024
2 parents ebb752a + bb6767e commit 0be755e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 29 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ jobs:
- name: Install Mage
run: go install github.com/magefile/[email protected]

- name: Installing quicktype
run: |
yarn global add quicktype@23
echo "$(yarn global bin)" >> $GITHUB_PATH
- name: Build
run: |
mage -v build:${{ matrix.target }} ${{ matrix.schema }}
13 changes: 7 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ jobs:
- name: Install Mage
run: go install github.com/magefile/[email protected]

- name: Installing quicktype
run: |
yarn global add quicktype@23
echo "$(yarn global bin)" >> $GITHUB_PATH
- name: Publish
run: |
mage -v publish:ts ${{ matrix.schema }} $GITHUB_REF
env:
NODE_AUTH_TOKEN: ${{ secrets[matrix.registry.auth-token-secret] }}

create_release:
runs-on: ubuntu-22.04
if: startsWith(github.ref, 'refs/tags/')
needs:
- lint
- build
steps:
- name: Create release
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
body: Schema changelog avalaible on [contract repository](https://github.com/axone-protocol/contracts/blob/${{ github.ref_name }}/CHANGELOG.md).
5 changes: 0 additions & 5 deletions .github/workflows/update-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ jobs:
- name: Install Mage
run: go install github.com/magefile/[email protected]

- name: Installing quicktype
run: |
yarn global add quicktype@23
echo "$(yarn global bin)" >> $GITHUB_PATH
- name: Build
run: |
mage -v build:${{ matrix.target }} ${{ matrix.schema }}
Expand Down
27 changes: 21 additions & 6 deletions magefiles/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,39 @@ func (Build) Ts(schema string) error {

// Go build go schema for the given contract schema.
func (Build) Go(schema string) error {
if schema == "axone-cognitarium" {
fmt.Println("🚪 Skipping axone-cognitarium schema since codegen generation is failing")
return nil
}

fmt.Printf("⚙️ Generate go types for %s\n", schema)

ensureQuicktype()
ensureGoCodegen()

_, dest := schemaDestination(schema, GO_DIR)

if err := os.MkdirAll(dest, os.ModePerm); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

err := sh.Run("bash", "-c",
fmt.Sprintf("quicktype -s schema %s -o %s --lang go --package schema",
filepath.Join(SCHEMA_DIR, schema, "*.json"),
filepath.Join(dest, "schema.go")))
err := sh.Run("go-codegen", "generate",
"messages",
filepath.Join(SCHEMA_DIR, fmt.Sprintf("%s.json", schema)),
"-o", filepath.Join(dest, "schema.go"),
"--package-name", "schema")
if err != nil {
return fmt.Errorf("failed to generate go types: %w", err)
}

fmt.Println("👨‍💻 Generate go client")
err = sh.Run("go-codegen", "generate",
"query-client",
filepath.Join(SCHEMA_DIR, fmt.Sprintf("%s.json", schema)),
"-o", filepath.Join(dest, "client.go"),
"--package-name", "schema")
if err != nil {
return fmt.Errorf("failed to generate go client: %w", err)
}

fmt.Println("🔨 Building go")
return runInPath(dest, "go", "build")
}
Expand Down
1 change: 1 addition & 0 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
GO_DIR = "go"

TS_CODEGEN_VERSION = "1.11.1"
GO_CODEGEN_VERSION = "0.2.5"
)
19 changes: 12 additions & 7 deletions magefiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,21 @@ func ensureTsCodegen() {
}
}

// EnsureQuicktype ensures that quicktype is installed, if not it panics.
func ensureQuicktype() {
if err := sh.Run("quicktype", "--help"); err != nil {
panic("quicktype is not installed")
}
}

// EnsureYarn ensures that yarn is installed, if not it panics.
func ensureYarn() {
if err := sh.Run("yarn", "--help"); err != nil {
panic("yarn is not installed")
}
}

// EnsureGoCodegen ensures that go-codegen is installed, if not it tries to install it.
func ensureGoCodegen() {
if err := sh.Run("go-codegen", "--help"); err == nil {
return
}

fmt.Printf("🔨 Installing go-codegen...\n")
if err := sh.Run("go", "install", fmt.Sprintf("github.com/srdtrk/go-codegen@v%s", GO_CODEGEN_VERSION)); err != nil {
panic(fmt.Sprintf("failed to install go-codegen: %v", err))
}
}

0 comments on commit 0be755e

Please sign in to comment.