| 1 | package Yote::YapiServer::Compiler; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use YAML; | ||
| 7 | use File::Path qw(make_path); | ||
| 8 | use File::Basename; | ||
| 9 | use File::Find; | ||
| 10 | use Yote::YapiServer::YapiDef; | ||
| 11 | |||
| 12 | #====================================================================== | ||
| 13 | # Public API | ||
| 14 | #====================================================================== | ||
| 15 | |||
| 16 | sub compile { | ||
| 17 | my ($input, $outdir) = @_; | ||
| 18 | |||
| 19 | my @files; | ||
| 20 | if (-d $input) { | ||
| 21 | find(sub { push @files, $File::Find::name if /\.(ya?ml|ydef)$/ }, $input); | ||
| 22 | } elsif (-f $input) { | ||
| 23 | @files = ($input); | ||
| 24 | } else { | ||
| 25 | die "Input not found: $input\n"; | ||
| 26 | } | ||
| 27 | |||
| 28 | die "No definition files found\n" unless @files; | ||
| 29 | |||
| 30 | for my $file (@files) { | ||
| 31 | print "Compiling $file...\n"; | ||
| 32 | |||
| 33 | my @defs; | ||
| 34 | if ($file =~ /\.ydef$/) { | ||
| 35 | @defs = @{ Yote::YapiServer::YapiDef::parse_file($file) }; | ||
| 36 | } else { | ||
| 37 | @defs = (YAML::LoadFile($file)); | ||
| 38 | } | ||
| 39 | |||
| 40 | for my $yaml (@defs) { | ||
| 41 | my $type = $yaml->{type} // 'app'; | ||
| 42 | |||
| 43 | my @outputs; # list of [package, content] pairs | ||
| 44 | if ($type eq 'app') { | ||
| 45 | @outputs = compile_app($yaml); | ||
| 46 | } elsif ($type eq 'object') { | ||
| 47 | @outputs = ( [$yaml->{package}, compile_object($yaml)] ); | ||
| 48 | } elsif ($type eq 'server') { | ||
| 49 | @outputs = ( [$yaml->{package}, compile_server($yaml)] ); | ||
| 50 | } else { | ||
| 51 | die "Unknown type '$type' in $file\n"; | ||
| 52 | } | ||
| 53 | |||
| 54 | for my $pair (@outputs) { | ||
| 55 | write_output($pair->[0], $pair->[1], $outdir); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | print "Done.\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | #====================================================================== | ||
| 64 | # Compile an app YAML into Perl | ||
| 65 | #====================================================================== | ||
| 66 | sub compile_app { | ||
| 67 | my ($yaml) = @_; | ||
| 68 | merge_method_shorthands($yaml); | ||
| 69 | my $pkg = $yaml->{package}; | ||
| 70 | my $base = $yaml->{base} // 'Yote::YapiServer::App::Base'; | ||
| 71 | |||
| 72 | my @out; | ||
| 73 | |||
| 74 | # Package header | ||
| 75 | push @out, "package $pkg;"; | ||
| 76 | push @out, ""; | ||
| 77 | push @out, "use strict;"; | ||
| 78 | push @out, "use warnings;"; | ||
| 79 | push @out, "use base '$base';"; | ||
| 80 | |||
| 81 | # Extra uses | ||
| 82 | if ($yaml->{uses}) { | ||
| 83 | push @out, ""; | ||
| 84 | for my $mod (@{$yaml->{uses}}) { | ||
| 85 | push @out, "use $mod;"; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # Use nested object packages so their tables get created | ||
| 90 | if ($yaml->{objects}) { | ||
| 91 | push @out, ""; | ||
| 92 | for my $obj_name (sort keys %{$yaml->{objects}}) { | ||
| 93 | push @out, "use ${pkg}::${obj_name};"; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | push @out, ""; | ||
| 98 | |||
| 99 | # Columns - merge with base | ||
| 100 | push @out, "# Inherit base columns and add our own"; | ||
| 101 | push @out, "our \%cols = ("; | ||
| 102 | push @out, " \%${base}::cols,"; | ||
| 103 | if ($yaml->{cols}) { | ||
| 104 | for my $col (sort keys %{$yaml->{cols}}) { | ||
| 105 | my $val = expand_col_type($yaml->{cols}{$col}, $pkg); | ||
| 106 | push @out, " $col => '$val',"; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | push @out, ");"; | ||
| 110 | |||
| 111 | push @out, ""; | ||
| 112 | |||
| 113 | # Methods | ||
| 114 | push @out, "# Method access control"; | ||
| 115 | push @out, "our \%METHODS = ("; | ||
| 116 | if ($yaml->{methods}) { | ||
| 117 | my @pub; my @auth; my @admin; | ||
| 118 | for my $name (sort keys %{$yaml->{methods}}) { | ||
| 119 | my $def = $yaml->{methods}{$name}; | ||
| 120 | my $access = $def->{access} // 'auth'; | ||
| 121 | if (ref $access eq 'HASH') { | ||
| 122 | push @auth, [$name, $def]; # compound access goes with auth | ||
| 123 | } elsif ($access eq 'public') { | ||
| 124 | push @pub, [$name, $def]; | ||
| 125 | } elsif ($access eq 'admin_only') { | ||
| 126 | push @admin, [$name, $def]; | ||
| 127 | } else { | ||
| 128 | push @auth, [$name, $def]; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | if (@pub) { | ||
| 132 | push @out, " # Public - no auth required"; | ||
| 133 | for my $item (@pub) { | ||
| 134 | my ($name, $def) = @$item; | ||
| 135 | my $pad = pad_to($name, \@pub, \@auth, \@admin); | ||
| 136 | push @out, " $name$pad => " . method_hash_str($def, 'public') . ","; | ||
| 137 | } | ||
| 138 | push @out, "" if @auth || @admin; | ||
| 139 | } | ||
| 140 | if (@auth) { | ||
| 141 | push @out, " # Authenticated users"; | ||
| 142 | for my $item (@auth) { | ||
| 143 | my ($name, $def) = @$item; | ||
| 144 | my $pad = pad_to($name, \@pub, \@auth, \@admin); | ||
| 145 | push @out, " $name$pad => " . method_hash_str($def, 'auth') . ","; | ||
| 146 | } | ||
| 147 | push @out, "" if @admin; | ||
| 148 | } | ||
| 149 | if (@admin) { | ||
| 150 | push @out, " # Admin only"; | ||
| 151 | for my $item (@admin) { | ||
| 152 | my ($name, $def) = @$item; | ||
| 153 | my $pad = pad_to($name, \@pub, \@auth, \@admin); | ||
| 154 | push @out, " $name$pad => " . method_hash_str($def, 'admin_only') . ","; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | push @out, ");"; | ||
| 159 | |||
| 160 | push @out, ""; | ||
| 161 | |||
| 162 | # Field access - merge with base | ||
| 163 | push @out, "# Field visibility"; | ||
| 164 | push @out, "our \%FIELD_ACCESS = ("; | ||
| 165 | push @out, " \%${base}::FIELD_ACCESS,"; | ||
| 166 | if ($yaml->{field_access}) { | ||
| 167 | for my $field (sort keys %{$yaml->{field_access}}) { | ||
| 168 | my $rule = $yaml->{field_access}{$field}; | ||
| 169 | push @out, " $field => " . format_access_hash($rule) . ","; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | push @out, ");"; | ||
| 173 | |||
| 174 | push @out, ""; | ||
| 175 | |||
| 176 | # Public vars | ||
| 177 | push @out, "# Public vars exposed on connect"; | ||
| 178 | push @out, "our \%PUBLIC_VARS = ("; | ||
| 179 | if ($yaml->{public_vars}) { | ||
| 180 | for my $k (sort keys %{$yaml->{public_vars}}) { | ||
| 181 | my $v = $yaml->{public_vars}{$k}; | ||
| 182 | push @out, " $k => " . perl_value($v) . ","; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | push @out, ");"; | ||
| 186 | |||
| 187 | # Package-level scalar vars | ||
| 188 | if ($yaml->{vars}) { | ||
| 189 | push @out, ""; | ||
| 190 | for my $var (sort keys %{$yaml->{vars}}) { | ||
| 191 | my $v = $yaml->{vars}{$var}; | ||
| 192 | push @out, "our \$$var = " . perl_value($v) . ";"; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | # Method implementations | ||
| 197 | if ($yaml->{methods}) { | ||
| 198 | # Group by access for section headers | ||
| 199 | my @pub_methods; my @auth_methods; my @admin_methods; | ||
| 200 | for my $name (sort keys %{$yaml->{methods}}) { | ||
| 201 | my $def = $yaml->{methods}{$name}; | ||
| 202 | my $access = $def->{access} // 'auth'; | ||
| 203 | my $access_str = ref $access ? 'auth' : $access; | ||
| 204 | if ($access_str eq 'public') { | ||
| 205 | push @pub_methods, $name; | ||
| 206 | } elsif ($access_str eq 'admin_only') { | ||
| 207 | push @admin_methods, $name; | ||
| 208 | } else { | ||
| 209 | push @auth_methods, $name; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | for my $group (['Public methods', \@pub_methods], | ||
| 214 | ['Authenticated methods', \@auth_methods], | ||
| 215 | ['Admin methods', \@admin_methods]) { | ||
| 216 | my ($label, $names) = @$group; | ||
| 217 | next unless @$names; | ||
| 218 | push @out, ""; | ||
| 219 | push @out, "#" . "-" x 70; | ||
| 220 | push @out, "# $label"; | ||
| 221 | push @out, "#" . "-" x 70; | ||
| 222 | for my $name (@$names) { | ||
| 223 | push @out, ""; | ||
| 224 | my $code = $yaml->{methods}{$name}{code}; | ||
| 225 | push @out, "sub $name {"; | ||
| 226 | push @out, indent_code($code); | ||
| 227 | push @out, "}"; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | # Non-API subs | ||
| 233 | if ($yaml->{subs}) { | ||
| 234 | push @out, ""; | ||
| 235 | push @out, "#" . "-" x 70; | ||
| 236 | push @out, "# Utility methods"; | ||
| 237 | push @out, "#" . "-" x 70; | ||
| 238 | for my $name (sort keys %{$yaml->{subs}}) { | ||
| 239 | push @out, ""; | ||
| 240 | push @out, "sub $name {"; | ||
| 241 | push @out, indent_code($yaml->{subs}{$name}); | ||
| 242 | push @out, "}"; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | push @out, ""; | ||
| 247 | push @out, "1;"; | ||
| 248 | |||
| 249 | push @out, ""; | ||
| 250 | push @out, "__END__"; | ||
| 251 | |||
| 252 | my @outputs = ( [$pkg, join("\n", @out) . "\n"] ); | ||
| 253 | |||
| 254 | # Nested objects - each gets its own file | ||
| 255 | if ($yaml->{objects}) { | ||
| 256 | for my $obj_name (sort keys %{$yaml->{objects}}) { | ||
| 257 | my $obj_def = $yaml->{objects}{$obj_name}; | ||
| 258 | my $obj_pkg = "${pkg}::${obj_name}"; | ||
| 259 | my $obj_content = compile_nested_object($obj_pkg, $obj_def, $pkg); | ||
| 260 | push @outputs, [$obj_pkg, $obj_content . "\n"]; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | return @outputs; | ||
| 265 | } | ||
| 266 | |||
| 267 | #====================================================================== | ||
| 268 | # Compile a standalone object YAML | ||
| 269 | #====================================================================== | ||
| 270 | sub compile_object { | ||
| 271 | my ($yaml) = @_; | ||
| 272 | merge_method_shorthands($yaml); | ||
| 273 | my $pkg = $yaml->{package}; | ||
| 274 | my $base = $yaml->{base} // 'Yote::YapiServer::BaseObj'; | ||
| 275 | |||
| 276 | my @out; | ||
| 277 | |||
| 278 | push @out, "package $pkg;"; | ||
| 279 | push @out, ""; | ||
| 280 | push @out, "use strict;"; | ||
| 281 | push @out, "use warnings;"; | ||
| 282 | push @out, "use base '$base';"; | ||
| 283 | |||
| 284 | if ($yaml->{uses}) { | ||
| 285 | push @out, ""; | ||
| 286 | for my $mod (@{$yaml->{uses}}) { | ||
| 287 | push @out, "use $mod;"; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | push @out, ""; | ||
| 292 | |||
| 293 | # Columns (no base merging for standalone objects) | ||
| 294 | push @out, "# Database column definitions"; | ||
| 295 | push @out, "our \%cols = ("; | ||
| 296 | if ($yaml->{cols}) { | ||
| 297 | for my $col (sort keys %{$yaml->{cols}}) { | ||
| 298 | my $val = expand_col_type($yaml->{cols}{$col}, $pkg); | ||
| 299 | push @out, " $col => '$val',"; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | push @out, ");"; | ||
| 303 | |||
| 304 | push @out, ""; | ||
| 305 | |||
| 306 | # Field access | ||
| 307 | push @out, "# Field visibility rules for client serialization"; | ||
| 308 | push @out, "our \%FIELD_ACCESS = ("; | ||
| 309 | if ($yaml->{field_access}) { | ||
| 310 | for my $field (sort keys %{$yaml->{field_access}}) { | ||
| 311 | my $rule = $yaml->{field_access}{$field}; | ||
| 312 | push @out, " $field => " . format_access_hash($rule) . ","; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | push @out, ");"; | ||
| 316 | |||
| 317 | push @out, ""; | ||
| 318 | |||
| 319 | # Methods | ||
| 320 | push @out, "# Methods callable from client"; | ||
| 321 | push @out, "our \%METHODS = ("; | ||
| 322 | if ($yaml->{methods}) { | ||
| 323 | for my $name (sort keys %{$yaml->{methods}}) { | ||
| 324 | my $def = $yaml->{methods}{$name}; | ||
| 325 | push @out, " $name => " . method_hash_str($def) . ","; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | push @out, ");"; | ||
| 329 | |||
| 330 | # field_access and method_defs inherited from Yote::YapiServer::BaseObj | ||
| 331 | |||
| 332 | # Method implementations | ||
| 333 | if ($yaml->{methods}) { | ||
| 334 | push @out, ""; | ||
| 335 | push @out, "#" . "-" x 70; | ||
| 336 | push @out, "# Client-callable methods"; | ||
| 337 | push @out, "#" . "-" x 70; | ||
| 338 | for my $name (sort keys %{$yaml->{methods}}) { | ||
| 339 | my $def = $yaml->{methods}{$name}; | ||
| 340 | next unless $def->{code}; | ||
| 341 | push @out, ""; | ||
| 342 | push @out, "sub $name {"; | ||
| 343 | push @out, indent_code($def->{code}); | ||
| 344 | push @out, "}"; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | # Non-API subs | ||
| 349 | if ($yaml->{subs}) { | ||
| 350 | push @out, ""; | ||
| 351 | push @out, "#" . "-" x 70; | ||
| 352 | push @out, "# Server-side utility methods"; | ||
| 353 | push @out, "#" . "-" x 70; | ||
| 354 | for my $name (sort keys %{$yaml->{subs}}) { | ||
| 355 | push @out, ""; | ||
| 356 | push @out, "sub $name {"; | ||
| 357 | push @out, indent_code($yaml->{subs}{$name}); | ||
| 358 | push @out, "}"; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | push @out, ""; | ||
| 363 | push @out, "1;"; | ||
| 364 | |||
| 365 | push @out, ""; | ||
| 366 | push @out, "__END__"; | ||
| 367 | |||
| 368 | return join("\n", @out) . "\n"; | ||
| 369 | } | ||
| 370 | |||
| 371 | #====================================================================== | ||
| 372 | # Compile a server YAML | ||
| 373 | #====================================================================== | ||
| 374 | sub compile_server { | ||
| 375 | my ($yaml) = @_; | ||
| 376 | my $pkg = $yaml->{package}; | ||
| 377 | my $base = $yaml->{base} // 'Yote::YapiServer::Site'; | ||
| 378 | |||
| 379 | my @out; | ||
| 380 | |||
| 381 | push @out, "package $pkg;"; | ||
| 382 | push @out, ""; | ||
| 383 | push @out, "use strict;"; | ||
| 384 | push @out, "use warnings;"; | ||
| 385 | push @out, "use base '$base'; # extends Yote::SQLObjectStore::BaseObj"; | ||
| 386 | |||
| 387 | if ($yaml->{uses}) { | ||
| 388 | push @out, ""; | ||
| 389 | for my $mod (@{$yaml->{uses}}) { | ||
| 390 | push @out, "use $mod;"; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | push @out, ""; | ||
| 395 | |||
| 396 | push @out, "our \%cols = ("; | ||
| 397 | push @out, " \%${base}::cols,"; | ||
| 398 | push @out, ");"; | ||
| 399 | |||
| 400 | push @out, ""; | ||
| 401 | |||
| 402 | push @out, "our \%INSTALLED_APPS = ("; | ||
| 403 | push @out, " \%${base}::INSTALLED_APPS,"; | ||
| 404 | if ($yaml->{apps}) { | ||
| 405 | for my $name (sort keys %{$yaml->{apps}}) { | ||
| 406 | push @out, " $name => '$yaml->{apps}{$name}',"; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | push @out, ");"; | ||
| 410 | |||
| 411 | push @out, ""; | ||
| 412 | push @out, "sub installed_apps {"; | ||
| 413 | push @out, " return \\\%INSTALLED_APPS;"; | ||
| 414 | push @out, "}"; | ||
| 415 | |||
| 416 | push @out, ""; | ||
| 417 | push @out, "1;"; | ||
| 418 | |||
| 419 | return join("\n", @out) . "\n"; | ||
| 420 | } | ||
| 421 | |||
| 422 | #====================================================================== | ||
| 423 | # Compile a nested object (inside an app) | ||
| 424 | #====================================================================== | ||
| 425 | sub compile_nested_object { | ||
| 426 | my ($obj_pkg, $def, $parent_pkg) = @_; | ||
| 427 | merge_method_shorthands($def); | ||
| 428 | my @out; | ||
| 429 | |||
| 430 | push @out, "package $obj_pkg;"; | ||
| 431 | push @out, ""; | ||
| 432 | push @out, "use strict;"; | ||
| 433 | push @out, "use warnings;"; | ||
| 434 | push @out, "use base 'Yote::YapiServer::BaseObj';"; | ||
| 435 | |||
| 436 | push @out, ""; | ||
| 437 | push @out, "our \%cols = ("; | ||
| 438 | if ($def->{cols}) { | ||
| 439 | for my $col (sort keys %{$def->{cols}}) { | ||
| 440 | my $val = expand_col_type($def->{cols}{$col}, $parent_pkg); | ||
| 441 | push @out, " $col => '$val',"; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | push @out, ");"; | ||
| 445 | |||
| 446 | push @out, ""; | ||
| 447 | push @out, "our \%FIELD_ACCESS = ("; | ||
| 448 | if ($def->{field_access}) { | ||
| 449 | for my $field (sort keys %{$def->{field_access}}) { | ||
| 450 | my $rule = $def->{field_access}{$field}; | ||
| 451 | push @out, " $field => " . format_access_hash($rule) . ","; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | push @out, ");"; | ||
| 455 | |||
| 456 | push @out, ""; | ||
| 457 | push @out, "our \%METHODS = ();"; | ||
| 458 | |||
| 459 | # field_access, method_defs, to_client_hash inherited from Yote::YapiServer::BaseObj | ||
| 460 | |||
| 461 | # Auto-generate _client_class_name from object name | ||
| 462 | my ($obj_name) = $obj_pkg =~ /::(\w+)$/; | ||
| 463 | push @out, ""; | ||
| 464 | push @out, "sub _client_class_name { return '$obj_name'; }"; | ||
| 465 | |||
| 466 | # to_client_hash inherited from BaseObj; custom overrides via subs: | ||
| 467 | |||
| 468 | # Object-level methods | ||
| 469 | if ($def->{methods}) { | ||
| 470 | for my $name (sort keys %{$def->{methods}}) { | ||
| 471 | my $mdef = $def->{methods}{$name}; | ||
| 472 | push @out, ""; | ||
| 473 | push @out, "sub $name {"; | ||
| 474 | push @out, indent_code($mdef->{code} // $mdef); | ||
| 475 | push @out, "}"; | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | # Non-API subs on objects | ||
| 480 | if ($def->{subs}) { | ||
| 481 | for my $name (sort keys %{$def->{subs}}) { | ||
| 482 | push @out, ""; | ||
| 483 | push @out, "sub $name {"; | ||
| 484 | push @out, indent_code($def->{subs}{$name}); | ||
| 485 | push @out, "}"; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | push @out, ""; | ||
| 490 | push @out, "1;"; | ||
| 491 | |||
| 492 | push @out, ""; | ||
| 493 | push @out, "__END__"; | ||
| 494 | |||
| 495 | return join("\n", @out); | ||
| 496 | } | ||
| 497 | |||
| 498 | #====================================================================== | ||
| 499 | # Helpers | ||
| 500 | #====================================================================== | ||
| 501 | |||
| 502 | # Merge methods_public and methods_auth into methods with implicit access | ||
| 503 | sub merge_method_shorthands { | ||
| 504 | my ($yaml) = @_; | ||
| 505 | for my $key (qw(methods_public methods_auth)) { | ||
| 506 | next unless $yaml->{$key}; | ||
| 507 | my $access = $key eq 'methods_public' ? 'public' : 'auth'; | ||
| 508 | $yaml->{methods} //= {}; | ||
| 509 | for my $name (keys %{$yaml->{$key}}) { | ||
| 510 | my $def = $yaml->{$key}{$name}; | ||
| 511 | # Accept bare code string or hash with code key | ||
| 512 | if (!ref $def) { | ||
| 513 | $def = { code => $def }; | ||
| 514 | } | ||
| 515 | $def->{access} //= $access; | ||
| 516 | $yaml->{methods}{$name} = $def; | ||
| 517 | } | ||
| 518 | delete $yaml->{$key}; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | # Expand relative package references: *::Foo -> *Parent::Package::Foo | ||
| 523 | # Only expands when * is immediately followed by :: (relative ref) | ||
| 524 | # Leaves absolute refs like *Yote::YapiServer::User unchanged | ||
| 525 | sub expand_col_type { | ||
| 526 | my ($type, $parent_pkg) = @_; | ||
| 527 | # *::Name or *ARRAY_*::Name patterns | ||
| 528 | $type =~ s/\*::/\*${parent_pkg}::/g; | ||
| 529 | return $type; | ||
| 530 | } | ||
| 531 | |||
| 532 | # Build method hash string from definition, including files flag | ||
| 533 | sub method_hash_str { | ||
| 534 | my ($def, $default_access) = @_; | ||
| 535 | my $access = $def->{access} // $default_access // 'auth'; | ||
| 536 | my @parts; | ||
| 537 | |||
| 538 | if (ref $access eq 'HASH') { | ||
| 539 | push @parts, map { "$_ => $access->{$_}" } sort keys %$access; | ||
| 540 | } elsif ($access eq 'public') { | ||
| 541 | push @parts, "public => 1"; | ||
| 542 | } elsif ($access eq 'admin_only') { | ||
| 543 | push @parts, "admin_only => 1"; | ||
| 544 | } else { | ||
| 545 | push @parts, "auth => 1"; | ||
| 546 | } | ||
| 547 | |||
| 548 | push @parts, "files => 1" if $def->{files}; | ||
| 549 | |||
| 550 | return "{ " . join(", ", @parts) . " }"; | ||
| 551 | } | ||
| 552 | |||
| 553 | # Format access rule as Perl hash ref | ||
| 554 | sub format_access_hash { | ||
| 555 | my ($rule) = @_; | ||
| 556 | if (ref $rule eq 'HASH') { | ||
| 557 | my @parts = map { "$_ => $rule->{$_}" } sort keys %$rule; | ||
| 558 | return "{ " . join(", ", @parts) . " }"; | ||
| 559 | } | ||
| 560 | # Simple string like "public", "auth", "never", "owner_only", "admin_only" | ||
| 561 | return "{ $rule => 1 }"; | ||
| 562 | } | ||
| 563 | |||
| 564 | # Format a Perl value (string or number) | ||
| 565 | sub perl_value { | ||
| 566 | my ($v) = @_; | ||
| 567 | return $v if $v =~ /^\d+$/; # integer | ||
| 568 | return "'$v'"; # string | ||
| 569 | } | ||
| 570 | |||
| 571 | # Indent code block for insertion into sub body | ||
| 572 | sub indent_code { | ||
| 573 | my ($code) = @_; | ||
| 574 | return "" unless defined $code; | ||
| 575 | chomp $code; | ||
| 576 | my @lines = split /\n/, $code; | ||
| 577 | return join("\n", map { " $_" } @lines); | ||
| 578 | } | ||
| 579 | |||
| 580 | # Calculate padding for alignment | ||
| 581 | sub pad_to { | ||
| 582 | my ($name, @groups) = @_; | ||
| 583 | my $max = 0; | ||
| 584 | for my $group (@groups) { | ||
| 585 | for my $item (@$group) { | ||
| 586 | my $n = ref $item eq 'ARRAY' ? $item->[0] : $item; | ||
| 587 | $max = length($n) if length($n) > $max; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | my $pad = $max - length($name); | ||
| 591 | return $pad > 0 ? ' ' x $pad : ''; | ||
| 592 | } | ||
| 593 | |||
| 594 | # Write generated Perl to the correct file path | ||
| 595 | sub write_output { | ||
| 596 | my ($package, $content, $outdir) = @_; | ||
| 597 | |||
| 598 | my $path = $package; | ||
| 599 | $path =~ s/::/\//g; | ||
| 600 | $path = "$outdir/$path.pm"; | ||
| 601 | |||
| 602 | my $dir = dirname($path); | ||
| 603 | make_path($dir) unless -d $dir; | ||
| 604 | |||
| 605 | open my $fh, '>', $path or die "Cannot write $path: $!\n"; | ||
| 606 | print $fh $content; | ||
| 607 | close $fh; | ||
| 608 | |||
| 609 | print " -> $path\n"; | ||
| 610 | } | ||
| 611 | |||
| 612 | 1; |