Padre-Plugin-Shopify
view release on metacpan or search on metacpan
lib/Padre/Plugin/Shopify.pm view on Meta::CPAN
}
sub menu_plugins_simple {
my $self = shift;
return $self->plugin_name => [
'Create Shop' => sub { $self->create_shop_dialog },
'Open Shop' => sub { $self->open_shop_dialog },
'Pull Open Shops' => sub { $self->pull_all },
'Push Open Shops' => sub { $self->push_all },
'Check Document Syntax' => sub { $self->check_syntax(1) },
'About' => sub { $self->show_about },
];
}
sub projects { return @{$_[0]->{projects}}; }
sub add_project {
my ($self, $project) = @_;
push(@{$self->{projects}}, $project);
return $project;
}
sub remove_project {
my ($self, $project) = @_;
$self->{projects} = [grep { $_ != $project } @{$self->{projects}}];
}
sub show_about {
my $self = shift;
# Generate the About dialog
my $about = Wx::AboutDialogInfo->new;
$about->SetName('Shopify Plug In');
$about->SetDescription('A plugin for the Shopify theme tool.');
# Show the About dialog
Wx::AboutBox($about);
return;
}
sub open_shop_dialog {
my ($self) = @_;
my $main = $self->main;
my $dialog = Wx::DirDialog->new($main, -1);
if ($dialog->ShowModal == Wx::wxID_OK) {
$self->open_shop($dialog->GetPath);
}
}
sub create_shop_dialog {
my ($self) = @_;
my $main = $self->main;
my $dialog = Wx::DirDialog->new($main, "Select an Empty Directory");
$self->create_shop($dialog->GetPath) if $dialog->ShowModal == Wx::wxID_OK;
}
use List::Util qw(first);
use Cwd 'abs_path';
sub open_shop {
my ($self, $directory) = @_;
eval {
die new Padre::Plugin::Shopify::Exception("Unable to find directory.") unless -d $directory;
my ($setting_file, $manifest_file) = ("$directory/.shopsettings", "$directory/.shopmanifest");
die new Padre::Plugin::Shopify::Exception("Unable to find directory files.") unless -e $setting_file && -e $manifest_file;
my $file_settings = decode_json(read_file($setting_file));
return if first { abs_path($_->directory) eq $directory } $self->projects;
$self->add_project(Padre::Plugin::Shopify::Project->new($self, $directory, $file_settings));
};
if ($@) {
$self->main->info(ref($@) ? $@->error : $@);
}
}
sub create_shop {
my ($self, $directory) = @_;
eval {
die new Padre::Plugin::Shopify::Exception("Unable to find directory.") unless -d $directory;
my ($setting_file, $manifest_file) = ("$directory/.shopsettings", "$directory/.shopmanifest");
die new Padre::Plugin::Shopify::Exception("Found already extant settings files. Not creating.") if -e $setting_file || -e $manifest_file;
my $dialog = Wx::Dialog->new($self->main, -1, "Create Shop", [-1, -1], [-1, -1]);
my $grid = Wx::FlexGridSizer->new( 4, 2, 1, 1);
my ($url_edit, $api_key_edit, $password_edit) = map { Wx::TextCtrl->new( $dialog, -1, '', [-1, -1], [400, -1] ) } 0..2;
my ($url_text, $password_text) = map { Wx::StaticText->new( $dialog, -1, $_ )} ("Shop URL", "Password");
my $api_key_text = Wx::ComboBox->new($dialog, -1, "API Key", [-1, -1], [-1, -1], ["API Key", "Email"]);
my ($okay_button, $cancel_button) = map { Wx::Button->new( $dialog, -1, $_ ) } ("OK", "Cancel");
$grid->Add($_, 0, Wx::wxGROW|Wx::wxALL, 2 ) for($url_text, $url_edit, $api_key_text, $api_key_edit, $password_text, $password_edit, $cancel_button, $okay_button);
$dialog->SetAutoLayout( 1 );
$dialog->SetSizer($grid);
$grid->Fit($dialog);
$grid->SetSizeHints($dialog);
Wx::Event::EVT_BUTTON( $dialog, $okay_button, sub { $dialog->EndModal(Wx::wxID_OK); });
Wx::Event::EVT_BUTTON( $dialog, $cancel_button, sub { $dialog->EndModal(Wx::wxID_CANCEL); });
if ($dialog->ShowModal == Wx::wxID_OK) {
my $settings = { url => $url_edit->GetValue, password => $password_edit->GetValue };
$settings->{api_key} = $api_key_edit->GetValue if $api_key_text->GetValue eq "API Key";
$settings->{email} = $api_key_edit->GetValue if $api_key_text->GetValue eq "Email";
$self->add_project(Padre::Plugin::Shopify::Project->new($self, $directory, $settings));
my %saveSettings = map { $_ => $settings->{$_} } keys(%$settings);
write_file("$directory/.shopsettings", encode_json(\%saveSettings));
}
};
if ($@) {
$self->main->info(ref($@) ? $@->error : $@);
}
}
use Padre::Locale::T;
sub plugin_enable {
my $self = shift;
my $return = $self->SUPER::plugin_enable(@_);
if (!Padre::MIME->find("application/liquid")->type) {
Padre::MIME->create(
type => 'application/liquid',
name => 'Liquid',
supertype => 'text/html',
document => 'Padre::Document::Liquid',
extensions => 'liquid'
);
Padre::Wx::Action->new(
name => "view.mime.application/liquid",
label => "Liquid",
comment => _T('Switch document type'),
menu_method => 'AppendRadioItem',
menu_event => sub {
$_[0]->set_mimetype("application/liquid");
},
);
$self->main->refresh;
}
my $config = $self->config_read;
if ($config && $config->{projects}) {
$self->open_shop($_) for (@{$config->{projects}});
}
return $return;
}
sub plugin_disable {
my $self = shift;
$self->config_write( { projects => [ map { $_->directory } $self->projects] } );
for my $package (CHILDREN) {
require Padre::Unload;
Padre::Unload->unload($package);
}
$self->plugin->main->bottom->hide( $_->panel ) for ($self->projects);
$self->SUPER::plugin_disable(@_);
return 1;
}
sub registered_highlighters {
}
sub provided_highlighters {
return (['Padre::Document::Liquid', "Liquid", "Liquid syntax highglithing for padre."]);
}
sub highlighting_mime_types {
( run in 1.053 second using v1.01-cache-2.11-cpan-71847e10f99 )