kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/config/runextractor/gradlecmd/gradlecmd.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  // Package gradlecmd extracts a gradle repo.
    18  package gradlecmd // import "kythe.io/kythe/go/extractors/config/runextractor/gradlecmd"
    19  
    20  import (
    21  	"context"
    22  	"flag"
    23  	"fmt"
    24  	"os"
    25  	"os/exec"
    26  
    27  	"kythe.io/kythe/go/extractors/config/preprocessor/modifier"
    28  	"kythe.io/kythe/go/extractors/config/runextractor/backup"
    29  	"kythe.io/kythe/go/extractors/constants"
    30  	"kythe.io/kythe/go/util/cmdutil"
    31  
    32  	"github.com/google/subcommands"
    33  )
    34  
    35  type gradleCommand struct {
    36  	cmdutil.Info
    37  
    38  	buildFile    string
    39  	javacWrapper string
    40  }
    41  
    42  // New creates a new subcommand for running gradle extraction.
    43  func New() subcommands.Command {
    44  	return &gradleCommand{
    45  		Info: cmdutil.NewInfo("gradle", "extract a repo built with gradle",
    46  			`docstring TBD`),
    47  	}
    48  }
    49  
    50  // SetFlags implements the subcommands interface and provides command-specific
    51  // flags for gradle extraction.
    52  func (g *gradleCommand) SetFlags(fs *flag.FlagSet) {
    53  	fs.StringVar(&g.javacWrapper, "javac_wrapper", "", "A required executable that wraps javac for Kythe extraction.")
    54  	fs.StringVar(&g.buildFile, "build_file", "build.gradle", "The config file for a gradle repo, defaults to 'build.gradle'")
    55  }
    56  
    57  func (g gradleCommand) checkFlags() error {
    58  	for _, key := range constants.RequiredJavaEnv {
    59  		if os.Getenv(key) == "" {
    60  			return fmt.Errorf("required env var %s not set", key)
    61  		}
    62  	}
    63  	if g.buildFile == "" {
    64  		return fmt.Errorf("gradle build file (e.g. 'build.gradle') not set")
    65  	}
    66  	if g.javacWrapper == "" {
    67  		return fmt.Errorf("required -javac_wrapper not set")
    68  	}
    69  	return nil
    70  }
    71  
    72  // Execute implements the subcommands interface and runs gradle extraction.
    73  func (g *gradleCommand) Execute(ctx context.Context, fs *flag.FlagSet, args ...any) subcommands.ExitStatus {
    74  	if err := g.checkFlags(); err != nil {
    75  		return g.Fail("incorrect flags: %v", err)
    76  	}
    77  	tf, err := backup.New(g.buildFile)
    78  	if err != nil {
    79  		return g.Fail("error backing up %s: %v", g.buildFile, err)
    80  	}
    81  	defer tf.Release()
    82  	if err := modifier.PreProcessBuildGradle(g.buildFile, g.javacWrapper); err != nil {
    83  		return g.Fail("error modifying %s: %v", g.buildFile, err)
    84  	}
    85  	if err := exec.Command("gradle", "clean", "build").Run(); err != nil {
    86  		return g.Fail("error executing gradle build: %v", err)
    87  	}
    88  	if err := tf.Restore(); err != nil {
    89  		return g.Fail("error restoring %s from %s: %v", g.buildFile, tf, err)
    90  	}
    91  	return subcommands.ExitSuccess
    92  }