#!/usr/bin/perl
# PODNAME: inline2test
# ABSTRACT: The Test::Inline 2 Test Compiler

#pod =pod
#pod
#pod =head1 SYNOPIS
#pod
#pod   > inline2test ./inline2test.conf
#pod   
#pod   # In your inline2test.conf
#pod   input=lib
#pod   output=t
#pod   execute=0
#pod   verbose=1
#pod   readonly=1
#pod   header=inline2text.txt
#pod
#pod =head1 DESCRIPTION
#pod
#pod C<inline2test> is the L<Test::Inline> 2 test compiler.
#pod
#pod It's job is to scan through an arbitrary tree of Perl source code files,
#pod locate inline test sections, extract them, convert them to test scripts,
#pod and write them to an output path.
#pod
#pod =cut

use strict;
use File::Spec::Functions  ':ALL';
use Getopt::Long           ();
use Config::Tiny           ();
use Test::Inline           ();
use Test::Inline::IO::File ();

our $VERSION = '2.214';

# Predeclare things
sub stop ($);





#####################################################################
# Process Options, Input and Config

my $execute  = '';
my $changed  = '';
my $verbose  = '';
my $readonly = '';
my $rv = Getopt::Long::GetOptions(
	execute  => \$execute,
	changed  => \$changed,
	verbose  => \$verbose,
	readonly => \$readonly,
	);
exit(0) unless $rv;

# Get the config file
my $config    = shift @ARGV;
my $configdir = 1;
unless ( $config ) {
	# Use a default inline2test.conf in the current directory if it exists
	my $default = catfile( curdir(), 'inline2test.conf' );
	if ( -f $default ) {
		$config = curdir();
	} else {
		stop("You did not provide an config file name");
	}
}
if ( -d $config ) {
	# They point to a directory, not a file
	$configdir = $config;
	my $file = catfile( $config, 'inline2test.conf' );
	if ( -f $file ) {
		$config = $file;
	} else {
		stop("The directory $config does not contain an inline2test.conf file");
	}
}
my $Config = Config::Tiny->read($config) or stop("Failed to load config file");
my %args = %{$Config->{_}}               or stop("No config entries found");

# Add any forced options
$args{execute}  = 1 if $execute;
$args{changed}  = 1 if $changed;
$args{verbose}  = 1 if $verbose;
$args{readonly} = 1 if $readonly;

# Automatically use an inline2test.tpl file if it exists
if ( $configdir and ! $args{template} ) {
	my $file = catfile( $configdir, 'inline2test.tpl' );
	$args{template} = $file if -f $file;
}

# Create ContentHandler if needed
if ( $args{template} ) {
	# Convert to a proper contenthandler
	my $template = delete $args{template};
	$args{ContentHandler} = Test::Inline::Content::Simple->new( $template )
		or stop("Failed to create ContentHandler for $template");
}

# Create InputHandler
if ( $args{input} ) {
	# Convert to a proper inputhandler
	my $input = delete $args{input};
	$args{InputHandler} = Test::Inline::IO::File->new( $input )
		or stop("Failed to create InputHandle for $input");
}

# We need an output
unless ( $args{output} ) {
	stop "No output path specified";
}





#####################################################################
# Generate the Test Scripts

my $Inline = Test::Inline->new( %args )
	or stop "Error creating Test::Inline object";
defined $Inline->add_all or stop "Error during ->add_all()";
defined $Inline->save    or stop "Error while saving scripts";

exit(0) unless $args{execute};





#####################################################################
# Execute Scripts

my $schedule = $Inline->schedule;
unless ( defined $schedule ) {
	stop "Error getting schedule to execute scripts";
}
unless ( $schedule ) {
	stop "Nothing to execute";
}

eval "use ExtUtils::Command::MM;";
die $@ if $@;

@ARGV = map { catfile($args{output}, $_) } @$schedule;
test_harness(0);





#####################################################################
# Support Functions

sub stop ($) {
	print "$_[0]\n";
	exit(1);
}

__END__

=pod

=encoding UTF-8

=head1 NAME

inline2test - The Test::Inline 2 Test Compiler

=head1 VERSION

version 2.214

=head1 DESCRIPTION

C<inline2test> is the L<Test::Inline> 2 test compiler.

It's job is to scan through an arbitrary tree of Perl source code files,
locate inline test sections, extract them, convert them to test scripts,
and write them to an output path.

=head1 SYNOPIS

  > inline2test ./inline2test.conf
  
  # In your inline2test.conf
  input=lib
  output=t
  execute=0
  verbose=1
  readonly=1
  header=inline2text.txt

=head1 SUPPORT

Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Inline>
(or L<bug-Test-Inline@rt.cpan.org|mailto:bug-Test-Inline@rt.cpan.org>).

=head1 AUTHOR

Adam Kennedy <adamk@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2003 by Adam Kennedy.

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