Skip to content

Commit

Permalink
Merge 9b1e569 into ea5b212
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyink committed Jul 1, 2022
2 parents ea5b212 + 9b1e569 commit 5de9dbf
Show file tree
Hide file tree
Showing 24 changed files with 1,118 additions and 303 deletions.
2 changes: 1 addition & 1 deletion .mite/config
@@ -1,6 +1,6 @@
---
compiled_to: lib
project: Mite
shim: Mite::Miteception::Shim
shim: Mite::Shim
source_from: lib
perltidy: 1
36 changes: 24 additions & 12 deletions devel.dogfood/compile.pl
Expand Up @@ -43,16 +43,16 @@ sub has {

sub extends {
my (@classes) = @_;
$class->superclasses(\@classes);
$class->superclasses( [ map "Fake::$_", @classes ] );
return;
}

sub compile {
my ( $module, $in_file, $out_file ) = @_;
sub load {
my ( $project, $module, $in_file, $out_file ) = @_;
$in_file //= path sprintf 'lib/%s.pm', ( $module =~ s{::}{/}gr );
$out_file //= path "$in_file.mite.pm";

warn "Compile $module [$in_file -> $out_file]\n";
warn "Load $in_file\n";

my $code = $in_file->slurp;
my ( $head, $tail ) = split '##-', $code;
Expand All @@ -63,12 +63,15 @@ sub compile {

my $source = Mite::Source->new(
file => $in_file,
project => Mite::Project->default,
project => $project,
);
$project->add_sources( $source );

$class = Mite::Class->new(
name => $fake_module,
source => $source,
);
$source->add_classes( $class );

do {
no strict 'refs';
Expand All @@ -82,21 +85,25 @@ sub compile {
local $@;
eval("$head; 1") or die($@);
};
}

# This is bad, but $class->project is undef, so
# otherwise it can't find attributes at all.
#
no warnings 'redefine';
local *Mite::Class::all_attributes = sub { shift->attributes };
sub compile {
my ( $project, $module, $in_file, $out_file ) = @_;
$in_file //= path sprintf 'lib/%s.pm', ( $module =~ s{::}{/}gr );
$out_file //= path "$in_file.mite.pm";

warn "Compile $out_file\n";

$class = $project->class( "Fake::$module" );

local $Type::Tiny::SafePackage = 'package Mite::Miteception;';
my $compiled = $class->compile;
$compiled =~ s/Fake:://;
$compiled =~ s/Fake:://g;
$compiled =~ s/use Mite::Miteception '-Basic'/use Mite::Miteception/;
$out_file->spew( $compiled );
}

compile($_) for qw(
my @packages = qw(
Mite::App::Command
Mite::App::Command::clean
Mite::App::Command::compile
Expand All @@ -107,5 +114,10 @@ sub compile {
Mite::Config
Mite::MakeMaker
Mite::Project
Mite::Role
Mite::Source
);

my $project = Mite::Project->default;
load($project, $_) for @packages;
compile($project, $_) for @packages;
14 changes: 13 additions & 1 deletion lib/Mite/App/Command.pm.mite.pm
@@ -1,6 +1,6 @@
{
package Mite::App::Command;
our $USES_MITE = 1;
our $USES_MITE = q[Mite::Class];
use strict;
use warnings;

Expand Down Expand Up @@ -72,6 +72,18 @@ sub __META__ {
};
}

sub DOES {
my ( $self, $role ) = @_;
our %DOES;
return $DOES{$role} if exists $DOES{$role};
return 1 if $role eq __PACKAGE__;
return $self->SUPER::DOES( $role );
}

sub does {
shift->DOES( @_ );
}


1;
}
14 changes: 13 additions & 1 deletion lib/Mite/App/Command/clean.pm.mite.pm
@@ -1,6 +1,6 @@
{
package Mite::App::Command::clean;
our $USES_MITE = 1;
our $USES_MITE = q[Mite::Class];
use strict;
use warnings;

Expand Down Expand Up @@ -72,6 +72,18 @@ sub __META__ {
};
}

sub DOES {
my ( $self, $role ) = @_;
our %DOES;
return $DOES{$role} if exists $DOES{$role};
return 1 if $role eq __PACKAGE__;
return $self->SUPER::DOES( $role );
}

sub does {
shift->DOES( @_ );
}


1;
}
14 changes: 13 additions & 1 deletion lib/Mite/App/Command/compile.pm.mite.pm
@@ -1,6 +1,6 @@
{
package Mite::App::Command::compile;
our $USES_MITE = 1;
our $USES_MITE = q[Mite::Class];
use strict;
use warnings;

Expand Down Expand Up @@ -72,6 +72,18 @@ sub __META__ {
};
}

sub DOES {
my ( $self, $role ) = @_;
our %DOES;
return $DOES{$role} if exists $DOES{$role};
return 1 if $role eq __PACKAGE__;
return $self->SUPER::DOES( $role );
}

sub does {
shift->DOES( @_ );
}


1;
}
14 changes: 13 additions & 1 deletion lib/Mite/App/Command/init.pm.mite.pm
@@ -1,6 +1,6 @@
{
package Mite::App::Command::init;
our $USES_MITE = 1;
our $USES_MITE = q[Mite::Class];
use strict;
use warnings;

Expand Down Expand Up @@ -72,6 +72,18 @@ sub __META__ {
};
}

sub DOES {
my ( $self, $role ) = @_;
our %DOES;
return $DOES{$role} if exists $DOES{$role};
return 1 if $role eq __PACKAGE__;
return $self->SUPER::DOES( $role );
}

sub does {
shift->DOES( @_ );
}


1;
}
24 changes: 15 additions & 9 deletions lib/Mite/Attribute.pm
Expand Up @@ -13,6 +13,13 @@ has class =>
isa => Object,
weak_ref => true;

has _class_for_default =>
is => rw,
isa => Object,
weak_ref => true,
lazy => true,
builder => sub { shift->class };

has name =>
is => rw,
isa => Str->where('length($_) > 0'),
Expand Down Expand Up @@ -77,7 +84,7 @@ has coderef_default_variable =>
lazy => true, # else $self->name might not be set
default => sub {
# This must be coordinated with Mite.pm
return sprintf '$__%s_DEFAULT__', $_[0]->name;
return sprintf '$%s::__%s_DEFAULT__', $_[0]->_class_for_default->name, $_[0]->name;
};

has [ 'trigger', 'builder' ] =>
Expand Down Expand Up @@ -159,14 +166,13 @@ sub BUILD {
sub clone {
my ( $self, %args ) = ( shift, @_ );

$args{name} //= $self->name;
$args{is} //= $self->is;
my %inherit = %$self;

# Because undef is a valid default
$args{default} = $self->default
if !exists $args{default} and $self->has_default;
# Lazy attributes should be rebuilt by clone
delete $inherit{type} if $args{isa} || $args{type};
delete $inherit{coderef_default_variable};

return ref($self)->new( %args );
return ref($self)->new( %inherit, %args );
}

sub is_private {
Expand Down Expand Up @@ -318,8 +324,8 @@ sub _compile_default {

if ( $self->has_coderef_default ) {
my $var = $self->coderef_default_variable;
return sprintf 'do { our %s; %s->(%s) }',
$var, $var, $selfvar;
return sprintf 'do { my $method = %s; %s->$method }',
$var, $selfvar;
}
elsif ( $self->has_simple_default ) {
require B;
Expand Down

0 comments on commit 5de9dbf

Please sign in to comment.