Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Arthur Axel 'fREW' Schmidt
committed
Jul 20, 2011
0 parents
commit 5066fb7
Showing
9 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package Javascript::Dependancy::Manager; | ||
|
||
use Moo; | ||
use Sub::Quote; | ||
use Tie::IxHash; | ||
|
||
has lib_dir => ( | ||
is => 'ro', | ||
required => 1, | ||
); | ||
|
||
has recurse => ( | ||
is => 'ro', | ||
default => quote_sub q{ 0 }, | ||
); | ||
|
||
has scan_files => ( | ||
is => 'ro', | ||
default => quote_sub q{ 1 }, | ||
); | ||
|
||
has _scanned_files => ( | ||
is => 'rw', | ||
); | ||
|
||
# hashref where the provision (what a file provides) is the key, | ||
# and an arrayref of the files that provide the feature are the value | ||
has provisions => ( | ||
is => 'ro', | ||
default => quote_sub q{ {} }, | ||
); | ||
|
||
# hashref where the filename is the key, and the value is required provisions in | ||
# an arrayref | ||
has requirements => ( | ||
is => 'ro', | ||
default => quote_sub q{ {} }, | ||
); | ||
|
||
sub file_list_for_provisions { | ||
my ($self, $provisions) = @_; | ||
|
||
if ($self->scan_files && !$self->_scanned_files) { | ||
for my $dir (@{$self->lib_dir}) { | ||
$self->scan_dir($dir); | ||
} | ||
$self->_scanned_files(1); | ||
} | ||
|
||
my %ret; | ||
tie %ret, 'Tie::IxHash'; | ||
for my $requested_provision (@$provisions) { | ||
my $files = $self->files_providing($requested_provision) | ||
|
||
# for now we just use the first file | ||
my $file = $files->[0]; | ||
if (my $requirements = $self->requirements->{$files->[0]}) { | ||
$ret{$_} = 1 for $self->file_list_for_provisions($requirements); | ||
} | ||
$ret{$file} = 1; | ||
} | ||
|
||
return keys %ret; | ||
} | ||
|
||
sub scan_dir { | ||
my ($self, $dir) = @_; | ||
my $dh; | ||
opendir $dh, $dir; | ||
my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dh; | ||
for (@files) { | ||
my $fqfn = "$dir/$_"; | ||
$self->scan_dir($fqfn) if $self->recurse && -d $fqfn; | ||
$self->scan_file($fqfn) if -f $fqfn; | ||
} | ||
} | ||
|
||
sub scan_file { | ||
my ($self, $file) = @_; | ||
open my $fh, '<', $file; | ||
while (<$fh>) { | ||
if (m[//\s*provides:\s*(\S+)]) { | ||
$self->provisions->{$1} ||= []; | ||
push @{$self->provisions->{$1}}, $file | ||
} elsif (m[//\s*requires:\s*(\S+)]) { | ||
$self->requirements->{$file} ||= []; | ||
push @{$self->requirements->{$file}}, $1 | ||
} | ||
} | ||
} | ||
|
||
sub files_providing { | ||
my ($self, $provision) = @_; | ||
|
||
$self->provisions->{$provision} | ||
or die "no such provision '$requested_provision' found!"; | ||
} | ||
|
||
1; | ||
|
||
# Dedicated to Arthur Dale Schmidt | ||
# This code was written at the Tallulalh Travel Center in Louisiana |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
|
||
use Javascript::Dependancy::Manager; | ||
|
||
my $mgr = Javascript::Dependancy::Manager->new( | ||
lib_dir => ['t/js-lib'], | ||
recurse => 1, | ||
provisions => { | ||
extjs => ['t/js-lib/ext/ext-all.js', 't/js-lib/ext/ext-all-debug.js'], | ||
}, | ||
requirements => { | ||
't/js-lib/ext/ext-all.js' => ['ext-core'], | ||
}, | ||
); | ||
|
||
is_deeply | ||
[$mgr->file_list_for_provisions(['b', 'a'])], | ||
['t/js-lib/ext/ext-core.js','t/js-lib/ext/ext-all.js','t/js-lib/A.js','t/js-lib/B.js'], | ||
'basic deps works'; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// provides: a | ||
// requires: extjs | ||
function a(arg) { | ||
console.log(arg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// provides: b | ||
// requires: a | ||
function b(arg) { | ||
a(arg + 'b') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// provides: c | ||
// requires: b | ||
function c(arg) { | ||
b(arg + 'c') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var console = { | ||
log: function(out) { | ||
return out + zomglowlevel(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var console = { log: function(out) { return out + zomglowlevel() } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// provides: ext-core | ||
function zomglowlevel() { | ||
return 1 + 2 | ||
} |