2
0

arduino-wrapper 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/perl
  2. #
  3. # Wrapper for calling arduino-cli with a bunch of obligatory options...
  4. #
  5. use File::Spec;
  6. use Cwd qw(abs_path realpath);
  7. use strict;
  8. use integer;
  9. sub dirname($) {
  10. my($path) = @_;
  11. $path = File::Spec->rel2abs($path);
  12. my($vol,$dir,$file) = File::Spec->splitpath($path);
  13. return File::Spec->catpath($vol, $dir, '');
  14. }
  15. my $here = realpath(dirname($0));
  16. my $arduino = File::Spec->catdir($here, 'arduino');
  17. $ENV{'ARDUINO_DIRECTORIES_DATA'} = $arduino;
  18. $ENV{'ARDUINO_DIRECTORIES_USER'} = $arduino;
  19. $ENV{'ARDUINO_LOGGING_FILE'} = File::Spec->catfile($arduino, 'arduino.log');
  20. $ENV{'ARDUINO_CONFIG_FILE'} = File::Spec->catfile($here, 'arduino_cli.yaml');
  21. my $arduino_cli = $ENV{'ARDUINO_CLI'} || 'arduino-cli';
  22. mkdir($arduino);
  23. my $subcmd = shift(@ARGV);
  24. my @args = ($subcmd);
  25. my %props;
  26. if (open(my $prop, '<', 'properties.txt')) {
  27. while (defined(my $p = <$prop>)) {
  28. chomp $p;
  29. next if ($p !~ /^\s*([\w.]+)\s*=\s*(.*)$/);
  30. $props{$1} = $2;
  31. }
  32. close($prop);
  33. }
  34. if ($subcmd eq 'compile') {
  35. my %named_opts = (
  36. 'build.fqbn' => '--fqbn',
  37. 'build.path' => '--build-path',
  38. 'build.output.path' => '--output-dir'
  39. );
  40. foreach my $pn ('build.path', 'build.output.path') {
  41. # Make sure directories exist
  42. my $pv = $props{$pn};
  43. mkdir($pv) if (defined($pv));
  44. }
  45. foreach my $pn (sort(keys(%props))) {
  46. my $pv = $props{$pn};
  47. my $opt_name = $named_opts{$pn};
  48. if ($opt_name) {
  49. push(@args, $opt_name, $pv);
  50. } elsif ($pn =~ /^build\./) {
  51. push(@args, '--build-property', "$pn=$pv");
  52. }
  53. }
  54. } elsif ($subcmd eq 'upload') {
  55. my %named_opts = (
  56. 'build.fqbn' => '--fqbn',
  57. 'build.path' => '--build-path',
  58. 'build.output.path' => '--input-dir'
  59. );
  60. foreach my $pn (sort(keys(%props))) {
  61. my $pv = $props{$pn};
  62. my $opt_name = $named_opts{$pn};
  63. if ($opt_name) {
  64. push(@args, $opt_name, $pv);
  65. } elsif ($pn =~ /^upload\./) {
  66. push(@args, '--upload-property', "$pn=$pv");
  67. }
  68. }
  69. }
  70. push(@args, @ARGV);
  71. exec($arduino_cli, @args);
  72. print STDERR "$0: failed to execute: $arduino_cli ", join(' ', @args), "\n";
  73. exit 1;