package Test::MooseX::Compiler;

use strict;
use warnings;
use autodie;

use Exporter qw( import );
use File::Temp qw( tempdir );
use Module::Runtime qw( module_notional_filename );
use Path::Class qw( dir );

our @EXPORT_OK = qw(
    save_class
);

my $Dir = dir( tempdir( CLEANUP => 1 ) );

sub save_class {
    my $class = shift;
    my $code  = shift;

    my $full_code = <<"EOF";
package $class;

use strict;
use warnings;

$code

1;
EOF

    {
        local $@;
        eval $full_code;
        die $@ if $@;
    }

    my $pm_file = module_notional_filename($class);
    my $path    = $Dir->file($pm_file);
    $path->dir()->mkpath( 0, 0755 );

    open my $fh, '>', $path;
    print {$fh} $full_code;
    close $fh;

    $INC{$pm_file} = $path;

    return $pm_file;
}

1;
