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.017';
3 19     19   2079833 use strict;
  19         200  
  19         578  
4 19     19   98 use warnings;
  19         44  
  19         482  
5 19     19   11819 use JSON;
  19         183324  
  19         123  
6 19     19   2083 use Carp;
  19         49  
  19         995  
7 19     19   10391 use File::HomeDir;
  19         103755  
  19         1100  
8 19     19   8210 use JSON::Karabiner::Rule;
  19         50  
  19         4176  
9              
10             sub new {
11 37     37 0 30621 my $class = shift;
12 37         82 my $title = shift;
13 37         66 my $file = shift;
14 37         74 my $opts = shift;
15              
16 37 100       117 if ($opts) {
17 4 50       16 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       90 croak 'JSON::Karabiner constructor requires a file name.' if !$file;
23 34 100       214 croak 'File names are required to have a .json extenstion' if $file !~ /\.json$/;
24 33         174 my $home = File::HomeDir->my_home;
25             my $self = {
26             _file => $file,
27 33   66     1644 _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       433 if (!-d $self->{_mod_file_dir}) {
33 31 100       127 if ($opts->{mod_file_dir}) {
34 2         33 croak "The directory you attempted to set with the 'mod_file_dir' option does not exist.\n\n";
35             } else {
36 29 50       140 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         109 bless $self, $class;
40 19     19   152 { no warnings 'once';
  19         43  
  19         4095  
  31         54  
41 31         56 $main::has_delayed_action = '';
42 31         61 $main::save_to_file_name = '';
43             }
44 31         149 return $self;
45             }
46              
47             # used by test scripts
48             sub _fake_write_file {
49 3     3   1143 my $s = shift;
50 3         7 $s->{_fake_write_flag} = 1;
51 3         9 $s->write_file;
52 0         0 $s->{_fake_write_flag} = 0;
53             }
54              
55             sub write_file {
56              
57 7     7 1 12 my $s = shift;
58 7         22 my $using_dsl = ((caller(0))[0] eq 'JSON::Karabiner::Manipulator');
59 7         260 my $file = $s->{_file};
60 7         15 my $dir = $s->{_mod_file_dir};
61 7         16 my $destination = $dir . $file;
62              
63 7 100       19 if ($using_dsl) {
64 4         8 my $rule = $s->{_karabiner}{rules};
65 4 50       12 if (!%main::saved_manips) {
66 4         14 %main::saved_manips = ();
67 19     19   168 { no warnings 'once'; $main::last_manip_set = ''; }
  19         57  
  19         1309  
  4         6  
  4         8  
68             }
69 4         9 foreach my $r (@$rule) {
70 4         8 my $current_manip_set = $r->{description};
71 19     19   147 { no warnings 'once'; $main::manip_sets{$current_manip_set}{description} = $current_manip_set; }
  19         50  
  19         10698  
  4         6  
  4         10  
72 4 100       21 if (! defined $main::manip_sets{$current_manip_set}{manipulators}) {
73 2         6 $main::manip_sets{$current_manip_set}{manipulators} = [];
74             }
75 4         9 foreach my $manip ($r->{manipulators}) {
76 4         5 push @main::saved_manips, @{$manip};
  4         11  
77 4         6 push @{$main::manip_sets{$current_manip_set}{manipulators}}, @{$manip};
  4         9  
  4         10  
78             }
79             }
80              
81 4         7 my $count = 0;
82 4         17 foreach my $k (sort keys %main::manip_sets) {
83 4         7 my $manipulators = $main::manip_sets{$k}{manipulators};
84 4         6 my $description = $main::manip_sets{$k}{description};
85 4         17 my $new_hash = { manipulators => $manipulators, description => $description };
86 4         14 $s->{_karabiner}->{rules}[$count++] = $new_hash;
87              
88             }
89              
90             }
91 7         17 my $json = $s->_get_json();
92              
93             #TODO ensure it works with utf8
94 4 50       16 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       16 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   12 my $s = shift;
105 7         59 my $json = JSON->new();
106 7         84 $json = $json->convert_blessed();
107 7         172 return $json->canonical->pretty->encode($s->{_karabiner});
108             }
109              
110             sub add_rule {
111 30     30 0 1961 my $s = shift;
112 30         55 my $desc = shift;
113 30 100       102 croak "No description passed to rule." if !$desc;
114 28         160 my $rule = JSON::Karabiner::Rule->new($desc);
115 28         88 $s->_add_rule($rule);
116 28         60 $s->{_rule_object} = $rule;
117 28         79 return $rule;
118             }
119              
120             sub _add_rule {
121 28     28   49 my $s = shift;
122 28         44 my $rule = shift;
123 28         45 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   7179 my $s = shift;
132 21         200 my $json = JSON->new();
133 21         131 $json = $json->convert_blessed();
134              
135             # suppress validity tests
136 21         105 $s->{_rule_object}->_disable_validity_tests();
137              
138 19     19   800 use Data::Dumper qw(Dumper);
  19         7027  
  19         2445  
139 21         345 print Dumper $json->canonical->pretty->encode($s->{_karabiner});
140              
141             # renable validity tests
142 21         3122 $s->{_rule_object}->_enable_validity_tests();
143             }
144              
145              
146             # ABSTRACT: easy JSON code generation for Karabiner-Elements
147              
148             1;
149              
150             __END__