Catalyst/Util.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 140 CC 16 SCAR 31.9
CC
16
Cov
0%
toggle_menu_items
72.0
get_plugin_menu_item_by_label
20.0
get_catalyst_project_name
6.0
CRAP 24.4
1 package Padre::Plugin::Catalyst::Util;
2 BEGIN {
3 $Padre::Plugin::Catalyst::Util::VERSION = '0.13';
4 }
5  
6 # ABSTRACT: A collection of utility functions
7  
8 use strict;
9 use warnings;
10  
11 # some code used all around the Plugin
12 use Cwd ();
13 use File::Spec ();
14 use Padre::Wx ();
15  
16 # get the Catalyst project name, so we can
17 # figure out the development server's name
18 # TODO: make this code suck less
19 sub get_catalyst_project_name {
20 my $project_dir = shift;
21 return unless $project_dir;
22  
23 require File::Spec;
24 my @dirs = File::Spec->splitdir($project_dir);
25 my $project_name = lc( $dirs[-1] );
26 $project_name =~ tr{-}{_};
27  
28 return $project_name;
29 }
30  
31 sub find_file_from_output {
32 my $filename = shift;
33 my $output_text = shift;
34  
35 $filename .= '.pm';
36  
37 if ( $output_text =~ m{created "(.+$filename(?:\.new)?)"} ) {
38 return $1;
39 } else {
40 return; # sorry, not found
41 }
42 }
43  
44 sub get_document_base_dir {
45 my $main = Padre->ide->wx->main;
46 my $doc = $main->current->document;
47  
48 unless ($doc) {
49 Wx::MessageBox(
50 Wx::gettext('Could not open current document. Please make sure you have at least one document open.'),
51 Wx::gettext('Catalyst project dir not found'), Wx::wxOK, $main
52 );
53 return;
54 }
55  
56 my $filename = $doc->filename;
57 return Padre::Util::get_project_dir($filename);
58 }
59  
60 # returns true if given filename (looks like) is inside a
61 # Catalyst project
62 sub in_catalyst_project {
63 require File::Spec;
64 my $filename = shift or return;
65  
66 my $project_dir = Padre::Util::get_project_dir($filename);
67  
68 foreach my $dir (qw(lib root script t)) {
69 return unless -d File::Spec->catdir( $project_dir, $dir );
70 }
71 return 1;
72 }
73  
74 #TODO: maybe this function (or some mutation of it)
75 # is useful to other plugin authors. In this case, we
76 # should move it to Padre::Plugin or similar
77 sub get_plugin_menu_item_by_label {
78 my $menu_item = shift;
79 my $main = Padre::ide->wx->main;
80  
81 # find plugin menu
82 my $menu = $main->menu->{'plugins'}->{'plugin_menus'};
83 my $plugin_menu;
84 foreach ( @{$menu} ) {
85 if ( $_->GetLabel eq 'Catalyst' ) {
86 $plugin_menu = $_;
87 last;
88 }
89 }
90 return unless $plugin_menu;
91  
92 # find requested menu element
93 my $submenu = $plugin_menu->GetSubMenu;
94 foreach my $item ( $submenu->GetMenuItems ) {
95 return $item if $item->GetLabel eq $menu_item;
96 }
97 return;
98 }
99  
100 sub toggle_server_menu {
101 my $toggle = shift;
102  
103 my $menu_start = get_plugin_menu_item_by_label( Wx::gettext('Start Web Server') );
104 my $menu_stop = get_plugin_menu_item_by_label( Wx::gettext('Stop Web Server') );
105 if ( $menu_start and $menu_stop ) {
106 $menu_start->Enable($toggle);
107 $menu_stop->Enable( !$toggle );
108 }
109 }
110  
111 sub toggle_menu_items {
112 my ( $toggle, $is_server_on ) = (@_);
113  
114 #TODO: caching this on startup would probably make things marginally faster
115 my $menu_helpers = get_plugin_menu_item_by_label( Wx::gettext('Create new...') );
116 my $menu_start = get_plugin_menu_item_by_label( Wx::gettext('Start Web Server') );
117 my $menu_stop = get_plugin_menu_item_by_label( Wx::gettext('Stop Web Server') );
118 my $menu_update = get_plugin_menu_item_by_label( Wx::gettext('Update Application Scripts') );
119  
120 $menu_helpers->Enable($toggle) if $menu_helpers;
121 $menu_update->Enable($toggle) if $menu_update;
122  
123 if ( $toggle == 0 ) {
124 $menu_start->Enable($toggle) if $menu_start;
125 $menu_stop->Enable($toggle) if $menu_stop;
126 } else {
127 $menu_start->Enable( !$is_server_on ) if $menu_start;
128 $menu_stop->Enable($is_server_on) if $menu_stop;
129 }
130 }
131  
132 1;
133  
134 __END__