github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/r8/flagsparser.go (about)

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package r8
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/args"
    23  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags"
    24  )
    25  
    26  // parseFlags is used to transform an r8 command into a CommandFlags structure.
    27  func parseFlags(ctx context.Context, command []string, workingDir, execRoot string) (*flags.CommandFlags, error) {
    28  	numArgs := len(command)
    29  	if numArgs < 2 {
    30  		return nil, fmt.Errorf("insufficient number of arguments in command: %v", command)
    31  	}
    32  
    33  	res := &flags.CommandFlags{
    34  		ExecutablePath:   command[0],
    35  		WorkingDirectory: workingDir,
    36  		ExecRoot:         execRoot,
    37  	}
    38  	s := args.Scanner{
    39  		Args: command[1:],
    40  		Flags: map[string]int{
    41  			"-libraryjars":        1,
    42  			"-include":            1,
    43  			"--output":            1,
    44  			"--main-dex-list":     1,
    45  			"-printmapping":       1,
    46  			"-printconfiguration": 1,
    47  			"-injars":             1,
    48  		},
    49  		Joined: []args.PrefixOption{
    50  			{"--main-dex-list=", 0},
    51  		},
    52  	}
    53  	for s.HasNext() {
    54  		flag, args, values, _ := s.Next()
    55  		switch flag {
    56  		case "-libraryjars", "-include":
    57  			res.Dependencies = append(res.Dependencies, strings.Split(values[0], ":")...)
    58  			continue
    59  		case "--output":
    60  			res.OutputDirPaths = append(res.OutputDirPaths, values[0])
    61  			continue
    62  		case "--main-dex-list=", "--main-dex-list":
    63  			res.Dependencies = append(res.Dependencies, values[0])
    64  			continue
    65  		case "-printmapping", "-printconfiguration":
    66  			res.OutputFilePaths = append(res.OutputFilePaths, values[0])
    67  			continue
    68  		case "-injars":
    69  			res.TargetFilePaths = append(res.TargetFilePaths, values[0])
    70  			continue
    71  		}
    72  		res.Flags = append(res.Flags, &flags.Flag{Value: args[0]})
    73  	}
    74  	return res, nil
    75  }