File Coverage

blib/lib/JSON/Karabiner.pm
Criterion Covered Total %
statement 106 111 95.5
branch 23 30 76.6
condition 2 3 66.6
subroutine 17 18 94.4
pod 1 3 33.3
total 149 165 90.3


line stmt bran cond sub pod time code
1             package JSON::Karabiner ;
2             $JSON::Karabiner::VERSION = '0.018';
3 19     19   2127047 use strict;
  19         209  
  19         567  
4 19     19   114 use warnings;
  19         40  
  19         480  
5 19     19   11316 use JSON;
  19         181438  
  19         115  
6 19     19   2158 use Carp;
  19         39  
  19         1045  
7 19     19   10261 use File::HomeDir;
  19         102778  
  19         1023  
8 19     19   8260 use JSON::Karabiner::Rule;
  19         49  
  19         4189  
9              
10             sub new {
11 37     37 0 29841 my $class = shift;
12 37         75 my $title = shift;
13 37         71 my $file = shift;
14 37         67 my $opts = shift;
15              
16 37 100       124 if ($opts) {
17 4 50       17 if (ref $opts ne 'HASH') {
18 0         0 croak 'Options must be passed as a hash reference.';
19             }
20             }
21 37 100       123 croak 'JSON::Karabiner constructor requires a title for the modification.' if !$title;
22 35 100       97 croak 'JSON::Karabiner constructor requires a file name.' if !$file;
23 34 100       209 croak 'File names are required to have a .json extenstion' if $file !~ /\.json$/;
24 33         312 my $home = File::HomeDir->my_home;
25             my $self = {
26             _file => $file,
27 33   66     1842 _mod_file_dir => $opts->{mod_file_dir} || "$home/.config/karabiner/assets/complex_modifications/",
28             _karabiner => { title => $title, rules => [] },
29             _fake_write_flag => 0,
30             _rule_obj => '',
31             };
32 33 100       467 if (!-d $self->{_mod_file_dir}) {
33 31 100       139 if ($opts->{mod_file_dir}) {
34 2         35 croak "The directory you attempted to set with the 'mod_file_dir' option does not exist.\n\n";
35             } else {
36 29 50       143 croak "The default directory for storing complex modifications does not exist. Do you have Karabiner-Elements installed? Is it installed with a non-standard configuration? Try setting the location of the directory manually with the 'mod_file_dir' option. Consult this module's documentation for more information with using the 'perldoc JSON::Karabiner' command in the terminal.\n\n" unless $ENV{HARNESS_ACTIVE};
37             }
38             }
39 31         82 bless $self, $class;
40 19     19   158 { no warnings 'once';
  19         44  
  19         4061  
  31         54  
41 31         71 $main::has_delayed_action = '';
42 31         62 $main::save_to_file_name = '';
43             }
44 31         138 return $self;
45             }
46              
47             # used by test scripts
48             sub _fake_write_file {
49 3     3   1123 my $s = shift;
50 3         5 $s->{_fake_write_flag} = 1;
51 3         10 $s->write_file;
52 0         0 $s->{_fake_write_flag} = 0;
53             }
54              
55             sub write_file {
56              
57 7     7 1 11 my $s = shift;
58 7         19 my $using_dsl = ((caller(0))[0] eq 'JSON::Karabiner::Manipulator');
59 7         258 my $file = $s->{_file};
60 7         14 my $dir = $s->{_mod_file_dir};
61 7         15 my $destination = $dir . $file;
62              
63 7 100       29 if ($using_dsl) {
64 4         9 my $rule = $s->{_karabiner}{rules};
65 4 50       11 if (!%main::saved_manips) {
66 4         18 %main::saved_manips = ();
67 19     19   155 { no warnings 'once'; $main::last_manip_set = ''; }
  19         47  
  19         1272  
  4         6  
  4         8  
68             }
69 4         9 foreach my $r (@$rule) {
70 4         6 my $current_manip_set = $r->{description};
71 19     19   131 { no warnings 'once'; $main::manip_sets{$current_manip_set}{description} = $current_manip_set; }
  19         39  
  19         10360  
  4         5  
  4         11  
72 4 100       10 if (! defined $main::manip_sets{$current_manip_set}{manipulators}) {
73 2         5 $main::manip_sets{$current_manip_set}{manipulators} = [];
74             }
75 4         9 foreach my $manip ($r->{manipulators}) {
76 4         6 push @main::saved_manips, @{$manip};
  4         8  
77 4         6 push @{$main::manip_sets{$current_manip_set}{manipulators}}, @{$manip};
  4         15  
  4         11  
78             }
79             }
80              
81 4         6 my $count = 0;
82 4         14 foreach my $k (sort keys %main::manip_sets) {
83 4         7 my $manipulators = $main::manip_sets{$k}{manipulators};
84 4         7 my $description = $main::manip_sets{$k}{description};
85 4         10 my $new_hash = { manipulators => $manipulators, description => $description };
86 4         12 $s->{_karabiner}->{rules}[$count++] = $new_hash;
87              
88             }
89              
90             }
91 7         18 my $json = $s->_get_json();
92              
93             #TODO ensure it works with utf8
94 4 50       12 if (!$s->{_fake_write_flag}) {
95 0 0       0 open (FH, '>', $destination) or die 'Could not open file for writing.';
96 0         0 print FH $json;
97 0         0 close FH;
98             }
99              
100 4 50       15 print "Your rules were successfully written to:\n\n $destination.\n\nOpen Karabiner-Elements to import the new rules you have generated.\n\nIf your rules do not appear, please report the issue to our issue queue:\n\nhttps://github.com/sdondley/JSON-Karabiner/issues \n\n" unless $ENV{HARNESS_ACTIVE};
101             }
102              
103             sub _get_json {
104 7     7   13 my $s = shift;
105 7         55 my $json = JSON->new();
106 7         87 $json = $json->convert_blessed();
107 7         140 return $json->canonical->pretty->encode($s->{_karabiner});
108             }
109              
110             sub add_rule {
111 30     30 0 2028 my $s = shift;
112 30         59 my $desc = shift;
113 30 100       107 croak "No description passed to rule." if !$desc;
114 28         176 my $rule = JSON::Karabiner::Rule->new($desc);
115 28         94 $s->_add_rule($rule);
116 28         62 $s->{_rule_object} = $rule;
117 28         89 return $rule;
118             }
119              
120             sub _add_rule {
121 28     28   48 my $s = shift;
122 28         48 my $rule = shift;
123 28         49 push @{$s->{_karabiner}{rules}}, $rule;
  28         129  
124             }
125              
126       0     sub _check_if_file_exits {
127              
128             }
129              
130             sub _dump_json {
131 21     21   7523 my $s = shift;
132 21         219 my $json = JSON->new();
133 21         131 $json = $json->convert_blessed();
134              
135             # suppress validity tests
136 21         114 $s->{_rule_object}->_disable_validity_tests();
137              
138 19     19   849 use Data::Dumper qw(Dumper);
  19         6993  
  19         2350  
139 21         403 print Dumper $json->canonical->pretty->encode($s->{_karabiner});
140              
141             # renable validity tests
142 21         9247 $s->{_rule_object}->_enable_validity_tests();
143             }
144              
145              
146             # ABSTRACT: easy JSON code generation for Karabiner-Elements
147              
148             1;
149              
150             __END__