Skip to content

Commit

Permalink
Add plugin dir
Browse files Browse the repository at this point in the history
  • Loading branch information
tenntenn committed Jun 7, 2020
1 parent 513a933 commit 7123a36
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pkgname
├── cmd
│   └── pkgname
│   └── main.go
├── plugin
│   └── pkgname
│   └── main.go
├── pkgname.go
├── pkgname_test.go
└── testdata
Expand All @@ -38,6 +41,12 @@ pkgname
```
$ skeleton -path="github.com/gostaticanalysis/pkgname"
pkgname
├── cmd
│   └── pkgname
│   └── main.go
├── plugin
│   └── pkgname
│   └── main.go
├── pkgname.go
├── pkgname_test.go
└── testdata
Expand All @@ -51,10 +60,39 @@ pkgname
```
$ skeleton -cmd=false pkgname
pkgname
├── plugin
│   └── pkgname
│   └── main.go
├── pkgname.go
├── pkgname_test.go
└── testdata
└── src
└── a
└── a.go
```

### Create skeleton codes without plugin directory

```
$ skeleton -cmd=false pkgname
pkgname
├── cmd
│   └── pkgname
│   └── main.go
├── pkgname.go
├── pkgname_test.go
└── testdata
└── src
└── a
└── a.go
```

## Build as a plugin for golangci-lint

`skeleton` generates plugin directory which has main.go.
The main.go can be built as a plugin for [golangci-lint](https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint).

```
$ skeleton pkgname
$ go build -buildmode=plugin -o path_to_plugin_dir importpath
```
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func main() {
var s Skeleton
flag.BoolVar(&s.Cmd, "cmd", true, "create cmd directory")
flag.BoolVar(&s.Plugin, "plugin", true, "create plugin directory")
flag.StringVar(&s.ImportPath, "path", "", "import path")
flag.Parse()
s.ExeName = os.Args[0]
Expand All @@ -35,6 +36,7 @@ type Skeleton struct {
ExeName string
Args []string
Cmd bool
Plugin bool
ImportPath string
}

Expand Down Expand Up @@ -107,6 +109,12 @@ func (s *Skeleton) Run() error {
}
}

if s.Plugin {
if err := s.createPlugin(dir, &info); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -152,3 +160,22 @@ func (s *Skeleton) createCmd(dir string, info *PkgInfo) error {

return nil
}

func (s *Skeleton) createPlugin(dir string, info *PkgInfo) error {
pluginDir := filepath.Join(dir, "plugin", info.Pkg)
if err := os.MkdirAll(pluginDir, 0777); err != nil {
return err
}

pluginMain, err := os.Create(filepath.Join(pluginDir, "main.go"))
if err != nil {
return err
}
defer pluginMain.Close()

if err := pluginMainTempl.Execute(pluginMain, info); err != nil {
return err
}

return nil
}
23 changes: 23 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,26 @@ import (
func main() { unitchecker.Main({{.Pkg}}.Analyzer) }
`))

var pluginMainTempl = template.Must(template.New("main.go").Parse(`// This file can build as a plugin for golangci-lint by below command.
// go build -buildmode=plugin -o path_to_plugin_dir {{.ImportPath}}/plugin/{{.Pkg}}
// See: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint
package main
import (
"{{.ImportPath}}"
"golang.org/x/tools/go/analysis"
)
// AnalyzerPlugin provides analyzers as a plugin.
// It follows golangci-lint style plugin.
var AnalyzerPlugin analyzerPlugin
type analyzerPlugin struct{}
func (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
return []*analysis.Analyzer{
{{.Pkg}}.Analyzer,
}
}
`))

0 comments on commit 7123a36

Please sign in to comment.