github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/rules/private/list_repository_tools_srcs.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Copyright 2014 The Bazel Authors. All rights reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //    http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  // list_go_repository_tools prints Bazel labels for source files that
    19  // gazelle and fetch_repo depend on. go_repository_tools resolves these
    20  // labels so that when a source file changes, the gazelle and fetch_repo
    21  // binaries are rebuilt for go_repository.
    22  
    23  package main
    24  
    25  import (
    26  	"bytes"
    27  	"flag"
    28  	"fmt"
    29  	"io/ioutil"
    30  	"log"
    31  	"os"
    32  	"path/filepath"
    33  	"strings"
    34  )
    35  
    36  func main() {
    37  	dir := flag.String("dir", "", "directory to run in")
    38  	check := flag.String("check", "", ".bzl file to check (relative to rules_proto root)")
    39  	generate := flag.String("generate", "", ".bzl file to generate (relative to rules_proto root)")
    40  	flag.Parse()
    41  	if *check == "" && *generate == "" {
    42  		log.Fatal("neither -check nor -generate were set")
    43  	}
    44  	if *check != "" && *generate != "" {
    45  		log.Fatal("both -check and -generate were set")
    46  	}
    47  	if *dir != "" {
    48  		if err := os.Chdir(filepath.FromSlash(*dir)); err != nil {
    49  			log.Fatal(err)
    50  		}
    51  	}
    52  	if *check != "" {
    53  		*check = filepath.FromSlash(*check)
    54  	}
    55  	if *generate != "" {
    56  		*generate = filepath.FromSlash(*generate)
    57  	}
    58  
    59  	var labels []string
    60  	err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
    61  		if err != nil {
    62  			return err
    63  		}
    64  		base := filepath.Base(path)
    65  		if base == "third_party" || base == "testdata" {
    66  			return filepath.SkipDir
    67  		}
    68  		if !info.IsDir() &&
    69  			(strings.HasSuffix(base, ".go") && !strings.HasSuffix(base, "_test.go") ||
    70  				base == "BUILD.bazel" || base == "BUILD") {
    71  			label := filepath.ToSlash(path)
    72  			if i := strings.LastIndexByte(label, '/'); i >= 0 {
    73  				label = "@build_stack_rules_proto//" + label[:i] + ":" + label[i+1:]
    74  			} else {
    75  				label = "@build_stack_rules_proto//:" + label
    76  			}
    77  			labels = append(labels, label)
    78  		}
    79  		return nil
    80  	})
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  
    85  	buf := &bytes.Buffer{}
    86  	fmt.Fprintln(buf, "\"\"\" Code generated by list_repository_tools_srcs.go; DO NOT EDIT.\"\"\"")
    87  	fmt.Fprintln(buf, "PROTO_REPOSITORY_TOOLS_SRCS = [")
    88  	for _, label := range labels {
    89  		fmt.Fprintf(buf, "    %q,\n", label)
    90  	}
    91  	fmt.Fprintln(buf, "]")
    92  
    93  	if *generate != "" {
    94  		if err := ioutil.WriteFile(*generate, buf.Bytes(), 0666); err != nil {
    95  			log.Fatal(err)
    96  		}
    97  	} else {
    98  		got, err := ioutil.ReadFile(*check)
    99  		if err != nil {
   100  			log.Fatal(err)
   101  		}
   102  		if !bytes.Equal(got, buf.Bytes()) {
   103  			log.Fatalf("generated file %s is not up to date", *check)
   104  		}
   105  	}
   106  }