lib/Mojolicious/Plugin/Fondation/Asset/Command/asset.pm

filter: Statementstmt 91.0% 81 / 89 Branchbran 63.1% 24 / 38 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 74.6% 115 / 154 CC 30 SCAR 36.0
CC
30
Cov
79%
run
40.0
__ANON__
2.0
BEGIN
1.0
CRAP 36.5
↑ Summary blib/lib/Mojolicious/Plugin/Fondation/Asset.pm »
1 package Mojolicious::Plugin::Fondation::Asset::Command::asset;
2 2 $Mojolicious::Plugin::Fondation::Asset::Command::asset::VERSION = '0.04';
3 # ABSTRACT: Generate merged assetpack.def and process assets through AssetPack
4  
5 2 ▶use Mojo::Base 'Mojolicious::Command', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 has description => 'Generate and process asset bundles';
10 0 ▶has usage => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
11 0 <<"USAGE";
12 Usage: APPLICATION asset generate [OPTIONS]
13  
14 myapp.pl asset generate Generate assets/assetpack.def and process
15 myapp.pl asset generate -y Force overwrite without prompt
16  
17 USAGE
18 2 };
19  
20 8 ▶sub run ($self, @args) {
Subroutine run: called CC=30 SCAR=36.9
Pod documented
21 8 my $app = $self->app;
22  
23 8 ▶ my $config = $app->defaults->{'asset.config'}
Branch: 1 branch
BranchTrueFalseTotal
my $config = $app->defaults->{"asset.config"} or die "Asset not configured. Add Fondation::Asset to your config.\n" 8 0 8
24 or die "Asset not configured. Add Fondation::Asset to your config.\n";
25  
26 8 ▶ my $asset_dir = $config->{asset_dir} // 'share/assets';
Condition50%1/2
$config->{"asset_dir"} // "share/assets"
l!l
08
MC/DC0%0/1
$config->{"asset_dir"} // "share/assets"
$config->{"asset_dir"}
Truth table50%1/2
A
A= $config->{"asset_dir"}
Aresult
11
00
27  
28 8 ▶ my $subcommand = shift @args || '';
Condition50%1/2
shift @args || ''
l!l
80
MC/DC0%0/1
shift @args || ''
shift @args
Truth table50%1/2
A
A= shift @args
Aresult
11
00
29  
30 8 ▶ die $self->usage unless $subcommand eq 'generate';
Branch: 1 branch
BranchTrueFalseTotal
unless $subcommand eq "generate" 0 8 8
31  
32 8 my $force = 0;
33  
34 8 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 8 8 16
35 8 my $arg = shift @args;
36 8 ▶ if ($arg eq '-y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq '-y') { } 8 0 8
37 8 $force = 1;
38 }
39 else {
40 0 die "Unknown option: $arg\n" . $self->usage;
41 }
42 }
43  
44 8 my $def_path = $app->home->child($asset_dir, 'assetpack.def');
45  
46 # Overwrite check
47 8 ▶ if (!$force && -f $def_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $force and -f $def_path) 0 8 8
Condition33%1/3
not $force and -f $def_path
!ll&&!rl&&r
800
MC/DC0%0/2
not $force and -f $def_path
not $force -f $def_path
Truth table33%1/3
not(A) and B
A= not $forceB= -f $def_path
ABresult
0X0
100
111
48 0 print "File '$asset_dir/assetpack.def' already exists. Overwrite? [y/N] ";
49 0 my $answer = <STDIN>;
50 0 chomp $answer;
51 0 ▶ exit(0) unless $answer =~ /^y(es)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer =~ /^y(es)?$/iu 0 0 0
52 }
53  
54 # ── Phase 1: Scan plugins and merge defs ──
55  
56 8 my %bundles;
57 my %seen;
58 8 my @public_dirs;
59  
60 8 $app->log->debug("Starting asset merge from all plugins");
61  
62 8 for my $long (@{ $app->manager->load_order }) {
63 23 my $entry = $app->manager->registry->{$long};
64 23 my $short = $entry->{short_name};
65  
66 23 ▶ next unless $entry->{share_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"share_dir"} 0 23 23
67  
68 23 my $share_dir = $entry->{share_dir};
69  
70 # Collect public dirs
71 23 my $public_dir = $entry->{public_dir};
72 23 ▶ if ($public_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($public_dir) 15 8 23
73 15 push @public_dirs, $public_dir->to_string;
74 }
75  
76 # Look for assetpack.def
77 23 my $def_file = $share_dir->child('assets', 'assetpack.def');
78 23 ▶ next unless -f $def_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $def_file 8 15 23
79  
80 15 $app->log->debug("$short Found assetpack.def");
81  
82 15 ▶ open my $fh, '<', $def_file or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $def_file or next 15 0 15
83 15 my $current_bundle;
84  
85 15 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 30 15 45
86 30 chomp $line;
87 30 ▶ next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/u or $line =~ /^\s*#/u 0 30 30
Condition33%1/3
$line =~ /^\s*$/u or $line =~ /^\s*#/u
l!l&&r!l&&!r
0030
MC/DC0%0/2
$line =~ /^\s*$/u or $line =~ /^\s*#/u
$line =~ /^\s*$/u $line =~ /^\s*#/u
Truth table33%1/3
A or B
A= $line =~ /^\s*$/uB= $line =~ /^\s*#/u
ABresult
1X1
011
000
88  
89 30 ▶ if ($line =~ /^\!\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\!\s*(\S+)/u) 15 15 30
90 15 $current_bundle = $1;
91 15 ▶ $bundles{$current_bundle} ||= [];
Condition50%1/2
$bundles{$current_bundle} ||= []
l!l
015
MC/DC0%0/1
$bundles{$current_bundle} ||= []
$bundles{$current_bundle}
Truth table50%1/2
A
A= $bundles{$current_bundle}
Aresult
11
00
92 15 next;
93 }
94  
95 15 ▶ if ($line =~ /^\s*(<<?)\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(<<?)\s*(.+)$/u) 15 0 15
96 15 my ($op, $asset_path) = ($1, $2);
97 15 my $key = "$current_bundle|$op|$asset_path";
98  
99 15 ▶ next if $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 0 15 15
100  
101 # Normalize remote assets to single "<" so AssetPack renders them in dev mode
102 15 ▶ $op = '<' if $asset_path =~ m{^https?://};
Branch: 1 branch
BranchTrueFalseTotal
if $asset_path =~ m[^https?://]u 0 15 15
103  
104 15 push @{ $bundles{$current_bundle} }, "$op $asset_path";
105 }
106 }
107 15 close $fh;
108 }
109  
110 # ── Phase 2: Write merged assetpack.def ──
111  
112 8 my $merged_dsl = '';
113 8 for my $bundle (sort keys %bundles) {
114 15 ▶ next unless @{ $bundles{$bundle} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$bundles{$bundle};} 0 15 15
115  
116 15 $merged_dsl .= "! $bundle\n";
117 15 for my $line (@{ $bundles{$bundle} }) {
118 15 $merged_dsl .= "$line\n";
119 }
120 15 $merged_dsl .= "\n";
121 }
122  
123 8 ▶ if ($merged_dsl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merged_dsl) { } 8 0 8
124 8 my $assets_dir = $app->home->child($asset_dir);
125 8 ▶ $assets_dir->make_path unless -d $assets_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $assets_dir 7 1 8
126  
127 8 $def_path->spurt($merged_dsl);
128  
129 8 my $count = scalar keys %bundles;
130 8 $app->log->debug("Merged assetpack.def written ($count bundles)");
131 }
132 else {
133 0 say "No asset definitions found. Nothing written.";
134 0 return;
135 }
136  
137 # ── Phase 3: Get or create AssetPack instance ──
138  
139 8 my $asset;
140  
141 8 ▶ my @pipes = split /\s*,\s*/, ($config->{pipes} // 'Fetch,Sass,Css,Combine');
Condition50%1/2
$config->{"pipes"} // "Fetch,Sass,Css,Combine"
l!l
08
MC/DC0%0/1
$config->{"pipes"} // "Fetch,Sass,Css,Combine"
$config->{"pipes"}
Truth table50%1/2
A
A= $config->{"pipes"}
Aresult
11
00
142 8 $app->plugin('AssetPack' => {
143 pipes => \@pipes,
144 });
145 8 $asset = $app->asset;
146  
147 # Point the store to the configured directory so it finds the def and writes there
148 8 $asset->store->paths([$app->home->child($asset_dir)]);
149  
150 # Push store paths for asset resolution
151 8 push @{ $asset->store->paths }, $app->home->child('public');
152 8 push @{ $asset->store->paths }, $_ for @public_dirs;
153  
154 # ── Phase 4: Re-process with updated definitions ──
155  
156 8 say "Processing assets...";
157 8 $asset->process();
158  
159 8 say "Assets generated successfully.";
160 }
161  
162 2 1;
163  
164 __END__
↑ Summary blib/lib/Mojolicious/Plugin/Fondation/Asset.pm »