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

     1  package model
     2  
     3  import "strings"
     4  
     5  type BuildReason int
     6  
     7  const BuildReasonNone = BuildReason(0)
     8  
     9  const (
    10  	BuildReasonFlagChangedFiles BuildReason = 1 << iota
    11  	BuildReasonFlagConfig
    12  
    13  	// NOTE(nick): In live-update-v1, if a container had live-updated changed,
    14  	// then crashed, we would automatically replace it with a fresh image.
    15  	// This approach was difficult to reason about and sometimes led to infinite loops.
    16  	// Users complained that it was too aggressive about doing an image build.
    17  	//
    18  	// In live-update-v2, the reconciler keeps track of how to bring crashing
    19  	// containers up to date. Instead, we only kick off fresh image builds
    20  	// if there's a new file change / trigger but the container has been
    21  	// marked unrecoverable. So this build reason is obsolete.
    22  	BuildReasonFlagCrashDeprecated
    23  
    24  	BuildReasonFlagInit
    25  
    26  	BuildReasonFlagTriggerWeb
    27  	BuildReasonFlagTriggerCLI
    28  	BuildReasonFlagTriggerHUD
    29  	BuildReasonFlagTriggerUnknown
    30  
    31  	// An external process called `tilt args`
    32  	BuildReasonFlagTiltfileArgs
    33  
    34  	// Suppose you have
    35  	// manifestA with imageA depending on imageCommon
    36  	// manifestB with imageB depending on imageCommon
    37  	//
    38  	// Building manifestA will mark imageB
    39  	// with changed dependencies.
    40  	BuildReasonFlagChangedDeps
    41  )
    42  
    43  func (r BuildReason) With(flag BuildReason) BuildReason {
    44  	return r | flag
    45  }
    46  
    47  func (r BuildReason) Has(flag BuildReason) bool {
    48  	return r&flag == flag
    49  }
    50  
    51  func (r BuildReason) HasTrigger() bool {
    52  	for _, v := range triggerBuildReasons {
    53  		if r.Has(v) {
    54  			return true
    55  		}
    56  	}
    57  	return false
    58  }
    59  
    60  func (r BuildReason) WithoutTriggers() BuildReason {
    61  	result := int(r)
    62  	for _, v := range triggerBuildReasons {
    63  		if r.Has(v) {
    64  			result -= int(v)
    65  		}
    66  	}
    67  	return BuildReason(result)
    68  }
    69  
    70  var translations = map[BuildReason]string{
    71  	BuildReasonFlagChangedFiles:    "Changed Files",
    72  	BuildReasonFlagConfig:          "Config Changed",
    73  	BuildReasonFlagCrashDeprecated: "Pod Crashed, Lost live_update Changes",
    74  	BuildReasonFlagInit:            "Initial Build",
    75  	BuildReasonFlagTriggerWeb:      "Web Trigger",
    76  	BuildReasonFlagTriggerCLI:      "CLI Trigger",
    77  	BuildReasonFlagTriggerHUD:      "HUD Trigger",
    78  	BuildReasonFlagTriggerUnknown:  "Unknown Trigger",
    79  	BuildReasonFlagTiltfileArgs:    "Tilt Args",
    80  	BuildReasonFlagChangedDeps:     "Dependency Updated",
    81  }
    82  
    83  var triggerBuildReasons = []BuildReason{
    84  	BuildReasonFlagTriggerWeb,
    85  	BuildReasonFlagTriggerCLI,
    86  	BuildReasonFlagTriggerHUD,
    87  	BuildReasonFlagTriggerUnknown,
    88  }
    89  
    90  var allBuildReasons = []BuildReason{
    91  	BuildReasonFlagInit,
    92  	BuildReasonFlagChangedFiles,
    93  	BuildReasonFlagConfig,
    94  	BuildReasonFlagCrashDeprecated,
    95  	BuildReasonFlagTriggerWeb,
    96  	BuildReasonFlagTriggerCLI,
    97  	BuildReasonFlagTriggerHUD,
    98  	BuildReasonFlagChangedDeps,
    99  	BuildReasonFlagTriggerUnknown,
   100  	BuildReasonFlagTiltfileArgs,
   101  }
   102  
   103  func (r BuildReason) String() string {
   104  	rs := []string{}
   105  
   106  	// The trigger build reasons should never be used in conjunction with another
   107  	// build reason, because it was explicitly specified by the user rather than implicit.
   108  	for _, v := range triggerBuildReasons {
   109  		if r.Has(v) {
   110  			return translations[v]
   111  		}
   112  	}
   113  
   114  	// The Init build reason should be listed alone too.
   115  	if r.Has(BuildReasonFlagInit) {
   116  		return translations[BuildReasonFlagInit]
   117  	}
   118  
   119  	// Use an array to iterate over the translations to ensure the iteration order
   120  	// is consistent.
   121  	for _, v := range allBuildReasons {
   122  		if r.Has(v) {
   123  			rs = append(rs, translations[v])
   124  		}
   125  	}
   126  	return strings.Join(rs, " | ")
   127  }