Permalink
Browse files

Make completion functions installable

  • Loading branch information...
1 parent c4ba53e commit 8d1e3275705b0543915212c2c190b35987b3106c @yasuyk yasuyk committed Jan 10, 2014
Showing with 37 additions and 4 deletions.
  1. +27 −4 nodebrew
  2. +8 −0 t/basic.t
  3. +2 −0 t/command.t
View
31 nodebrew
@@ -12,7 +12,8 @@ use warnings;
package Nodebrew;
-use File::Path qw/rmtree/;
+use File::Path qw/rmtree mkpath/;
+use File::Basename qw/basename/;
our $VERSION = '0.7.1';
@@ -23,6 +24,8 @@ sub new {
my @props = qw/
brew_dir
nodebrew_url
+ bash_completion_url
+ zsh_completion_url
remote_list_url
fetcher
tarballs
@@ -52,6 +55,8 @@ sub init {
$self->{current} = $self->{brew_dir} . '/current';
$self->{default_dir} = $self->{brew_dir} . '/default';
$self->{alias_file} = $self->{brew_dir} . '/alias';
+ $self->{bash_completion_dir} = $self->{brew_dir} . '/completions/bash';
+ $self->{zsh_completion_dir} = $self->{brew_dir} . '/completions/zsh';
}
sub run {
@@ -244,6 +249,8 @@ sub _cmd_setup {
mkdir $self->{node_dir} unless -e $self->{node_dir};
mkdir $self->{default_dir} unless -e $self->{default_dir};
mkdir "$self->{default_dir}/bin" unless -e "$self->{default_dir}/bin";
+ mkpath $self->{bash_completion_dir} unless -e $self->{bash_completion_dir};
+ mkpath $self->{zsh_completion_dir} unless -e $self->{zsh_completion_dir};
my $nodebrew_path = "$self->{brew_dir}/nodebrew";
$self->fetch_nodebrew();
@@ -450,10 +457,21 @@ sub fetch_nodebrew {
print "fetching nodebrew...\n";
my $nodebrew_source = $self->{fetcher}->fetch($self->{nodebrew_url});
+ my $bash_completion = $self->{fetcher}->fetch($self->{bash_completion_url});
+ my $zsh_completion = $self->{fetcher}->fetch($self->{zsh_completion_url});
my $nodebrew_path = "$self->{brew_dir}/nodebrew";
+ my $bash_completion_path = $self->{bash_completion_dir} . '/' . basename($self->{bash_completion_url});
+ my $zsh_completion_path = $self->{zsh_completion_dir} . '/' . basename($self->{zsh_completion_url});
- open my $fh, '>', $nodebrew_path or die "Error: $!";
- print $fh $nodebrew_source;
+ $self->make_file($nodebrew_source, $nodebrew_path);
+ $self->make_file($bash_completion, $bash_completion_path);
+ $self->make_file($zsh_completion, $zsh_completion_path);
+}
+
+sub make_file {
+ my ($self, $content, $dest) = @_;
+ open my $fh, '>', $dest or die "Error: $!";
+ print $fh $content;
}
sub get_local_version {
@@ -761,7 +779,10 @@ use Cwd 'abs_path';
sub main {
my $brew_dir = abs_path($ENV{'NODEBREW_ROOT'} || $ENV{'HOME'} . '/.nodebrew');
- my $nodebrew_url = 'https://raw.github.com/hokaccha/nodebrew/master/nodebrew';
+ my $base_url = 'https://raw.github.com/hokaccha/nodebrew/master/';
+ my $nodebrew_url = "${base_url}nodebrew";
+ my $bash_completion_url = "${base_url}completions/bash/nodebrew-completion";
+ my $zsh_completion_url = "${base_url}completions/zsh/_nodebrew";
my $fetcher_type = `which curl` ? 'curl' :
`which wget` ? 'wget' :
die 'Need curl or wget';
@@ -770,6 +791,8 @@ sub main {
Nodebrew->new(
brew_dir => $brew_dir,
nodebrew_url => $nodebrew_url,
+ bash_completion_url => $bash_completion_url,
+ zsh_completion_url => $zsh_completion_url,
remote_list_url => 'http://nodejs.org/dist/',
fetcher => Nodebrew::Fetcher::get($fetcher_type),
tarballs => [
View
8 t/basic.t
@@ -7,6 +7,8 @@ require 'nodebrew';
my $brew_dir = "$FindBin::Bin/.nodebrew";
my $nodebrew_url = 'http://127.0.0.1:8000/nodebrew';
+my $bash_completion_url = 'http://127.0.0.1:8000/completions/bash/nodebrew-completion';
+my $zsh_completion_url = 'http://127.0.0.1:8000/completions/zsh/_nodebrew';
my $remote_list_url = 'http://127.0.0.1:8000/list.html';
my $fetcher = Nodebrew::Fetcher::get('curl');
my $tarballs = [
@@ -18,6 +20,8 @@ my $tarballs_binary = [
my $nodebrew = Nodebrew->new(
brew_dir => $brew_dir,
nodebrew_url => $nodebrew_url,
+ bash_completion_url => $bash_completion_url,
+ zsh_completion_url => $zsh_completion_url,
remote_list_url => $remote_list_url,
fetcher => $fetcher,
tarballs => $tarballs,
@@ -30,6 +34,8 @@ is $nodebrew->{node_dir}, "$brew_dir/node";
is $nodebrew->{current}, "$brew_dir/current";
is $nodebrew->{default_dir}, "$brew_dir/default";
is $nodebrew->{nodebrew_url}, $nodebrew_url;
+is $nodebrew->{bash_completion_url}, $bash_completion_url;
+is $nodebrew->{zsh_completion_url}, $zsh_completion_url;
is $nodebrew->{remote_list_url}, $remote_list_url;
is $nodebrew->{fetcher}, $fetcher;
is $nodebrew->{tarballs}, $tarballs;
@@ -39,6 +45,8 @@ eval {
Nodebrew->new(
brew_dir => $brew_dir,
nodebrew_url => $nodebrew_url,
+ bash_completion_url => $bash_completion_url,
+ zsh_completion_url => $zsh_completion_url,
remote_list_url => $remote_list_url,
fetcher => $fetcher,
);
View
2 t/command.t
@@ -71,6 +71,8 @@ sub run_test {
my $nodebrew = Nodebrew->new(
brew_dir => "$FindBin::Bin/.nodebrew",
nodebrew_url => "$url/nodebrew",
+ bash_completion_url => "$url/completions/bash/nodebrew-completion",
+ zsh_completion_url => "$url/completions/zsh/_nodebrew",
remote_list_url => "$url/list.html",
fetcher => Nodebrew::Fetcher::get('curl'),
tarballs => [

0 comments on commit 8d1e327

Please sign in to comment.