App-LinkSite

 view release on metacpan or  search on metacpan

lib/App/LinkSite.pm  view on Meta::CPAN

=head1 NAME

App::LinkSite - Create a website listing all of your links

=head1 SYNOPIS

(You probably want to just look at the L<linksite> application.)

=head1 DESCRIPTION

The main driver class for App::LinkSite.

=cut

use Feature::Compat::Class;

class App::LinkSite {
  our $VERSION = '0.1.1';
  use strict;
  use warnings;
  use feature qw[say signatures];
  no if $] >= 5.038, 'warnings', qw[experimental::signatures experimental::class];

  use Template;
  use JSON;
  use Path::Tiny;
  use File::Find;
  use File::Basename;
  use FindBin '$Bin';
  use File::ShareDir 'dist_dir';

  use App::LinkSite::Site;
  use App::LinkSite::Link;
  use App::LinkSite::Social;
  use App::LinkSite::Section;

  field $file :reader :param = 'links.json';
  # Where to look for the templates.
  # If we've been installed from CPAN, then File::Share::dist_name
  # gives us the correct directory. Otherwise, just look in the local
  # src directory. Note that dist_name() dies if the directory is not
  # found - hence the use of eval.
  field $src :reader :param = eval { dist_dir("App-LinkSite") } || "$Bin/../src";
  field $out :reader :param = 'docs';
  field $ga4 :reader :param = undef;
  field $site :reader :param = undef;

  field $tt;

  ADJUST {
    my $json = path($file)->slurp;
    my $data = JSON->new->decode($json);

    $ga4 = $data->{ga4} // '';

    $tt = Template->new({
      # Templates in the CPAN distro directory
      INCLUDE_PATH => $src,
      # Output in the data directory
      OUTPUT_PATH  => $out,
      VARIABLES    => {
        ga4        => $ga4,
        version    => $VERSION,
      }
    });

    my $socials = [ map {
      $_->{handle} //= $data->{handle};
      App::LinkSite::Social->new(%$_)
    } $data->{social}->@* ];

    my $links = [ map { App::LinkSite::Link->new(%$_) } $data->{links}->@* ];

    my $sections = [];
    if ($data->{sections}) {
      $sections = [ map {
        my $section_links = [ map { App::LinkSite::Link->new(%$_) } $_->{links}->@* ];
        App::LinkSite::Section->new(
          title => $_->{title},
          links => $section_links,
        )
      } $data->{sections}->@* ];
    }

    $site = App::LinkSite::Site->new(
      name    => $data->{name},
      handle  => $data->{handle},
      image   => $data->{image},
      desc    => $data->{desc},
      og_image => $data->{og_image},
      site_url => $data->{site_url},
      socials => $socials,
      links   => $links,
      sections => $sections,
      $data->{text_color}       ? (text_color => $data->{text_color}) : (),
      $data->{background_color} ? (background_color => $data->{background_color} ) : (),
    );
  }

=head1 METHODS

=head2 run

The main driver method for the process.

=cut

  method run {
    debug("src is: $src");
    debug("out is: $out");
    path($out)->mkdir;
    find( { wanted => sub { $self->do_this }, no_chdir => 1 }, $src);

    if ($site->image or $site->og_image) {
      path("$out/img")->mkdir;
      debug("Copy images");
      for my $img ($site->image, $site->og_image) {
        next unless $img;
        path("img/$img")->copy("$out/img");
      }



( run in 0.463 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )