@@ -33,6 +33,8 @@ import (
3333 "github.com/arduino/arduino-cli/commands"
3434 "github.com/arduino/arduino-cli/configuration"
3535 "github.com/arduino/arduino-cli/i18n"
36+ "github.com/arduino/arduino-cli/table"
37+ "github.com/fatih/color"
3638 "github.com/sirupsen/logrus"
3739
3840 "github.com/arduino/arduino-cli/cli/errorcodes"
6870 clean bool // Cleanup the build folder and do not use any cached build
6971 compilationDatabaseOnly bool // Only create compilation database without actually compiling
7072 sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
73+ dumpProfile bool // Create and print a profile configuration from the build
7174 // library and libraries sound similar but they're actually different.
7275 // library expects a path to the root folder of one single library.
7376 // libraries expects a path to a directory containing multiple libraries, similarly to the <directories.user>/libraries path.
@@ -93,6 +96,7 @@ func NewCommand() *cobra.Command {
9396
9497 fqbnArg .AddToCommand (compileCommand )
9598 profileArg .AddToCommand (compileCommand )
99+ compileCommand .Flags ().BoolVar (& dumpProfile , "dump-profile" , false , tr ("Create and print a profile configuration from the build." ))
96100 compileCommand .Flags ().BoolVar (& showProperties , "show-properties" , false , tr ("Show all build properties used instead of compiling." ))
97101 compileCommand .Flags ().BoolVar (& preprocess , "preprocess" , false , tr ("Print preprocessed code to stdout instead of compiling." ))
98102 compileCommand .Flags ().StringVar (& buildCachePath , "build-cache-path" , "" , tr ("Builds of 'core.a' are saved into this path to be cached and reused." ))
@@ -142,6 +146,10 @@ func NewCommand() *cobra.Command {
142146func runCompileCommand (cmd * cobra.Command , args []string ) {
143147 logrus .Info ("Executing `arduino-cli compile`" )
144148
149+ if dumpProfile && feedback .GetFormat () != feedback .Text {
150+ feedback .Errorf (tr ("You cannot use the %[1]s flag together with %[2]s." , "--dump-profile" , "--format json" ))
151+ os .Exit (errorcodes .ErrBadArgument )
152+ }
145153 if profileArg .Get () != "" {
146154 if len (libraries ) > 0 {
147155 feedback .Errorf (tr ("You cannot use the %s flag while compiling with a profile." , "--libraries" ))
@@ -268,6 +276,54 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
268276 }
269277 }
270278
279+ if dumpProfile {
280+ libs := ""
281+ hasVendoredLibs := false
282+ for _ , lib := range compileRes .GetUsedLibraries () {
283+ if lib .Location != rpc .LibraryLocation_LIBRARY_LOCATION_USER && lib .Location != rpc .LibraryLocation_LIBRARY_LOCATION_UNMANAGED {
284+ continue
285+ }
286+ if lib .GetVersion () == "" {
287+ hasVendoredLibs = true
288+ continue
289+ }
290+ libs += fmt .Sprintln (" - " + lib .GetName () + " (" + lib .GetVersion () + ")" )
291+ }
292+ if hasVendoredLibs {
293+ fmt .Println ()
294+ fmt .Println (tr ("WARNING: The sketch is compiled using one or more custom libraries." ))
295+ fmt .Println (tr ("Currently, Build Profiles only support libraries available through Arduino Library Manager." ))
296+ }
297+
298+ newProfileName := "my_profile_name"
299+ if split := strings .Split (compileRequest .GetFqbn (), ":" ); len (split ) > 2 {
300+ newProfileName = split [2 ]
301+ }
302+ fmt .Println ()
303+ fmt .Println ("profile:" )
304+ fmt .Println (" " + newProfileName + ":" )
305+ fmt .Println (" fqbn: " + compileRequest .GetFqbn ())
306+ fmt .Println (" platforms:" )
307+ boardPlatform := compileRes .GetBoardPlatform ()
308+ fmt .Println (" - platform: " + boardPlatform .GetId () + " (" + boardPlatform .GetVersion () + ")" )
309+ if url := boardPlatform .GetPackageUrl (); url != "" {
310+ fmt .Println (" platform_index_url: " + url )
311+ }
312+
313+ if buildPlatform := compileRes .GetBuildPlatform (); buildPlatform != nil &&
314+ buildPlatform .Id != boardPlatform .Id &&
315+ buildPlatform .Version != boardPlatform .Version {
316+ fmt .Println (" - platform: " + buildPlatform .GetId () + " (" + buildPlatform .GetVersion () + ")" )
317+ if url := buildPlatform .GetPackageUrl (); url != "" {
318+ fmt .Println (" platform_index_url: " + url )
319+ }
320+ }
321+ if len (libs ) > 0 {
322+ fmt .Println (" libraries:" )
323+ fmt .Print (libs )
324+ }
325+ }
326+
271327 feedback .PrintResult (& compileResult {
272328 CompileOut : compileStdOut .String (),
273329 CompileErr : compileStdErr .String (),
@@ -316,6 +372,45 @@ func (r *compileResult) Data() interface{} {
316372}
317373
318374func (r * compileResult ) String () string {
319- // The output is already printed via os.Stdout/os.Stdin
320- return ""
375+ titleColor := color .New (color .FgHiGreen )
376+ nameColor := color .New (color .FgHiYellow )
377+ pathColor := color .New (color .FgHiBlack )
378+ build := r .BuilderResult
379+
380+ res := "\n "
381+ libraries := table .New ()
382+ if len (build .GetUsedLibraries ()) > 0 {
383+ libraries .SetHeader (
384+ table .NewCell (tr ("Used library" ), titleColor ),
385+ table .NewCell (tr ("Version" ), titleColor ),
386+ table .NewCell (tr ("Path" ), pathColor ))
387+ for _ , l := range build .GetUsedLibraries () {
388+ libraries .AddRow (
389+ table .NewCell (l .GetName (), nameColor ),
390+ l .GetVersion (),
391+ table .NewCell (l .GetInstallDir (), pathColor ))
392+ }
393+ }
394+ res += libraries .Render () + "\n "
395+
396+ platforms := table .New ()
397+ platforms .SetHeader (
398+ table .NewCell (tr ("Used platform" ), titleColor ),
399+ table .NewCell (tr ("Version" ), titleColor ),
400+ table .NewCell (tr ("Path" ), pathColor ))
401+ boardPlatform := build .GetBoardPlatform ()
402+ platforms .AddRow (
403+ table .NewCell (boardPlatform .GetId (), nameColor ),
404+ boardPlatform .GetVersion (),
405+ table .NewCell (boardPlatform .GetInstallDir (), pathColor ))
406+ if buildPlatform := build .GetBuildPlatform (); buildPlatform != nil &&
407+ buildPlatform .Id != boardPlatform .Id &&
408+ buildPlatform .Version != boardPlatform .Version {
409+ platforms .AddRow (
410+ table .NewCell (buildPlatform .GetId (), nameColor ),
411+ buildPlatform .GetVersion (),
412+ table .NewCell (buildPlatform .GetInstallDir (), pathColor ))
413+ }
414+ res += platforms .Render ()
415+ return res
321416}
0 commit comments