diff --git a/addons/ThemeExport.plugin/config.yaml b/addons/ThemeExport.plugin/config.yaml index 022199651..c83579fc4 100644 --- a/addons/ThemeExport.plugin/config.yaml +++ b/addons/ThemeExport.plugin/config.yaml @@ -3,7 +3,7 @@ name: 'Theme Exporter' description: "A plugin that provides a simple user interface for exporting a blog's templates into an easily distributable theme." plugin_author: 'Endevver, LLC.' plugin_link: 'http://endevver.com/plugins/theme-export/' -version: 1.0 +version: 1.0.1 static_version: 1 applications: diff --git a/addons/ThemeExport.plugin/lib/MT/Theme/Exporter.pm b/addons/ThemeExport.plugin/lib/MT/Theme/Exporter.pm index ab2ce273e..dacccfbf5 100644 --- a/addons/ThemeExport.plugin/lib/MT/Theme/Exporter.pm +++ b/addons/ThemeExport.plugin/lib/MT/Theme/Exporter.pm @@ -8,6 +8,7 @@ use File::Copy::Recursive qw(dircopy); use File::Path; use File::Spec; use MT; +use MT::PublishOption; use MT::FileMgr; use MT::Util qw( dirify ); use YAML::Tiny; @@ -33,8 +34,8 @@ sub new { my $params = shift; my $self = {}; foreach my $prop ( - qw( id key pack_name pack_description pack_version dryrun verbose - logger app author_name author_link basedir outdir zip ) + qw( id key pack_name pack_description pack_version dryrun verbose + logger app author_name author_link basedir outdir zip ) ) { if ( exists $params->{$prop} ) { @@ -60,10 +61,11 @@ sub new { # TODO - error check #my @formats = MT::Util::Archive->available_formats(); require MT::Util::Archive; - $self->{'zipfilepath'} = File::Spec->catfile( $self->{'outdir'}, - $self->{'basedir'} . ".zip" ); - $self->{'ziparchive'} - = MT::Util::Archive->new( 'zip', $self->{'zipfilepath'} ); + $self->{'zipfilepath'} = + File::Spec->catfile( $self->{'outdir'}, + $self->{'basedir'} . ".zip" ); + $self->{'ziparchive'} = + MT::Util::Archive->new( 'zip', $self->{'zipfilepath'} ); } @@ -72,20 +74,19 @@ sub new { $opts{'author_name'} = $self->{'author_name'} if $self->{'author_name'}; $self->{'yaml'} = YAML::Tiny->new; - $self->{'yaml'}->[0] - = { - id => $self->{'id'}, - key => $self->{'key'}, - name => $self->{'pack_name'}, - description => $self->{'pack_description'}, - %opts - }; + $self->{'yaml'}->[0] = { + id => $self->{'id'}, + key => $self->{'key'}, + name => $self->{'pack_name'}, + description => $self->{'pack_description'}, + %opts + }; # End init code bless $self, $class; return $self; -} ## end sub new +} sub _archive_file { my $self = shift; @@ -104,9 +105,9 @@ sub write_config { sub write { my $self = shift; - my $filepath - = File::Spec->catfile( $self->{'outdir'}, $self->{'basedir'}, 'plugins', - $self->{'key'}, 'config.yaml' ); + my $filepath = + File::Spec->catfile( $self->{'outdir'}, $self->{'basedir'}, 'plugins', + $self->{'key'}, 'config.yaml' ); $self->_debug(" - Writing config.yaml file $filepath"); unless ( $self->_is_dryrun() ) { my $header = ''; @@ -127,16 +128,16 @@ sub write { if ( $self->{'zip'} ) { $self->_archive_file( - $self->{'outdir'}, - File::Spec->catfile( - $self->{'basedir'}, 'plugins', - $self->{'key'}, 'config.yaml' - ) + $self->{'outdir'}, + File::Spec->catfile( + $self->{'basedir'}, 'plugins', + $self->{'key'}, 'config.yaml' + ) ); $self->{'ziparchive'}->close(); } - } ## end unless ( $self->_is_dryrun...) -} ## end sub write + } +} sub export { my $self = shift; @@ -151,33 +152,34 @@ sub export { # Initialize the data structure for the current template set # we are exporting. $self->{'yaml'}->[0]->{'template_sets'}->{$ts_id} = { - label => $name, - base_path => File::Spec->catdir( 'templates', $ts_id ) + label => $name, + base_path => File::Spec->catdir( 'templates', $ts_id ) }; # Create a shortcut to our template set node, this is in essense a cursor # pointing to the current template set being exported. $self->{'current_ts'} = $self->{'yaml'}->[0]->{'template_sets'}->{$ts_id}; - # Set the current template path. Each theme will live in its own subdirectory - # under the theme pack's 'templates' directory. + # Set the current template path. Each theme will live in its own subdirectory + # under the theme pack's 'templates' directory. my $templates_path = File::Spec->catdir( $self->{'outdir'}, $self->{'basedir'}, 'plugins', - $self->{'key'}, 'templates', $ts_id ); + $self->{'key'}, 'templates', $ts_id ); unless ( $self->_is_dryrun() ) { $self->{'fmgr'}->exists($templates_path) or $self->{'fmgr'}->mkpath($templates_path) or die sprintf( "Could not make path %s: %s\n", - $templates_path, $self->{'fmgr'}->errstr ); + $templates_path, $self->{'fmgr'}->errstr ); } # TODO - static files need utilize config assistant's new static file structure - my $from - = File::Spec->catdir( _static_file_path( $self->{'app'} ), $static ); + my $from = + File::Spec->catdir( _static_file_path( $self->{'app'} ), $static ); if ( -e $from && $static ) { - my $to = File::Spec->catdir( $self->{'outdir'}, $self->{'basedir'}, - 'mt-static', $static ); + my $to = + File::Spec->catdir( $self->{'outdir'}, $self->{'basedir'}, + 'mt-static', $static ); $self->_debug( 'Copying static files from ' . $from . ' to ' . $to ); unless ( $self->_is_dryrun() ) { $self->{'fmgr'}->mkpath($to); @@ -199,14 +201,14 @@ sub export { foreach map { @{ _template_group( $blog_id, $_ ) } } ( $set_type, 'all' ); - # Store the provided configuration data in the node - # corresponding to the template's type and basename. - # REMOVED because it is in _process_templates - # $self->{'current_ts'}->{templates}->{ $cfg->{type} }->{ $cfg->{basename} } - # = $cfg->{data}; + # Store the provided configuration data in the node + # corresponding to the template's type and basename. + # REMOVED because it is in _process_templates + # $self->{'current_ts'}->{templates}->{ $cfg->{type} }->{ $cfg->{basename} } + # = $cfg->{data}; $self->_process_export_queue(); -} ## end sub export +} =head2 @@ -231,20 +233,18 @@ sub _process_export_queue { # FIXME: this is hard coded to global templates based upon the assumption that # only global templates will be automatically included # my $cfg = $self->_process_templates( $inc->{'blog_id'}, 'global', { - my $cfg = - $self->_process_templates( - 0, 'global', - { - type => 'module', - basename => dirify( $inc->{'name'} ), - load_args => [ { blog_id => 0, id => $inc->{'id'} } ] - } - ); + my $cfg = $self->_process_templates( + 0, 'global', + { type => 'module', + basename => dirify( $inc->{'name'} ), + load_args => [ { blog_id => 0, id => $inc->{'id'} } ] + } + ); $seen{ $inc->{id} } = 1; } - } ## end foreach my $inc ( sort { $a...}) -} ## end sub _process_export_queue + } +} =head2 @@ -264,34 +264,31 @@ sub _template_group { # Blog-level templates # blog => [ { - type => 'index', - load_args => { %blog_id, type => 'index' }, - config => sub { - { - outfile => $_[0]->outfile, - rebuild_me => $_[0]->rebuild_me - }; - }, + type => 'index', + load_args => { %blog_id, type => 'index' }, + config => sub { + { outfile => $_[0]->outfile, + rebuild_me => $_[0]->rebuild_me + }; + }, }, - { - type => 'archive', - config => \&_templatemap_config, - load_args => [ - { %blog_id, type => 'archive' } => -or => - { %blog_id, type => 'individual' } => -or => - { %blog_id, type => 'page' }, - ], + { type => 'archive', + config => \&_templatemap_config, + load_args => [ + { %blog_id, type => 'archive' } => -or => + { %blog_id, type => 'individual' } => -or => + { %blog_id, type => 'page' }, + ], }, - { - type => 'system', - basename => sub { $_[0]->type }, - load_args => [ - { %blog_id, type => 'popup_image' } => -or => - { %blog_id, type => 'dynamic_error' } => -or => - { %blog_id, type => 'search_results' } => -or => - { %blog_id, type => 'comment_preview' } => -or => - { %blog_id, type => 'comment_response' } - ], + { type => 'system', + basename => sub { $_[0]->type }, + load_args => [ + { %blog_id, type => 'popup_image' } => -or => + { %blog_id, type => 'dynamic_error' } => -or => + { %blog_id, type => 'search_results' } => -or => + { %blog_id, type => 'comment_preview' } => -or => + { %blog_id, type => 'comment_response' } + ], } ], @@ -299,42 +296,37 @@ sub _template_group { # Global shared templates # global => [ { - type => 'system', - basename => sub { $_[0]->type }, - load_args => [ - { %blog_id, type => 'login_form' } => -or => - { %blog_id, type => 'profile_view' } => -or => - { %blog_id, type => 'profile_feed' } => -or => - { %blog_id, type => 'register_form' } => -or => - { %blog_id, type => 'profile_error' } => -or => - { %blog_id, type => 'profile_edit_form' } => - -or => - { %blog_id, type => 'password_reset_form' } => - -or => - { %blog_id, type => 'register_confirmation' } - ], - }, - { - type => 'email', - load_args => { %blog_id, type => 'email' }, - }, + type => 'system', + basename => sub { $_[0]->type }, + load_args => [ + { %blog_id, type => 'login_form' } => -or => + { %blog_id, type => 'profile_view' } => -or => + { %blog_id, type => 'profile_feed' } => -or => + { %blog_id, type => 'register_form' } => -or => + { %blog_id, type => 'profile_error' } => -or => + { %blog_id, type => 'profile_edit_form' } => -or => + { %blog_id, type => 'password_reset_form' } => -or => + { %blog_id, type => 'register_confirmation' } + ], + }, + { type => 'email', + load_args => { %blog_id, type => 'email' }, + }, ], # # Templates found in both the global shared and blog sets # all => [ { - type => 'module', - load_args => { %blog_id, type => 'custom' }, - }, - { - type => 'widget', - load_args => { %blog_id, type => 'widget' }, - }, - { - type => 'widgetset', - load_args => { %blog_id, type => 'widgetset' }, - }, + type => 'module', + load_args => { %blog_id, type => 'custom' }, + }, + { type => 'widget', + load_args => { %blog_id, type => 'widget' }, + }, + { type => 'widgetset', + load_args => { %blog_id, type => 'widgetset' }, + }, ], ); @@ -345,7 +337,7 @@ sub _template_group { unless exists $template_groups{$key}; return $template_groups{$key}; -} ## end sub _template_group +} sub _process_templates { my $self = shift; @@ -363,7 +355,7 @@ sub _process_templates { # use Data::Dumper; # $self->_debug(Dumper($t)); $self->_debug( sprintf ' - Creating %s: %s', - $args->{type}, $t->name ); + $args->{type}, $t->name ); my $basename ||= $t->identifier || _create_basename( $t->name ); @@ -383,7 +375,8 @@ sub _process_templates { my $includes = $self->_find_includes($t); foreach my $i (@$includes) { - push @{ $self->{'export_queue'} }, $i if ( $i->{blog_id} eq 0 ); + push @{ $self->{'export_queue'} }, $i + if ( $i->{blog_id} eq 0 ); # TODO - for now it might be best if this only processed # global templates? @@ -392,13 +385,25 @@ sub _process_templates { # Write the template text out to a file within the plugin envelope if ( $self->_write_tmpl( $t, $ts_id, $args->{type}, $basename ) ) { - + print "Wrote template " . $t->name . " (".$t->type.",build=".$t->build_type.")\n"; # Using the template in context, compile the YAML config my $cfg = { - label => $t->name, - filename => - File::Spec->catfile( $args->{type}, $basename . '.mtml' ) + label => $t->name, + filename => File::Spec->catfile( $args->{type}, $basename . '.mtml' ), + ($t->type eq 'index' && $t->build_type != MT::PublishOption::ONDEMAND()) ? ( build_type => $t->build_type ) : (), }; + use Data::Dumper; +# print Dumper($cfg); + if ($t->type =~ /^(module|widget|custom)$/) { + $cfg->{'include_with_ssi'} = 1 if ($t->include_with_ssi); + foreach (qw( expire_type expire_event expire_interval )) { + my $var = 'cache_'.$_; + if (my $val = $t->$var()) { + $cfg->{cache}->{$_} = $val; + $cfg->{cache}->{$_} *= 60 if ($_ eq 'expire_interval'); + } + } + } # The "config" attribute can be either a code or hash reference if ( 'CODE' eq ref( $args->{config} ) ) { @@ -412,10 +417,10 @@ sub _process_templates { # corresponding to the template's type and basename. if ( $blog_id == 0 ) { unless ( $self->{'yaml'}->[0]->{'default_templates'} - ->{'base_path'} ) + ->{'base_path'} ) { - $self->{'yaml'}->[0]->{'default_templates'}->{'base_path'} - = 'templates/global'; + $self->{'yaml'}->[0]->{'default_templates'} + ->{'base_path'} = 'templates/global'; } $self->{'yaml'}->[0]->{'default_templates'} ->{ 'global:' . $args->{'type'} }->{$basename} = $cfg; @@ -424,9 +429,9 @@ sub _process_templates { $self->{'current_ts'}->{'templates'}->{ $args->{'type'} } ->{$basename} = $cfg; } - } ## end if ( $self->_write_tmpl...) - } ## end while ( my $t = $iter->) -} ## end sub _process_templates + } + } +} sub _templatemap_config { my $t = shift; @@ -437,17 +442,18 @@ sub _templatemap_config { my $type = lc( $map->archive_type ); $type =~ s/ /-/g; $mappings->{$type} = { - archive_type => $map->archive_type, - preferred => $map->is_preferred, - $map->file_template - && $map->file_template ne '~' && $map->file_template ne '' - ? ( file_template => $map->file_template ) - : (), + archive_type => $map->archive_type, + preferred => $map->is_preferred, + $map->build_type != MT::PublishOption::ONDEMAND() ? (build_type => $map->build_type) : (), + $map->file_template + && $map->file_template ne '~' && $map->file_template ne '' + ? ( file_template => $map->file_template ) + : (), }; } return { mappings => $mappings }; -} ## end sub _templatemap_config +} sub _create_basename { my $name = shift; @@ -495,27 +501,24 @@ sub _write_tmpl { unless ($basename) { $self->_debug( - sprintf 'No template basename for template "%s". Skipping...', - $tmpl->name ); + sprintf 'No template basename for template "%s". Skipping...', + $tmpl->name ); return; } if ( $self->{'created'}{"$ts_id/$type/$basename"} ) { $self->_error( - sprintf - 'Template basename "%s" previously used for template "%s".' - . ' Skipping...', - "$ts_id/$type/$basename", $tmpl->name + sprintf + 'Template basename "%s" previously used for template "%s".' + . ' Skipping...', + "$ts_id/$type/$basename", $tmpl->name ); return; } - my $fn = File::Spec->catfile( - $self->{'basedir'}, 'plugins', - $self->{'key'}, 'templates', - $ts_id, $type, - $basename . '.mtml' - ); + my $fn = + File::Spec->catfile( $self->{'basedir'}, 'plugins', $self->{'key'}, + 'templates', $ts_id, $type, $basename . '.mtml' ); my $fn_abs = File::Spec->catfile( $self->{'outdir'}, $fn ); $self->_debug("\t-- Writing template text file out to $fn"); @@ -523,7 +526,7 @@ sub _write_tmpl { $self->{'fmgr'}->exists( dirname($fn_abs) ) or $self->{'fmgr'}->mkpath( dirname($fn_abs) ) or die sprintf( "Could not make path %s: %s\n", - dirname($fn_abs), $self->{'fmgr'}->errstr ); + dirname($fn_abs), $self->{'fmgr'}->errstr ); open FILE, ">$fn_abs" or die "Could not open $fn_abs for writing: $!\n"; @@ -532,7 +535,7 @@ sub _write_tmpl { } $self->_archive_file( $self->{'outdir'}, $fn ); $self->{'created'}{"$ts_id/$type/$basename"} = $tmpl->name; -} ## end sub _write_tmpl +} sub _find_includes { my $self = shift; @@ -551,32 +554,30 @@ sub _find_includes { next if exists $seen{$type}{$mod}; $seen{$type}{$mod} = 1; my $other = MT->model('template')->load( { - blog_id => ( - $tag->[1]->{global} - ? 0 - : [ $obj->blog_id, 0 ] - ), - name => $mod, - type => $type, - }, - { - sort => 'blog_id', - direction => 'descend', - } + blog_id => ( + $tag->[1]->{global} + ? 0 + : [ $obj->blog_id, 0 ] + ), + name => $mod, + type => $type, + }, + { sort => 'blog_id', + direction => 'descend', + } ); if ($other) { push @includes, - { - type => $type, + { type => $type, name => $mod, id => $other->id, blog_id => $other->blog_id }; } - } ## end foreach my $tag (@$includes) - } ## end if ( my $includes = $obj...) + } + } return \@includes; -} ## end sub _find_includes +} 1; __END__ diff --git a/addons/ThemeExport.plugin/lib/ThemeExport/Plugin.pm b/addons/ThemeExport.plugin/lib/ThemeExport/Plugin.pm index d30198656..670fb2f50 100644 --- a/addons/ThemeExport.plugin/lib/ThemeExport/Plugin.pm +++ b/addons/ThemeExport.plugin/lib/ThemeExport/Plugin.pm @@ -7,7 +7,7 @@ use MT::Util qw( format_ts epoch2ts caturl ); sub export { my $app = shift; - my $q = $app->query; + my $q = $app->{query}; my $blog = $app->blog; $| = 1; @@ -16,38 +16,35 @@ sub export { my $temp_dir = $app->config('TempDir'); require File::Temp; - my $basedir - = File::Temp::tempdir( 'mt-theme-export-XXXX', DIR => $temp_dir ); - my $fmgr = MT::FileMgr->new('Local'); + my $basedir = + File::Temp::tempdir( 'mt-theme-export-XXXX', DIR => $temp_dir ); + my $fmgr = MT::FileMgr->new('Local'); my $options = { - verbose => 1, - zip => 1, - logger => sub { $app->print( $_[0] ); }, - fmgr => $fmgr, - pack_name => $q->param('theme_name') || '', - pack_description => $q->param('theme_description') || '', - pack_version => $q->param('theme_version') || '', - author_name => $q->param('designer_name') || '', - author_link => $q->param('designer_link') || '', - outdir => $basedir + verbose => 1, + zip => 1, + logger => sub { $app->print( $_[0] ); }, + fmgr => $fmgr, + pack_name => $q->param('theme_name') || '', + pack_description => $q->param('theme_description') || '', + pack_version => $q->param('theme_version') || '', + author_name => $q->param('designer_name') || '', + author_link => $q->param('designer_link') || '', + outdir => $basedir }; my $exporter = MT::Theme::Exporter->new($options); - $exporter->export( - { blog_id => $blog->id, name => $q->param('theme_name') } ); + $exporter->export( { + blog_id => $blog->id, + name => $q->param('theme_name') + } + ); $exporter->write_config(); my ( $support_dir, $target_path, $target_url ); - $support_dir = File::Spec->catdir( - 'support', - 'theme-export', - $blog->id, - format_ts( - "%Y-%m-%d", epoch2ts( undef, time ) - ) - ); + $support_dir = + File::Spec->catdir( 'support', 'theme-export', $blog->id, + format_ts( "%Y-%m-%d", epoch2ts( undef, time ) ) ); $target_path = File::Spec->catdir( $app->static_file_path, $support_dir ); - unless ( $fmgr->exists($target_path) ) { $fmgr->mkpath($target_path); } @@ -60,24 +57,25 @@ sub export { File::Path::rmtree($basedir); - return - $app->print( - 'JSON:' - . MT::Util::to_json( - { 'download_url' => $target_url, 'zipfilename' => $zipfilename, } - ) - ); -} ## end sub export + return $app->print( + 'JSON:' + . MT::Util::to_json( { + 'download_url' => $target_url, + 'zipfilename' => $zipfilename, + } + ) + ); +} sub export_start { my $app = shift; - my $q = $app->query; + my $q = $app->{query}; my $blog = $app->blog; my $tmpl = $app->load_tmpl('dialog_export.tmpl'); $tmpl->param( blog_id => $blog->id ); $tmpl->param( theme_name => $blog->name . ' Theme' ); $tmpl->param( theme_description => - "Theme created by the Theme Export Plugin by Endevver." ); + "Theme created by the Theme Export Plugin by Endevver." ); $tmpl->param( theme_version => '1.0' ); $tmpl->param( designer_name => $app->user->nickname ); $tmpl->param( designer_url => $app->user->url ); diff --git a/tools/export-theme b/tools/export-theme index ec839a572..76f1cf7a3 100755 --- a/tools/export-theme +++ b/tools/export-theme @@ -13,7 +13,7 @@ use strict; use warnings; use Carp; -use lib qw( lib extlib plugins/ThemeExport/lib ); +use lib qw( lib extlib addons/ThemeExport.plugin/lib ); use base qw( MT::Tool ); use MT::Theme::Util qw( find_theme_plugin ); @@ -54,13 +54,10 @@ sub help { --zip Produce a zip file of the resulting theme. (default: off) }; -} ## end sub help +} -my ( - $BLOG_IDS, $TS_NAME, $TS_ID, $TS_KEY, $DRYRUN, - $INTERACTIVE, $ZIP, $TS_VERSION, $VERBOSE, $BASE_DIR, - $STATIC, %seen, %static_map -); +my ( $BLOG_IDS, $TS_NAME, $TS_ID, $TS_KEY, $DRYRUN, $INTERACTIVE, $ZIP, + $TS_VERSION, $VERBOSE, $BASE_DIR, $STATIC, %seen, %static_map ); $TS_NAME = 'My Template Set'; $TS_VERSION = '1.0'; @@ -68,15 +65,15 @@ $INTERACTIVE = 0; sub options { return ( - 'blog=s' => \$BLOG_IDS, - 'name=s' => \$TS_NAME, - 'static' => \$STATIC, - 'id=s' => \$TS_ID, - 'key=s' => \$TS_KEY, - 'version=s' => \$TS_VERSION, - 'dryrun' => \$DRYRUN, - 'i' => \$INTERACTIVE, - 'zip' => \$ZIP, + 'blog=s' => \$BLOG_IDS, + 'name=s' => \$TS_NAME, + 'static' => \$STATIC, + 'id=s' => \$TS_ID, + 'key=s' => \$TS_KEY, + 'version=s' => \$TS_VERSION, + 'dryrun' => \$DRYRUN, + 'i' => \$INTERACTIVE, + 'zip' => \$ZIP, ); } @@ -89,36 +86,36 @@ sub main { # No use doing a dry run if we can't see what's happening $VERBOSE = $DRYRUN if $DRYRUN; - - $class->show_usage() && exit if ( !defined $BLOG_IDS ) || !$TS_NAME; + + $class->show_usage() && exit if (! defined $BLOG_IDS) || !$TS_NAME; my $blog; - my ( %pack_opts, %export_opts ); + my (%pack_opts, %export_opts); $pack_opts{'pack_name'} = 'My Theme Pack'; - my $id = $TS_ID; - my $key = $TS_KEY; - my $name = $TS_NAME; - - $id ||= MT::Util::dirify($TS_NAME); - $key ||= MT::Util::dirify($TS_NAME); + my $id = $TS_ID; + my $key = $TS_KEY; + my $name = $TS_NAME; + + $id ||= MT::Util::dirify($TS_NAME); + $key ||= MT::Util::dirify($TS_NAME); my $pack_opts = { - logger => sub { print STDERR $_[0] }, - key => $TS_KEY, - pack_version => $TS_VERSION, - verbose => $VERBOSE, - dryrun => $DRYRUN, - zip => $ZIP, + logger => sub { print STDERR $_[0] }, + key => $TS_KEY, + pack_version => $TS_VERSION, + verbose => $VERBOSE, + dryrun => $DRYRUN, + zip => $ZIP, }; - my @blog_ids = split( ',', $BLOG_IDS ); - if ( $#blog_ids == 0 && !$name ) { + my @blog_ids = split(',',$BLOG_IDS); + if ($#blog_ids == 0 && !$name) { $blog = MT->model('blog')->load( $blog_ids[0] ); $pack_opts->{'pack_name'} = $blog->name . ' Theme Pack'; } - my $exporter = MT::Theme::Exporter->new($pack_opts); + my $exporter = MT::Theme::Exporter->new( $pack_opts ); my $static_base = MT::Theme::Exporter::_static_file_path( MT->instance ); foreach my $blog_id (@blog_ids) { $blog = MT->model('blog')->load( MT::Util::trim($blog_id) ); @@ -127,84 +124,70 @@ sub main { if ( $blog->template_set ) { my $plugin = find_theme_plugin( $blog->template_set ); my $static; - if ( defined( $static_map{ $blog->template_set } ) ) { + if (defined( $static_map{ $blog->template_set }) ) { $export_opts{'static'} = $static_map{ $blog->template_set }; - } - else { - $static = File::Spec->catdir( $static_base, 'plugins', - $plugin->key ); - if ( -e $static ) { - $export_opts{'static'} - = File::Spec->catdir( 'plugins', $plugin->key ); + } else { + $static = File::Spec->catdir( $static_base, 'plugins', $plugin->key ); + if (-e $static) { + $export_opts{'static'} = File::Spec->catdir( 'plugins', $plugin->key ); } } } - if ( !defined( $export_opts{'static'} ) && $INTERACTIVE ) { - PROMPT: - my $path = _prompt( - "Path to static files for " - . $blog->name - . " (press to skip)\n" - . "Relative to '" - . $static_base . "'", - '' - ); + if (!defined($export_opts{'static'}) && $INTERACTIVE) { +PROMPT: + my $path = _prompt("Path to static files for " . $blog->name ." (press to skip)\n". + "Relative to '".$static_base."'",''); my $fullpath = File::Spec->catdir( $static_base, $path ); - if ( !-e $fullpath ) { + if (! -e $fullpath ) { print "The path you entered '$fullpath' does not exist.\n"; goto PROMPT; } - if ( $path ne "" ) { + if ($path ne "") { $export_opts{'static'} = $path; $static_map{ $blog->template_set } = $path; } - } ## end if ( !defined( $export_opts...)) + } my $id = _generate_unique_id( $blog->name ); - $exporter->export( { - blog_id => $blog_id, - name => $blog->name . " Theme", - id => $id, - key => $id, - %export_opts - } - ); - } ## end foreach my $blog_id (@blog_ids) + $exporter->export({ + blog_id => $blog_id, + name => $blog->name . " Theme", + id => $id, + key => $id, + %export_opts + }); + } $exporter->write(); -} ## end sub main +} sub _generate_unique_id { my ($str) = @_; - my $name = MT::Util::dirify($str); - if ( defined $seen{$name} ) { - if ( $name =~ s/_(\d+)$// ) { - $name = $name . ( $1 + 1 ); - return _generate_unique_id($name); - } - else { + my $name = MT::Util::dirify( $str ); + if ( defined $seen{ $name } ) { + if ($name =~ s/_(\d+)$//) { + $name = $name . ($1 + 1); + return _generate_unique_id( $name ); + } else { return _generate_unique_id( $name . "_1" ); } - } - else { - $seen{$name} = 1; + } else { + $seen{ $name } = 1; return $name; } } sub _prompt { - my ( $promptString, $defaultValue ) = @_; + my ($promptString,$defaultValue) = @_; if ($defaultValue) { print $promptString, "[", $defaultValue, "]: "; - } - else { + } else { print $promptString, ": "; } - $| = 1; # force a flush after our print - $_ = ; # get the input from STDIN (presumably the keyboard) + $| = 1; # force a flush after our print + $_ = ; # get the input from STDIN (presumably the keyboard) chomp; if ("$defaultValue") { return $_ ? $_ : $defaultValue; # return $_ if it has a value - } - else { + } else { return $_; } }