Skip to content

Commit

Permalink
Merge pull request #12 from AkihiroSuda/dev
Browse files Browse the repository at this point in the history
 golang: support Go 1.17 compiler flags (e.g., `//go:build !windows`)
  • Loading branch information
kunalkushwaha authored Aug 24, 2021
2 parents 1f92d0b + 3266b56 commit a58a4aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.13
- name: Set up Go 1.17
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.17
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi

- name: Test
run: go test ./...
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kunalkushwaha/ltag

go 1.17
31 changes: 23 additions & 8 deletions golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -16,7 +15,7 @@ type golangApplier struct {
func (g *golangApplier) CheckHeader(target *os.File, t *TagContext) (bool, error) {

//Check compiler flags.
cFlags, cbuf, err := g.checkSpecialConditions(target)
cFlags, cbufs, err := g.checkSpecialConditions(target)
if err != nil {
return false, err
}
Expand All @@ -33,7 +32,11 @@ func (g *golangApplier) CheckHeader(target *os.File, t *TagContext) (bool, error

var templateBuf string
if cFlags == CompilerFlags {
templateBuf = fmt.Sprintf("%s%s%s", cbuf, "\n\n", tbuf)
for _, cbuf := range cbufs {
templateBuf += string(cbuf) + "\n"
}
templateBuf += "\n"
templateBuf += string(tbuf)
} else {
templateBuf = string(tbuf)
}
Expand Down Expand Up @@ -92,9 +95,11 @@ func (g *golangApplier) ApplyHeader(path string, t *TagContext) error {

reader := bufio.NewReader(file)
if sFlags == CompilerFlags {
tFile.Write(flags)
tFile.Write([]byte("\n\n"))
_, _, err = reader.ReadLine()
for _, f := range flags {
tFile.Write(f)
tFile.Write([]byte("\n"))
_, _, err = reader.ReadLine()
}
_, _, err = reader.ReadLine()
}

Expand All @@ -120,13 +125,23 @@ func (g *golangApplier) ApplyHeader(path string, t *TagContext) error {
return nil
}

func (g *golangApplier) checkSpecialConditions(target *os.File) (uint8, []byte, error) {
func (g *golangApplier) checkSpecialConditions(target *os.File) (uint8, [][]byte, error) {
reader := bufio.NewReader(target)
buf, _, err := reader.ReadLine()
if err != nil {
return NormalFiles, nil, err
}

// Go 1.17 compiler flags (e.g., `//go:build !windows`)
if strings.HasPrefix(string(buf), "//go:") {
// read next line too: (`// +build !windows`)
if buf2, _, err := reader.ReadLine(); err == nil && strings.HasPrefix(string(buf2), "// +build ") {
return CompilerFlags, [][]byte{buf, buf2}, nil
}
return CompilerFlags, [][]byte{buf}, nil
}

// Old compiler flags (e.g., `// +build !windows`)
// checks for Package comments as per https://blog.golang.org/godoc-documenting-go-code
if strings.HasPrefix(string(buf), "//") &&
(strings.Contains(string(buf), "build") ||
Expand All @@ -136,7 +151,7 @@ func (g *golangApplier) checkSpecialConditions(target *os.File) (uint8, []byte,
strings.Contains(string(buf), "darwin") ||
strings.Contains(string(buf), "freebsd")) &&
!strings.Contains(string(buf), "Package") {
return CompilerFlags, buf, nil
return CompilerFlags, [][]byte{buf}, nil
}
if strings.HasPrefix(string(buf), "//") &&
(strings.Contains(string(buf), "DO NOT EDIT")) {
Expand Down

0 comments on commit a58a4aa

Please sign in to comment.