build-profile.pl untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 118 CC 7 SCAR 21.8
CC
7
Cov
0%
parseProfileData
20.0
cleanUpUnits
12.0
writeProfileModule
6.0
CRAP 8.9
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5 use feature 'say';
6 use Text::CSV qw(csv);
7 use Mojo::JSON qw/j/;
8 use Data::Dumper;
9 use Getopt::Long;
10  
11 my $forceEnabled = undef;
12 GetOptions("force" => \$forceEnabled);
13  
14 my $file = $ARGV[0];
15  
16 my $data = csv(in => $file, headers => "auto", blank_is_undef => 1);
17 my $profile = parseProfileData($data);
18 writeProfileModule($profile);
19  
20 sub writeProfileModule {
21 my $profileFileName = "./lib/Parser/FIT/Profile.pm";
22  
23 if(-f $profileFileName and !$forceEnabled) {
24 say STDERR "[ERROR] Profile.pm already exists! Use force (--force) to overwrite!";
25 exit 1;
26 }
27  
28 open(my $fh, ">", $profileFileName) or die "Failed to open '$profileFileName': $!";
29 say $fh "# WARNING! This file is auto generated by '$0'! Changes will be lost by the next run!";
30 say $fh "package Parser::FIT::Profile;";
31 say $fh "use strict;";
32 say $fh "use warnings;";
33 print $fh 'our $PROFILE = ';
34  
35 $Data::Dumper::Sortkeys = 1;
36 $Data::Dumper::Varname = "PROFILE";
37 $Data::Dumper::Terse = 1;
38  
39 print $fh Dumper($profile);
40 }
41  
42  
43 sub parseProfileData {
44 my $data = shift;
45  
46 my $currentMessageType = undef;
47 my %profile;
48  
49 foreach my $row (@$data) {
50 if(!defined $currentMessageType && !defined $row->{"Message Name"}) {
51 next;
52 }
53  
54 my $messageName = $row->{"Message Name"};
55  
56 if(defined $messageName) {
57 $currentMessageType = {
58 name => $messageName,
59 fields => {}
60 };
61  
62 $profile{$messageName} = $currentMessageType;
63  
64 # say "Found MessageType=" . $currentMessageType->{name};
65 }
66  
67 if(!defined $row->{"Message Name"} && defined $row->{"Field Def #"} && defined $row->{"Field Name"} && defined $row->{"Field Type"}) {
68 my $fieldId = $row->{"Field Def #"};
69 my $fieldName = $row->{"Field Name"};
70 my $fieldType = $row->{"Field Type"};
71 my $scale = $row->{"Scale"};# eq '' ? 1 : $row->{"Scale"};
72 my $offset = $row->{"Offset"};
73 my $unit = cleanUpUnits($row->{"Units"});
74  
75 $currentMessageType->{fields}->{$fieldId} = {
76 id => $fieldId,
77 name => $fieldName,
78 type => $fieldType,
79 scale => $scale,
80 offset => $offset,
81 unit => $unit,
82 };
83 }
84 }
85  
86 return \%profile;
87 }
88  
89 sub cleanUpUnits {
90 my $unitString = shift;
91  
92 if(!defined $unitString) {
93 return $unitString;
94 }
95  
96 if($unitString eq "percent") {
97 return "%";
98 }
99  
100 return $unitString;
101 }
102