Skip to content

Commit

Permalink
add stylish-project
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Apr 29, 2010
1 parent 3f50759 commit b6569cd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
15 changes: 15 additions & 0 deletions lib/Stylish/Project.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class Stylish::Project with AnyEvent::Inotify::EventReceiver {
},
);

has 'on_destroy' => (
is => 'ro',
traits => ['Array'],
isa => 'ArrayRef[CodeRef]',
default => sub { [] },
handles => {
on_destroy_hooks => 'elements',
add_destroy_hook => 'push',
},
);

multi method _is_library(Dir $file){ return }

Expand Down Expand Up @@ -133,4 +143,9 @@ class Stylish::Project with AnyEvent::Inotify::EventReceiver {
}

method BUILD { $self->_inotify }

method DEMOLISH {
undef $self->{_inotify};
$_->() for $self->on_destroy_hooks;
}
}
75 changes: 66 additions & 9 deletions lib/Stylish/Server/Session.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Stylish::Server::Session {
use Stylish::Types qw(REPL);
use MooseX::Types::Moose qw(HashRef);
use MooseX::Types::Path::Class qw(Dir);
use MooseX::MultiMethods;

use AnyEvent::REPL;
use Coro;
Expand Down Expand Up @@ -44,7 +45,9 @@ class Stylish::Server::Session {
isa => 'Set::Object',
default => sub { Set::Object->new },
handles => {
add_project => 'insert',
add_project => 'insert',
list_projects => 'members',
remove_project => 'remove',
},
);

Expand All @@ -54,10 +57,11 @@ class Stylish::Server::Session {
default => sub { {} },
traits => ['Hash'],
handles => {
get_repl => 'get',
has_repl => 'exists',
add_repl => 'set',
list_repls => 'keys',
get_repl => 'get',
has_repl => 'exists',
add_repl => 'set',
list_repls => 'keys',
remove_repl => 'delete',
},
);

Expand Down Expand Up @@ -120,10 +124,20 @@ class Stylish::Server::Session {
# rebuild tags table
}

method register_project(Str $name, Dir $root, CodeRef $on_output, CodeRef $on_repl) {
method ensure_project_uniqueness(Str $name, Dir $root){
die 'this project is not unique'
if grep { $_->root eq $root } $self->list_projects;
}

method register_project(Str $name, Dir $root, CodeRef $on_output, CodeRef $on_repl, CodeRef $on_change) {
$self->ensure_project_uniqueness($name, $root);

my $project; $project = Stylish::Project->new(
root => $root,
on_change => [ sub { $self->project_change($project, $name) } ],
on_change => [
$on_change,
sub { $self->project_change($project, $name) },
],
);
$self->add_project($project);

Expand All @@ -135,9 +149,33 @@ class Stylish::Server::Session {

$self->add_repl($name, $repl);

Scalar::Util::weaken($repl->{project});

$project->add_destroy_hook(sub {
$self->remove_repl($name);
});

return { root => $root->stringify, name => $name };
}

multi method unregister_project(Stylish::Project $p){
$self->remove_project($p);
$p->DEMOLISH;
return 1;
}

multi method unregister_project(Dir $root){
for my $project ($self->list_projects) {
if ($root && $project->root eq $root) {
return $self->unregister_project($project);
}
}
}

multi method unregister_project(Str $name){
$self->unregister_project($self->get_repl($name)->project);
}

method repl(Str $repl_name, Str $code, CodeRef $on_output){
my $repl = $self->get_repl($repl_name);
# todo: return an error if the REPL is busy? or just queue it
Expand Down Expand Up @@ -202,7 +240,9 @@ class Stylish::Server::Session {
return 1;
}
when('register_project'){
my $dir = Path::Class::dir($args->{root} || die 'need root');
my $dir = Path::Class::dir($args->{root} || die 'need root')
->resolve->absolute;

my $name = $args->{name} || $dir->basename;
return $self->register_project(
$name, $dir,
Expand All @@ -218,12 +258,29 @@ class Stylish::Server::Session {
repl => $name,
});
},
sub {
$respond_cb->( 'project_change', {
project => $name,
});
},
);
}
when('unregister_project') {
my $name = $args->{name};
my $root = $args->{root};
die 'need root or name' if !$name && !$root;

$root = Path::Class::dir($root)->resolve->absolute
if $root;

return $self->unregister_project($root || $name);
}
when('list_projects'){
return [ $self->list_projects ];
}
default {
die "unknown command '$cmd'";
}
}
}

}

0 comments on commit b6569cd

Please sign in to comment.