github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/commands/compile.go (about) 1 package commands 2 3 import ( 4 "errors" 5 6 "github.com/spf13/cobra" 7 ) 8 9 func getCompileCmd(env *CommandEnv) *cobra.Command { 10 var all bool 11 var fqbn string 12 var buildProps []string 13 var showProps bool 14 var watch bool 15 var compileCmd = &cobra.Command{ 16 Use: "compile [sketch|build(s)]", 17 Long: "\nCompile sketches and builds for specified boards. When " + 18 "compiling for a sketch, you must provide the board FQBN. If left " + 19 "unspecified, a list of available choices will be be printed. If the " + 20 "sketch argument matches a user defined build in ardi.json, the values " + 21 "defined in build will be used to compile", 22 Short: "Compile specified sketch or build(s)", 23 Aliases: []string{"build"}, 24 RunE: func(cmd *cobra.Command, args []string) error { 25 if err := requireProjectInit(); err != nil { 26 return err 27 } 28 defer env.ArdiCore.Compiler.StopWatching() // noop if not watching 29 30 board, _ := env.ArdiCore.Cli.GetTargetBoard(fqbn, "", true) 31 32 if board != nil { 33 fqbn = board.FQBN 34 } 35 36 if all { 37 if watch { 38 return errors.New("cannot watch all builds. You can only watch one build at a time") 39 } 40 41 ardiBuilds := env.ArdiCore.Config.GetBuilds() 42 43 for name := range ardiBuilds { 44 opts, err := env.ArdiCore.Config.GetCompileOpts(name) 45 if err != nil { 46 return err 47 } 48 if err := env.ArdiCore.Compiler.Compile(*opts); err != nil { 49 return err 50 } 51 } 52 return nil 53 } 54 55 opts, err := env.ArdiCore.GetCompileOptsFromArgs(fqbn, buildProps, showProps, args) 56 if err != nil { 57 return err 58 } 59 60 if len(opts) > 1 && watch { 61 return errors.New("cannot specifify watch with mutiple builds. You can only watch one build at a time") 62 } 63 64 for _, compileOpts := range opts { 65 if compileOpts.FQBN == "" { 66 return errors.New("must provide fqbn when no boards are connected") 67 } 68 compileOpts.ShowProps = showProps 69 if err := env.ArdiCore.Compiler.Compile(*compileOpts); err != nil { 70 return err 71 } 72 } 73 74 if watch { 75 return env.ArdiCore.Compiler.WatchForChanges(*opts[0]) 76 } 77 78 return nil 79 }, 80 } 81 compileCmd.Flags().BoolVarP(&all, "all", "a", false, "Compile all builds specified in ardi.json") 82 compileCmd.Flags().StringVarP(&fqbn, "fqbn", "f", "", "Specify fully qualified board name") 83 compileCmd.Flags().StringArrayVarP(&buildProps, "build-prop", "p", []string{}, "Specify build property to compiler") 84 compileCmd.Flags().BoolVarP(&showProps, "show-props", "s", false, "Show all build properties (does not compile)") 85 compileCmd.Flags().BoolVarP(&watch, "watch", "w", false, "Watch sketch file for changes and recompile") 86 87 return compileCmd 88 }