| 1 | 3 | package Rails::Assets { | |
| 2 | 3 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 3 | ▶ | use Rails::Assets::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use Rails::Assets::Processor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Clone qw(clone); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 3 | our $VERSION = '0.02'; | |
| 11 | 3 | our $TEMPLATE_DIR = [qw( app/views/)]; | |
| 12 | 3 | our $TEMPLATE_EXT = [qw(.haml .erb)]; | |
| 13 | 3 | our $ASSETS_DIR = [qw( app/assets/ public/ vendor/assets/ )]; | |
| 14 | 3 | our $ASSETS_EXT = { | |
| 15 | fonts => [qw(.woff2 .woff .ttf .eot .otf)], | ||
| 16 | images => [qw(.png .jpg .gif .svg .ico)], | ||
| 17 | javascripts => [qw(.js .map)], | ||
| 18 | stylesheets => [qw(.css .scss)], | ||
| 19 | }; | ||
| 20 | |||
| 21 | 2 | ▶ | sub new { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 22 | 2 | my $class = shift; | |
| 23 | 2 | my $self = { | |
| 24 | TEMPLATE_DIR => [@$Rails::Assets::TEMPLATE_DIR], | ||
| 25 | TEMPLATE_EXT => [@$Rails::Assets::TEMPLATE_EXT], | ||
| 26 | ASSETS_DIR => [@$Rails::Assets::ASSETS_DIR], | ||
| 27 | ASSETS_EXT => {%$Rails::Assets::ASSETS_EXT}, | ||
| 28 | }; | ||
| 29 | 2 | bless $self, $class; | |
| 30 | } | ||
| 31 | |||
| 32 | 4 | ▶ | sub template_dir { $_[0]->{TEMPLATE_DIR} } |
|
Subroutine
template_dir: called CC=1 SCAR=0.0
Pod
documented
| |||
| 33 | 8 | ▶ | sub template_ext { $_[0]->{TEMPLATE_EXT} } |
|
Subroutine
template_ext: called CC=1 SCAR=0.0
Pod
documented
| |||
| 34 | 3 | ▶ | sub assets_dir { $_[0]->{ASSETS_DIR} } |
|
Subroutine
assets_dir: called CC=1 SCAR=0.0
Pod
documented
| |||
| 35 | 3 | ▶ | sub assets_ext { $_[0]->{ASSETS_EXT} } |
|
Subroutine
assets_ext: called CC=1 SCAR=0.0
Pod
documented
| |||
| 36 | 2 | ▶ | sub assets_hash { $_[0]->{ASSETS_HASH} } |
|
Subroutine
assets_hash: called CC=1 SCAR=0.0
Pod
documented
| |||
| 37 | 2 | ▶ | sub template_hash { $_[0]->{TEMPLATE_HASH} } |
|
Subroutine
template_hash: called CC=1 SCAR=0.0
Pod
documented
| |||
| 38 | 2 | ▶ | sub scss_hash { $_[0]->{SCSS_HASH} } |
|
Subroutine
scss_hash: called CC=1 SCAR=0.0
Pod
documented
| |||
| 39 | 2 | ▶ | sub map_hash { $_[0]->{MAP_HASH} } |
|
Subroutine
map_hash: called CC=1 SCAR=0.0
Pod
documented
| |||
| 40 | 2 | ▶ | sub assets_paths { $_[0]->{ASSETS_PATHS} } |
|
Subroutine
assets_paths: called CC=1 SCAR=0.0
Pod
documented
| |||
| 41 | 2 | ▶ | sub reversed_ext { $_[0]->{REVERSED_EXT} } |
|
Subroutine
reversed_ext: called CC=1 SCAR=0.0
Pod
documented
| |||
| 42 | |||
| 43 | 1 | ▶ | sub analyse { |
|
Subroutine
analyse: called CC=5 SCAR=16.1
Pod
documented
| |||
| 44 | 1 | my $self = shift; | |
| 45 | 1 | my ($assets_hash, $assets_paths, $reversed_ext) = | |
| 46 | prepare_assets_refs($self->assets_dir, $self->assets_ext); | ||
| 47 | |||
| 48 | 1 | $self->{ASSETS_HASH} = clone($assets_hash); | |
| 49 | 1 | $self->{TEMPLATE_HASH} = clone($assets_hash); | |
| 50 | 1 | $self->{SCSS_HASH} = clone($assets_hash); | |
| 51 | 1 | $self->{MAP_HASH} = clone($assets_hash); | |
| 52 | 1 | $self->{ASSETS_PATHS} = $assets_paths; | |
| 53 | 1 | $self->{REVERSED_EXT} = {%$reversed_ext}; | |
| 54 | |||
| 55 | process_asset_file($_, $self->{REVERSED_EXT}, $self->{ASSETS_HASH}, $self->{ASSETS_PATHS}) | ||
| 56 | 1 | foreach @{find_files($self->assets_dir())}; | |
| 57 | process_template_file($_, $self->{TEMPLATE_HASH}, $self->template_ext()) | ||
| 58 | 1 | foreach @{find_files($self->template_dir())}; | |
| 59 | |||
| 60 | 1 | my $scss_files = [grep { $_->{ext} eq '.scss' } @{$self->{ASSETS_HASH}->{stylesheets}}]; | |
| 61 | 1 | my $js_files = [grep { $_->{ext} eq '.js' } @{$self->{ASSETS_HASH}->{javascripts}}]; | |
| 62 | |||
| 63 | 1 | process_scss_file($_, $self->{REVERSED_EXT}, $self->{SCSS_HASH}) foreach map {$_->{full_path}} @{$scss_files}; | |
| 64 | 1 | process_map_file($_, , $self->{REVERSED_EXT}, $self->{MAP_HASH}) foreach map {$_->{full_path}} @{$js_files}; | |
| 65 | 1 | $self; | |
| 66 | } | ||
| 67 | |||
| 68 | } | ||
| 69 | =head1 NAME | ||
| 70 | |||
| 71 | Rails::Assets - Class for Rails Projects Assets Analysis. | ||
| 72 | |||
| 73 | =head1 VERSION | ||
| 74 | |||
| 75 | Version 0.02 | ||
| 76 | |||
| 77 | =head1 SYNOPSIS | ||
| 78 | |||
| 79 | This module provides an object for parsing Assets directories | ||
| 80 | |||
| 81 | use Rails::Assets; | ||
| 82 | |||
| 83 | my $assets = Rails::Assets->new(); | ||
| 84 | |||
| 85 | my $template_directories = $assets->template_dir(); | ||
| 86 | my $template_extensions = $assets->template_ext(); | ||
| 87 | my $assets_directories = $assets->assets_dir(); | ||
| 88 | my $assets_extensions = $assets->assets_ext(); | ||
| 89 | |||
| 90 | $assets->analyse(); | ||
| 91 | ... | ||
| 92 | |||
| 93 | You can find a sample script in the project L<github repository|https://github.com/mberlanda/rails-assets-coverage> | ||
| 94 | |||
| 95 | =head1 SUBROUTINES/METHODS | ||
| 96 | |||
| 97 | =head2 new | ||
| 98 | |||
| 99 | The constructor takes no argument for now. It would be interesting to add additional paths | ||
| 100 | and extensions in the future. This would require some validation and it could be quite tricky. | ||
| 101 | |||
| 102 | use Rails::Assets; | ||
| 103 | |||
| 104 | my $assets = Rails::Assets->new(); | ||
| 105 | |||
| 106 | At the current state, you can modify the defaults as follows: | ||
| 107 | |||
| 108 | #!usr/bin/perl -t | ||
| 109 | |||
| 110 | use strict; | ||
| 111 | use warnings; | ||
| 112 | use Test::More tests => 3; | ||
| 113 | use Test::Deep; | ||
| 114 | use Rails::Assets; | ||
| 115 | |||
| 116 | DEFAULT: { | ||
| 117 | my $default_dir = [qw( app/views/ )]; | ||
| 118 | is_deeply($Rails::Assets::TEMPLATE_DIR, $default_dir, | ||
| 119 | '$Rails::Assets::TEMPLATE_DIR has the expected default' | ||
| 120 | ); | ||
| 121 | |||
| 122 | my $assets = Rails::Assets->new; | ||
| 123 | is_deeply($assets->template_dir, $default_dir, | ||
| 124 | 'Rails::Assets instance has default template_dir()' | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | CUSTOM: { | ||
| 129 | push @$Rails::Assets::TEMPLATE_DIR, 'app/folder/'; | ||
| 130 | my $expected_dir = [qw( app/views/ app/folder/ )]; | ||
| 131 | |||
| 132 | my $assets = Rails::Assets->new; | ||
| 133 | is_deeply($assets->template_dir, $expected_dir, | ||
| 134 | 'Rails::Assets instance template_dir() changes according to $Rails::Assets::TEMPLATE_DIR'); | ||
| 135 | } | ||
| 136 | |||
| 137 | =head2 template_dir | ||
| 138 | |||
| 139 | Getter for template directories. It is a copy of C<$Rails::Assets::TEMPLATE_DIR> reference | ||
| 140 | |||
| 141 | my $template_directories = [qw( app/views/)]; | ||
| 142 | is_deeply( $Rails::Assets::TEMPLATE_DIR, $template_directories); | ||
| 143 | is_deeply( $assets->template_dir(), $template_directories); | ||
| 144 | |||
| 145 | =head2 template_ext | ||
| 146 | |||
| 147 | Getter for template extensions. It is a copy of C<$Rails::Assets::TEMPLATE_EXT> reference | ||
| 148 | |||
| 149 | my $template_extensions = [qw(.haml .erb)]; | ||
| 150 | is_deeply( $Rails::Assets::TEMPLATE_EXT, $template_extensions); | ||
| 151 | is_deeply( $assets->template_ext(), $template_extensions); | ||
| 152 | |||
| 153 | =head2 assets_dir | ||
| 154 | |||
| 155 | Getter for assets directions. It is a copy of C<$Rails::Assets::ASSETS_DIR> reference | ||
| 156 | |||
| 157 | my $assets_directories = [qw( app/assets/ public/ vendor/assets/ )]; | ||
| 158 | is_deeply( $Rails::Assets::ASSETS_DIR, $assets_directories); | ||
| 159 | is_deeply( $assets->assets_dir(), $assets_directories); | ||
| 160 | |||
| 161 | =head2 assets_ext | ||
| 162 | |||
| 163 | Getter for assets extensions. It is a copy of C<$Rails::Assets::ASSETS_EXT> reference | ||
| 164 | |||
| 165 | my $assets_extensions = { | ||
| 166 | fonts => [qw(.woff2 .woff .ttf .eot .otf)], | ||
| 167 | images => [qw(.png .jpg .gif .svg .ico)], | ||
| 168 | javascripts => [qw(.js .map)], | ||
| 169 | stylesheets => [qw(.css .scss)], | ||
| 170 | }; | ||
| 171 | is_deeply( $Rails::Assets::ASSETS_EXT, $assets_extensions); | ||
| 172 | is_deeply( $assets->assets_ext(), $assets_extensions); | ||
| 173 | |||
| 174 | =head2 assets_hash | ||
| 175 | |||
| 176 | C<undef> by default. Hash reference initialized by C<$assets->analyse()> with keys fonts, images, javascripts, stylesheets | ||
| 177 | |||
| 178 | =head2 template_hash | ||
| 179 | |||
| 180 | see L<assets_hash|"#assets_hash"> | ||
| 181 | |||
| 182 | =head2 scss_hash | ||
| 183 | |||
| 184 | see L<assets_hash|"#assets_hash"> | ||
| 185 | |||
| 186 | =head2 map_hash | ||
| 187 | |||
| 188 | see L<assets_hash|"#assets_hash"> | ||
| 189 | |||
| 190 | =head2 assets_paths | ||
| 191 | |||
| 192 | C<undef> by default. Array reference containing C<$Rails::Assets::ASSETS_DIR> and their subfolders named as assets_hash keys | ||
| 193 | |||
| 194 | my $expected_paths = [qw( | ||
| 195 | app/assets/fonts/ app/assets/javascripts/ app/assets/stylesheets/ app/assets/ public/ | ||
| 196 | vendor/assets/fonts/ vendor/assets/javascripts/ vendor/assets/stylesheets/ vendor/assets/ | ||
| 197 | )]; | ||
| 198 | is_deeply($assets->assets_paths(), $expected_paths); | ||
| 199 | |||
| 200 | =head2 reversed_ext | ||
| 201 | |||
| 202 | C<undef> by default. Hash reference created reversing key value of C<$assets_hash> | ||
| 203 | |||
| 204 | is_deeply( | ||
| 205 | [sort keys %{$assets->reversed_ext()}], | ||
| 206 | [sort map {@$_} values %{$assets->assets_ext()}] | ||
| 207 | ); | ||
| 208 | |||
| 209 | =head2 analyse | ||
| 210 | |||
| 211 | Method that perform analysis. It populate all the references above | ||
| 212 | |||
| 213 | =head1 AUTHOR | ||
| 214 | |||
| 215 | Mauro Berlanda, C<< <kupta at cpan.org> >> | ||
| 216 | |||
| 217 | =head1 BUGS | ||
| 218 | |||
| 219 | Please report any bugs or feature requests to C<bug-rails-assets at rt.cpan.org>, or through | ||
| 220 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rails-Assets>. I will be notified, and then you'll | ||
| 221 | automatically be notified of progress on your bug as I make changes. | ||
| 222 | |||
| 223 | Pull Requests, Issues, Stars and Forks on the project L<github repository|https://github.com/mberlanda/rails-assets-coverage> are welcome! | ||
| 224 | |||
| 225 | =head1 SUPPORT | ||
| 226 | |||
| 227 | You can find documentation for this module with the perldoc command. | ||
| 228 | |||
| 229 | perldoc Rails::Assets | ||
| 230 | |||
| 231 | You can also look for information at: | ||
| 232 | |||
| 233 | =over 4 | ||
| 234 | |||
| 235 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 236 | |||
| 237 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rails-Assets> | ||
| 238 | |||
| 239 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 240 | |||
| 241 | L<http://annocpan.org/dist/Rails-Assets> | ||
| 242 | |||
| 243 | =item * CPAN Ratings | ||
| 244 | |||
| 245 | L<http://cpanratings.perl.org/d/Rails-Assets> | ||
| 246 | |||
| 247 | =item * Search CPAN | ||
| 248 | |||
| 249 | L<http://search.cpan.org/dist/Rails-Assets/> | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =head1 ACKNOWLEDGEMENTS | ||
| 254 | |||
| 255 | =head1 LICENSE AND COPYRIGHT | ||
| 256 | |||
| 257 | Copyright 2017 Mauro Berlanda. | ||
| 258 | |||
| 259 | This program is free software; you can redistribute it and/or modify it | ||
| 260 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 261 | copy of the full license at: | ||
| 262 | |||
| 263 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 264 | |||
| 265 | Any use, modification, and distribution of the Standard or Modified | ||
| 266 | Versions is governed by this Artistic License. By using, modifying or | ||
| 267 | distributing the Package, you accept this license. Do not use, modify, | ||
| 268 | or distribute the Package, if you do not accept this license. | ||
| 269 | |||
| 270 | If your Modified Version has been derived from a Modified Version made | ||
| 271 | by someone other than you, you are nevertheless required to ensure that | ||
| 272 | your Modified Version complies with the requirements of this license. | ||
| 273 | |||
| 274 | This license does not grant you the right to use any trademark, service | ||
| 275 | mark, tradename, or logo of the Copyright Holder. | ||
| 276 | |||
| 277 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 278 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 279 | otherwise transfer the Package with respect to any patent claims | ||
| 280 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 281 | Package. If you institute patent litigation (including a cross-claim or | ||
| 282 | counterclaim) against any party alleging that the Package constitutes | ||
| 283 | direct or contributory patent infringement, then this Artistic License | ||
| 284 | to you shall terminate on the date that such litigation is filed. | ||
| 285 | |||
| 286 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 287 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 288 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 289 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 290 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 291 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 292 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 293 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | 3 | 1; # End of Rails::Assets | |