Padre-Plugin-Shopify

 view release on metacpan or  search on metacpan

lib/Padre/Plugin/Shopify.pm  view on Meta::CPAN

	my $height = 30;
	my $self = $package->SUPER::new($project->plugin->main->bottom, -1, [-1,-1], [-1, $height]);
	$self->{project} = $project;
	my $box = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
	my $theme_selector = Wx::ComboBox->new( $self, -1, "<< ALL >>", [-1, -1], [200, $height], ["<< ALL >>"], Wx::wxCB_READONLY);
	my ($home_button, $autopush_button, $pull_button, $push_button, $activate_button, $preview_button, $check_syntax_button) = map { Wx::BitmapButton->new( $self, -1, $self->project->plugin->{images}->{$_}, [-1, -1], [-1, $height], Wx::wxBU_EXACTFIT ) }...
	my $loading_bar = Wx::Gauge->new($self, -1, 100, [-1, -1], [200, $height]);
	$home_button->SetToolTip(Wx::ToolTip->new("Click this to open up the theme.liquid of the selected project. Useful with the project browser."));
	$autopush_button->SetToolTip(Wx::ToolTip->new("Click this to enable/disable automatic pushing on file saving."));
	$pull_button->SetToolTip(Wx::ToolTip->new("Click this to pull all changed assets from Shopify. Overwrites your own changes, if any."));
	$push_button->SetToolTip(Wx::ToolTip->new("Click this to push all changed assets to Shopify. Throws an error if there's a change conflict."));
	$preview_button->SetToolTip(Wx::ToolTip->new("Click this to open a browser window, previewing the selected theme."));
	$activate_button->SetToolTip(Wx::ToolTip->new("Click this set the selected theme as the store's main theme."));
	$check_syntax_button->SetToolTip(Wx::ToolTip->new("Click this to check the liquid syntax of your file. Right-click to autocheck each file when saved."));
	$box->Add($_) for ($theme_selector, $home_button, $autopush_button, $pull_button, $push_button, $activate_button, $preview_button, $check_syntax_button);
	$box->Add($loading_bar, 1);
	$self->{loading_bar} = $loading_bar;
	$self->{theme_selector} = $theme_selector;
	$self->{autopush_button} = $autopush_button;
	$self->{check_syntax_button} = $check_syntax_button;
	$self->{pull_button} = $pull_button;
	$self->{push_button} = $push_button;
	$self->{activate_button} = $activate_button;
	Wx::Event::EVT_BUTTON( $self, $home_button, sub { $self->project->open_home; });
	Wx::Event::EVT_BUTTON( $self, $pull_button, sub { $self->project->pull; });
	Wx::Event::EVT_BUTTON( $self, $push_button, sub { $self->project->push; });
	Wx::Event::EVT_BUTTON( $self, $autopush_button, sub { $self->project->autopush($self->project->autopush ? 0 : 1); });
	Wx::Event::EVT_BUTTON( $self, $activate_button, sub { $self->project->activate; });
	Wx::Event::EVT_BUTTON( $self, $preview_button, sub { $self->project->preview; });
	Wx::Event::EVT_BUTTON( $self, $check_syntax_button, sub { $self->project->plugin->check_syntax(1); });
	Wx::Event::EVT_RIGHT_DOWN($check_syntax_button, sub { $self->project->autocheck($self->project->autocheck ? 0 : 1); });
	$self->SetSizerAndFit($box);
	# So that we ensure that the bottom panel is showing. Weird stuff happens otherwise.
	$self->project->plugin->main->show_output(1);
	$self->project->plugin->main->bottom->show( $self );
	$self->project->plugin->main->refresh;
	return $self;
}

sub progress {
	my ($self, $percent) = @_;
	$self->{loading_bar}->SetValue(int($percent * 100));
}

sub view_panel { return 'bottom'; }
sub view_label { return Wx::gettext(shift->project->name); }
sub view_close { shift->project->remove; }

package Padre::Plugin::Shopify::Project;
use parent 'Padre::Role::Task';
use JSON qw(decode_json encode_json);
use File::Slurp;
use WWW::Shopify;
use WWW::Shopify::Model::Shop;
use Padre::Plugin::Shopify::Task;
use WWW::Shopify::Liquid;
use Scalar::Util qw(weaken);

sub manifest { $_[0]->{manifest} = $_[1] if defined $_[1]; return $_[0]->{manifest}; }

sub update_combobox {
	my ($self) = @_;
	my $name = $self->panel->{theme_selector}->GetValue;
	
	$self->panel->{theme_selector}->Clear;
	$self->panel->{theme_selector}->Append("<< ALL >>");
	my @themes = @{$self->manifest->{themes}};
	$self->panel->{theme_selector}->Append($_->{name}) for (@themes);
	$self->panel->{theme_selector}->SetStringSelection($name) if ($name);
}

sub task_status {
	my ($self, $message) = @_;
	if ($message =~ m/^\[\s*(.*?)\s*\%\s*\]\s*(.*?)\s*$/) {
		my $percent = $1/100;
		$self->progress($percent);
		$self->plugin->main->status($message);
	}
	elsif ($message =~ m/Error: /) {
		$self->plugin->main->info($message);
	}
}

sub task_finish {
	my ($self, $task) = @_;
	$self->plugin->main->status("Complete.");
	$self->manifest($task->{manifest});
	$self->update_combobox;
	$self->set_buttons_state(1);
	$self->progress(1);
}

sub task_run {
	my ($self, $task) = @_;
	$self->manifest($task->{manifest});
	$self->progress(0);
}

sub new {
	my ($package, $plugin, $directory, $settings) = @_;
	my $self = bless { %$settings, directory => $directory, plugin => $plugin, sa => undef, panel => undef, autopush => 0, manifest => { themes => [] } }, $package;
	my $height = 30;
	#weaken($self->{plugin});
	$self->{panel} = Padre::Plugin::Shopify::Panel->new( $self );
	if (-e $directory . "/.shopmanifest") {
		$self->manifest(decode_json(read_file($directory . "/.shopmanifest")));
	}
	else {
		write_file($directory . "/.shopmanifest", encode_json($self->manifest));
	}
	$self->update_combobox;
	return $self;
}

sub plugin { return shift->{plugin}; }
sub panel { return shift->{panel}; }
sub directory { return shift->{directory}; }

sub progress {
	my ($self, $percent) = @_;
	$self->panel->progress($percent);
}

sub remove {
	my ($self) = @_;
	$self->plugin->remove_project($self);
}

sub name { my ($self) = @_; return $self->shop->name; }

sub shop {
	my ($self) = @_;
	my $directory = $self->directory;
	return $self->{shop} if $self->{shop};
	if (-e "$directory/.shopinfo") {
		$self->{shop} = WWW::Shopify::Model::Shop->from_json(decode_json(read_file("$directory/.shopinfo")));
	}
	else {
		# To remove unecessary code references and whatnot, which will conflict with Storable.
		$self->{shop} = WWW::Shopify::Model::Shop->from_json(WWW::Shopify->new($self->url, $self->email, $self->password)->get_shop->to_json);
		write_file("$directory/.shopinfo", encode_json($self->{shop}->to_json));
	}
	return $self->{shop};
}

use List::Util qw(first);

sub get_current_theme {
	my ($self) = @_;
	my $name = $self->panel->{theme_selector}->GetValue;
	return undef if ($name eq "<< ALL >>");
	return first { $_->{name} eq $name } @{$self->manifest->{themes}};
}

sub open_home {
	my ($self) = @_;
	my $theme = $self->get_current_theme;
	if ($theme) {
		my $domain = $self->shop->{myshopify_domain};
		$domain =~ s/\.myshopify.com//;
		my $path = $self->directory . "/" . $domain . "-" . $theme->{id} . "/layout/theme.liquid";
		if (-e $path) {
			$self->plugin->main->setup_editor($path);
		}
		else {
			$self->plugin->main->info("Can't find theme.liquid for this theme.");
		}
	}
	else {
		$self->plugin->main->info("Please select a specific theme to open.");
	}



( run in 0.535 second using v1.01-cache-2.11-cpan-39bf76dae61 )