Skip to content

Commit

Permalink
build(mage): add action to generate readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jun 13, 2024
1 parent 3ac5808 commit 3279e9a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 16 deletions.
6 changes: 2 additions & 4 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

package main

const SCHEMA_DIR = "schema"

// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build




57 changes: 57 additions & 0 deletions magefiles/readme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//go:build mage
// +build mage

package main

import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
)

type ReadmeParams struct {
Docs string
Name string
ContractName string
SchemaName string
Ref string
}

func generateReadme(contract string) error {
fmt.Printf(" 📄 Generating %s readme\n", contract)

readme, err := os.ReadFile(filepath.Join(CONTRACTS_TMP_DIR, "docs", fmt.Sprintf("%s.md", contract)))
name := strings.TrimPrefix(contract, "axone-")
schemaName := fmt.Sprintf("%s-schema", name)
if err != nil {
return err
}

params := ReadmeParams{
Docs: string(readme),
Name: name,
ContractName: contract,
SchemaName: schemaName,
Ref: tag(),
}

return ts(params)
}

func ts(params ReadmeParams) error {
fmt.Println(" ➡️ Typescript readme")
tmpl, err := template.ParseFiles(filepath.Join("ts", "README.md.template"))
if err != nil {
return err
}

readmeFile, err := os.Create(filepath.Join("ts", params.SchemaName, "README.md"))
if err != nil {
return err
}
defer readmeFile.Close()

return tmpl.Execute(readmeFile, params)
}
33 changes: 31 additions & 2 deletions magefiles/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
const (
CONTRACTS_REPOSITORY = "https://github.com/axone-protocol/contracts.git"
CONTRACTS_TMP_DIR = "tmp"
SCHEMA_DIR = "schema"
)

type Schema mg.Namespace
Expand All @@ -40,7 +39,7 @@ func (Schema) Download(ref string) error {
return nil
}

// Generate build and generate contracts json schema
// Generate build and generate contracts json schema and readme
func (s Schema) Generate(ref string) error {
mg.Deps(mg.F(Schema.Download, ref))

Expand Down Expand Up @@ -76,6 +75,36 @@ func (s Schema) Generate(ref string) error {
return nil
}

// Readme generate contracts readme on all target
func (s Schema) Readme(ref string) error {
mg.Deps(mg.F(Schema.Download, ref))

fmt.Println("📄 Generating contracts readme")

contractsDir, err := os.ReadDir(SCHEMA_DIR)
if err != nil {
return err
}

for _, contract := range contractsDir {
if !contract.IsDir() {
continue
}

err := generateReadme(contract.Name())
if err != nil {
return err
}
}
return nil
}

// tag returns the git tag for the current branch or "" if none.
func tag() string {
s, _ := sh.Output("git", "describe", "--tags")
return s
}

// Clean remove temporary files
func (Schema) Clean() error {
fmt.Println("🧹 Cleaning schema temporary files")
Expand Down
20 changes: 10 additions & 10 deletions ts/README.md.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AXONE {{ (ds "data").name }} schema
# AXONE {{ .Name }} schema

> Generated typescript types for [{{ (ds "data").contract_name }} contract](https://github.com/axone-protocol/contracts/tree/{{ (ds "data").ref }}/contracts/{{ (ds "data").contract_name }}).
> Generated typescript types for [{{ .ContractName }} contract](https://github.com/axone-protocol/contracts/tree/{{ .Ref }}/contracts/{{ .ContractName }}).

[![version](https://img.shields.io/github/v/release/axone-protocol/axone-contract-schema?style=for-the-badge&logo=github)](https://github.com/axone-protocol/axone-contract-schema/releases)
[![build](https://img.shields.io/github/actions/workflow/status/axone-protocol/axone-contract-schema/build.yml?branch=main&label=build&style=for-the-badge&logo=github)](https://github.com/axone-protocol/axone-contract-schema/actions/workflows/build.yml)
Expand All @@ -11,26 +11,26 @@
[![contributor covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg?style=for-the-badge)](https://github.com/axone-protocol/.github/blob/main/CODE_OF_CONDUCT.md)
[![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg?style=for-the-badge)](https://opensource.org/licenses/BSD-3-Clause)

## Usage
## Usage

First add or install the module to your existing project using either `yarn` or `npm`.
First add or install the module to your existing project using either `yarn` or `npm`.

```bash
yarn add @axone/{{ (ds "data").schema_name }}
yarn add @axone/{{ .SchemaName }}
```

or
or
```bash
npm install --save @axone/{{ (ds "data").schema_name }}
npm install --save @axone/{{ .SchemaName }}
```

Then import wanted type :
Then import wanted type :

```typescript
import type { InstantiateMsg } from "@axone/{{ (ds "data").schema_name }}";
import type { InstantiateMsg } from "@axone/{{ .SchemaName }}";
```

---

{{ (ds "data").docs }}
{{ .Docs }}

0 comments on commit 3279e9a

Please sign in to comment.