Last active
December 27, 2015 14:19
-
-
Save Maki-Daisuke/7339872 to your computer and use it in GitHub Desktop.
カレントディレクトリ以下の Git の管理下にあるファイルのみを、指定したディレクトリにコピーするコマンドを実装してみました。
環境変数 GIPLOY_DEST でもコピー先を指定できるので、post-comit hook に登録することもできます。
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
our $VERSION = '0.01'; | |
use constant DEBUG => (!!$ENV{DEBUG}); | |
use Getopt::Std; | |
use File::Spec; | |
use File::Copy 'cp'; | |
$Getopt::Std::STANDARD_HELP_VERSION = 1; | |
sub HELP_MESSAGE { | |
my $fh = shift; | |
print $fh <<END; | |
Usage: $0 [--help] [-C] [DIRECTORY_TO_DEPLOY] | |
Recursively copies only Git-tracked files under the current directory | |
to DIRECTORY_TO_DEPLOY. | |
Options: | |
-C Delete all files/directories in the destination directory | |
before copying. | |
--help Display this help message | |
Environment Variable: | |
You can specify the destination directory by environment variable | |
GIPLOY_DEST instead. You must not omit the argument if the environment | |
variable is unset. | |
Author: | |
Written by Daisuke (Yet another) Maki in 2013. | |
License: | |
This program is free software; you can redistribute it and/or modify | |
it under the same terms as Perl itself. | |
END | |
} | |
my %opts; | |
getopts('C', \%opts); | |
my $dest = @ARGV ? shift : $ENV{GIPLOY_DEST} or do{ HELP_MESSAGE(\*STDERR); exit 1; }; | |
unless ( -d $dest ) { | |
print STDERR "`$dest' is not a directory.\n\n"; | |
HELP_MESSAGE(\*STDERR); | |
exit 1; | |
} | |
if ( $opts{C} ) { # Delere all | |
print "Deleting all files under $dest... "; | |
opendir DIR, $dest; | |
foreach ( grep !/^\.{1,2}$/, readdir DIR ) { | |
my $item = File::Spec->catfile($dest, $_); | |
exec_cmd('rm', '-rf', $item) or error("Can't remove $item"); | |
} | |
closedir DIR; | |
print "done.\n"; | |
} | |
copy_to_dest($_) foreach ls_files(); | |
exit; | |
sub error { | |
print STDERR "ERROR: ", @_, "\n"; | |
exit 1; | |
} | |
sub exec_cmd { | |
my ($cmd, @args) = @_; | |
print "$cmd ", join(" ", @args), "\n" if DEBUG; | |
!system $cmd, @args; | |
} | |
sub ls_files { | |
split /\n/, `git ls-files`; | |
} | |
sub copy_to_dest { | |
my $src = shift; | |
my $dst = File::Spec->catfile($dest, $src); | |
my (undef, $dir, undef) = File::Spec->splitpath($dst); | |
print "Conpying $src -> $dst... "; | |
mkdir_p($dir); | |
-e $src or error "$src is tracked by Git but missing."; | |
cp $src, $dst or error "Can't copy file."; | |
print "done.\n"; | |
} | |
sub mkdir_p { | |
exec_cmd 'mkdir', '-p', shift or exit 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Windowsユーザーのそこのあなた!
そんなあなたは pl2bat で、この Perl スクリプトを WIndows の bat ファイル化することができますよ↓
http://search.cpan.org/perldoc?pl2bat