File Coverage

blib/lib/Rails/Assets/Processor.pm
Criterion Covered Total %
statement 68 68 100.0
branch 16 16 100.0
condition 6 6 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 103 103 100.0


line stmt bran cond sub pod time code
1             package Rails::Assets::Processor {
2              
3 4     4   476   use 5.006;
  4         12  
4 4     4   18   use strict;
  4         6  
  4         65  
5 4     4   15   use warnings;
  4         15  
  4         149  
6              
7               our $VERSION = '0.01';
8              
9 4     4   19   use Exporter qw(import);
  4         4  
  4         193  
10               our @EXPORT = qw(
11             process_asset_file
12             process_template_file
13             process_scss_file
14             process_map_file
15             );
16              
17 4         2956   use Rails::Assets::Formatter qw(
18             format_asset_elem
19             format_referral_elem
20             format_template_elem
21 4     4   899 );
  4         8  
22              
23               sub process_asset_file {
24 30     30 1 2447     my ($filename, $reversed_ext, $assets_hash, $assets_paths) = @_;
25 30         102     my ($ext) = $filename =~ /(\.[a-zA-Z0-9]+)$/;
26 30   100     80     my $type = $reversed_ext->{$ext} || 'unknown';
27              
28 30 100       51     if ($type ne 'unknown'){
29 26         54       my $elem = format_asset_elem($filename, $ext, $assets_paths);
30 26         38       push @{$assets_hash->{$type}}, $elem;
  26         75  
31                 } else {
32 4 100       39       print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
33                 }
34               }
35              
36               sub process_template_file {
37 12     12 1 2034     my ($filename, $template_hash, $template_extensions) = @_;
38 12         50     my ($ext) = $filename =~ /(\.[a-zA-Z0-9]+)$/;
39 12 100       77     if (grep /$ext/, @$template_extensions){
40              
41 9         141       open FILE, $_;
42 9         91       while (my $line=<FILE>){
43 66         149         my @stylesheet_tags = $line =~ /stylesheet_link_tag\s*\(*\s*['"](.+?)['"]\s*\)*/g;
44 66         101         my @javascript_tags = $line =~ /javascript_include_tag\s*\(*\s*['"](.+)['"]\s*\)*/g;
45 66         102         my @image_tags = $line =~ /asset_path\s*\(*\s*['"](.+?)['"]\s*\)*/g;
46              
47 66         92         push @{$template_hash->{stylesheets}}, $_ foreach (map {format_template_elem($filename, $_)} @stylesheet_tags);
  9         17  
  9         20  
48 66         83         push @{$template_hash->{javascripts}}, $_ foreach (map {format_template_elem($filename, $_)} @javascript_tags);
  9         17  
  9         22  
49 66         222         push @{$template_hash->{images}}, $_ foreach (map {format_template_elem($filename, $_)} @image_tags);
  3         10  
  3         13  
50                   }
51                 } else {
52 3 100       77       print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
53                 }
54               }
55              
56               sub process_scss_file {
57 3     3 1 2256     my ($filename, $reversed_ext, $scss_hash) = @_;
58 3         57     open FILE, $filename;
59 3         35     while (my $line=<FILE>){
60 24         126       my @assets_tags = $line =~ /asset\-url\s*\(*\s*['"](.+?)['"]\s*\)*/g;
61 24         86       foreach my $asset (@assets_tags){
62 21         31         my $clean_name = $asset;
63 21         57         $clean_name =~ s/([\?#].*)//;
64 21         60         my ($ext) = $clean_name =~ /(\.[a-zA-Z0-9]+)$/;
65 21   100     54         my $type = $reversed_ext->{$ext} || 'unknown';
66 21 100       37         if ($type ne 'unknown'){
67 18         35           my $elem = format_referral_elem($clean_name, $ext, $filename);
68 18         26           push @{$scss_hash->{$type}}, $elem;
  18         54  
69                     } else {
70 3 100       35           print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
71                     }
72                   };
73                 }
74               }
75              
76               sub process_map_file {
77 8     8 1 2328     my ($filename, $reversed_ext, $map_hash) = @_;
78 8         132     open FILE, $_;
79 8         74     while (my $line=<FILE>){
80 4         24       my @assets_tags = $line =~ /sourceMappingURL=(.+\.map?)/;
81 4         10       foreach my $asset (@assets_tags){
82 4         9         my $clean_name = $asset;
83 4         8         $clean_name =~ s/([\?#].*)//;
84 4         13         my ($ext) = $clean_name =~ /(\.[a-zA-Z0-9]+)$/;
85 4   100     17         my $type = $reversed_ext->{$ext} || 'unknown';
86 4 100       11         if ($type ne 'unknown'){
87 2         6           my $elem = format_referral_elem($clean_name, $ext, $filename);
88 2         4           push @{$map_hash->{$type}}, $elem;
  2         17  
89                     } else {
90 2 100       37           print "Found unknown type: $ext ($filename)" . "\n" if $ENV{VERBOSE};
91                     }
92                   };
93                 }
94               }
95             }
96              
97             =head1 NAME
98            
99             Rails::Assets::Processor - provide utility functions for formatting assets refs
100            
101             =head1 VERSION
102            
103             Version 0.02
104            
105             =head1 SYNOPSIS
106            
107             This module contains some functions for processing data references.
108            
109             use Rails::Assets::Processor;
110            
111             process_template_file($_, $template_hash, $template_extensions) foreach @{find_files($template_directories)};
112             process_asset_file($_, $reversed_ext, $assets_hash, $assets_paths) foreach @{find_files($assets_directories)};
113            
114             my $scss_files = [grep { $_->{ext} eq '.scss' } @{$assets_hash->{stylesheets}}];
115             my $js_files = [grep { $_->{ext} eq '.js' } @{$assets_hash->{javascripts}}];
116            
117             process_scss_file($_, $reversed_ext, $scss_hash) foreach map {$_->{full_path}} @{$scss_files};
118             process_map_file($_, , $reversed_ext, $map_hash) foreach map {$_->{full_path}} @{$js_files};
119             ...
120            
121             =head1 EXPORT
122            
123             =head2 process_asset_file
124            
125             =head2 process_template_file
126            
127             =head2 process_scss_file
128            
129             =head2 process_map_file
130            
131             =head1 SUBROUTINES/METHODS
132            
133             =head1 AUTHOR
134            
135             Mauro Berlanda, C<< <kupta at cpan.org> >>
136            
137             =head1 BUGS
138            
139             Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
140             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
141             automatically be notified of progress on your bug as I make changes.
142            
143             =head1 SUPPORT
144            
145             You can find documentation for this module with the perldoc command.
146            
147             perldoc Rails::Assets::Processor
148            
149             You can also look for information at:
150            
151             =over 4
152            
153             =item * RT: CPAN's request tracker (report bugs here)
154            
155             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
156            
157             =item * AnnoCPAN: Annotated CPAN documentation
158            
159             L<http://annocpan.org/dist/.>
160            
161             =item * CPAN Ratings
162            
163             L<http://cpanratings.perl.org/d/.>
164            
165             =item * Search CPAN
166            
167             L<http://search.cpan.org/dist/./>
168            
169             =back
170            
171             =head1 ACKNOWLEDGEMENTS
172            
173             =head1 LICENSE AND COPYRIGHT
174            
175             Copyright 2017 Mauro Berlanda.
176            
177             This program is free software; you can redistribute it and/or modify it
178             under the terms of the the Artistic License (2.0). You may obtain a
179             copy of the full license at:
180            
181             L<http://www.perlfoundation.org/artistic_license_2_0>
182            
183             Any use, modification, and distribution of the Standard or Modified
184             Versions is governed by this Artistic License. By using, modifying or
185             distributing the Package, you accept this license. Do not use, modify,
186             or distribute the Package, if you do not accept this license.
187            
188             If your Modified Version has been derived from a Modified Version made
189             by someone other than you, you are nevertheless required to ensure that
190             your Modified Version complies with the requirements of this license.
191            
192             This license does not grant you the right to use any trademark, service
193             mark, tradename, or logo of the Copyright Holder.
194            
195             This license includes the non-exclusive, worldwide, free-of-charge
196             patent license to make, have made, use, offer to sell, sell, import and
197             otherwise transfer the Package with respect to any patent claims
198             licensable by the Copyright Holder that are necessarily infringed by the
199             Package. If you institute patent litigation (including a cross-claim or
200             counterclaim) against any party alleging that the Package constitutes
201             direct or contributory patent infringement, then this Artistic License
202             to you shall terminate on the date that such litigation is filed.
203            
204             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
205             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
206             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
207             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
208             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
209             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
210             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
211             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212            
213             =cut
214              
215             1; # End of Rails::Assets::Processor
216