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.44 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.43 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.42 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.41 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.40 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.39 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.38 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.37 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.36 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.35 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.34 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.33 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.32 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.31 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.30 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.29 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.28 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.27 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.26 tracks/perl5/bin/exercise-gen.pl
trackler-2.2.1.25 tracks/perl5/bin/exercise-gen.pl