kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/config/preprocessor/preprocessor.go (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     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   *   http://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  // Binary preprocessor can modify a repo's build configuration file with the
    18  // necessary bits to run Kythe extraction.
    19  //
    20  // For detailed usage, see the README.md file.
    21  package main
    22  
    23  import (
    24  	"flag"
    25  	"fmt"
    26  	"strings"
    27  
    28  	"kythe.io/kythe/go/extractors/config/preprocessor/modifier"
    29  	"kythe.io/kythe/go/extractors/constants"
    30  	"kythe.io/kythe/go/util/log"
    31  )
    32  
    33  var (
    34  	forcedBuilder = flag.String("builder", "", "To force preprocessor to interpret its input file, specify -builder=gradle or -builder=mvn")
    35  	javac         = flag.String("javac_wrapper", "", "A wrapper script around javac that runs kythe extraction.  Some builders may require this to go directly into the build file itself.")
    36  )
    37  
    38  func preProcess(buildFile string) error {
    39  	builder, err := findBuilder(buildFile)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	switch builder {
    45  	case "gradle":
    46  		return modifier.PreProcessBuildGradle(buildFile, javacWrapper())
    47  	case "mvn":
    48  		return modifier.PreProcessPomXML(buildFile)
    49  	default:
    50  		return fmt.Errorf("unsupported builder type: %s", builder)
    51  	}
    52  }
    53  
    54  func findBuilder(buildFile string) (string, error) {
    55  	if *forcedBuilder != "" {
    56  		return *forcedBuilder, nil
    57  	}
    58  
    59  	if strings.HasSuffix(buildFile, "build.gradle") {
    60  		return "gradle", nil
    61  	}
    62  	if strings.HasSuffix(buildFile, "pom.xml") {
    63  		return "mvn", nil
    64  	}
    65  	return "", fmt.Errorf("unrecognized or unsupported build file: %s", buildFile)
    66  }
    67  
    68  func javacWrapper() string {
    69  	if *javac != "" {
    70  		return *javac
    71  	}
    72  	return constants.DefaultJavacWrapperLocation
    73  }
    74  
    75  func main() {
    76  	flag.Parse()
    77  	args := flag.Args()
    78  
    79  	if len(args) != 1 {
    80  		log.Fatal("Must specify a build file via ./preprocessor <build-file>")
    81  	}
    82  
    83  	err := preProcess(args[0])
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  }