#!/usr/bin/perl
# PODNAME: ct-build
our $VERSION = '0.12'; # VERSION 0.02
# ABSTRACT: build (and run) your tests

use strict;
use warnings;

use File::Spec;

my $ct_dir;
my $t_dir;
my $prove;
my $prove_args;
my $check;
my $curdir;

BEGIN {
    $curdir = File::Spec->curdir();

    use Getopt::Long;
    use Pod::Usage;
    my $man = 0;
    my $help = 0;

    GetOptions(
        'help|?' => \$help,
        man      => \$man,

        "dir=s"           => \$ct_dir,
        "out=s"           => \$t_dir,

        "p|prove"         => \$prove,
        "prove_args=s"    => \$prove_args,
        "c|check-syntax!"  => \$check,

    ) or pod2usage(2);
    pod2usage(1) if $help;
    pod2usage(-exitstatus => 0, -verbose => 2) if $man;

    $prove_args||= '-lv';

    print "current dir: $curdir\n";
    $ct_dir = File::Spec->catdir( $curdir, 'ct' ) if (!$ct_dir);

    die "Can't find CT directory ($ct_dir)\n" unless -d $ct_dir;


    $t_dir = File::Spec->catdir( $curdir, 't' ) if (!$t_dir);

    die "Can't find tests output directory ($t_dir)\n" unless -d $t_dir;
}

use Test::CT::Assembly;
use App::Prove;
$check //= 1;

my $compiler = Test::CT::Assembly->new(
    ct_dir          => $ct_dir,
    (defined $t_dir ? (test_dir_output => $t_dir) : ()),
    check_syntax => $check
);

my $tests = $compiler->write_tests;

if ($prove && exists $tests->{tests}){

    print "\nauto-execute prove on!\n";


    print "executing prove $prove_args for @{$tests->{tests}}...\n\n";

    my $app = App::Prove->new;
    $app->process_args(@{$tests->{tests}}, split ' ', $prove_args);

    print $app->run ? "\ndone!\n" : "prove failed!\n";

}

__END__

=pod

=head1 NAME

ct-build - build (and run) your tests

=head1 VERSION

version 0.12

=head1 SYNOPSIS

ct-init [options]

    Options:

        -help             this help message
        -man              full documentation

        -dir              directory to read CT files (defaults to: $PWD/ct/)
        -out              directory to write tests files (defaults to: $PWD/t/)

        -p
        -prove            run prove command after compiling tests.

        -prove_args       arguments to pass to prove. defaults to '-lv'

        -c
        -check-syntax     check syntax on scripts written
                            note: you may NEVER do "use strict" by yourself
                                  this will make this verification fail every time!

        -no-c
        -no-check-syntax  disable syntax checking

=head1 DESCRIPTION

B<ct-init> will create a new Test::CT directory from interative input.

=head1 NAME

ct-build - Compile a CT Directory into tests files.

=head1 ABSTRACT

Compile a CT Directory into tests files.

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-dir>

Directory to read files from CT
defaults to: $PWD/ct/

=item B<-out>

Directory to write tests files generated from CT Directory
defaults to: $PWD/t/

=back

=head1 AUTHOR

Renato Cron <rentocron@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Renato Cron.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
