| 1 | package Mojolicious::Plugin::Fondation::Setup::Controller::Setup; | ||
| 2 | $Mojolicious::Plugin::Fondation::Setup::Controller::Setup::VERSION = '0.11'; | ||
| 3 | # ABSTRACT: Session-based setup wizard — no Workflow, no file persister | ||
| 4 | |||
| 5 | use Mojo::Base 'Mojolicious::Controller', -signatures; | ||
| 6 | use version; | ||
| 7 | |||
| 8 | use Mojo::File 'path'; | ||
| 9 | use Mojo::Loader; | ||
| 10 | use Mojo::Util qw(encode); | ||
| 11 | use Mojolicious::Plugin::Fondation::Setup::MetaCPAN; | ||
| 12 | |||
| 13 | # ══════════════════════════════════════════════════════════════════════ | ||
| 14 | # Session keys used by this controller: | ||
| 15 | # | ||
| 16 | # setup_plugins => [$class, ...] selected plugin classes | ||
| 17 | # setup_index => $int current wizard step | ||
| 18 | # setup_context => {$key => $val, ...} accumulated form values | ||
| 19 | # setup_states => [{label, plugin, params}, ...] | ||
| 20 | # setup_not_installed => [$class, ...] | ||
| 21 | # setup_retry => [$class, ...] previous selection for retry | ||
| 22 | # | ||
| 23 | # Wizard indexes: | ||
| 24 | # 0 .. $#states → form for that plugin | ||
| 25 | # @states → review | ||
| 26 | # @states + 1 → done | ||
| 27 | # ══════════════════════════════════════════════════════════════════════ | ||
| 28 | |||
| 29 | # ── GET /setup/plugins ────────────────────────────────────────────── | ||
| 30 | |||
| 31 | sub plugins ($self) { | ||
| 32 | $self->render_later; | ||
| 33 | |||
| 34 | my %preselected = $self->_read_conf_plugins; | ||
| 35 | $preselected{$_} = 1 for @{ $self->session('setup_retry') // [] }; | ||
| 36 | |||
| 37 | $self->_discover(sub ($plugins) { | ||
| 38 | $_->{badges} = $self->_badges_for($_) for @$plugins; | ||
| 39 | $self->stash(plugins => $plugins, preselected => \%preselected); | ||
| 40 | $self->render(template => 'setup/plugins'); | ||
| 41 | }); | ||
| 42 | } | ||
| 43 | |||
| 44 | # ── GET /setup/discover (JSON API) ─────────────────────────────────── | ||
| 45 | |||
| 46 | sub discover ($self) { | ||
| 47 | $self->render_later; | ||
| 48 | $self->_discover(sub ($plugins) { | ||
| 49 | $_->{badges} = $self->_badges_for($_) for @$plugins; | ||
| 50 | $self->render(json => { plugins => $plugins }); | ||
| 51 | }); | ||
| 52 | } | ||
| 53 | |||
| 54 | # ── POST /setup/start ─────────────────────────────────────────────── | ||
| 55 | |||
| 56 | sub start ($self) { | ||
| 57 | my @selected = @{ $self->every_param('selected_plugins') // [] }; | ||
| 58 | return $self->reply->not_found unless @selected; | ||
| 59 | |||
| 60 | # Separate installed from not-installed | ||
| 61 | my (@installed, @not_installed); | ||
| 62 | for my $class (@selected) { | ||
| 63 | if ($self->_try_load($class)) { | ||
| 64 | push @installed, $class; | ||
| 65 | } else { | ||
| 66 | push @not_installed, $class; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | # Build one state per installed plugin that has setup parameters | ||
| 71 | my @states; | ||
| 72 | my %context; | ||
| 73 | my $conf_config = $self->_read_conf_for(@selected); | ||
| 74 | |||
| 75 | for my $class (@installed) { | ||
| 76 | my $meta = $self->_fondation_meta($class) or next; | ||
| 77 | my $setup = $meta->{setup} or next; | ||
| 78 | my $params = $setup->{parameters} // []; | ||
| 79 | |||
| 80 | my @fields; | ||
| 81 | my $plugin_conf = $conf_config->{$class}; | ||
| 82 | |||
| 83 | for my $p (@$params) { | ||
| 84 | my $key = $p->{key} or next; | ||
| 85 | my $val = $self->_resolve_config($plugin_conf, $key) // $p->{default}; | ||
| 86 | $context{$key} = $val; | ||
| 87 | |||
| 88 | push @fields, { | ||
| 89 | key => $key, | ||
| 90 | label => $p->{label} // $key, | ||
| 91 | type => $p->{type} // 'string', | ||
| 92 | default => $p->{default}, | ||
| 93 | current => $val, | ||
| 94 | required => $p->{required} ? 1 : 0, | ||
| 95 | placeholder => $p->{placeholder}, | ||
| 96 | options => $p->{options}, | ||
| 97 | min => $p->{min}, | ||
| 98 | max => $p->{max}, | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | next unless @fields; | ||
| 103 | |||
| 104 | (my $short = $class) =~ s/^Mojolicious::Plugin:://; | ||
| 105 | push @states, { | ||
| 106 | label => $setup->{label} // $short, | ||
| 107 | plugin => $short, | ||
| 108 | description => $setup->{description} // '', | ||
| 109 | fields => \@fields, | ||
| 110 | }; | ||
| 111 | } | ||
| 112 | |||
| 113 | # Store everything in session | ||
| 114 | $self->session( | ||
| 115 | setup_plugins => \@selected, | ||
| 116 | setup_states => \@states, | ||
| 117 | setup_context => \%context, | ||
| 118 | setup_index => 0, | ||
| 119 | setup_not_installed => \@not_installed, | ||
| 120 | setup_retry => \@selected, | ||
| 121 | ); | ||
| 122 | |||
| 123 | $self->redirect_to('setup_wizard'); | ||
| 124 | } | ||
| 125 | |||
| 126 | # ── GET /setup ────────────────────────────────────────────────────── | ||
| 127 | |||
| 128 | sub wizard ($self) { | ||
| 129 | $self->render_later; | ||
| 130 | |||
| 131 | my $plugins = $self->session('setup_plugins'); | ||
| 132 | my $states = $self->session('setup_states'); | ||
| 133 | my $index = $self->session('setup_index'); | ||
| 134 | my $context = $self->session('setup_context'); | ||
| 135 | my $missing = $self->session('setup_not_installed'); | ||
| 136 | |||
| 137 | return $self->redirect_to('setup_plugins') unless $plugins; | ||
| 138 | |||
| 139 | my $is_review = ($index == @$states); | ||
| 140 | my $is_done = ($index > @$states); | ||
| 141 | my $state = $is_done || $is_review ? undef : $states->[$index]; | ||
| 142 | |||
| 143 | $self->stash( | ||
| 144 | states => $states, | ||
| 145 | state_index => $index, | ||
| 146 | current_state => $state, | ||
| 147 | is_first => ($index == 0), | ||
| 148 | is_last => ($index == @$states - 1), | ||
| 149 | is_review => $is_review, | ||
| 150 | is_done => $is_done, | ||
| 151 | context => $context, | ||
| 152 | selected_plugins => $plugins, | ||
| 153 | not_installed => $missing, | ||
| 154 | conf_path => $context->{_conf_path}, | ||
| 155 | config_loaded => $INC{'Mojolicious/Plugin/Config.pm'} ? 1 : 0, | ||
| 156 | ); | ||
| 157 | |||
| 158 | # Async: fetch CPAN versions for upgrade/dependency checks | ||
| 159 | $self->_discover(sub ($all_plugins) { | ||
| 160 | my %cpan_ver; | ||
| 161 | my %cpan_deps; | ||
| 162 | for my $p (@$all_plugins) { | ||
| 163 | $cpan_ver{$p->{module_class}} = $p->{version}; | ||
| 164 | $cpan_deps{$p->{module_class}} = $p->{dependencies} // []; | ||
| 165 | } | ||
| 166 | |||
| 167 | my (@upgradable, @unresolvable); | ||
| 168 | my %selected = map { $_ => 1 } @$plugins; | ||
| 169 | my $dev_dir = $self->_dev_plugins_dir; | ||
| 170 | my $mc = Mojolicious::Plugin::Fondation::Setup::MetaCPAN->new; | ||
| 171 | |||
| 172 | for my $class (@$plugins) { | ||
| 173 | # --- upgradable --- | ||
| 174 | if (my $inst = $mc->installed_version($class)) { | ||
| 175 | my $cpan = $cpan_ver{$class}; | ||
| 176 | if ($cpan && version->parse($cpan) > version->parse($inst)) { | ||
| 177 | push @upgradable, { module_class => $class, installed => $inst, cpan => $cpan }; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | # --- dependencies --- | ||
| 182 | my $deps; | ||
| 183 | my $meta = $self->_fondation_meta($class); | ||
| 184 | if ($meta && $meta->{dependencies}) { | ||
| 185 | $deps = $meta->{dependencies}; | ||
| 186 | } else { | ||
| 187 | $deps = $cpan_deps{$class} // []; | ||
| 188 | } | ||
| 189 | |||
| 190 | for my $dep (@$deps) { | ||
| 191 | $dep = "Mojolicious::Plugin::$dep" unless $dep =~ /^Mojolicious::/; | ||
| 192 | next if $dep eq 'Mojolicious::Plugin::Fondation'; | ||
| 193 | next if $selected{$dep}; | ||
| 194 | next if $mc->installed_version($dep); | ||
| 195 | next if $cpan_ver{$dep}; | ||
| 196 | next if $dev_dir && $self->_try_load_dev($dep); | ||
| 197 | push @unresolvable, { plugin => $class, dependency => $dep }; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | # If anything blocks configuration (not installed, upgradable, | ||
| 202 | # or unresolvable deps), skip to review and show only the alerts. | ||
| 203 | my $review_entries; | ||
| 204 | my $can_configure = 1; | ||
| 205 | if (@$missing || @upgradable || @unresolvable) { | ||
| 206 | $self->session(setup_index => scalar @$states) if $index <= @$states; | ||
| 207 | $self->stash(is_review => 1, is_done => 0, current_state => undef) if $index <= @$states; | ||
| 208 | $review_entries = []; | ||
| 209 | $can_configure = 0; | ||
| 210 | } else { | ||
| 211 | $review_entries = $self->_review_entries($context); | ||
| 212 | } | ||
| 213 | |||
| 214 | $self->stash( | ||
| 215 | upgradable => \@upgradable, | ||
| 216 | unresolvable => \@unresolvable, | ||
| 217 | install_command => $self->_install_command(\@upgradable, $missing, \%cpan_ver), | ||
| 218 | review_entries => $review_entries, | ||
| 219 | can_configure => $can_configure, | ||
| 220 | ); | ||
| 221 | |||
| 222 | $self->render(template => 'setup/wizard'); | ||
| 223 | }); | ||
| 224 | } | ||
| 225 | |||
| 226 | # ── POST /setup/execute ───────────────────────────────────────────── | ||
| 227 | |||
| 228 | sub execute ($self) { | ||
| 229 | my $action = $self->param('action'); | ||
| 230 | my $states = $self->session('setup_states') // []; | ||
| 231 | my $index = $self->session('setup_index'); | ||
| 232 | my $context = $self->session('setup_context') // {}; | ||
| 233 | |||
| 234 | return $self->reply->not_found unless defined $action; | ||
| 235 | |||
| 236 | if ($action eq 'back') { | ||
| 237 | $self->session(setup_index => $index - 1); | ||
| 238 | } | ||
| 239 | elsif ($action eq 'next' || $action eq 'save') { | ||
| 240 | # Save form values | ||
| 241 | my $state = $states->[$index]; | ||
| 242 | if ($state && $state->{fields}) { | ||
| 243 | for my $f (@{$state->{fields}}) { | ||
| 244 | my $val = $self->param($f->{key}); | ||
| 245 | $context->{$f->{key}} = $val if defined $val; | ||
| 246 | } | ||
| 247 | $self->session(setup_context => $context); | ||
| 248 | } | ||
| 249 | |||
| 250 | if ($action eq 'save') { | ||
| 251 | my $path = $self->_generate_conf; | ||
| 252 | $context->{_conf_path} = $path; | ||
| 253 | $self->session( | ||
| 254 | setup_context => $context, | ||
| 255 | setup_index => scalar @$states + 1, | ||
| 256 | ); | ||
| 257 | } else { | ||
| 258 | $self->session(setup_index => $index + 1); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | $self->redirect_to('setup_wizard'); | ||
| 263 | } | ||
| 264 | |||
| 265 | # ── GET /setup/reset ──────────────────────────────────────────────── | ||
| 266 | |||
| 267 | sub reset ($self) { | ||
| 268 | $self->session( | ||
| 269 | setup_plugins => undef, | ||
| 270 | setup_states => undef, | ||
| 271 | setup_context => undef, | ||
| 272 | setup_index => undef, | ||
| 273 | setup_not_installed => undef, | ||
| 274 | setup_retry => undef, | ||
| 275 | ); | ||
| 276 | $self->redirect_to('setup_plugins'); | ||
| 277 | } | ||
| 278 | |||
| 279 | # ══════════════════════════════════════════════════════════════════════ | ||
| 280 | # INTERNAL HELPERS | ||
| 281 | # ══════════════════════════════════════════════════════════════════════ | ||
| 282 | |||
| 283 | # Shared discovery: MetaCPAN + dev plugins merge. | ||
| 284 | # Calls $cb with the merged plugin arrayref. | ||
| 285 | sub _discover ($self, $cb) { | ||
| 286 | my $mc = Mojolicious::Plugin::Fondation::Setup::MetaCPAN->new; | ||
| 287 | $mc->discover_p($self->app)->then(sub ($plugins) { | ||
| 288 | my $dev = $self->_dev_plugins; | ||
| 289 | |||
| 290 | my %dev_by_class = map { $_->{module_class} => $_ } @$dev; | ||
| 291 | for my $p (@$plugins) { | ||
| 292 | $p->{is_dev} = 0; | ||
| 293 | $p->{cpan_version} = $p->{version}; | ||
| 294 | if (my $d = $dev_by_class{$p->{module_class}}) { | ||
| 295 | $p->{is_dev} = 1; | ||
| 296 | $p->{installed} = 1; | ||
| 297 | $p->{installed_version} = $d->{installed_version}; | ||
| 298 | # Replace MetaCPAN dependencies with dev (dev is the source of truth) | ||
| 299 | $p->{dependencies} = $d->{dependencies}; | ||
| 300 | } | ||
| 301 | $p->{upgrade_available} = 0; | ||
| 302 | $p->{release_pending} = 0; | ||
| 303 | if ($p->{installed_version} && $p->{version}) { | ||
| 304 | my $local = version->parse($p->{installed_version}); | ||
| 305 | my $cpan = version->parse($p->{version}); | ||
| 306 | $p->{upgrade_available} = 1 if $cpan > $local; | ||
| 307 | $p->{release_pending} = 1 if $local > $cpan; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | my %seen_cpan = map { $_->{module_class} => 1 } @$plugins; | ||
| 312 | for my $d (@$dev) { | ||
| 313 | next if $seen_cpan{$d->{module_class}}; | ||
| 314 | push @$plugins, $d; | ||
| 315 | } | ||
| 316 | |||
| 317 | $cb->($plugins); | ||
| 318 | })->catch(sub ($err) { | ||
| 319 | my $dev = $self->_dev_plugins; | ||
| 320 | if (@$dev) { | ||
| 321 | $_->{is_dev} = 1 for @$dev; | ||
| 322 | $cb->($dev); | ||
| 323 | } else { | ||
| 324 | $self->stash(error => "MetaCPAN unavailable: $err"); | ||
| 325 | $cb->([]); | ||
| 326 | } | ||
| 327 | }); | ||
| 328 | } | ||
| 329 | |||
| 330 | # Parse $moniker.conf and return { $full_class => 1 } for already-configured plugins. | ||
| 331 | sub _read_conf_plugins ($self) { | ||
| 332 | my %selected; | ||
| 333 | my $conf_path = $self->app->home->child($self->app->moniker . '.conf'); | ||
| 334 | return %selected unless -f $conf_path; | ||
| 335 | |||
| 336 | my $conf = do($conf_path->to_string); | ||
| 337 | return %selected unless $conf && $conf->{Fondation} && $conf->{Fondation}{dependencies}; | ||
| 338 | |||
| 339 | for my $dep (@{$conf->{Fondation}{dependencies}}) { | ||
| 340 | my $name = ref $dep ? (keys %$dep)[0] : $dep; | ||
| 341 | $name = "Mojolicious::Plugin::$name" unless $name =~ /^Mojolicious::/; | ||
| 342 | $selected{$name} = 1; | ||
| 343 | } | ||
| 344 | return %selected; | ||
| 345 | } | ||
| 346 | |||
| 347 | # Parse $moniker.conf and return { $full_class => $config_hash } for selected plugins. | ||
| 348 | sub _read_conf_for ($self, @classes) { | ||
| 349 | my %plugin_config; | ||
| 350 | my $conf_path = $self->app->home->child($self->app->moniker . '.conf'); | ||
| 351 | return \%plugin_config unless -f $conf_path; | ||
| 352 | |||
| 353 | my $conf = do($conf_path->to_string); | ||
| 354 | return \%plugin_config unless $conf; | ||
| 355 | |||
| 356 | for my $class (@classes) { | ||
| 357 | (my $short = $class) =~ s/^Mojolicious::Plugin:://; | ||
| 358 | my $cfg = $conf->{$short}; | ||
| 359 | $plugin_config{$class} = $cfg if $cfg && ref $cfg eq 'HASH'; | ||
| 360 | } | ||
| 361 | return \%plugin_config; | ||
| 362 | } | ||
| 363 | |||
| 364 | # Resolve a dotted key in a config hash. Supports array notation (+prefix). | ||
| 365 | sub _resolve_config ($self, $config, $key_path) { | ||
| 366 | return undef unless $config; | ||
| 367 | $key_path =~ s/^\+//; | ||
| 368 | my @parts = split /\./, $key_path; | ||
| 369 | my $cur = $config; | ||
| 370 | |||
| 371 | for my $part (@parts) { | ||
| 372 | if (ref $cur eq 'HASH') { | ||
| 373 | return undef unless exists $cur->{$part}; | ||
| 374 | $cur = $cur->{$part}; | ||
| 375 | } | ||
| 376 | elsif (ref $cur eq 'ARRAY') { | ||
| 377 | my $found = 0; | ||
| 378 | for (my $i = 0; $i < @$cur; $i += 2) { | ||
| 379 | if ($cur->[$i] eq $part) { | ||
| 380 | $cur = $cur->[$i + 1]; | ||
| 381 | $found = 1; | ||
| 382 | last; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | return undef unless $found; | ||
| 386 | } | ||
| 387 | else { return undef } | ||
| 388 | } | ||
| 389 | return $cur unless ref $cur; | ||
| 390 | return undef; | ||
| 391 | } | ||
| 392 | |||
| 393 | # Load a class safely. Returns true on success. | ||
| 394 | sub _try_load ($self, $class) { | ||
| 395 | my $pm = $class =~ s{::}{/}gr . '.pm'; | ||
| 396 | return 1 if $INC{$pm}; | ||
| 397 | return 1 if eval "require $class; 1"; | ||
| 398 | |||
| 399 | # Try dev_plugins_dir | ||
| 400 | if ($self->_try_load_dev($class)) { | ||
| 401 | return 1 if eval "require $class; 1"; | ||
| 402 | } | ||
| 403 | return 0; | ||
| 404 | } | ||
| 405 | |||
| 406 | # Load a class from dev_plugins_dir by adding its lib/ to @INC. | ||
| 407 | sub _try_load_dev ($self, $class) { | ||
| 408 | my $dev_dir = $self->_dev_plugins_dir or return 0; | ||
| 409 | (my $rel = "$class.pm") =~ s{::}{/}g; | ||
| 410 | my @found = glob("$dev_dir/Mojolicious-Plugin-Fondation-*/lib/$rel"); | ||
| 411 | return 0 unless @found && -f $found[0]; | ||
| 412 | |||
| 413 | (my $lib_dir = $found[0]) =~ s{/lib/.*}{/lib}; | ||
| 414 | return 0 unless -d $lib_dir; | ||
| 415 | unshift @INC, $lib_dir; | ||
| 416 | return 1; | ||
| 417 | } | ||
| 418 | |||
| 419 | # Call fondation_meta() on a class. Returns hashref or undef. | ||
| 420 | sub _fondation_meta ($self, $class) { | ||
| 421 | eval { | ||
| 422 | my $err = Mojo::Loader::load_class($class); | ||
| 423 | die $err if $err; | ||
| 424 | $class->can('fondation_meta') ? $class->fondation_meta : undef; | ||
| 425 | } // undef; | ||
| 426 | } | ||
| 427 | |||
| 428 | # Scan dev_plugins_dir for locally-developed Fondation plugins. | ||
| 429 | # | ||
| 430 | # Expects directories named Mojolicious-Plugin-Fondation-* under | ||
| 431 | # $app->manager->config->{dev_plugins_dir}. Each directory contains | ||
| 432 | # a standard Dist::Zilla layout (lib/, dist.ini). | ||
| 433 | # | ||
| 434 | # For each plugin found, this method: | ||
| 435 | # 1. Derives the module class from the directory name | ||
| 436 | # Mojolicious-Plugin-Fondation-Foo-Bar → Mojolicious::Plugin::Fondation::Foo::Bar | ||
| 437 | # 2. Reads the .pm source file (without executing it — no require) | ||
| 438 | # 3. Extracts $VERSION (from source, fallback to dist.ini) | ||
| 439 | # 4. Extracts # ABSTRACT: comment | ||
| 440 | # | ||
| 441 | # Returns an arrayref of plugin hashes with the same shape as | ||
| 442 | # MetaCPAN::discover_p, all marked installed=1, is_dev=1. | ||
| 443 | sub _dev_plugins ($self) { | ||
| 444 | my $dev_dir = $self->_dev_plugins_dir or return []; | ||
| 445 | my @plugins; | ||
| 446 | |||
| 447 | # Scan each Mojolicious-Plugin-Fondation-* directory | ||
| 448 | for my $dir (glob("$dev_dir/Mojolicious-Plugin-Fondation-*")) { | ||
| 449 | next unless -d $dir; | ||
| 450 | |||
| 451 | # Extract distribution name from path (e.g. "Mojolicious-Plugin-Fondation-Asset") | ||
| 452 | (my $dist = $dir) =~ s{.*/}{}; | ||
| 453 | |||
| 454 | # Derive Perl module class: Foo-Bar → Mojolicious::Plugin::Fondation::Foo::Bar | ||
| 455 | (my $module_class = $dist) =~ s/^Mojolicious-Plugin-//; | ||
| 456 | $module_class = "Mojolicious::Plugin::$module_class"; | ||
| 457 | $module_class =~ s/-/::/g; | ||
| 458 | |||
| 459 | my ($abstract, $version) = ('', ''); | ||
| 460 | |||
| 461 | # Locate the .pm file (e.g. lib/Mojolicious/Plugin/Fondation/Foo/Bar.pm) | ||
| 462 | (my $pm_rel = "$module_class.pm") =~ s{::}{/}g; | ||
| 463 | my $pm_path = "$dir/lib/$pm_rel"; | ||
| 464 | |||
| 465 | my $deps = []; | ||
| 466 | |||
| 467 | if (-f $pm_path) { | ||
| 468 | # Read source WITHOUT requiring it — avoids triggering register() | ||
| 469 | open my $fh, '<', $pm_path or next; | ||
| 470 | my $src = do { local $/; <$fh> }; | ||
| 471 | close $fh; | ||
| 472 | |||
| 473 | # Extract $VERSION from source: $VERSION = '0.02'; | ||
| 474 | if ($src =~ /\$VERSION\s*=\s*['"]?([^'";]+)/) { | ||
| 475 | $version = $1; | ||
| 476 | } | ||
| 477 | # Fallback: read version from dist.ini | ||
| 478 | else { | ||
| 479 | my $ini = "$dir/dist.ini"; | ||
| 480 | if (-f $ini) { | ||
| 481 | open my $fh2, '<', $ini; | ||
| 482 | while (my $line = <$fh2>) { | ||
| 483 | if ($line =~ /^version\s*=\s*(\S+)/) { $version = $1; last } | ||
| 484 | } | ||
| 485 | close $fh2; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | # Extract ABSTRACT comment: # ABSTRACT: Foo bar | ||
| 490 | if ($src =~ /^\s*#\s*ABSTRACT:\s*(.+)$/m) { $abstract = $1 } | ||
| 491 | |||
| 492 | # Extract dependencies from fondation_meta in source | ||
| 493 | # (brace-counted eval, safe — no require) | ||
| 494 | $deps = []; | ||
| 495 | if ($src =~ /(sub\s+fondation_meta\s*\{)/) { | ||
| 496 | my $pos = $-[0] + length($1); | ||
| 497 | my $depth = 1; | ||
| 498 | my $end = $pos; | ||
| 499 | while ($depth > 0 && $end < length($src)) { | ||
| 500 | my $c = substr($src, $end, 1); | ||
| 501 | $depth++ if $c eq '{'; | ||
| 502 | $depth-- if $c eq '}'; | ||
| 503 | $end++; | ||
| 504 | } | ||
| 505 | my $body = substr($src, $-[0], $end - $-[0]); | ||
| 506 | my $meta = eval qq{no warnings 'redefine'; package _FondationDevMeta; $body; _FondationDevMeta::fondation_meta();}; | ||
| 507 | if ($meta && !$@ && ref $meta eq 'HASH' && $meta->{dependencies}) { | ||
| 508 | $deps = $meta->{dependencies}; | ||
| 509 | # Normalize to full class names | ||
| 510 | $deps = [ map { /^Mojolicious::Plugin::/ ? $_ : "Mojolicious::Plugin::$_" } @$deps ]; | ||
| 511 | } | ||
| 512 | } | ||
| 513 | } | ||
| 514 | |||
| 515 | # Build entry in the same shape as MetaCPAN::discover_p | ||
| 516 | push @plugins, { | ||
| 517 | distribution => $dist, | ||
| 518 | version => $version, | ||
| 519 | abstract => $abstract, | ||
| 520 | module_class => $module_class, | ||
| 521 | installed => 1, | ||
| 522 | installed_version => $version, | ||
| 523 | is_dev => 1, | ||
| 524 | dependencies => $deps, | ||
| 525 | }; | ||
| 526 | } | ||
| 527 | return \@plugins; | ||
| 528 | } | ||
| 529 | |||
| 530 | sub _dev_plugins_dir ($self) { | ||
| 531 | my $manager = $self->app->manager; | ||
| 532 | return $manager ? ($manager->config->{dev_plugins_dir} // undef) : undef; | ||
| 533 | } | ||
| 534 | |||
| 535 | # ── Display helpers (pre-compute data for templates) ────────────────── | ||
| 536 | |||
| 537 | # Build badge list for a plugin entry (used by plugins.html.ep and discover JSON). | ||
| 538 | sub _badges_for ($self, $p) { | ||
| 539 | my @badges; | ||
| 540 | |||
| 541 | if ($p->{is_dev}) { | ||
| 542 | push @badges, { text => 'Dev', class => 'info' }; | ||
| 543 | push @badges, { text => 'Installed', class => 'success ms-2' }; | ||
| 544 | push @badges, { text => "v$p->{installed_version}", class => 'light text-dark ms-1' } if $p->{installed_version}; | ||
| 545 | push @badges, { text => "CPAN v$p->{cpan_version}", class => 'light text-dark ms-1' } if $p->{cpan_version}; | ||
| 546 | push @badges, { text => 'Update', class => 'warning text-dark ms-1' } if $p->{upgrade_available}; | ||
| 547 | push @badges, { text => 'Release', class => 'danger ms-1' } if $p->{release_pending}; | ||
| 548 | } else { | ||
| 549 | push @badges, { text => 'CPAN', class => 'secondary ms-2' }; | ||
| 550 | if ($p->{installed}) { | ||
| 551 | push @badges, { text => 'Installed', class => 'success ms-2' }; | ||
| 552 | push @badges, { text => "v$p->{installed_version}", class => 'light text-dark ms-1' } if $p->{installed_version}; | ||
| 553 | push @badges, { text => "CPAN v$p->{cpan_version}", class => 'light text-dark ms-1' } if $p->{cpan_version}; | ||
| 554 | push @badges, { text => 'Update', class => 'warning text-dark ms-1' } if $p->{upgrade_available}; | ||
| 555 | push @badges, { text => 'Release', class => 'danger ms-1' } if $p->{release_pending}; | ||
| 556 | } else { | ||
| 557 | push @badges, { text => 'Not installed', class => 'warning text-dark ms-2' }; | ||
| 558 | push @badges, { text => "v$p->{version}", class => 'light text-dark ms-1' } if $p->{version}; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | my $deps = $p->{dependencies} // []; | ||
| 563 | if (@$deps) { | ||
| 564 | push @badges, { text => scalar(@$deps) . ' dep(s)', class => 'info ms-1', title => join(', ', @$deps) }; | ||
| 565 | } | ||
| 566 | |||
| 567 | return \@badges; | ||
| 568 | } | ||
| 569 | |||
| 570 | # Build the cpanm command string for upgradable + not-installed plugins. | ||
| 571 | sub _install_command ($self, $upgradable, $missing, $cpan_ver) { | ||
| 572 | my @parts; | ||
| 573 | for my $u (@{$upgradable // []}) { | ||
| 574 | push @parts, "$u->{module_class}\@$u->{cpan}"; | ||
| 575 | } | ||
| 576 | for my $m (@{$missing // []}) { | ||
| 577 | my $v = $cpan_ver->{$m}; | ||
| 578 | push @parts, $v ? "$m\@$v" : $m; | ||
| 579 | } | ||
| 580 | return @parts ? join(' ', @parts) : undef; | ||
| 581 | } | ||
| 582 | |||
| 583 | # Build sorted, filtered review entries from context. | ||
| 584 | sub _review_entries ($self, $context) { | ||
| 585 | my @entries; | ||
| 586 | for my $key (sort keys %$context) { | ||
| 587 | next if $key =~ /^_/; | ||
| 588 | my $val = $context->{$key}; | ||
| 589 | next unless defined $val && $val ne ''; | ||
| 590 | push @entries, { key => $key, value => $val }; | ||
| 591 | } | ||
| 592 | return \@entries; | ||
| 593 | } | ||
| 594 | |||
| 595 | # ── .conf generation ───────────────────────────────────────────────── | ||
| 596 | |||
| 597 | sub _generate_conf ($self) { | ||
| 598 | my $plugins = $self->session('setup_plugins') // []; | ||
| 599 | my $context = $self->session('setup_context') // {}; | ||
| 600 | |||
| 601 | # Build key → plugin mapping from fondation_meta | ||
| 602 | my %key_to_plugin; | ||
| 603 | for my $class (@$plugins) { | ||
| 604 | my $meta = $self->_fondation_meta($class) or next; | ||
| 605 | my $params = $meta->{setup}{parameters} // []; | ||
| 606 | for my $p (@$params) { | ||
| 607 | $key_to_plugin{ $p->{key} } = $class if $p->{key}; | ||
| 608 | } | ||
| 609 | } | ||
| 610 | |||
| 611 | # Collect values per plugin | ||
| 612 | my %plugin_config; | ||
| 613 | for my $key (sort keys %$context) { | ||
| 614 | next unless defined $context->{$key} && $context->{$key} ne ''; | ||
| 615 | next if $key =~ /^_/; # skip internal keys | ||
| 616 | my $plugin = $key_to_plugin{$key} or next; | ||
| 617 | |||
| 618 | my $is_array = ($key =~ s/^\+//); | ||
| 619 | my @parts = split /\./, $key; | ||
| 620 | my $target = ($plugin_config{$plugin} //= {}); | ||
| 621 | |||
| 622 | if ($is_array) { | ||
| 623 | $target->{$parts[0]} //= []; | ||
| 624 | my $entry; | ||
| 625 | for (my $j = 0; $j < @{$target->{$parts[0]}}; $j += 2) { | ||
| 626 | if ($target->{$parts[0]}[$j] eq $parts[1]) { | ||
| 627 | $entry = $target->{$parts[0]}[$j + 1]; | ||
| 628 | last; | ||
| 629 | } | ||
| 630 | } | ||
| 631 | unless ($entry) { | ||
| 632 | $entry = {}; | ||
| 633 | push @{$target->{$parts[0]}}, $parts[1], $entry; | ||
| 634 | } | ||
| 635 | $target = $entry; | ||
| 636 | for my $i (2 .. $#parts - 1) { | ||
| 637 | $target->{$parts[$i]} //= {}; | ||
| 638 | $target = $target->{$parts[$i]}; | ||
| 639 | } | ||
| 640 | $target->{$parts[-1]} = $context->{"+$key"}; | ||
| 641 | } else { | ||
| 642 | for my $i (0 .. $#parts - 1) { | ||
| 643 | $target->{$parts[$i]} //= {}; | ||
| 644 | $target = $target->{$parts[$i]}; | ||
| 645 | } | ||
| 646 | $target->{$parts[-1]} = $context->{$key}; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | |||
| 650 | # Build conf structure | ||
| 651 | my @deps; | ||
| 652 | for my $class (@$plugins) { | ||
| 653 | (my $short = $class) =~ s/^Mojolicious::Plugin:://; | ||
| 654 | push @deps, $short; | ||
| 655 | } | ||
| 656 | |||
| 657 | my %conf = ( Fondation => { dependencies => \@deps } ); | ||
| 658 | for my $class (@$plugins) { | ||
| 659 | next unless $plugin_config{$class}; | ||
| 660 | (my $short = $class) =~ s/^Mojolicious::Plugin:://; | ||
| 661 | $conf{$short} = $plugin_config{$class}; | ||
| 662 | } | ||
| 663 | |||
| 664 | require Data::Dumper; | ||
| 665 | local $Data::Dumper::Indent = 1; | ||
| 666 | local $Data::Dumper::Terse = 1; | ||
| 667 | local $Data::Dumper::Sortkeys = 1; | ||
| 668 | local $Data::Dumper::Quotekeys = 0; | ||
| 669 | |||
| 670 | my $content = Data::Dumper::Dumper(\%conf); | ||
| 671 | my $conf_path = $self->app->home->child($self->app->moniker . '.conf'); | ||
| 672 | $conf_path->spurt(encode('UTF-8', $content)); | ||
| 673 | |||
| 674 | $self->app->log->info("Configuration saved to $conf_path"); | ||
| 675 | return $conf_path->to_string; | ||
| 676 | } | ||
| 677 | |||
| 678 | 1; | ||
| 679 | |||
| 680 | __END__ |