github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/commands/compile_and_upload.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	cli "github.com/robgonnella/ardi/v2/cli-wrapper"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func getCompileAndUploadCmd(env *CommandEnv) *cobra.Command {
    12  	var fqbn string
    13  	var buildProps []string
    14  	var baud int
    15  	var port string
    16  	compileAndUploadCmd := &cobra.Command{
    17  		Use:     "compile-and-upload [build|sketch]",
    18  		Aliases: []string{"build-and-upload", "deploy"},
    19  		Short:   "Compiles then uploads to connected arduino board",
    20  		Long: "\nCompiles and uploads sketches for connected boards. If a " +
    21  			"connected board cannot be detected, you can provide the fqbn and port " +
    22  			"via command flags. If the sketch argument matches a user defined " +
    23  			"build in ardi.json, the values defined in build will be used to " +
    24  			"compile and upload.",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			if err := requireProjectInit(); err != nil {
    27  				return err
    28  			}
    29  			optsList, err := env.ArdiCore.GetCompileOptsFromArgs(fqbn, buildProps, false, args)
    30  			if err != nil {
    31  				return err
    32  			}
    33  			if len(optsList) == 0 {
    34  				return fmt.Errorf("unable to generate compile options from provided args: %s", args)
    35  			}
    36  
    37  			compileOpts := optsList[0]
    38  
    39  			// Ignore errors here as user may have provided fqbn via build to mitigate
    40  			// custom boards that don't show up via auto detect for some reason
    41  			board, _ := env.ArdiCore.Cli.GetTargetBoard(fqbn, port, true)
    42  
    43  			if board == nil && compileOpts.FQBN != "" && port != "" {
    44  				board = &cli.BoardWithPort{FQBN: compileOpts.FQBN, Port: port}
    45  			}
    46  
    47  			if board == nil {
    48  				return errors.New("no connected boards detected")
    49  			}
    50  
    51  			compileOpts.FQBN = board.FQBN
    52  			baud = env.ArdiCore.GetBaudFromArgs(baud, args)
    53  
    54  			sketchDir, _, err := env.ArdiCore.GetSketchPathsFromArgs(args)
    55  			if err != nil {
    56  				return err
    57  			}
    58  
    59  			if err := env.ArdiCore.Compiler.Compile(*compileOpts); err != nil {
    60  				return err
    61  			}
    62  
    63  			return env.ArdiCore.Uploader.Upload(board, sketchDir)
    64  		},
    65  	}
    66  
    67  	compileAndUploadCmd.Flags().StringVarP(&fqbn, "fqbn", "f", "", "Specify fully qualified board name")
    68  	compileAndUploadCmd.Flags().StringArrayVarP(&buildProps, "build-prop", "p", []string{}, "Specify build property to compiler")
    69  	compileAndUploadCmd.Flags().IntVarP(&baud, "baud", "b", 0, "Specify baud rate when using \"attach\" flag")
    70  	compileAndUploadCmd.Flags().StringVar(&port, "port", "", "The port your arduino board is connected to")
    71  
    72  	return compileAndUploadCmd
    73  }