...

Source file src/github.com/bazelbuild/buildtools/buildozer/main.go

Documentation: github.com/bazelbuild/buildtools/buildozer

     1  /*
     2  Copyright 2016 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Entry-point for Buildozer binary.
    18  
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	"os"
    25  	"strings"
    26  
    27  	"github.com/bazelbuild/buildtools/build"
    28  	"github.com/bazelbuild/buildtools/edit"
    29  	"github.com/bazelbuild/buildtools/tables"
    30  )
    31  
    32  type flagArray []string
    33  
    34  func (i *flagArray) String() string { return strings.Join(*i, ",") }
    35  func (i *flagArray) Set(value string) error {
    36  	*i = append(*i, value)
    37  	return nil
    38  }
    39  
    40  var (
    41  	buildVersion     = "redacted"
    42  	buildScmRevision = "redacted"
    43  
    44  	commandsFiles flagArray
    45  
    46  	version           = flag.Bool("version", false, "Print the version of buildozer")
    47  	stdout            = flag.Bool("stdout", false, "write changed BUILD file to stdout")
    48  	buildifier        = flag.String("buildifier", "", "format output using a specific buildifier binary. If empty, use built-in formatter")
    49  	parallelism       = flag.Int("P", 0, "number of cores to use for concurrent actions")
    50  	numio             = flag.Int("numio", 200, "number of concurrent actions")
    51  	keepGoing         = flag.Bool("k", false, "apply all commands, even if there are failures")
    52  	filterRuleTypes   = stringList("types", "comma-separated list of rule types to change, the default empty list means all rules")
    53  	preferEOLComments = flag.Bool("eol-comments", true, "when adding a new comment, put it on the same line if possible")
    54  	rootDir           = flag.String("root_dir", "", "If present, use this folder rather than $PWD to find the root directory.")
    55  	quiet             = flag.Bool("quiet", false, "suppress informational messages")
    56  	editVariables     = flag.Bool("edit-variables", false, "For attributes that simply assign a variable (e.g. hdrs = LIB_HDRS), edit the build variable instead of appending to the attribute.")
    57  	isPrintingProto   = flag.Bool("output_proto", false, "output serialized devtools.buildozer.Output protos instead of human-readable strings.")
    58  	isPrintingJSON    = flag.Bool("output_json", false, "output serialized devtools.buildozer.Output json instead of human-readable strings.")
    59  	tablesPath        = flag.String("tables", "", "path to JSON file with custom table definitions which will replace the built-in tables")
    60  	addTablesPath     = flag.String("add_tables", "", "path to JSON file with custom table definitions which will be merged with the built-in tables")
    61  
    62  	shortenLabelsFlag  = flag.Bool("shorten_labels", true, "convert added labels to short form, e.g. //foo:bar => :bar")
    63  	deleteWithComments = flag.Bool("delete_with_comments", true, "If a list attribute should be deleted even if there is a comment attached to it")
    64  )
    65  
    66  func stringList(name, help string) func() []string {
    67  	f := flag.String(name, "", help)
    68  	return func() []string {
    69  		if *f == "" {
    70  			return nil
    71  		}
    72  		res := strings.Split(*f, ",")
    73  		for i := range res {
    74  			res[i] = strings.TrimSpace(res[i])
    75  		}
    76  		return res
    77  	}
    78  }
    79  
    80  func main() {
    81  	flag.Var(&commandsFiles, "f", "file name(s) to read commands from, use '-' for stdin (format:|-separated command line arguments to buildozer, excluding flags)")
    82  	flag.Parse()
    83  
    84  	if *version {
    85  		fmt.Printf("buildozer version: %s \n", buildVersion)
    86  		fmt.Printf("buildozer scm revision: %s \n", buildScmRevision)
    87  		os.Exit(0)
    88  	}
    89  
    90  	if *tablesPath != "" {
    91  		if err := tables.ParseAndUpdateJSONDefinitions(*tablesPath, false); err != nil {
    92  			fmt.Fprintf(os.Stderr, "buildifier: failed to parse %s for -tables: %s\n", *tablesPath, err)
    93  			os.Exit(2)
    94  		}
    95  	}
    96  
    97  	if *addTablesPath != "" {
    98  		if err := tables.ParseAndUpdateJSONDefinitions(*addTablesPath, true); err != nil {
    99  			fmt.Fprintf(os.Stderr, "buildifier: failed to parse %s for -add_tables: %s\n", *addTablesPath, err)
   100  			os.Exit(2)
   101  		}
   102  	}
   103  
   104  	if !(*shortenLabelsFlag) {
   105  		build.DisableRewrites = []string{"label"}
   106  	}
   107  	edit.ShortenLabelsFlag = *shortenLabelsFlag
   108  	edit.DeleteWithComments = *deleteWithComments
   109  	opts := &edit.Options{
   110  		Stdout:            *stdout,
   111  		Buildifier:        *buildifier,
   112  		Parallelism:       *parallelism,
   113  		NumIO:             *numio,
   114  		CommandsFiles:     commandsFiles,
   115  		KeepGoing:         *keepGoing,
   116  		FilterRuleTypes:   filterRuleTypes(),
   117  		PreferEOLComments: *preferEOLComments,
   118  		RootDir:           *rootDir,
   119  		Quiet:             *quiet,
   120  		EditVariables:     *editVariables,
   121  		IsPrintingProto:   *isPrintingProto,
   122  		IsPrintingJSON:    *isPrintingJSON,
   123  	}
   124  	os.Exit(edit.Buildozer(opts, flag.Args()))
   125  }
   126  

View as plain text