github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/compute/language.go (about)

     1  package compute
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/fastly/cli/pkg/config"
    10  )
    11  
    12  // NewLanguages returns a list of supported programming languages.
    13  //
    14  // NOTE: The 'timeout' value zero is passed into each New<Language> call as it's
    15  // only useful during the `compute build` phase and is expected to be
    16  // provided by the user via a flag on the build command.
    17  func NewLanguages(kits config.StarterKitLanguages) []*Language {
    18  	// WARNING: Do not reorder these options as they affect the rendered output.
    19  	// They are placed in order of language maturity/importance.
    20  	//
    21  	// A change to this order will also break the tests, as the logic defaults to
    22  	// the first language in the list if nothing entered at the relevant language
    23  	// prompt.
    24  	return []*Language{
    25  		NewLanguage(&LanguageOptions{
    26  			Name:        "rust",
    27  			DisplayName: "Rust",
    28  			StarterKits: kits.Rust,
    29  		}),
    30  		NewLanguage(&LanguageOptions{
    31  			Name:        "javascript",
    32  			DisplayName: "JavaScript",
    33  			StarterKits: kits.JavaScript,
    34  		}),
    35  		NewLanguage(&LanguageOptions{
    36  			Name:        "go",
    37  			DisplayName: "Go",
    38  			StarterKits: kits.Go,
    39  		}),
    40  		NewLanguage(&LanguageOptions{
    41  			Name:        "other",
    42  			DisplayName: "Other ('bring your own' Wasm binary)",
    43  		}),
    44  	}
    45  }
    46  
    47  // NewLanguage constructs a new Language from a LangaugeOptions.
    48  func NewLanguage(options *LanguageOptions) *Language {
    49  	// Ensure the 'default' starter kit is always first.
    50  	sort.Slice(options.StarterKits, func(i, j int) bool {
    51  		suffix := fmt.Sprintf("%s-default", options.Name)
    52  		a := strings.HasSuffix(options.StarterKits[i].Path, suffix)
    53  		b := strings.HasSuffix(options.StarterKits[j].Path, suffix)
    54  		var (
    55  			bitSetA int8
    56  			bitSetB int8
    57  		)
    58  		if a {
    59  			bitSetA = 1
    60  		}
    61  		if b {
    62  			bitSetB = 1
    63  		}
    64  		return bitSetA > bitSetB
    65  	})
    66  
    67  	return &Language{
    68  		options.Name,
    69  		options.DisplayName,
    70  		options.StarterKits,
    71  		options.SourceDirectory,
    72  		options.Toolchain,
    73  	}
    74  }
    75  
    76  // Language models a Compute source language.
    77  type Language struct {
    78  	Name            string
    79  	DisplayName     string
    80  	StarterKits     []config.StarterKit
    81  	SourceDirectory string
    82  
    83  	Toolchain
    84  }
    85  
    86  // LanguageOptions models configuration options for a Language.
    87  type LanguageOptions struct {
    88  	Name            string
    89  	DisplayName     string
    90  	StarterKits     []config.StarterKit
    91  	SourceDirectory string
    92  	Toolchain       Toolchain
    93  }
    94  
    95  // Shell represents a subprocess shell used by `compute` environment where
    96  // `[scripts.build]` has been defined within fastly.toml manifest.
    97  type Shell struct{}
    98  
    99  // Build expects a command that can be prefixed with an appropriate subprocess
   100  // shell.
   101  //
   102  // Example:
   103  // build = "yarn install && yarn build"
   104  //
   105  // Should be converted into a command such as (on unix):
   106  // sh -c "yarn install && yarn build".
   107  func (s Shell) Build(command string) (cmd string, args []string) {
   108  	cmd = "sh"
   109  	args = []string{"-c"}
   110  
   111  	if runtime.GOOS == "windows" {
   112  		cmd = "cmd.exe"
   113  		args = []string{"/C"}
   114  	}
   115  
   116  	args = append(args, command)
   117  
   118  	return cmd, args
   119  }