[Jifty-commit] r2772 - jifty/trunk/lib/Jifty/Script

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Feb 9 05:36:22 EST 2007


Author: ewilhelm
Date: Fri Feb  9 05:36:21 2007
New Revision: 2772

Added:
   jifty/trunk/lib/Jifty/Script/Adopt.pm
   jifty/trunk/lib/Jifty/Script/Env.pm

Log:
lib/Jifty/Script/Adopt.pm - explore and steal from the stock components directory
lib/Jifty/Script/Env.pm   - braindump Jifty.config.stash and such


Added: jifty/trunk/lib/Jifty/Script/Adopt.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Script/Adopt.pm	Fri Feb  9 05:36:21 2007
@@ -0,0 +1,127 @@
+package Jifty::Script::Adopt;
+
+use warnings;
+use strict;
+
+use base qw/App::CLI::Command/;
+
+use File::Copy ();
+use File::Spec ();
+use File::Basename ();
+
+use Jifty::Util;
+
+=head1 NAME
+
+Jifty::Script::Adopt - localize a stock jifty component
+
+=head1 DESCRIPTION
+
+Creates directories and copies files for you, launching $ENV{EDITOR} if
+it is defined.
+
+=head2 options
+
+=over
+
+=item --ls PATH
+
+List the contents of the stock components path.
+
+=back
+
+=cut
+
+sub options {
+    (
+     'l|ls' => 'list',
+     't|tree' => 'tree',
+    )
+}
+
+=head2 run
+
+  jifty adopt web/templates/_elements/nav
+
+  jifty adopt --ls web/static/
+
+=cut
+
+sub run {
+    my $self = shift;
+    my (@args) = @_;
+
+    my $filename = shift(@args);
+    $filename ||= '';
+    my @parts = split(/[\/\\]/, $filename);
+
+    if($self->{list}) {
+        my $dir = File::Spec->catfile(Jifty::Util->share_root, @parts);
+        unless(-d $dir) {
+            warn "no such directory $dir";
+        }
+        opendir(my $dh, $dir) or die;
+        my @files = sort(grep(! /^\.\.?$/, readdir($dh)));
+        my @dirs;
+        # sort directories first
+        for(my $i = 0; $i < @files; $i++) { # List::MoreUtil::part ?
+            if(-d File::Spec->catfile($dir, $files[$i])) {
+                push(@dirs, splice(@files, $i, 1) . '/');
+                $i--;
+            }
+        }
+        print join("\n", @dirs, @files, '');
+
+        exit;
+    }
+    elsif($self->{tree}) {
+        # Just punting here, maybe don't need this usage except when you
+        # have no tree command?  Oh, the irony.
+        my $dir = File::Spec->catfile(Jifty::Util->share_root, @parts);
+        unless(-d $dir) {
+            warn "no such directory $dir";
+        }
+
+        system('tree', $dir) and die "oops $!";
+
+        exit;
+    }
+
+    unless($filename) {
+        die "usage: jifty adopt <filename>\n";
+    }
+
+    my $share = 'share';
+    unless(-d $share) {
+        die "must be run from your app directory\n";
+    }
+
+    my $source = File::Spec->catfile(Jifty::Util->share_root, @parts);
+    (-e $source) or die "no such source file '$source'\n";
+
+    my $dest = File::Spec->catfile($share, @parts);
+
+    unless(-d File::Basename::dirname($dest)) {
+        Jifty::Util->make_path($dest);
+    }
+
+    if(-e $dest) {
+        print "$dest exists, overwrite? [n]\n";
+        chomp(my $ans = <STDIN>); $ans ||= 'n';
+        exit 1 unless(lc($ans) eq 'y');
+    }
+    File::Copy::copy($source, $dest) or die "copy failed $!";
+    chmod(0644, $dest) or die "cannot change mode $!";
+
+    # TODO put an option on that?
+    if($ENV{EDITOR}) {
+        fork and exit;
+        exec("$ENV{EDITOR} $dest");
+    }
+
+} # end run
+
+# original author:  Eric Wilhelm
+
+1;
+# vim:ts=4:sw=4:et:sta

Added: jifty/trunk/lib/Jifty/Script/Env.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Script/Env.pm	Fri Feb  9 05:36:21 2007
@@ -0,0 +1,92 @@
+package Jifty::Script::Env;
+
+use warnings;
+use strict;
+
+use base qw/App::CLI::Command/;
+
+use Scalar::Util ();
+
+use Jifty::Config;
+use Jifty::YAML;
+
+=head1 NAME
+
+Jifty::Script::Env - access the Jifty environment
+
+=head1 DESCRIPTION
+
+Loads Jifty and your configuration, allowing you to verify and examine
+your setup.
+
+=head2 run
+
+  jifty env <Class> <method> [arguments]
+
+Loads Jifty::Class and calls method on it, providing shortcuts for
+things like:
+
+  perl -MJifty::Util -e 'print Jifty::Util->share_root, "\n";'
+
+The class and method can be combined with a '->' But, unquoted '>' is a
+redirect so simply use the '-' or '.' characters.
+
+  jifty env  Util share_root
+  jifty env 'Util->share_root'
+  jifty env  Util.share_root
+
+You may chain accessors.  A leading dot also means the class is Jifty.
+
+  jifty env Jifty.config.framework ApplicationName
+  jifty env .config.framework ApplicationName
+
+With no arguments, acts as 'C<jifty env Jifty.config.stash>'.
+
+=cut
+
+sub run {
+    my $self = shift;
+    my (@args) = @_;
+
+    Jifty->new();
+
+    unless(@args) {
+        return($self->run('Jifty.config.stash'));
+    }
+
+    my ($class, $method, @and) = split(/(?:->?|\.)/, shift(@args));
+    $class ||= 'Jifty';
+    $method ||= shift(@args);
+
+    my @ans;
+
+    # enable Jifty.config.stash usage
+    unless($class eq 'Jifty') {
+        $class = 'Jifty::' . $class;
+        eval("require $class") or die $@;
+    }
+
+    # walk down the chain of methods
+    unshift(@and, $method);
+    $method = pop(@and);
+    while(my $attrib = shift(@and)) {
+        $class = $class->$attrib;
+    }
+
+    @ans = $class->$method(@args);
+
+    # if something in the answer is a reference, just dump
+    if(grep({Scalar::Util::reftype($_)} @ans)) {
+        print Jifty::YAML::Dump(\@ans);
+    }
+    else {
+        print join("\n", @ans, '');
+    }
+
+
+} # end run
+
+# original author:  Eric Wilhelm
+
+1;
+# vim:ts=4:sw=4:et:sta


More information about the Jifty-commit mailing list