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.64 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.63 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.62 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.61 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.60 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.59 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.58 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.57 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.56 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.55 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.54 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.53 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.52 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.51 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.50 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.49 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.48 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.47 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.46 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.45 tracks/perl5/bin/exercise-gen.pl