blib/lib/Langertha/Raider/Config/Migrate.pm

filter: Statementstmt 96.1% 100 / 104 Branchbran 86.1% 31 / 36 Conditioncond 66.6% 14 / 21 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 20 / 20 Podpod 42.8% 3 / 7 totaltotal 87.5% 175 / 200 CC 27 SCAR 20.9
CC
27
Cov
90%
plan
17.2
apply
6.3
_apply_step
4.0
CRAP 8.1
« blib/lib/Langertha/Raider/Session/Journal.pm ↑ Summary blib/lib/Langertha/Raider/Hall/Protocol.pm »
1 package Langertha::Raider::Config::Migrate;
2 # ABSTRACT: Internal converter of the legacy project files to .raider/ (raider config migrate)
3 55 our $VERSION = '0.503';
4 55 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use Langertha::Raider::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use Langertha::Raider::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use Langertha::Raider::Instructions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 55 ▶use Langertha::Raider::SessionStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 55 has root => (
15 is => 'ro',
16 isa => 'Str',
17 required => 1,
18 );
19  
20 20 ▶sub config_class { 'Langertha::Raider::Config' }
Subroutine config_class: called CC=1 SCAR=0.0
Pod undocumented
21 36 ▶sub home_class { 'Langertha::Raider::Home' }
Subroutine home_class: called CC=1 SCAR=0.0
Pod undocumented
22 19 ▶sub instructions_class { 'Langertha::Raider::Instructions' }
Subroutine instructions_class: called CC=1 SCAR=0.0
Pod undocumented
23 9 ▶sub session_store_class { 'Langertha::Raider::SessionStore' }
Subroutine session_store_class: called CC=1 SCAR=0.0
Pod undocumented
24  
25  
26 55 has config => (
27 is => 'ro',
28 lazy_build => 1,
29 );
30  
31 20 ▶sub _build_config { $_[0]->config_class->new(root => $_[0]->root) }
Subroutine _build_config: called CC=1 SCAR=0.0
32  
33 55 has instructions => (
34 is => 'ro',
35 lazy_build => 1,
36 );
37  
38 19 ▶sub _build_instructions { $_[0]->instructions_class->new(root => $_[0]->root) }
Subroutine _build_instructions: called CC=1 SCAR=0.0
39  
40  
41 24 ▶sub backup_suffix { '.bak' }
Subroutine backup_suffix: called CC=1 SCAR=0.0
Pod documented
42  
43  
44 20 ▶sub plan {
Subroutine plan: called CC=17 SCAR=28.4
Pod documented
45 20 my ( $self ) = @_;
46 20 my $root = path($self->root)->absolute;
47 20 my ( @steps, @refused );
48 20 my $config = $self->config;
49 20 ▶ if (-f $config->legacy_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $config->legacy_file) 16 4 20
50 16 my $m = $config->migration_content;
51 15 my $bytes = $m->{text};
52 15 utf8::encode($bytes);
53 push @steps, $self->_step(config => $config->legacy_file, $config->native_file, $bytes,
54 13 changed => ( $m->{rewritten} || @{ $m->{removed_lines} } ) ? 1 : 0,
55 api_keys => $m->{api_keys},
56 removed_lines => $m->{removed_lines},
57 rewritten => $m->{rewritten},
58 15 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$$m{'rewritten'} || @{$$m{'removed_lines'};} ? : 4 11 15
Condition100%3/3
$$m{"rewritten"} or @{$$m{"removed_lines"};}
l!l&&r!l&&!r
2211
MC/DC100%2/2
$$m{"rewritten"} or @{$$m{"removed_lines"};}
$$m{"rewritten"} @{$$m{"removed_lines"};}
Truth table100%3/3
A or B
A= $$m{"rewritten"}B= @{$$m{"removed_lines"};}
ABresult
1X1
011
000
59 }
60 19 my $instructions = $self->instructions;
61 19 ▶ if (-f $instructions->legacy_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $instructions->legacy_file) 9 10 19
62 9 push @steps, $self->_step(instructions => $instructions->legacy_file, $instructions->native_file,
63 $instructions->legacy_file->slurp_raw, changed => 0, api_keys => [], removed_lines => [], rewritten => 0);
64 }
65  
66 19 my $base = $self->home_class->project_base($root);
67 19 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) 17 2 19
68 17 my $home = $self->home_class->home_dir;
69 17 ▶ push @refused, $root.' is the home directory: there .raider/config.yml is the home config of every'
Branch: 1 branch
BranchTrueFalseTotal
if defined $home and -d $home and -d $root and path($home)->realpath eq $root->realpath 1 16 17
Condition44%4/9
defined $home and -d $home and -d $root and path($home)->realpath eq $root->realpath
!ll&&!rl&&r
0161
defined $home and -d $home and -d $root
!ll&&!rl&&r
0017
defined $home and -d $home
!ll&&!rl&&r
0017
MC/DC25%1/4
defined $home and -d $home and -d $root and path($home)->realpath eq $root->realpath
defined $home -d $home -d $root path($home)->realpath eq $root->realpath
Truth table40%2/5
A and B and C and D
A= defined $homeB= -d $homeC= -d $rootD= path($home)->realpath eq $root->realpath
ABCDresult
0XXX0
10XX0
110X0
11100
11111
70 .' project, not this one\'s; move ~/.raider.yml and ~/.raider.md by hand if that is what you want'
71 if defined $home && -d $home && -d $root && path($home)->realpath eq $root->realpath;
72 17 ▶ push @refused, '.raider exists and is not a directory' if -e $base && !-d $base;
Branch: 1 branch
BranchTrueFalseTotal
if -e $base and not -d $base 1 16 17
Condition100%3/3
-e $base and not -d $base
!ll&&!rl&&r
1241
MC/DC100%2/2
-e $base and not -d $base
-e $base not -d $base
Truth table100%3/3
A and not(B)
A= -e $baseB= not -d $base
ABresult
0X0
100
111
73 }
74 19 for my $step (@steps) {
75 push @refused, $step->{to_label}.' already exists next to '.$step->{from_label}.': nothing is merged;'
76 .' move what you need into '.$step->{to_label}.' by hand and remove '.$step->{from_label}
77 .' (raider config explain shows which file is read)'
78 24 ▶ if -e $step->{to};
Branch: 1 branch
BranchTrueFalseTotal
if -e $$step{"to"} 3 21 24
79 push @refused, $step->{backup_label}.' already exists: move it away first, '
80 .$step->{from_label}.' is kept as '.$step->{backup_label}
81 24 ▶ if -e $step->{backup};
Branch: 1 branch
BranchTrueFalseTotal
if -e $$step{"backup"} 1 23 24
82 }
83 return {
84 19 ▶ root => $root->stringify,
Branch: 1 branch
BranchTrueFalseTotal
@steps && !(-e $base->child('.gitignore')) ? : 16 3 19
Condition100%3/3
@steps and not -e $base->child(".gitignore")
!ll&&!rl&&r
2116
MC/DC100%2/2
@steps and not -e $base->child(".gitignore")
@steps not -e $base->child(".gitignore")
Truth table100%3/3
A and not(B)
A= @stepsB= not -e $base->child(".gitignore")
ABresult
0X0
100
111
85 steps => \@steps,
86 refused => \@refused,
87 gitignore => ( @steps && !-e $base->child('.gitignore') ) ? 1 : 0,
88 };
89 }
90  
91 24 ▶sub _step {
Subroutine _step: called CC=3 SCAR=12.0
92 24 my ( $self, $kind, $from, $to, $bytes, %info ) = @_;
93 24 my $root = path($self->root)->absolute;
94 24 my $backup = path($from.$self->backup_suffix);
95 return {
96 24 ▶ kind => $kind,
Branch: 1 branch
BranchTrueFalseTotal
length $bytes && !($bytes =~ /\n\z/) ? : 0 24 24
Condition33%1/3
length $bytes and not $bytes =~ /\n\z/
!ll&&!rl&&r
0240
MC/DC0%0/2
length $bytes and not $bytes =~ /\n\z/
length $bytes not $bytes =~ /\n\z/
Truth table33%1/3
A and not(B)
A= length $bytesB= not $bytes =~ /\n\z/
ABresult
0X0
100
111
97 from => $from,
98 to => $to,
99 backup => $backup,
100 from_label => $from->relative($root)->stringify,
101 to_label => $to->relative($root)->stringify,
102 backup_label => $backup->relative($root)->stringify,
103 bytes => $bytes,
104 lines => scalar( () = $bytes =~ /\n/g ) + ( length $bytes && $bytes !~ /\n\z/ ? 1 : 0 ),
105 %info,
106 };
107 }
108  
109  
110 9 ▶sub apply {
Subroutine apply: called CC=6 SCAR=18.4
Pod documented
111 9 my ( $self, $plan ) = @_;
112 0 croak __PACKAGE__.'->apply: the migration was refused: '.join('; ', @{ $plan->{refused} })
113 9 ▶ if @{ $plan->{refused} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$$plan{"refused"};} 0 9 9
114 9 my @steps = @{ $plan->{steps} };
115 9 ▶ return unless @steps;
Branch: 1 branch
BranchTrueFalseTotal
unless @steps 0 9 9
116 9 my @results;
117 9 ▶ unless (eval { $self->session_store_class->new(root => $self->root)->prepare_base; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->session_store_class->new("root", $self->root)->prepare_base; 1 } }) 0 9 9
118 0 my $error = $@;
119 0 return ( { step => shift(@steps), error => $error }, map { { step => $_, skipped => 1 } } @steps );
120 }
121 9 ▶ while (my $step = shift @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = shift @steps) 12 6 18
122 12 ▶ if (eval { $self->_apply_step($step); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->_apply_step($step); 1 } }) 9 3 12
123 9 push @results, { step => $step };
124 9 next;
125 }
126 3 push @results, { step => $step, error => $@ }, map { { step => $_, skipped => 1 } } @steps;
127 3 last;
128 }
129 9 return @results;
130 }
131  
132 # One step: the new file through a temporary file next to it, then the
133 # legacy file renamed to its backup. When that rename fails, the new file
134 # goes again, so the legacy file stays the one in use.
135 12 ▶sub _apply_step {
Subroutine _apply_step: called CC=4 SCAR=13.9
136 12 my ( $self, $step ) = @_;
137 # Checked again: a rename would replace a file that appeared since plan.
138 12 ▶ croak $step->{to_label}.' already exists' if -e $step->{to};
Branch: 1 branch
BranchTrueFalseTotal
if -e $$step{"to"} 1 11 12
139 11 ▶ croak $step->{backup_label}.' already exists' if -e $step->{backup};
Branch: 1 branch
BranchTrueFalseTotal
if -e $$step{"backup"} 0 11 11
140 11 my $tmp = $step->{to}->parent->tempfile('.'.$step->{to}->basename.'.XXXXXX');
141 11 $tmp->spew_raw($step->{bytes});
142 11 chmod( (stat $step->{from})[2] & 07777, $tmp );
143 11 $self->_move($tmp, $step->{to});
144 10 ▶ unless (eval { $self->_move($step->{from}, $step->{backup}); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_move($$step{"from"}, $$step{"backup"}); 1 } }) 1 9 10
145 1 my $error = $@;
146 1 $step->{to}->remove;
147 1 croak $error;
148 }
149 9 return;
150 }
151  
152 19 ▶sub _move {
Subroutine _move: called CC=1 SCAR=0.0
153 19 my ( $self, $from, $to ) = @_;
154 19 $from->move($to);
155 19 return;
156 }
157  
158 55 __PACKAGE__->meta->make_immutable;
159  
160 55 1;
161  
162 __END__
163  
164 =pod
165  
166 =encoding UTF-8
167  
168 =head1 NAME
169  
170 Langertha::Raider::Config::Migrate - Internal converter of the legacy project files to .raider/ (raider config migrate)
171  
172 =head1 VERSION
173  
174 version 0.503
175  
176 =head1 SYNOPSIS
177  
178 # Internal to Langertha-Raider -- no API promise.
179 my $migrate = Langertha::Raider::Config::Migrate->new(root => $root);
180 my $plan = $migrate->plan; # croaks when .raider.yml does not parse
181 die join "\n", @{ $plan->{refused} } if @{ $plan->{refused} };
182 for my $result ($migrate->apply($plan)) {
183 warn $result->{step}{from_label}.': '.$result->{error} if $result->{error};
184 }
185  
186 =head1 DESCRIPTION
187  
188 B<Internal module.> Its interface may change without notice.
189  
190 What C<raider config migrate> does (ADR 0011): the legacy project files of
191 L</root> move to the F<.raider/> layout,
192  
193 =over
194  
195 =item F<.raider.yml> to F<.raider/config.yml>, without any C<api_key>
196 (L<Langertha::Raider::Config/migration_content>): that file is meant to be
197 shared, and a project must not choose a secret;
198  
199 =item F<.raider.md> to F<.raider/instructions.md>, byte for byte.
200  
201 =back
202  
203 Each legacy file that exists is one step. A step writes the new file
204 atomically (a temporary file next to it, renamed into place, with the
205 legacy file's permissions), then renames the legacy file to its backup
206 (F<.raider.yml.bak>, F<.raider.md.bak>), so afterwards only the new file
207 is read and the effective config is the same (minus the C<api_key>). A
208 step that fails leaves its legacy file in use and no new file behind.
209 F<.raider/> is created through
210 L<Langertha::Raider::SessionStore/prepare_base>, which also writes its
211 F<.gitignore>.
212  
213 Nothing is merged: L</plan> refuses the whole migration, before anything
214 is written, when a new file or a backup already exists, when F<.raider>
215 is not a directory, or when L</root> is the home directory (there
216 F<.raider/config.yml> would be the home config of every project).
217  
218 =head2 root
219  
220 The project directory. Required.
221  
222 =head2 config
223  
224 The L<Langertha::Raider::Config> of L</root>.
225  
226 =head2 instructions
227  
228 The L<Langertha::Raider::Instructions> of L</root>.
229  
230 =head2 backup_suffix
231  
232 C<.bak>: the backup of F<.raider.yml> is F<.raider.yml.bak>.
233  
234 =head2 plan
235  
236 my $plan = $migrate->plan;
237 # { root => '/abs/project',
238 # steps => [ { kind => 'config', from => $path, to => $path, backup => $path,
239 # from_label => '.raider.yml', to_label => '.raider/config.yml',
240 # backup_label => '.raider.yml.bak', bytes => '...', lines => 12,
241 # changed => 1, api_keys => ['top'], removed_lines => [3],
242 # rewritten => 0 },
243 # { kind => 'instructions', ... } ],
244 # refused => [ 'reason', ... ],
245 # gitignore => 1 }
246  
247 What a migration of L</root> would do, without touching the disk:
248 C<steps> in order (the config first), C<refused> the reasons it cannot be
249 done (then nothing may be written), C<gitignore> whether
250 F<.raider/.gitignore> would be created. C<changed> is false when the new
251 file is the legacy file byte for byte. Croaks like
252 L<Langertha::Raider::Config/data> when F<.raider.yml> does not parse.
253  
254 =head2 apply
255  
256 my @results = $migrate->apply($plan);
257 # ( { step => $step }, { step => $step, error => '...' }, { step => $step, skipped => 1 } )
258  
259 Carries out a L</plan>: creates F<.raider/> and its F<.gitignore>, then
260 the steps in order. Stops at the first step that fails; that step's
261 result carries C<error> (its legacy file is still in use, its new file is
262 not there), the steps after it C<skipped>. Croaks, writing nothing, when
263 the plan was refused.
264  
265 =head1 SEE ALSO
266  
267 =over
268  
269 =item * L<Langertha::Raider::Config>
270  
271 =item * L<Langertha::Raider::Instructions>
272  
273 =back
274  
275 =head1 SUPPORT
276  
277 =head2 Issues
278  
279 Please report bugs and feature requests on GitHub at
280 L<https://github.com/Getty/langertha-raider/issues>.
281  
282 =head2 IRC
283  
284 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
285  
286 =head1 CONTRIBUTING
287  
288 Contributions are welcome! Please fork the repository and submit a pull request.
289  
290 =head1 AUTHOR
291  
292 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
293  
294 =head1 COPYRIGHT AND LICENSE
295  
296 This software is copyright (c) 2026 by Torsten Raudssus.
297  
298 This is free software; you can redistribute it and/or modify it under
299 the same terms as the Perl 5 programming language system itself.
300  
301 =cut
« blib/lib/Langertha/Raider/Session/Journal.pm ↑ Summary blib/lib/Langertha/Raider/Hall/Protocol.pm »