Skip to content

Commit

Permalink
feat(copier): add the -dir flag (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored Sep 29, 2023
1 parent 5094bb4 commit 183c0bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![goreportcard](https://goreportcard.com/badge/go-simpler.org/assert)](https://goreportcard.com/report/go-simpler.org/assert)
[![codecov](https://codecov.io/gh/go-simpler/assert/branch/main/graph/badge.svg)](https://codecov.io/gh/go-simpler/assert)

Common assertions to use with the standard testing package
Common assertions to use with the standard `testing` package

## 📌 About

Expand All @@ -30,7 +30,7 @@ There is also a special tool to do this automatically:
just add the following directive to any `.go` file and run `go generate ./...`:

```go
//go:generate go run -tags=copier go-simpler.org/assert/cmd/copier@latest .
//go:generate go run -tags=copier go-simpler.org/assert/cmd/copier@latest
```

See the `cmd/copier` documentation for details.
Expand Down
53 changes: 22 additions & 31 deletions cmd/copier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
// Copier is a convenience tool that copies the assertions into a project,
// so that they can be used without introducing a direct dependency.
//
// Add the following directive to any .go file and run `go generate ./...`:
//
// go:generate go run -tags=copier go-simpler.org/assert/cmd/copier@latest <path/to/pkg>
//
// Where <path/to/pkg> is the location where you want to put the generated package,
// e.g. `.` for the project root (hint: for libraries you may want to put it in `internal`).
// Usage: //go:generate go run -tags=copier go-simpler.org/assert/cmd/copier@<version> [-dir=<working-directory>]
package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"os/exec"
Expand All @@ -36,36 +32,37 @@ func main() {
}

func run(ctx context.Context) error {
if len(os.Args) < 2 {
return errors.New("path to package is not specified")
var workDir string

fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
fs.StringVar(&workDir, "dir", ".", "the working directory")
if err := fs.Parse(os.Args[1:]); err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

moduleName, err := readModuleName(ctx)
if err != nil {
return err
}

path := filepath.Join(os.Args[1], "assert")
fullpath := filepath.Join(path, "EF")

if err := os.MkdirAll(fullpath, 0755); err != nil && !errors.Is(err, os.ErrExist) {
return err
}
if err := os.Chdir(path); err != nil {
return err
importPath := filepath.Join(moduleName, "assert")
if workDir != "." {
importPath = filepath.Join(moduleName, workDir, "assert")
}
assert.AliasFile = strings.Replace(assert.AliasFile, "go-simpler.org/assert", importPath, 1)

importPath := moduleName
if path != "." {
importPath = filepath.Join(moduleName, path)
dirPath := filepath.Join(workDir, "assert", "EF")
if err := os.MkdirAll(dirPath, 0755); err != nil && !errors.Is(err, os.ErrExist) {
return err
}

aliasFile := strings.Replace(assert.AliasFile, "go-simpler.org/assert", importPath, 1)

if err := writeFile("assert.go", assert.MainFile); err != nil {
mainPath := filepath.Join(workDir, "assert", "assert.go")
if err := writeFile(mainPath, assert.MainFile); err != nil {
return err
}
if err := writeFile("EF/alias.go", aliasFile); err != nil {

aliasPath := filepath.Join(workDir, "assert", "EF", "alias.go")
if err := writeFile(aliasPath, assert.AliasFile); err != nil {
return err
}

Expand Down Expand Up @@ -96,19 +93,13 @@ func readModuleName(ctx context.Context) (string, error) {
return info.Module.Path, nil
}

func writeFile(name, content string) error {
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()

func writeFile(path, content string) error {
const header = `// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Code generated by go-simpler.org/assert/cmd/copier. DO NOT EDIT.
`
return os.WriteFile(name, []byte(header+content), 0644)
return os.WriteFile(path, []byte(header+content), 0644)
}
2 changes: 1 addition & 1 deletion embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package assert

import _ "embed"

// These files are used by the `cmd/copier` tool. Ignore them.
// These files are used by the cmd/copier tool. Ignore them.
var (
//go:embed assert.go
MainFile string
Expand Down

0 comments on commit 183c0bf

Please sign in to comment.