| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Scalar::Util qw(weaken); | ||
| 6 | |||
| 7 | package Padre::Plugin::Shopify::Exception; | ||
| 8 | sub new { return bless { error => $_[1] }, $_[0]; } | ||
| 9 | sub error { return $_[0]->{error}; } | ||
| 10 | |||
| 11 | package Padre::Plugin::Shopify::Panel; | ||
| 12 | use base qw(Padre::Wx::Role::Main Padre::Wx::Role::View Wx::Panel); | ||
| 13 | use Scalar::Util qw(weaken); | ||
| 14 | |||
| 15 | |||
| 16 | sub project { return shift->{project}; } | ||
| 17 | sub new { | ||
| 18 | my ($package, $project) = @_; | ||
| 19 | my $height = 30; | ||
| 20 | my $self = $package->SUPER::new($project->plugin->main->bottom, -1, [-1,-1], [-1, $height]); | ||
| 21 | $self->{project} = $project; | ||
| 22 | my $box = Wx::BoxSizer->new(Wx::wxHORIZONTAL); | ||
| 23 | my $theme_selector = Wx::ComboBox->new( $self, -1, "<< ALL >>", [-1, -1], [200, $height], ["<< ALL >>"], Wx::wxCB_READONLY); | ||
| 24 | 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 ) } ("home", "refresh", "pull", "push", "activate", "preview", "check-syntax"); | ||
| 25 | my $loading_bar = Wx::Gauge->new($self, -1, 100, [-1, -1], [200, $height]); | ||
| 26 | $home_button->SetToolTip(Wx::ToolTip->new("Click this to open up the theme.liquid of the selected project. Useful with the project browser.")); | ||
| 27 | $autopush_button->SetToolTip(Wx::ToolTip->new("Click this to enable/disable automatic pushing on file saving.")); | ||
| 28 | $pull_button->SetToolTip(Wx::ToolTip->new("Click this to pull all changed assets from Shopify. Overwrites your own changes, if any.")); | ||
| 29 | $push_button->SetToolTip(Wx::ToolTip->new("Click this to push all changed assets to Shopify. Throws an error if there's a change conflict.")); | ||
| 30 | $preview_button->SetToolTip(Wx::ToolTip->new("Click this to open a browser window, previewing the selected theme.")); | ||
| 31 | $activate_button->SetToolTip(Wx::ToolTip->new("Click this set the selected theme as the store's main theme.")); | ||
| 32 | $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.")); | ||
| 33 | $box->Add($_) for ($theme_selector, $home_button, $autopush_button, $pull_button, $push_button, $activate_button, $preview_button, $check_syntax_button); | ||
| 34 | $box->Add($loading_bar, 1); | ||
| 35 | $self->{loading_bar} = $loading_bar; | ||
| 36 | $self->{theme_selector} = $theme_selector; | ||
| 37 | $self->{autopush_button} = $autopush_button; | ||
| 38 | $self->{check_syntax_button} = $check_syntax_button; | ||
| 39 | $self->{pull_button} = $pull_button; | ||
| 40 | $self->{push_button} = $push_button; | ||
| 41 | $self->{activate_button} = $activate_button; | ||
| 42 | Wx::Event::EVT_BUTTON( $self, $home_button, sub { $self->project->open_home; }); | ||
| 43 | Wx::Event::EVT_BUTTON( $self, $pull_button, sub { $self->project->pull; }); | ||
| 44 | Wx::Event::EVT_BUTTON( $self, $push_button, sub { $self->project->push; }); | ||
| 45 | Wx::Event::EVT_BUTTON( $self, $autopush_button, sub { $self->project->autopush($self->project->autopush ? 0 : 1); }); | ||
| 46 | Wx::Event::EVT_BUTTON( $self, $activate_button, sub { $self->project->activate; }); | ||
| 47 | Wx::Event::EVT_BUTTON( $self, $preview_button, sub { $self->project->preview; }); | ||
| 48 | Wx::Event::EVT_BUTTON( $self, $check_syntax_button, sub { $self->project->plugin->check_syntax(1); }); | ||
| 49 | Wx::Event::EVT_RIGHT_DOWN($check_syntax_button, sub { $self->project->autocheck($self->project->autocheck ? 0 : 1); }); | ||
| 50 | $self->SetSizerAndFit($box); | ||
| 51 | # So that we ensure that the bottom panel is showing. Weird stuff happens otherwise. | ||
| 52 | $self->project->plugin->main->show_output(1); | ||
| 53 | $self->project->plugin->main->bottom->show( $self ); | ||
| 54 | $self->project->plugin->main->refresh; | ||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub progress { | ||
| 59 | my ($self, $percent) = @_; | ||
| 60 | $self->{loading_bar}->SetValue(int($percent * 100)); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub view_panel { return 'bottom'; } | ||
| 64 | sub view_label { return Wx::gettext(shift->project->name); } | ||
| 65 | sub view_close { shift->project->remove; } | ||
| 66 | |||
| 67 | package Padre::Plugin::Shopify::Project; | ||
| 68 | use parent 'Padre::Role::Task'; | ||
| 69 | use JSON qw(decode_json encode_json); | ||
| 70 | use File::Slurp; | ||
| 71 | use WWW::Shopify; | ||
| 72 | use WWW::Shopify::Model::Shop; | ||
| 73 | use Padre::Plugin::Shopify::Task; | ||
| 74 | use WWW::Shopify::Liquid; | ||
| 75 | use Scalar::Util qw(weaken); | ||
| 76 | |||
| 77 | sub manifest { $_[0]->{manifest} = $_[1] if defined $_[1]; return $_[0]->{manifest}; } | ||
| 78 | |||
| 79 | sub update_combobox { | ||
| 80 | my ($self) = @_; | ||
| 81 | my $name = $self->panel->{theme_selector}->GetValue; | ||
| 82 | |||
| 83 | $self->panel->{theme_selector}->Clear; | ||
| 84 | $self->panel->{theme_selector}->Append("<< ALL >>"); | ||
| 85 | my @themes = @{$self->manifest->{themes}}; | ||
| 86 | $self->panel->{theme_selector}->Append($_->{name}) for (@themes); | ||
| 87 | $self->panel->{theme_selector}->SetStringSelection($name) if ($name); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub task_status { | ||
| 91 | my ($self, $message) = @_; | ||
| 92 | if ($message =~ m/^\[\s*(.*?)\s*\%\s*\]\s*(.*?)\s*$/) { | ||
| 93 | my $percent = $1/100; | ||
| 94 | $self->progress($percent); | ||
| 95 | $self->plugin->main->status($message); | ||
| 96 | } | ||
| 97 | elsif ($message =~ m/Error: /) { | ||
| 98 | $self->plugin->main->info($message); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub task_finish { | ||
| 103 | my ($self, $task) = @_; | ||
| 104 | $self->plugin->main->status("Complete."); | ||
| 105 | $self->manifest($task->{manifest}); | ||
| 106 | $self->update_combobox; | ||
| 107 | $self->set_buttons_state(1); | ||
| 108 | $self->progress(1); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub task_run { | ||
| 112 | my ($self, $task) = @_; | ||
| 113 | $self->manifest($task->{manifest}); | ||
| 114 | $self->progress(0); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub new { | ||
| 118 | my ($package, $plugin, $directory, $settings) = @_; | ||
| 119 | my $self = bless { %$settings, directory => $directory, plugin => $plugin, sa => undef, panel => undef, autopush => 0, manifest => { themes => [] } }, $package; | ||
| 120 | my $height = 30; | ||
| 121 | #weaken($self->{plugin}); | ||
| 122 | $self->{panel} = Padre::Plugin::Shopify::Panel->new( $self ); | ||
| 123 | if (-e $directory . "/.shopmanifest") { | ||
| 124 | $self->manifest(decode_json(read_file($directory . "/.shopmanifest"))); | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | write_file($directory . "/.shopmanifest", encode_json($self->manifest)); | ||
| 128 | } | ||
| 129 | $self->update_combobox; | ||
| 130 | return $self; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub plugin { return shift->{plugin}; } | ||
| 134 | sub panel { return shift->{panel}; } | ||
| 135 | sub directory { return shift->{directory}; } | ||
| 136 | |||
| 137 | sub progress { | ||
| 138 | my ($self, $percent) = @_; | ||
| 139 | $self->panel->progress($percent); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub remove { | ||
| 143 | my ($self) = @_; | ||
| 144 | $self->plugin->remove_project($self); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub name { my ($self) = @_; return $self->shop->name; } | ||
| 148 | |||
| 149 | sub shop { | ||
| 150 | my ($self) = @_; | ||
| 151 | my $directory = $self->directory; | ||
| 152 | return $self->{shop} if $self->{shop}; | ||
| 153 | if (-e "$directory/.shopinfo") { | ||
| 154 | $self->{shop} = WWW::Shopify::Model::Shop->from_json(decode_json(read_file("$directory/.shopinfo"))); | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | # To remove unecessary code references and whatnot, which will conflict with Storable. | ||
| 158 | $self->{shop} = WWW::Shopify::Model::Shop->from_json(WWW::Shopify->new($self->url, $self->email, $self->password)->get_shop->to_json); | ||
| 159 | write_file("$directory/.shopinfo", encode_json($self->{shop}->to_json)); | ||
| 160 | } | ||
| 161 | return $self->{shop}; | ||
| 162 | } | ||
| 163 | |||
| 164 | use List::Util qw(first); | ||
| 165 | |||
| 166 | sub get_current_theme { | ||
| 167 | my ($self) = @_; | ||
| 168 | my $name = $self->panel->{theme_selector}->GetValue; | ||
| 169 | return undef if ($name eq "<< ALL >>"); | ||
| 170 | return first { $_->{name} eq $name } @{$self->manifest->{themes}}; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub open_home { | ||
| 174 | my ($self) = @_; | ||
| 175 | my $theme = $self->get_current_theme; | ||
| 176 | if ($theme) { | ||
| 177 | my $domain = $self->shop->{myshopify_domain}; | ||
| 178 | $domain =~ s/\.myshopify.com//; | ||
| 179 | my $path = $self->directory . "/" . $domain . "-" . $theme->{id} . "/layout/theme.liquid"; | ||
| 180 | if (-e $path) { | ||
| 181 | $self->plugin->main->setup_editor($path); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $self->plugin->main->info("Can't find theme.liquid for this theme."); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | $self->plugin->main->info("Please select a specific theme to open."); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub activate { | ||
| 193 | my ($self) = @_; | ||
| 194 | my $theme = $self->get_current_theme; | ||
| 195 | if ($theme) { | ||
| 196 | if ($theme->{role} eq "main") { | ||
| 197 | $self->plugin->main->info("This theme is already the main theme!"); | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | my $dialog = Wx::MessageDialog->new($self->plugin->main, "This will set " . $theme->{name} . " as the main theme. Are you sure you want to do this?", "Activate " . $theme->{name} . "?", Wx::YES_NO | Wx::NO_DEFAULT | Wx::ICON_QUESTION); | ||
| 201 | if ($dialog->ShowModal == Wx::ID_YES) { | ||
| 202 | $self->plugin->main->status("Activating " . $theme->{name} . "..."); | ||
| 203 | $self->set_buttons_state(0); | ||
| 204 | $self->task_request( | ||
| 205 | task => 'Padre::Plugin::Shopify::Task', | ||
| 206 | on_finish => 'task_finish', | ||
| 207 | on_status => 'task_status', | ||
| 208 | on_run => 'task_run', | ||
| 209 | action => "activate:" . $theme->{id}, | ||
| 210 | project => $self | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | $self->plugin->main->info("Please select a theme to activate."); | ||
| 217 | } | ||
| 218 | |||
| 219 | } | ||
| 220 | |||
| 221 | use Browser::Open qw(open_browser_cmd); | ||
| 222 | sub preview { | ||
| 223 | my ($self) = @_; | ||
| 224 | my $theme = $self->get_current_theme; | ||
| 225 | if (!$theme) { | ||
| 226 | $self->plugin->main->info("Please select a theme to preview."); | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | $self->plugin->main->status("Previewing " . $theme->{name} . "..."); | ||
| 230 | my $cmd = open_browser_cmd(); | ||
| 231 | my $url = $self->shop->domain . "?preview_theme_id=" . $theme->{id}; | ||
| 232 | exec($cmd . " " . $url) if (fork() == 0); | ||
| 233 | $self->plugin->main->status("Done."); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | sub push { | ||
| 238 | my ($self) = @_; | ||
| 239 | $self->progress(0); | ||
| 240 | my $name = $self->panel->{theme_selector}->GetValue; | ||
| 241 | if ($name eq "<< ALL >>") { | ||
| 242 | $self->plugin->main->status("Pushing all themes..."); | ||
| 243 | $self->set_buttons_state(0); | ||
| 244 | $self->task_request( | ||
| 245 | task => 'Padre::Plugin::Shopify::Task', | ||
| 246 | on_finish => 'task_finish', | ||
| 247 | on_status => 'task_status', | ||
| 248 | on_run => 'task_run', | ||
| 249 | action => "push_all", | ||
| 250 | project => $self | ||
| 251 | ); | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $self->plugin->main->status("Pushing theme $name..."); | ||
| 255 | my $theme = first { $_->{name} eq $name } @{$self->manifest->{themes}}; | ||
| 256 | if ($theme) { | ||
| 257 | my $id = $theme->{id}; | ||
| 258 | $self->set_buttons_state(0); | ||
| 259 | $self->task_request( | ||
| 260 | task => 'Padre::Plugin::Shopify::Task', | ||
| 261 | on_finish => 'task_finish', | ||
| 262 | on_status => 'task_status', | ||
| 263 | on_run => 'task_run', | ||
| 264 | action => "push:$id", | ||
| 265 | project => $self | ||
| 266 | ); | ||
| 267 | } | ||
| 268 | else { | ||
| 269 | $self->plugin->main->info("Can't find theme $name."); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | } | ||
| 273 | sub pull { | ||
| 274 | my ($self) = @_; | ||
| 275 | $self->progress(0); | ||
| 276 | my $name = $self->panel->{theme_selector}->GetValue; | ||
| 277 | if ($name eq "<< ALL >>") { | ||
| 278 | $self->plugin->main->status("Pulling all themes..."); | ||
| 279 | $self->set_buttons_state(0); | ||
| 280 | $self->task_request( | ||
| 281 | task => 'Padre::Plugin::Shopify::Task', | ||
| 282 | on_finish => 'task_finish', | ||
| 283 | on_status => 'task_status', | ||
| 284 | on_run => 'task_run', | ||
| 285 | action => "pull_all", | ||
| 286 | project => $self | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | $self->plugin->main->status("Pulling theme $name..."); | ||
| 291 | my $theme = first { $_->{name} eq $name } @{$self->manifest->{themes}}; | ||
| 292 | if ($theme) { | ||
| 293 | my $id = $theme->{id}; | ||
| 294 | $self->set_buttons_state(0); | ||
| 295 | $self->task_request( | ||
| 296 | task => 'Padre::Plugin::Shopify::Task', | ||
| 297 | on_finish => 'task_finish', | ||
| 298 | on_status => 'task_status', | ||
| 299 | on_run => 'task_run', | ||
| 300 | action => "pull:$id", | ||
| 301 | project => $self | ||
| 302 | ); | ||
| 303 | } | ||
| 304 | else { | ||
| 305 | $self->plugin->main->info("Can't find theme $name."); | ||
| 306 | } | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | sub set_buttons_state { | ||
| 311 | my ($self, $state) = @_; | ||
| 312 | $self->panel->{theme_selector}->Enable($state); | ||
| 313 | $self->panel->{activate_button}->Enable($state); | ||
| 314 | $self->panel->{pull_button}->Enable($state); | ||
| 315 | $self->panel->{push_button}->Enable($state); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub url { $_[0]->{url} = $_[1] if defined $_[1]; return $_[0]->{url}; } | ||
| 319 | sub api_key { $_[0]->{api_key} = $_[1] if defined $_[1]; return $_[0]->{api_key}; } | ||
| 320 | sub password { $_[0]->{password} = $_[1] if defined $_[1]; return $_[0]->{password}; } | ||
| 321 | sub email { $_[0]->{email} = $_[1] if defined $_[1]; return $_[0]->{email}; } | ||
| 322 | |||
| 323 | |||
| 324 | sub autopush { | ||
| 325 | my ($self) = @_; | ||
| 326 | if (defined $_[1]) { | ||
| 327 | $self->{autopush} = $_[1]; | ||
| 328 | my $bitmap = $self->plugin->{images}->{$self->{autopush} ? "refresh-off" : "refresh"}; | ||
| 329 | $self->panel->{autopush_button}->SetBitmapSelected($bitmap); | ||
| 330 | $self->panel->{autopush_button}->SetBitmapFocus($bitmap); | ||
| 331 | $self->panel->{autopush_button}->SetBitmapDisabled($bitmap); | ||
| 332 | $self->panel->{autopush_button}->SetBitmapHover($bitmap); | ||
| 333 | $self->panel->{autopush_button}->SetBitmapLabel($bitmap); | ||
| 334 | } | ||
| 335 | return $_[0]->{autopush}; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub autocheck { | ||
| 339 | my ($self) = @_; | ||
| 340 | if (defined $_[1]) { | ||
| 341 | $self->{autocheck} = $_[1]; | ||
| 342 | my $bitmap = $self->plugin->{images}->{$self->{autocheck} ? "check-syntax-off" : "check-syntax"}; | ||
| 343 | $self->panel->{check_syntax_button}->SetBitmapSelected($bitmap); | ||
| 344 | $self->panel->{check_syntax_button}->SetBitmapFocus($bitmap); | ||
| 345 | $self->panel->{check_syntax_button}->SetBitmapDisabled($bitmap); | ||
| 346 | $self->panel->{check_syntax_button}->SetBitmapHover($bitmap); | ||
| 347 | $self->panel->{check_syntax_button}->SetBitmapLabel($bitmap); | ||
| 348 | } | ||
| 349 | return $_[0]->{autocheck}; | ||
| 350 | } | ||
| 351 | |||
| 352 | package Padre::Plugin::Shopify; | ||
| 353 | use base 'Padre::Plugin'; | ||
| 354 | use File::ShareDir qw(dist_dir); | ||
| 355 | use File::Slurp; | ||
| 356 | use JSON qw(decode_json encode_json); | ||
| 357 | |||
| 358 | our $VERSION = '0.05'; | ||
| 359 | |||
| 360 | sub new { | ||
| 361 | my ($package, @args) = @_; | ||
| 362 | my $self = $package->SUPER::new(@args); | ||
| 363 | $self->{projects} = []; | ||
| 364 | $self->{autopush} = 0; | ||
| 365 | |||
| 366 | $self->{images} = {}; | ||
| 367 | my $dist_dir = dist_dir("WWW-Shopify-Tools-Themer"); | ||
| 368 | for ("home", "pull", "push", "refresh", "refresh-off", "activate", "deactivate", "preview", "check-syntax", "check-syntax-off") { | ||
| 369 | my $image = Wx::Image->new(); | ||
| 370 | $image->LoadFile("$dist_dir/$_.png", Wx::wxBITMAP_TYPE_PNG); | ||
| 371 | my $bitmap = Wx::Bitmap->new($image); | ||
| 372 | if ($bitmap->Ok) { | ||
| 373 | $self->{images}->{$_} = $bitmap; | ||
| 374 | } | ||
| 375 | else { | ||
| 376 | print STDERR "Unable to load $_\n"; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | return $self; | ||
| 380 | } | ||
| 381 | |||
| 382 | sub plugin_name { "Shopify Plug-In"; } | ||
| 383 | |||
| 384 | sub padre_interfaces { | ||
| 385 | return ( | ||
| 386 | 'Padre::Plugin' => '0.91', | ||
| 387 | 'Padre::Wx::Role::Main' => '0.91', | ||
| 388 | 'Padre::Wx' => '0.91', | ||
| 389 | ); | ||
| 390 | } | ||
| 391 | |||
| 392 | use List::Util qw(first); | ||
| 393 | sub padre_hooks { return {'after_save' => sub { | ||
| 394 | my ($self, $document) = @_; | ||
| 395 | my $project = first { index($document->filename, $_->directory) != -1 } $self->projects; | ||
| 396 | if ($project && (!$project->autocheck || ($project->autocheck && $self->check_syntax(0, $document)))) { | ||
| 397 | $project->push if $project->autopush; | ||
| 398 | } | ||
| 399 | }}; } | ||
| 400 | |||
| 401 | use constant CHILDREN => 'Padre::Plugin::Shopify', 'Padre::Plugin::Shopify::Task'; | ||
| 402 | |||
| 403 | sub pull_all { my ($self) = @_; $_->pull for ($self->projects); } | ||
| 404 | sub push_all { my ($self) = @_; $_->push for ($self->projects); } | ||
| 405 | |||
| 406 | |||
| 407 | my $liquid = WWW::Shopify::Liquid->new; | ||
| 408 | |||
| 409 | sub check_syntax { | ||
| 410 | my ($self, $alert, $document) = @_; | ||
| 411 | $document = Padre::Current->document unless $document; | ||
| 412 | my $text = $document->text_get; | ||
| 413 | eval { | ||
| 414 | $liquid->parse_text($text); | ||
| 415 | }; | ||
| 416 | if (my $e = $@) { | ||
| 417 | if (defined $e->{line}) { | ||
| 418 | $self->main->info("Syntax error in document on line " . $e->line . ": " . $e->english); | ||
| 419 | # We index from 0 for lines, apparently. | ||
| 420 | if ($e->line) { | ||
| 421 | my $pos = $self->current->editor->PositionFromLine($e->line-1) + ($e->column ? $e->column : 0); | ||
| 422 | $self->current->editor->goto_pos_centerize($pos); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | else { | ||
| 426 | $self->main->info("Syntax error in document: " . $e->english); | ||
| 427 | } | ||
| 428 | return 0; | ||
| 429 | } elsif ($alert) { | ||
| 430 | $self->main->info("No syntax errors found!"); | ||
| 431 | } | ||
| 432 | return 1; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub menu_plugins_simple { | ||
| 436 | my $self = shift; | ||
| 437 | return $self->plugin_name => [ | ||
| 438 | 'Create Shop' => sub { $self->create_shop_dialog }, | ||
| 439 | 'Open Shop' => sub { $self->open_shop_dialog }, | ||
| 440 | 'Pull Open Shops' => sub { $self->pull_all }, | ||
| 441 | 'Push Open Shops' => sub { $self->push_all }, | ||
| 442 | 'Check Document Syntax' => sub { $self->check_syntax(1) }, | ||
| 443 | 'About' => sub { $self->show_about }, | ||
| 444 | ]; | ||
| 445 | } | ||
| 446 | |||
| 447 | sub projects { return @{$_[0]->{projects}}; } | ||
| 448 | sub add_project { | ||
| 449 | my ($self, $project) = @_; | ||
| 450 | push(@{$self->{projects}}, $project); | ||
| 451 | return $project; | ||
| 452 | } | ||
| 453 | |||
| 454 | sub remove_project { | ||
| 455 | my ($self, $project) = @_; | ||
| 456 | $self->{projects} = [grep { $_ != $project } @{$self->{projects}}]; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub show_about { | ||
| 460 | my $self = shift; | ||
| 461 | # Generate the About dialog | ||
| 462 | my $about = Wx::AboutDialogInfo->new; | ||
| 463 | $about->SetName('Shopify Plug In'); | ||
| 464 | $about->SetDescription('A plugin for the Shopify theme tool.'); | ||
| 465 | # Show the About dialog | ||
| 466 | Wx::AboutBox($about); | ||
| 467 | |||
| 468 | return; | ||
| 469 | } | ||
| 470 | |||
| 471 | sub open_shop_dialog { | ||
| 472 | my ($self) = @_; | ||
| 473 | my $main = $self->main; | ||
| 474 | my $dialog = Wx::DirDialog->new($main, -1); | ||
| 475 | if ($dialog->ShowModal == Wx::wxID_OK) { | ||
| 476 | $self->open_shop($dialog->GetPath); | ||
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | sub create_shop_dialog { | ||
| 481 | my ($self) = @_; | ||
| 482 | my $main = $self->main; | ||
| 483 | my $dialog = Wx::DirDialog->new($main, "Select an Empty Directory"); | ||
| 484 | $self->create_shop($dialog->GetPath) if $dialog->ShowModal == Wx::wxID_OK; | ||
| 485 | } | ||
| 486 | |||
| 487 | use List::Util qw(first); | ||
| 488 | use Cwd 'abs_path'; | ||
| 489 | sub open_shop { | ||
| 490 | my ($self, $directory) = @_; | ||
| 491 | eval { | ||
| 492 | die new Padre::Plugin::Shopify::Exception("Unable to find directory.") unless -d $directory; | ||
| 493 | my ($setting_file, $manifest_file) = ("$directory/.shopsettings", "$directory/.shopmanifest"); | ||
| 494 | die new Padre::Plugin::Shopify::Exception("Unable to find directory files.") unless -e $setting_file && -e $manifest_file; | ||
| 495 | my $file_settings = decode_json(read_file($setting_file)); | ||
| 496 | return if first { abs_path($_->directory) eq $directory } $self->projects; | ||
| 497 | $self->add_project(Padre::Plugin::Shopify::Project->new($self, $directory, $file_settings)); | ||
| 498 | }; | ||
| 499 | if ($@) { | ||
| 500 | $self->main->info(ref($@) ? $@->error : $@); | ||
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 | sub create_shop { | ||
| 505 | my ($self, $directory) = @_; | ||
| 506 | eval { | ||
| 507 | die new Padre::Plugin::Shopify::Exception("Unable to find directory.") unless -d $directory; | ||
| 508 | my ($setting_file, $manifest_file) = ("$directory/.shopsettings", "$directory/.shopmanifest"); | ||
| 509 | die new Padre::Plugin::Shopify::Exception("Found already extant settings files. Not creating.") if -e $setting_file || -e $manifest_file; | ||
| 510 | my $dialog = Wx::Dialog->new($self->main, -1, "Create Shop", [-1, -1], [-1, -1]); | ||
| 511 | my $grid = Wx::FlexGridSizer->new( 4, 2, 1, 1); | ||
| 512 | |||
| 513 | my ($url_edit, $api_key_edit, $password_edit) = map { Wx::TextCtrl->new( $dialog, -1, '', [-1, -1], [400, -1] ) } 0..2; | ||
| 514 | my ($url_text, $password_text) = map { Wx::StaticText->new( $dialog, -1, $_ )} ("Shop URL", "Password"); | ||
| 515 | my $api_key_text = Wx::ComboBox->new($dialog, -1, "API Key", [-1, -1], [-1, -1], ["API Key", "Email"]); | ||
| 516 | my ($okay_button, $cancel_button) = map { Wx::Button->new( $dialog, -1, $_ ) } ("OK", "Cancel"); | ||
| 517 | $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); | ||
| 518 | $dialog->SetAutoLayout( 1 ); | ||
| 519 | $dialog->SetSizer($grid); | ||
| 520 | $grid->Fit($dialog); | ||
| 521 | $grid->SetSizeHints($dialog); | ||
| 522 | Wx::Event::EVT_BUTTON( $dialog, $okay_button, sub { $dialog->EndModal(Wx::wxID_OK); }); | ||
| 523 | Wx::Event::EVT_BUTTON( $dialog, $cancel_button, sub { $dialog->EndModal(Wx::wxID_CANCEL); }); | ||
| 524 | if ($dialog->ShowModal == Wx::wxID_OK) { | ||
| 525 | my $settings = { url => $url_edit->GetValue, password => $password_edit->GetValue }; | ||
| 526 | $settings->{api_key} = $api_key_edit->GetValue if $api_key_text->GetValue eq "API Key"; | ||
| 527 | $settings->{email} = $api_key_edit->GetValue if $api_key_text->GetValue eq "Email"; | ||
| 528 | $self->add_project(Padre::Plugin::Shopify::Project->new($self, $directory, $settings)); | ||
| 529 | my %saveSettings = map { $_ => $settings->{$_} } keys(%$settings); | ||
| 530 | write_file("$directory/.shopsettings", encode_json(\%saveSettings)); | ||
| 531 | } | ||
| 532 | }; | ||
| 533 | if ($@) { | ||
| 534 | $self->main->info(ref($@) ? $@->error : $@); | ||
| 535 | } | ||
| 536 | } | ||
| 537 | |||
| 538 | use Padre::Locale::T; | ||
| 539 | sub plugin_enable { | ||
| 540 | my $self = shift; | ||
| 541 | my $return = $self->SUPER::plugin_enable(@_); | ||
| 542 | |||
| 543 | if (!Padre::MIME->find("application/liquid")->type) { | ||
| 544 | Padre::MIME->create( | ||
| 545 | type => 'application/liquid', | ||
| 546 | name => 'Liquid', | ||
| 547 | supertype => 'text/html', | ||
| 548 | document => 'Padre::Document::Liquid', | ||
| 549 | extensions => 'liquid' | ||
| 550 | ); | ||
| 551 | Padre::Wx::Action->new( | ||
| 552 | name => "view.mime.application/liquid", | ||
| 553 | label => "Liquid", | ||
| 554 | comment => _T('Switch document type'), | ||
| 555 | menu_method => 'AppendRadioItem', | ||
| 556 | menu_event => sub { | ||
| 557 | $_[0]->set_mimetype("application/liquid"); | ||
| 558 | }, | ||
| 559 | ); | ||
| 560 | $self->main->refresh; | ||
| 561 | } | ||
| 562 | |||
| 563 | my $config = $self->config_read; | ||
| 564 | if ($config && $config->{projects}) { | ||
| 565 | $self->open_shop($_) for (@{$config->{projects}}); | ||
| 566 | } | ||
| 567 | return $return; | ||
| 568 | } | ||
| 569 | |||
| 570 | sub plugin_disable { | ||
| 571 | my $self = shift; | ||
| 572 | $self->config_write( { projects => [ map { $_->directory } $self->projects] } ); | ||
| 573 | for my $package (CHILDREN) { | ||
| 574 | require Padre::Unload; | ||
| 575 | Padre::Unload->unload($package); | ||
| 576 | } | ||
| 577 | $self->plugin->main->bottom->hide( $_->panel ) for ($self->projects); | ||
| 578 | $self->SUPER::plugin_disable(@_); | ||
| 579 | return 1; | ||
| 580 | } | ||
| 581 | |||
| 582 | sub registered_highlighters { | ||
| 583 | |||
| 584 | } | ||
| 585 | |||
| 586 | sub provided_highlighters { | ||
| 587 | return (['Padre::Document::Liquid', "Liquid", "Liquid syntax highglithing for padre."]); | ||
| 588 | } | ||
| 589 | |||
| 590 | sub highlighting_mime_types { | ||
| 591 | return ('Padre::Document::Liquid' => ['application/liquid']); | ||
| 592 | } | ||
| 593 | |||
| 594 | sub registered_documents { | ||
| 595 | return 'application/liquid' => 'Padre::Document::Liquid'; | ||
| 596 | } | ||
| 597 | |||
| 598 | 1; | ||
| 599 | |||
| 600 | __END__ |