Skip to content

Commit

Permalink
ignore files marked with go:build tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Apr 17, 2024
1 parent b2aac11 commit 3cc54b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/paramgen/internal/paramgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ func parsePackage(path string) (*ast.Package, error) {
if len(pkgs) == 0 {
return nil, fmt.Errorf("no source-code package in directory %s", path)
}
// Ignore files with go:build constraint set to "tools" (common pattern in
// Conduit connectors).
for pkgName, pkg := range pkgs {
for fileName, f := range pkg.Files {
if hasBuildConstraint(f, "tools") {
delete(pkg.Files, fileName)
}
}
if len(pkg.Files) == 0 {
delete(pkgs, pkgName)
}
}
if len(pkgs) > 1 {
return nil, fmt.Errorf("multiple packages %v in directory %s", maps.Keys(pkgs), path)
}
Expand All @@ -132,6 +144,20 @@ func parsePackage(path string) (*ast.Package, error) {
panic("unreachable")
}

// hasBuildConstraint is a very naive way to check if a file has a build
// constraint. It is sufficient for our use case.
func hasBuildConstraint(f *ast.File, constraint string) bool {
text := fmt.Sprintf("//go:build %s", constraint)
for _, cg := range f.Comments {
for _, c := range cg.List {
if c.Text == text {
return true
}
}
}
return false
}

func findStruct(pkg *ast.Package, name string) (*ast.StructType, *ast.File, error) {
var structType *ast.StructType
var file *ast.File
Expand Down
19 changes: 19 additions & 0 deletions cmd/paramgen/internal/testdata/complex/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build tools

// This file should be ignored because of the build tag.

package main

0 comments on commit 3cc54b1

Please sign in to comment.