Sha256: d276ed85efa4c2d2e1aa54666f113438b0a72021ec9372dd8a69a8cf7a8a1361

Contents?: true

Size: 1.84 KB

Versions: 114

Compression:

Stored size: 1.84 KB

Contents

#!/usr/bin/env perl
use feature qw(lexical_subs say);
use YAML 'LoadFile';
use Path::Tiny qw(:DEFAULT cwd);
use Template::Mustache 'render';

my $base_dir = path($0)->realpath->parent->parent;
my @exercises;

if (@ARGV) {
  if ($ARGV[0] eq '--all') {
    push @exercises, $_->basename foreach $base_dir->child('exercises')->children;
  } else {
    @exercises = @ARGV;
  }
} else {
  say 'No args given; working in current directory.';
  if ( path('example.yaml')->is_file ) {
    push @exercises, cwd->basename;
  } else {
    say 'example.yaml not found in current directory; exiting.';
    exit;
  }
}

my @dir_not_found;
my @yaml_not_found;
for my $exercise (@exercises) {
  my $exercise_dir = $base_dir->child("exercises/$exercise");
  my $yaml = $exercise_dir->child('example.yaml');

  unless ($exercise_dir->is_dir) {
    push @dir_not_found, $exercise;
    next;
  }
  unless ($yaml->is_file) {
    push @yaml_not_found, $exercise;
    next;
  }
  print "Generating $exercise... ";

  my $data = LoadFile $yaml;

  my $cdata = $base_dir->child("problem-specifications/exercises/$exercise/canonical-data.json");
  if ($cdata->is_file) {$data->{cdata}{json} = $cdata->slurp}

  my sub create_file {
    my ($filename, $template) = @_;
    my $file = $exercise_dir->child($filename);
    $file->spew(Template::Mustache->render(
      $base_dir->child("templates/$template.mustache")->slurp, $data
    ));
    $file->chmod(0755) if $template eq 'test';
  }

  create_file("$exercise.t", 'test');

  $data->{module_file} = $data->{example};
  create_file(qw{Example.pm module});

  $data->{module_file} = $data->{stub};
  create_file($data->{exercise}.'.pm', 'module');

  say 'Generated.';
}

if (@dir_not_found)  {warn 'exercise directory does not exist for: ' . join ' ', @dir_not_found}
if (@yaml_not_found) {warn 'example.yaml not found for: ' . join ' ', @yaml_not_found}

Version data entries

114 entries across 114 versions & 1 rubygems

Version Path
trackler-2.2.1.104 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.103 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.102 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.101 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.100 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.99 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.98 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.97 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.96 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.95 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.94 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.93 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.92 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.91 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.90 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.89 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.88 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.87 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.86 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.85 tracks/perl5/bin/exercise-gen.pl