|
| 1 | +package WGDev::Command::Snapshot; |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use 5.008008; |
| 5 | + |
| 6 | +our $VERSION = '0.2.0'; |
| 7 | + |
| 8 | +use WGDev::Command::Base; |
| 9 | +use Carp; |
| 10 | +BEGIN { our @ISA = qw(WGDev::Command::Base) } |
| 11 | + |
| 12 | +use WGDev::X (); |
| 13 | + |
| 14 | +sub config_options { |
| 15 | + return qw( |
| 16 | + to=s |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +sub process { |
| 21 | + my $self = shift; |
| 22 | + |
| 23 | + require Cwd; |
| 24 | + require File::Spec; |
| 25 | + require File::Path; |
| 26 | + require File::Slurp::Tree; |
| 27 | + |
| 28 | + my $to = $self->option('to') || Cwd::getcwd; |
| 29 | + print "Writing to: $to\n"; |
| 30 | + File::Path::make_path("$to"); |
| 31 | + require Encode; |
| 32 | + for my $asset_spec ( $self->arguments ? $self->arguments : 'root' ) { |
| 33 | + my $asset = eval { $self->wgd->asset->find($asset_spec) }; |
| 34 | + if ( !$asset ) { |
| 35 | + warn $@; |
| 36 | + next; |
| 37 | + } |
| 38 | + my %tree; |
| 39 | + my $assets = $asset->getLineageIterator(['self', 'descendants']); |
| 40 | + while ( my $asset = $assets->() ) { |
| 41 | + my $url = $asset->get('url'); |
| 42 | + File::Path::make_path("$to/$url"); |
| 43 | + $tree{"${url}_"} = Encode::encode('utf8', $self->wgd->asset->serialize($asset)); |
| 44 | + } |
| 45 | + File::Slurp::Tree::spew_tree( $to => \%tree ); |
| 46 | + } |
| 47 | + return 1; |
| 48 | +} |
| 49 | + |
| 50 | +1; |
| 51 | + |
| 52 | +__END__ |
| 53 | +
|
| 54 | +=head1 NAME |
| 55 | +
|
| 56 | +WGDev::Command::Snapshot - Snapshots asset tree to a file tree |
| 57 | +
|
| 58 | +=head1 SYNOPSIS |
| 59 | +
|
| 60 | + wgd snapshot [--to dir] <asset> [<asset> ...] |
| 61 | +
|
| 62 | +=head1 DESCRIPTION |
| 63 | +
|
| 64 | +Snapshots asset tree to file tree. |
| 65 | +
|
| 66 | +=head1 OPTIONS |
| 67 | +
|
| 68 | +=over 8 |
| 69 | +
|
| 70 | +=item C<--to> |
| 71 | +
|
| 72 | +Target directory (defaults to current directory). |
| 73 | +
|
| 74 | +=item C<< <asset> >> |
| 75 | +
|
| 76 | +Either an asset URL, ID, class name. As many can be specified as desired. |
| 77 | +
|
| 78 | +=back |
| 79 | +
|
| 80 | +=head1 AUTHOR |
| 81 | +
|
| 82 | +Patrick Donelan <pat@patspam.com> |
| 83 | +
|
| 84 | +=head1 LICENSE |
| 85 | +
|
| 86 | +Copyright (c) Patrick Donelan. |
| 87 | +
|
| 88 | +This library is free software; you can redistribute it and/or modify it under |
| 89 | +the same terms as Perl itself. |
| 90 | +
|
| 91 | +=cut |
| 92 | +
|
0 commit comments