Plugin/Plack/Util.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 101 SCAR 42.8
CC
8
Cov
0%
get_plugin_menu_item_by_label
20.0
get_plack_project_name
6.0
find_file_from_output
6.0
CRAP 72.0
1 package Padre::Plugin::Plack::Util;
2 use strict;
3 use warnings;
4  
5 # some code used all around the Plugin
6 use Cwd ();
7 use File::Spec ();
8 use Padre::Util ('_T');
9  
10 our $VERSION = '0.01';
11  
12 # get the Plack project name, so we can
13 # figure out the development server's name
14 # TODO: make this code suck less
15 sub get_plack_project_name {
16 my $project_dir = shift;
17 return unless $project_dir;
18  
19 require File::Spec;
20 my @dirs = File::Spec->splitdir($project_dir);
21 my $project_name = lc($dirs[-1]);
22 $project_name =~ tr{-}{_};
23
24 return $project_name;
25 }
26  
27 sub find_file_from_output {
28 my $filename = shift;
29 my $output_text = shift;
30
31 $filename .= '.pm';
32
33 if ($output_text =~ m{created "(.+$filename(?:\.new)?)"}) {
34 return $1;
35 }
36 else {
37 return; # sorry, not found
38 }
39 }
40  
41 sub get_document_base_dir {
42 my $main = Padre->ide->wx->main;
43 my $doc = $main->current->document;
44 my $filename = $doc->filename;
45 return Padre::Util::get_project_dir($filename);
46 }
47  
48 # returns true if given filename (looks like) is inside a
49 # Plack project
50 sub in_plack_project {
51 require File::Spec;
52 my $filename = shift or return;
53  
54 my $project_dir = Padre::Util::get_project_dir($filename);
55
56 foreach my $dir ( qw(lib root script t) ) {
57 return unless -d File::Spec->catdir( $project_dir, $dir );
58 }
59 return 1;
60 }
61  
62 #TODO: maybe this function (or some mutation of it)
63 # is useful to other plugin authors. In this case, we
64 # should move it to Padre::Plugin or similar
65 sub get_plugin_menu_item_by_label {
66 my $menu_item = shift;
67 my $main = Padre::ide->wx->main;
68  
69 # find plugin menu
70 my $menu = $main->menu->{'plugins'}->{'plugin_menus'};
71 my $plugin_menu;
72 foreach (@{$menu}) {
73 if ($_->GetLabel eq 'Plack') {
74 $plugin_menu = $_;
75 last;
76 }
77 }
78 return unless $plugin_menu;
79  
80 # find requested menu element
81 my $submenu = $plugin_menu->GetSubMenu;
82 foreach my $item ($submenu->GetMenuItems) {
83 return $item if $item->GetLabel eq $menu_item;
84 }
85 return;
86 }
87  
88 sub toggle_server_menu {
89 my $toggle = shift;
90  
91 my $menu_start = get_plugin_menu_item_by_label(
92 _T('Start Web Server')
93 );
94 my $menu_stop = get_plugin_menu_item_by_label(
95 _T('Stop Web Server')
96 );
97 if ($menu_start and $menu_stop) {
98 $menu_start->Enable($toggle);
99 $menu_stop->Enable(!$toggle);
100 }
101 }
102  
103 42;