From: Andrew Chilton Date: Sun, 13 Jul 2008 10:34:55 +0000 (+1200) Subject: Added initial hook framework infrastructure (closed #41b351fc). X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c2ac9fe54880e460e0e559ff1b8071ac229b356f;p=cil.git Added initial hook framework infrastructure (closed #41b351fc). * this includes some VCS information we can use in later hooks --- diff --git a/Changes b/Changes index beaf0a4..f22211c 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,7 @@ +0.6.0 (Unreleased) + + * New hook infrastructure, currently only 'issue_post_save' is allowed + 0.5.1 (2008-07-05) * New release minus all the cruft diff --git a/bin/cil b/bin/cil index 4ff4d32..90a7681 100755 --- a/bin/cil +++ b/bin/cil @@ -106,6 +106,9 @@ my %BOOLEAN_ARGS = ( $cil->read_config_user(); $cil->read_config_file(); + # add any hooks we want + # none yet + &{"cmd_$command"}($cil, $args, @ARGV); } @@ -759,6 +762,10 @@ sub add_issue_loop { my $unique_str = time . $issue->Inserted . $issue->Summary . $issue->Description; $issue->set_name( substr(md5_hex($unique_str), 0, 8) ); $issue->save($cil); + + # should probably be run from with $cil + $cil->run_hook('issue_post_save', $issue); + display_issue($cil, $issue); return $issue; @@ -879,6 +886,11 @@ sub print_fsck_errors { } } +## ---------------------------------------------------------------------------- +# hooks + +# none yet + ## ---------------------------------------------------------------------------- # input/output diff --git a/issues/c_7b75b82c.cil b/issues/c_7b75b82c.cil new file mode 100644 index 0000000..a95c0ed --- /dev/null +++ b/issues/c_7b75b82c.cil @@ -0,0 +1,7 @@ +Issue: 41b351fc +CreatedBy: Andrew Chilton +Inserted: 2008-07-13T10:29:22 +Updated: 2008-07-13T10:30:58 + +Added the initial go at the hook system. It seems to work but until we actually +do some, it'll just be a framework for the time being. diff --git a/issues/i_41b351fc.cil b/issues/i_41b351fc.cil index 4c3d040..9e96c08 100644 --- a/issues/i_41b351fc.cil +++ b/issues/i_41b351fc.cil @@ -1,13 +1,14 @@ Summary: Add some sort of hook system -Status: New +Status: Finished CreatedBy: Andrew Chilton AssignedTo: Andrew Chilton Label: Milestone-v0.5 Label: Type-Enhancement +Comment: 7b75b82c Inserted: 2008-07-13T05:08:27 -Updated: 2008-07-13T10:28:36 +Updated: 2008-07-13T10:34:43 -For example, when you add an issue, you might want to do the followin things: +For example, when you add an issue, you might want to do the following things: * reindex all the issues (for faster search - #98203ce8) * generate an issues/index.html file (for poor man's cilweb - #0e004cde) diff --git a/lib/CIL.pm b/lib/CIL.pm index 9a87bf9..1049f59 100644 --- a/lib/CIL.pm +++ b/lib/CIL.pm @@ -23,8 +23,11 @@ package CIL; use strict; use warnings; +use Carp; use File::Glob qw(:glob); +use CIL::VCS::Factory; + use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw( IssueDir @@ -32,6 +35,7 @@ __PACKAGE__->mk_accessors(qw( LabelStrict LabelAllowed VCS UserName UserEmail + vcs hook )); my $defaults = { @@ -47,6 +51,15 @@ my $defaults_user = { UserEmail => 'me@example.com', }; +my $allowed = { + vcs => { + 'Git' => 1, + }, + hook => { + 'issue_post_save' => 1, + }, +}; + ## ---------------------------------------------------------------------------- sub new { @@ -251,7 +264,37 @@ sub read_config_file { $self->LabelStrict( $cfg->{LabelStrict} ); $self->LabelAllowed( $cfg->{LabelAllowed} ); - $self->VCS( $cfg->{VCS} ); + # if we are allowed this VCS, create the hook instance + if ( exists $allowed->{vcs}{$cfg->{VCS}} ) { + $self->VCS( $cfg->{VCS} ); + my $vcs = CIL::VCS::Factory->new( $cfg->{VCS} ); + $self->vcs( $vcs ); + } +} + +sub register_hook { + my ($self, $hook_name, $code) = @_; + + unless ( defined $allowed->{hook}{$hook_name} ) { + croak "hook '$hook_name' not allowed"; + } + + push @{$self->{hook}{$hook_name}}, $code; +} + +sub run_hook { + my ($self, $hook_name, @rest) = @_; + + print "s=$self, hn=$hook_name, r=@rest\n"; + + unless ( defined $allowed->{hook}{$hook_name} ) { + croak "hook '$hook_name' not allowed"; + } + + # call all the hooks with all the args + foreach my $code ( @{$self->hook->{$hook_name}} ) { + &$code( $self, @rest ); + } } ## ---------------------------------------------------------------------------- diff --git a/lib/CIL/VCS/Factory.pm b/lib/CIL/VCS/Factory.pm new file mode 100644 index 0000000..7b427e9 --- /dev/null +++ b/lib/CIL/VCS/Factory.pm @@ -0,0 +1,45 @@ +## ---------------------------------------------------------------------------- +# cil is a Command line Issue List +# Copyright (C) 2008 Andrew Chilton +# +# This file is part of 'cil'. +# +# cil is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . +# +## ---------------------------------------------------------------------------- + +package CIL::VCS::Factory; + +use strict; +use warnings; +use Carp; + +use base qw( Class::Factory ); + +__PACKAGE__->register_factory_type( Git => 'CIL::VCS::Git' ); + +foreach my $method_name ( qw(post_add) ) { + my $method = sub { + my ($self) = @_; + my $class = ref $self || $self; + die "Method '$method_name' not overriden in derived class '$class'"; + }; + + no strict 'refs'; + *{$method_name} = $method; +} + +## ---------------------------------------------------------------------------- +1; +## ---------------------------------------------------------------------------- diff --git a/lib/CIL/VCS/Git.pm b/lib/CIL/VCS/Git.pm new file mode 100644 index 0000000..e4a8286 --- /dev/null +++ b/lib/CIL/VCS/Git.pm @@ -0,0 +1,44 @@ +## ---------------------------------------------------------------------------- +# cil is a Command line Issue List +# Copyright (C) 2008 Andrew Chilton +# +# This file is part of 'cil'. +# +# cil is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . +# +## ---------------------------------------------------------------------------- + +package CIL::VCS::Git; + +use strict; +use warnings; +use Carp; + +use base qw(CIL::VCS::Factory); + +sub post_add { + my ($self, $issue) = @_; + + my $issue_dir = $issue->cil->IssueDir(); + my @files; + push @files, "$issue_dir/i_" . $issue->name . '.cil'; + push @files, map { "$issue_dir/c_${_}.cil" } @{ $issue->CommentList }; + push @files, map { "$issue_dir/a_${_}.cil" } @{ $issue->AttachmentList }; + + return [ "git add @files" ]; +} + +## ---------------------------------------------------------------------------- +1; +## ---------------------------------------------------------------------------- diff --git a/lib/CIL/VCS/Null.pm b/lib/CIL/VCS/Null.pm new file mode 100644 index 0000000..b062771 --- /dev/null +++ b/lib/CIL/VCS/Null.pm @@ -0,0 +1,35 @@ +## ---------------------------------------------------------------------------- +# cil is a Command line Issue List +# Copyright (C) 2008 Andrew Chilton +# +# This file is part of 'cil'. +# +# cil is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . +# +## ---------------------------------------------------------------------------- + +package CIL::VCS::Null; + +use strict; +use warnings; +use Carp; + +foreach my $method_name ( qw(post_add) ) { + no strict 'refs'; + *{"${class}::$method_name"} = sub {}; +} + +## ---------------------------------------------------------------------------- +1; +## ----------------------------------------------------------------------------