| 1 | package Jifty::Script::Adopt; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw/Jifty::Script/; | ||
| 7 | |||
| 8 | use File::Copy (); | ||
| 9 | use File::Spec (); | ||
| 10 | use File::Basename (); | ||
| 11 | |||
| 12 | use Jifty::Util; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Jifty::Script::Adopt - localize a stock jifty component | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | jifty adopt web/templates/_elements/nav | ||
| 21 | jifty adopt --ls web/static/ | ||
| 22 | |||
| 23 | Options: | ||
| 24 | --ls <path> list components for adoption | ||
| 25 | --tree <path> list components for adoption | ||
| 26 | |||
| 27 | --help brief help message | ||
| 28 | --man full documentation | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This script will let you create an application-specific replacement for stock | ||
| 33 | Jifty components. For various reasons, Jifty does not actually create these | ||
| 34 | skeleton files in your application's directory tree. While this makes upgrading | ||
| 35 | easier, it can make finding which files to create a little difficult. | ||
| 36 | |||
| 37 | =head2 options | ||
| 38 | |||
| 39 | =over 8 | ||
| 40 | |||
| 41 | =item B<-l>, B<--ls> PATH | ||
| 42 | |||
| 43 | Lists the contents of the stock components path. | ||
| 44 | |||
| 45 | =item B<-t>, B<--tree> PATH | ||
| 46 | |||
| 47 | Lists the contents of the stock components path recursively. | ||
| 48 | |||
| 49 | =item B<--help> | ||
| 50 | |||
| 51 | Print a brief help message and exits. | ||
| 52 | |||
| 53 | =item B<--man> | ||
| 54 | |||
| 55 | Prints the manual page and exits. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub options { | ||
| 62 | my $self = shift; | ||
| 63 | return ( | ||
| 64 | $self->SUPER::options, | ||
| 65 | 'l|ls' => 'list', | ||
| 66 | 't|tree' => 'tree', | ||
| 67 | ) | ||
| 68 | } | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | Creates directories and copies files for you, launching $ENV{EDITOR} if | ||
| 73 | it is defined. | ||
| 74 | |||
| 75 | =head1 METHODS | ||
| 76 | |||
| 77 | =head2 run | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub run { | ||
| 82 | my $self = shift; | ||
| 83 | my (@args) = @_; | ||
| 84 | |||
| 85 | $self->print_help(); | ||
| 86 | |||
| 87 | my $filename = shift(@args); | ||
| 88 | $filename ||= ''; | ||
| 89 | my @parts = split(/[\/\\]/, $filename); | ||
| 90 | |||
| 91 | if($self->{list}) { | ||
| 92 | my $dir = File::Spec->catfile(Jifty::Util->share_root, @parts); | ||
| 93 | unless(-d $dir) { | ||
| 94 | warn "no such directory $dir"; | ||
| 95 | } | ||
| 96 | opendir(my $dh, $dir) or die; | ||
| 97 | my @files = sort(grep(! /^\.\.?$/, readdir($dh))); | ||
| 98 | my @dirs; | ||
| 99 | # sort directories first | ||
| 100 | for(my $i = 0; $i < @files; $i++) { # List::MoreUtil::part ? | ||
| 101 | if(-d File::Spec->catfile($dir, $files[$i])) { | ||
| 102 | push(@dirs, splice(@files, $i, 1) . '/'); | ||
| 103 | $i--; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | print join("\n", @dirs, @files, ''); | ||
| 107 | |||
| 108 | exit; | ||
| 109 | } | ||
| 110 | elsif($self->{tree}) { | ||
| 111 | # Just punting here, maybe don't need this usage except when you | ||
| 112 | # have no tree command? Oh, the irony. | ||
| 113 | my $dir = File::Spec->catfile(Jifty::Util->share_root, @parts); | ||
| 114 | unless(-d $dir) { | ||
| 115 | warn "no such directory $dir"; | ||
| 116 | } | ||
| 117 | |||
| 118 | system('tree', $dir) and die "oops $!"; | ||
| 119 | |||
| 120 | exit; | ||
| 121 | } | ||
| 122 | |||
| 123 | unless($filename) { | ||
| 124 | die "usage: jifty adopt <filename>\n"; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $share = 'share'; | ||
| 128 | unless(-d $share) { | ||
| 129 | die "must be run from your app directory\n"; | ||
| 130 | } | ||
| 131 | |||
| 132 | my $source = File::Spec->catfile(Jifty::Util->share_root, @parts); | ||
| 133 | (-e $source) or die "no such source file '$source'\n"; | ||
| 134 | |||
| 135 | my $dest = File::Spec->catfile($share, @parts); | ||
| 136 | |||
| 137 | unless(-d File::Basename::dirname($dest)) { | ||
| 138 | Jifty::Util->make_path($dest); | ||
| 139 | } | ||
| 140 | |||
| 141 | if(-e $dest) { | ||
| 142 | print "$dest exists, overwrite? [n] "; | ||
| 143 | chomp(my $ans = <STDIN>); $ans ||= 'n'; | ||
| 144 | exit 1 unless(lc($ans) eq 'y'); | ||
| 145 | } | ||
| 146 | File::Copy::copy($source, $dest) or die "copy failed $!"; | ||
| 147 | chmod(0644, $dest) or die "cannot change mode $!"; | ||
| 148 | |||
| 149 | # TODO put an option on that? | ||
| 150 | if($ENV{EDITOR}) { | ||
| 151 | system($ENV{EDITOR}, $dest); | ||
| 152 | } | ||
| 153 | |||
| 154 | } # end run | ||
| 155 | |||
| 156 | # original author: Eric Wilhelm | ||
| 157 | |||
| 158 | 1; | ||
| 159 | # vim:ts=4:sw=4:et:sta |