# Copyright (c) 2006-2010 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Reload.pm,v 1.18 2010/06/02 07:18:53 schellj Exp $ package Bivio::Test::Reload; use strict; use Bivio::Base 'Bivio.UNIVERSAL'; use File::Find (); our($VERSION) = sprintf('%d.%02d', q$Revision: 1.18 $ =~ /\d+/g); my($_CL) = b_use('IO.ClassLoader'); my($_R) = b_use('Agent.Request'); my($_LAST_TIME) = time; my($_WATCH) = [grep(s{/BConf.pm$}{}, @{[values(%INC)]})]; # File::Find doesn't put '.' in the Path #TODO: Use "relative_path", which I think File::Find uses my($_INC) = [map($_ eq '.' ? '' : "$_/", @INC)]; Bivio::IO::Alert->info('Watching: ', $_WATCH); my($_HANDLERS) = b_use('Biz.Registrar')->new; my($_DDL); my($_DONE) = 1; $_R->if_apache_version(2 => sub { use attributes (); Bivio::Die->eval(q{use attributes __PACKAGE__, \&handler, 'handler'}); $_DONE = b_use('Ext.ApacheConstants')->OK; return; }); sub handler { if (my $modified = _modified_pm()) { map($_HANDLERS->call_fifo(handle_unload_class => [$_]), @$modified); _do(delete_require => $modified); _do(simple_require => $modified); map($_HANDLERS->call_fifo(handle_reload_class => [$_]), @$modified); } foreach my $modified (@{_modified_ddl()}) { my($realm, $path, $file) = @$modified; b_use('ShellUtil.RealmFile')->main( '-realm', $realm, '-input', $file, create_or_update => $path, ); } $_LAST_TIME = time; $_R->clear_current; return $_DONE; } sub register_handler { shift; $_HANDLERS->push_object(@_); return; } sub _do { my($method, $modules) = @_; foreach my $m (@$modules) { Bivio::IO::Alert->info($method, ': ', $m); $_CL->$method($m); } return; } sub _modified_ddl { return [] unless -d ($_DDL ||= Bivio::UI::Facade->get_default->get_local_file_name('DDL', '')); my($res) = []; File::Find::find({ no_chdir => 1, wanted => sub { my($p) = $File::Find::name; if ($p =~ m{(?:^|/)(?:\.|CVS$)}) { $File::Find::prune = 1; return; } return if $p =~ m{[\~\#]$}s || -d $p; push(@$res, _modified_ddl_file($1, $2, $p)) if $p =~ m{ddl/([\w-]+)(/Public/.*)}is; return; }, }, $_DDL); return $res; } sub _modified_ddl_file { my($realm, $path, $file) = @_; return _newer($file) ? Bivio::IO::Alert->debug([$realm, $path, $file]) : (); } sub _modified_pm { my($res) = []; File::Find::find({ no_chdir => 1, wanted => sub { push(@$res, _modified_pm_path($_)) unless $File::Find::prune = $_ =~ m{(?:^|/)(?:Test|files|CVS|t|\..*)$}s; return; }, }, @$_WATCH); return @$res ? $res : undef; } sub _modified_pm_path { my($path) = @_; return unless $path =~ /\.pm$/ and _newer($path) and $path = (map($path =~ m{^$_(.*)\.pm$}, @$_INC))[0]; $path =~ s{/}{::}g; return $path; } sub _newer { my($file) = @_; return ((stat($file))[9] || 0) > $_LAST_TIME; } 1;