Skip to content

Commit

Permalink
Merge pull request #24 from link-duan/main
Browse files Browse the repository at this point in the history
refactor: rename project name to eapi
  • Loading branch information
link-duan authored Dec 2, 2022
2 parents 10a8152 + 377862d commit 1367584
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: docker/metadata-action@v3
with:
images: |
gotomicro/egogen
gotomicro/eapi
tags: |
type=schedule
type=ref,event=branch
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ logs

bin/
bak
ego-gen-api
tmpls/dist
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ WORKDIR /data
COPY go.mod go.sum ./
RUN go mod download -x
COPY . .
RUN go build -o ./bin/egogen cmd/egogen/egogen.go
RUN go build -o ./bin/eapi cmd/eapi/eapi.go

# Fianl running stage
FROM golang:1.18.3-alpine3.16
LABEL maintainer="[email protected]"

WORKDIR /data

COPY --from=go-builder /data/bin/egogen /bin/
COPY --from=go-builder /data/bin/eapi /bin/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

CMD ["sh", "-c", "/bin/egogen"]
CMD ["sh", "-c", "/bin/eapi"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build

build:
@go build -o ./bin/egogen cmd/egogen/egogen.go
@go build -o ./bin/eapi cmd/eapi/eapi.go
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# ego-gen [WIP]
# eapi

`ego-gen-api` 通过分析 AST 解析出代码中的路由(方法/路径)和 Handler 函数(请求响应参数/响应状态码等)获取接口的信息,进而生成 Swagger 文档
EAPI 是一个通过分析 AST 生成 **接口文档****前端接口请求代码** 的命令行工具。作为 swaggo 等工具的替代品,EAPI 无需编写复杂的注解即可使用

由于本工具只分析 AST 中的关键部分(类似于静态分析)而不执行代码,因此并不能覆盖到任一种使用场景。如果你要在项目中使用本工具,必须要按照一定的约定编写代码,否则将不能生成完整的文档。虽然这给你的代码编写带来了一定的约束,但从另一个角度看,这也使得代码风格更加统一
eapi 通过分析 AST 解析出代码中的路由(方法/路径)和 Handler 函数(请求响应参数/响应状态码等)获取接口的信息,进而生成 OpenAPI(Swagger) 文档。eapi 目前支持了 gin 框架的文档生成,echo 等其他主流框架在计划中。如果你需要将 eapi 应用在其他未被支持的框架,可以通过编写自定义插件的方式进行实现,或者给我们提交 PR

本工具目前支持了 `gin` 框架的文档生成。如果你需要将本项目进行扩展或应用于未支持的框架,可以通过编写自定义插件的方式进行实现
由于本工具只分析 AST 中的关键部分(类似于静态分析)而不执行代码,因此如果你要在项目中使用本工具,必须要按照一定的约定编写代码,否则将不能生成完整的文档。虽然这给你的代码编写带来了一定的约束,但从另一个角度看,这也使得代码风格更加统一

## 安装 Install

> goproxy 可能会存在缓存,可以优先尝试指定版本号进行安装。
>
```shell
go install github.com/gotomicro/ego-gen-api/cmd/egogen@latest
go install github.com/gotomicro/eapi/cmd/eapi@latest
```

## 如何使用 How to Use
## 如何使用
### 通过配置文件
**egogen.yaml**:
**eapi.yaml**:
```yaml
output: docs
plugin: gin
Expand All @@ -26,14 +26,14 @@ dir: 'api' # 需要解析的代码根目录
在代码根目录下执行:
```shell
egogen --config egogen.yaml
eapi --config eapi.yaml
```

### 通过命令行参数

在代码根目录下执行:
```shell
egogen --output docs --plugin gin --dir api
eapi --output docs --plugin gin --dir api
```

执行以上命令后,会在 `/docs` 目录下生成 `swagger.json` 文件。
Expand Down Expand Up @@ -114,3 +114,6 @@ generators:
- name: ts
output: ./src/types # 输出文件的目录
```

# TODO
- [ ] support for `echo` framework
4 changes: 2 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

import (
"fmt"
Expand All @@ -9,7 +9,7 @@ import (
"path/filepath"
"strings"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/eapi/spec"
"github.com/knadh/koanf"
"github.com/samber/lo"
"golang.org/x/mod/modfile"
Expand Down
4 changes: 2 additions & 2 deletions cmd/egogen/egogen.go → cmd/eapi/eapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"os"

analyzer "github.com/gotomicro/ego-gen-api"
"github.com/gotomicro/ego-gen-api/plugins/gin"
analyzer "github.com/gotomicro/eapi"
"github.com/gotomicro/eapi/plugins/gin"
)

func main() {
Expand Down
6 changes: 3 additions & 3 deletions comment.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package analyzer
package eapi

import (
"go/ast"
"strings"

"github.com/gotomicro/ego-gen-api/annotation"
"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/eapi/annotation"
"github.com/gotomicro/eapi/spec"
"github.com/samber/lo"
)

Expand Down
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

import (
"fmt"
Expand All @@ -7,7 +7,7 @@ import (
"go/types"
"strconv"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/eapi/spec"
"golang.org/x/tools/go/packages"
)

Expand Down
4 changes: 2 additions & 2 deletions definition.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package analyzer
package eapi

import (
"fmt"
"go/ast"
"strings"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/eapi/spec"
"golang.org/x/tools/go/packages"
)

Expand Down
14 changes: 7 additions & 7 deletions entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

import (
"encoding/json"
Expand All @@ -8,8 +8,8 @@ import (
"os"
"path/filepath"

_ "github.com/gotomicro/ego-gen-api/generators/ts"
_ "github.com/gotomicro/ego-gen-api/generators/umi"
_ "github.com/gotomicro/eapi/generators/ts"
_ "github.com/gotomicro/eapi/generators/umi"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
Expand Down Expand Up @@ -47,14 +47,14 @@ func NewEntrypoint(plugins ...Plugin) *Entrypoint {
}

const usageText = `Generate Doc:
egogen --config config.yaml
eapi --config config.yaml
or
egogen --plugin gin --dir src/ --output docs/
eapi --plugin gin --dir src/ --output docs/
Generate Frontend Code:
egogen --config config.yaml gencode
eapi --config config.yaml gencode
or
egogen --plugin gin --dir src/ --output docs/ gencode`
eapi --plugin gin --dir src/ --output docs/ gencode`

func (e *Entrypoint) Run(args []string) {
app := cli.NewApp()
Expand Down
2 changes: 1 addition & 1 deletion environment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

type Environment struct {
parent *Environment
Expand Down
6 changes: 3 additions & 3 deletions generator_executor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package analyzer
package eapi

import (
"fmt"
"os"
"path/filepath"

"github.com/gotomicro/ego-gen-api/generators"
"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/eapi/generators"
"github.com/gotomicro/eapi/spec"
)

type generatorExecutor struct {
Expand Down
2 changes: 1 addition & 1 deletion generators/generators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package generators

import "github.com/gotomicro/ego-gen-api/spec"
import "github.com/gotomicro/eapi/spec"

type Generator struct {
Type string
Expand Down
6 changes: 3 additions & 3 deletions generators/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package ts
import (
_ "embed"

f "github.com/gotomicro/ego-gen-api/formatter"
"github.com/gotomicro/ego-gen-api/generators"
"github.com/gotomicro/ego-gen-api/spec"
f "github.com/gotomicro/eapi/formatter"
"github.com/gotomicro/eapi/generators"
"github.com/gotomicro/eapi/spec"
"github.com/samber/lo"
)

Expand Down
8 changes: 4 additions & 4 deletions generators/umi/umi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"sort"
"strings"

f "github.com/gotomicro/ego-gen-api/formatter"
"github.com/gotomicro/ego-gen-api/generators"
"github.com/gotomicro/ego-gen-api/generators/ts"
"github.com/gotomicro/ego-gen-api/spec"
f "github.com/gotomicro/eapi/formatter"
"github.com/gotomicro/eapi/generators"
"github.com/gotomicro/eapi/generators/ts"
"github.com/gotomicro/eapi/spec"
"github.com/iancoleman/strcase"
"github.com/samber/lo"
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gotomicro/ego-gen-api
module github.com/gotomicro/eapi

go 1.18

Expand Down
2 changes: 1 addition & 1 deletion mod.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

import (
"golang.org/x/mod/modfile"
Expand Down
6 changes: 3 additions & 3 deletions param_parser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package analyzer
package eapi

import (
"go/ast"
"go/types"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/ego-gen-api/tag"
"github.com/gotomicro/eapi/spec"
"github.com/gotomicro/eapi/tag"
)

type ParamParser struct {
Expand Down
2 changes: 1 addition & 1 deletion plugin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

import (
"go/ast"
Expand Down
2 changes: 1 addition & 1 deletion plugins/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
"strings"

analyzer "github.com/gotomicro/ego-gen-api"
analyzer "github.com/gotomicro/eapi"
"github.com/knadh/koanf"
)

Expand Down
2 changes: 1 addition & 1 deletion plugins/gin/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"runtime"
"testing"

analyzer "github.com/gotomicro/ego-gen-api"
analyzer "github.com/gotomicro/eapi"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
Expand Down
4 changes: 2 additions & 2 deletions plugins/gin/handler_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http"
"strings"

analyzer "github.com/gotomicro/ego-gen-api"
"github.com/gotomicro/ego-gen-api/spec"
analyzer "github.com/gotomicro/eapi"
"github.com/gotomicro/eapi/spec"
"github.com/iancoleman/strcase"
"github.com/robertkrimen/otto"
"github.com/samber/lo"
Expand Down
4 changes: 2 additions & 2 deletions plugins/gin/testdata/server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@
}
},
"required": [
"msg",
"data",
"code",
"msg"
"code"
],
"type": "object"
}
Expand Down
4 changes: 2 additions & 2 deletions route.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package analyzer
package eapi

import (
"go/ast"
"net/http"
"strings"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/eapi/spec"
)

type RouteGroup struct {
Expand Down
6 changes: 3 additions & 3 deletions schema.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package analyzer
package eapi

import (
"fmt"
"go/ast"
"go/types"
"strings"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/gotomicro/ego-gen-api/tag"
"github.com/gotomicro/eapi/spec"
"github.com/gotomicro/eapi/tag"
"github.com/samber/lo"
)

Expand Down
2 changes: 1 addition & 1 deletion stack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

type Stack[T any] []T

Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package analyzer
package eapi

import (
"io/ioutil"
Expand Down

0 comments on commit 1367584

Please sign in to comment.