github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/pkg/model/squash.go (about)

     1  package model
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func TrySquash(runs []Cmd) []Cmd {
     8  	newRuns := make([]Cmd, 0)
     9  	for i := 0; i < len(runs); i++ {
    10  		toSquash := []Cmd{}
    11  		for j := i; j < len(runs); j++ {
    12  			runJ := runs[j]
    13  			if !runJ.IsShellStandardForm() {
    14  				break
    15  			}
    16  
    17  			toSquash = append(toSquash, runJ)
    18  		}
    19  
    20  		if len(toSquash) < 2 {
    21  			newRuns = append(newRuns, runs[i])
    22  			continue
    23  		}
    24  
    25  		newRuns = append(newRuns, squashHelper(toSquash))
    26  		i += len(toSquash) - 1
    27  	}
    28  	return newRuns
    29  }
    30  
    31  // Create a new shell script that combines the individual runs.
    32  // We know all the scripts are in shell standard form.
    33  func squashHelper(runs []Cmd) Cmd {
    34  	scripts := make([]string, len(runs))
    35  	for i, c := range runs {
    36  		scripts[i] = c.ShellStandardScript()
    37  	}
    38  
    39  	return Cmd{
    40  		// This could potentially break things (because it converts normal shell
    41  		// scripts to scripts run with -ex). We're not too worried about it right
    42  		// now.  In the future, we might need to do manual exit code checks for
    43  		// correctness.
    44  		Argv: []string{
    45  			"sh",
    46  			"-exc",
    47  			strings.Join(scripts, ";\n"),
    48  		},
    49  	}
    50  }