Skip to content

Commit

Permalink
add skip header lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kmrmt committed Feb 13, 2025
1 parent 085a838 commit 2676396
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions hack/license/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
"github.com/vdaas/vald/internal/sync"
)

var (
apache = template.Must(template.New("Apache License").Parse(`{{.Escape}}
const (
apacheTemplate = `{{.Escape}}
{{.Escape}} Copyright (C) 2019-{{.Year}} {{.Maintainer}}
{{.Escape}}
{{.Escape}} Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -49,8 +49,9 @@ var (
{{.Escape}} See the License for the specific language governing permissions and
{{.Escape}} limitations under the License.
{{.Escape}}
`))
docker = template.Must(template.New("Apache License").Parse(`{{.Escape}} syntax = docker/dockerfile:latest
`

dockerTemplate = `{{.Escape}} syntax = docker/dockerfile:latest
{{.Escape}} check=error=true
{{.Escape}}
{{.Escape}} Copyright (C) 2019-{{.Year}} {{.Maintainer}}
Expand All @@ -67,9 +68,9 @@ var (
{{.Escape}} See the License for the specific language governing permissions and
{{.Escape}} limitations under the License.
{{.Escape}}
`))
`

googleProtoApache = template.Must(template.New("Google Proto Apache License").Parse(`{{.Escape}}
googoleProtoApacheTemplate = `{{.Escape}}
{{.Escape}} Copyright (C) {{.Year}} Google LLC
{{.Escape}} Modified by {{.Maintainer}}
{{.Escape}}
Expand All @@ -85,9 +86,9 @@ var (
{{.Escape}} See the License for the specific language governing permissions and
{{.Escape}} limitations under the License.
{{.Escape}}
`))
`

goStandard = template.Must(template.New("Go License").Parse(`{{.Escape}}
goStandardTemplate = `{{.Escape}}
{{.Escape}} Copyright (c) 2009-{{.Year}} The Go Authors. All rights resered.
{{.Escape}} Modified by {{.Maintainer}}
{{.Escape}}
Expand Down Expand Up @@ -117,7 +118,18 @@ var (
{{.Escape}} (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
{{.Escape}} OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{{.Escape}}
`))
`
)

var (
apache = template.Must(template.New("Apache License").Parse(apacheTemplate))
apacheLineCount = strings.Count(apacheTemplate, "\n")
docker = template.Must(template.New("Apache License").Parse(dockerTemplate))
dockerLineCount = strings.Count(dockerTemplate, "\n")
googleProtoApache = template.Must(template.New("Google Proto Apache License").Parse(googoleProtoApacheTemplate))
googleProtoApacheLineCount = strings.Count(googoleProtoApacheTemplate, "\n")
goStandard = template.Must(template.New("Go License").Parse(goStandardTemplate))
goStandardLineCount = strings.Count(goStandardTemplate, "\n")

slushEscape = "//"
sharpEscape = "#"
Expand Down Expand Up @@ -291,6 +303,7 @@ func readAndRewrite(path string) error {
}
} else {
tmpl := apache
count := apacheLineCount
switch filepath.Ext(path) {
case ".go", ".c", ".h", ".hpp", ".cpp":
d.Escape = slushEscape
Expand All @@ -302,25 +315,30 @@ func readAndRewrite(path string) error {
"semaphore_example_test.go",
"semaphore_test.go":
tmpl = goStandard
count = goStandardLineCount
case "error_details.pb.go",
"error_details.pb.json.go",
"error_details_vtproto.pb.go":
tmpl = googleProtoApache
count = googleProtoApacheLineCount
default:
}
case ".proto":
if fi.Name() == "error_details.proto" {
tmpl = googleProtoApache
count = googleProtoApacheLineCount
}
d.Escape = slushEscape
case ".rs":
d.Escape = slushEscape
default:
if fi.Name() == "Dockerfile" {
tmpl = docker
count = dockerLineCount
}
}
lf := true
cnt := 0
bf := false
sc := bufio.NewScanner(f)
once := sync.Once{}
Expand Down Expand Up @@ -362,7 +380,8 @@ func readAndRewrite(path string) error {
}
continue
}
if lf && strings.HasPrefix(line, d.Escape) {
if lf && strings.HasPrefix(line, d.Escape) && cnt < count {
cnt++
continue
} else if !bf {
once.Do(func() {
Expand Down

0 comments on commit 2676396

Please sign in to comment.