1 |
package Frey::Class::Create; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Path'; |
7 |
#with 'Frey::Storage'; |
8 |
|
9 |
use File::Slurp; |
10 |
|
11 |
has class => ( |
12 |
documentation => 'Name of class to create', |
13 |
is => 'rw', |
14 |
isa => 'Str', |
15 |
required => 1, |
16 |
default => 'App::Skeleton', |
17 |
); |
18 |
|
19 |
has svk_add => ( |
20 |
documentation => 'Add created files to SVK', |
21 |
is => 'rw', |
22 |
isa => 'Bool', |
23 |
required => 1, |
24 |
default => 1, |
25 |
); |
26 |
|
27 |
sub create_class_source_as_markup { |
28 |
my ($self) = @_; |
29 |
|
30 |
my $class = $self->class; |
31 |
|
32 |
my $class_path = $class; |
33 |
$class_path =~ s{::}{/}g; |
34 |
$class_path = "lib/$class_path.pm"; |
35 |
|
36 |
my $test_path; |
37 |
|
38 |
my @c = split(m{::}, $class); |
39 |
foreach ( -1 .. $#c ) { |
40 |
my $to = $#c - $_; |
41 |
my $path = 't/' . join('/', @c[ 0 .. $to ] ); |
42 |
if ( -e $path ) { |
43 |
my $file = join('-', @c[ $to + 1 .. $#c ] ); |
44 |
$test_path = $path . '/' . $file . '.t'; |
45 |
last; |
46 |
} |
47 |
} |
48 |
|
49 |
if ( ! $test_path ) { |
50 |
$test_path = $class; |
51 |
$test_path =~ s{::}{-}g; |
52 |
$test_path = "t/30-$test_path.t"; |
53 |
} |
54 |
|
55 |
die qq|class "$class" exists as |, $self->path_size($class_path) if -e $class_path; |
56 |
die qq|class "$class" test exists as |, $self->path_size($test_path) if -e $test_path; |
57 |
|
58 |
warn "## path $class_path"; |
59 |
$self->mkbasepath( $class_path ); |
60 |
|
61 |
my $skeleton = 'Frey::Skeleton'; |
62 |
|
63 |
my $code = read_file( 'lib/Frey/Skeleton.pm' ); |
64 |
$code =~ s{$skeleton}{$class}g; |
65 |
write_file( $class_path, $code ); |
66 |
warn "# created class $class at ", $self->path_size($class_path); |
67 |
system "svk add $class_path" if $self->svk_add; |
68 |
|
69 |
$code = read_file( 't/30-frey-skeleton.t' ); |
70 |
$code =~ s{$skeleton}{$class}g; |
71 |
write_file( $test_path, $code ); |
72 |
chmod 0755, $test_path; |
73 |
warn "# created class $class at ", $self->path_size($test_path); |
74 |
system "svk add $test_path" if $self->svk_add; |
75 |
|
76 |
my $html |
77 |
= qq|Created class "$class" at | |
78 |
. $self->path_size($class_path) |
79 |
. qq| and test | |
80 |
. $self->path_size($test_path) |
81 |
; |
82 |
|
83 |
$html = $self->html_links( $html ); |
84 |
|
85 |
return $html; |
86 |
} |
87 |
|
88 |
=head1 DESCRIPTION |
89 |
|
90 |
Create Frey class based on L<Frey::Skeleton>. |
91 |
|
92 |
Blame this on my Smalltalk influence, because creating classes there is just |
93 |
interface and message sends wrapped in GUI, but that makes Smalltalk books |
94 |
much harder to read... |
95 |
|
96 |
On the other hand, having handy class creator within L<Frey> makes sense. |
97 |
|
98 |
=cut |
99 |
|
100 |
1; |