Skip to content

Commit

Permalink
refactor: rename make-constructor to newc
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Aug 16, 2022
1 parent a4b63e7 commit 0913328
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 44 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# make-constructor
# newc

Doc: **English** | [中文](README_zh.md)

Expand All @@ -9,21 +9,21 @@ A cli tool to generate constructor code for a Golang struct.
## Installation

```bash
go install github.com/Bin-Huang/make-constructor@latest
go install github.com/Bin-Huang/newc@latest
```

## Usage

Add a `go:generate` command line to the struct which you want to generate a constructor.

```go
//go:generate make-constructor
//go:generate newc
```

For example:

```go
//go:generate make-constructor
//go:generate newc
type UserService struct {
baseService
userRepository *repositories.UserRepository
Expand All @@ -46,7 +46,7 @@ func NewUserService(baseService baseService, userRepository *repositories.UserRe
}
```

See [more examples here](https://github.com/Bin-Huang/make-constructor/tree/master/test)
See [more examples here](https://github.com/Bin-Huang/newc/tree/master/test)

## Usage without manual installation

Expand All @@ -55,13 +55,13 @@ See [more examples here](https://github.com/Bin-Huang/make-constructor/tree/mast
Without manual installation, just add this comment line to the struct. Go will automatically install this tool if missing.

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.4
//go:generate go run github.com/Bin-Huang/newc@v0.8.0
```

For example:

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.4
//go:generate go run github.com/Bin-Huang/newc@v0.8.0
type UserService struct {
baseService
userRepository *repositories.UserRepository
Expand All @@ -77,7 +77,7 @@ This is very useful, especially in teamwork. **It can run without manual install
2. Write an `init` method for the struct

```go
//go:generate make-constructor --init
//go:generate newc --init
type Controller struct {
logger *zap.Logger
debug bool
Expand Down Expand Up @@ -129,7 +129,7 @@ Don't worry about the imports, variable naming, and code style in the generated
It doesn't break the work of other people who don't have installed this tool in collaboration. Go will automatically install this tool if missing.

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.4
//go:generate go run github.com/Bin-Huang/newc@v0.8.0
```

## Sponsoring
Expand Down
20 changes: 10 additions & 10 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# make-constructor
# newc

Doc: [English](README.md) | **中文**

Expand All @@ -9,21 +9,21 @@ Doc: [English](README.md) | **中文**
## 安装

```bash
go install github.com/Bin-Huang/make-constructor@latest
go install github.com/Bin-Huang/newc@latest
```

## 使用方法

在需要生成构造器的结构体上添加一行 `go:generate` 注释。

```go
//go:generate make-constructor
//go:generate newc
```

比如这样:

```go
//go:generate make-constructor
//go:generate newc
type UserService struct {
baseService
userRepository *repositories.UserRepository
Expand All @@ -46,7 +46,7 @@ func NewUserService(baseService baseService, userRepository *repositories.UserRe
}
```

这里可以[查看更多例子](https://github.com/Bin-Huang/make-constructor/tree/master/test)
这里可以[查看更多例子](https://github.com/Bin-Huang/newc/tree/master/test)

## 使用方式2(无需手动安装)

Expand All @@ -55,13 +55,13 @@ func NewUserService(baseService baseService, userRepository *repositories.UserRe
无需手动安装,只需要给结构体添加下面这行注释就行。Go 会在缺失时自动下载这个工具。

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.4
//go:generate go run github.com/Bin-Huang/newc@v0.8.0
```

比如这样:

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.4
//go:generate go run github.com/Bin-Huang/newc@v0.8.0
type UserService struct {
baseService
userRepository *repositories.UserRepository
Expand All @@ -77,7 +77,7 @@ type UserService struct {
2. 为结构体实现一个 `init` 方法

```go
//go:generate make-constructor --init
//go:generate newc --init
type Controller struct {
logger *zap.Logger
debug bool
Expand Down Expand Up @@ -118,7 +118,7 @@ func NewController(logger *zap.Logger, debug bool) *Controller {

不管是编写还是更新构造器代码,都是一个费力且容易出错的事情,尤其当代码量很大的时候。这些繁琐易错的工作应该交给自动程序来完成,比如这个工具。

同时,这个工具还能完美兼容像[**wire**](https://github.com/google/wire)这种依赖注入工具。如果你的项目中也使用了 **wire**,那你可能非常需要这个工具。**wire****make-constructor** 的“加持”下会变得更加好用。
同时,这个工具还能完美兼容像[**wire**](https://github.com/google/wire)这种依赖注入工具。如果你的项目中也使用了 **wire**,那你可能非常需要这个工具。**wire****newc** 的“加持”下会变得更加好用。

**2. 你不需要担心自动生成的代码**.

Expand All @@ -129,7 +129,7 @@ func NewController(logger *zap.Logger, debug bool) *Controller {
就算其他同事没有安装这个工具,这么做也不会影响到他们的工作。因为 Go 会在必要时自动安装这个工具。

```go
//go:generate go run github.com/Bin-Huang/make-constructor@v0.7.4
//go:generate go run github.com/Bin-Huang/newc@v0.8.0
```

## 赞赏
Expand Down
2 changes: 1 addition & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"golang.org/x/tools/imports"
)

var templ = `// Code generated by github.com/Bin-Huang/make-constructor; DO NOT EDIT.
var templ = `// Code generated by github.com/Bin-Huang/newc; DO NOT EDIT.
package {{.PkgName}}
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/Bin-Huang/make-constructor
module github.com/Bin-Huang/newc

go 1.18

Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
func main() {
err := makeConstructor()
if err != nil {
fmt.Printf("make-constructor: [ERROR] %v\n", err)
fmt.Printf("newc: [ERROR] %v\n", err)
fmt.Printf("It seems like there is some trouble here. Try this:\n")
fmt.Printf("\t1. Check and upgrade this tool (https://github.com/Bin-Huang/make-constructor)\n")
fmt.Printf("\t2. Submit an issue on Github (https://github.com/Bin-Huang/make-constructor/issues)\n")
fmt.Printf("\t1. Check and upgrade this tool (https://github.com/Bin-Huang/newc)\n")
fmt.Printf("\t2. Submit an issue on Github (https://github.com/Bin-Huang/newc/issues)\n")
os.Exit(1)
}
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func makeConstructor() error {
if err != nil {
return err
}
fmt.Printf("make-constructor: [INFO] wrote %v\n", genFilename)
fmt.Printf("newc: [INFO] wrote %v\n", genFilename)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func GetPackageInfo(dir string) (*packages.Package, error) {
return pkgs[0], nil
}

// IncludeMakeMark check whether a code file contains "make-constructor" comment
// IncludeMakeMark check whether a code file contains "newc" comment
func IncludeMakeMark(filepath string) (bool, error) {
file, err := os.Open(filepath)
if err != nil {
Expand Down Expand Up @@ -168,7 +168,7 @@ func ParseCodeFile(filename string) ([]StructInfo, []ImportInfo, error) {
// isMakeComment ...
func isMakeComment(s string) bool {
s = strings.TrimSpace(s)
return strings.HasPrefix(s, "//go:generate") && strings.Contains(s, "make-constructor")
return strings.HasPrefix(s, "//go:generate") && strings.Contains(s, "newc")
}

// isInitModeEnable check if this struct enable the init mode
Expand Down
2 changes: 1 addition & 1 deletion test/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/Bin-Huang/make-constructor/test/services"
import "github.com/Bin-Huang/newc/test/services"

func main() {
services.NewBaseService(100)
Expand Down
6 changes: 3 additions & 3 deletions test/repositories/constructor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/repositories/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repositories
import "time"

// database the internal database client for example
//go:generate go run ../../../make-constructor
//go:generate go run ../../../newc
type database struct {
DSN string
Timeout time.Duration
Expand Down
6 changes: 3 additions & 3 deletions test/repositories/pro-repository.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package repositories

import (
"github.com/Bin-Huang/make-constructor/test/config"
"github.com/Bin-Huang/make-constructor/test/pkg/errors"
"github.com/Bin-Huang/newc/test/config"
"github.com/Bin-Huang/newc/test/pkg/errors"
)

// ProRepository a repository for example
//go:generate go run ../../../make-constructor
//go:generate go run ../../../newc
type ProRepository struct {
conf config.Config
db *database
Expand Down
4 changes: 2 additions & 2 deletions test/repositories/user-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package repositories
import (
"errors"

config2 "github.com/Bin-Huang/make-constructor/test/config2"
config2 "github.com/Bin-Huang/newc/test/config2"
)

// UserRepository the user repository for example
//go:generate go run ../../../make-constructor --init
//go:generate go run ../../../newc --init
type UserRepository struct {
conf *config2.Config
db *database
Expand Down
2 changes: 1 addition & 1 deletion test/services/base-service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package services

// baseService the base service for example
//go:generate go run ../../../make-constructor
//go:generate go run ../../../newc
type baseService struct {
debugLevel int
}
4 changes: 2 additions & 2 deletions test/services/constructor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/services/email-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package services
import (
"log"

"github.com/Bin-Huang/make-constructor/test/repositories"
"github.com/Bin-Huang/newc/test/repositories"
)

// EmailService email service for example
//go:generate go run ../../../make-constructor
//go:generate go run ../../../newc
type EmailService struct {
baseService
userRepository *repositories.UserRepository
Expand Down
4 changes: 2 additions & 2 deletions test/services/user-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package services
import (
"log"

"github.com/Bin-Huang/make-constructor/test/repositories"
"github.com/Bin-Huang/newc/test/repositories"
)

// UserService a user service for example
//go:generate go run ../../../make-constructor
//go:generate go run ../../../newc
type UserService struct {
baseService

Expand Down

0 comments on commit 0913328

Please sign in to comment.