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