1 |
dpavlin |
778 |
package Frey::Class::Create; |
2 |
dpavlin |
679 |
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 |
dpavlin |
1089 |
default => 'App::Skeleton', |
17 |
dpavlin |
679 |
); |
18 |
|
|
|
19 |
|
|
has svk_add => ( |
20 |
|
|
documentation => 'Add created files to SVK', |
21 |
|
|
is => 'rw', |
22 |
|
|
isa => 'Bool', |
23 |
|
|
required => 1, |
24 |
dpavlin |
711 |
default => 1, |
25 |
dpavlin |
679 |
); |
26 |
|
|
|
27 |
dpavlin |
680 |
sub create_class_source_as_markup { |
28 |
dpavlin |
679 |
my ($self) = @_; |
29 |
|
|
|
30 |
|
|
my $class = $self->class; |
31 |
|
|
|
32 |
|
|
my $class_path = $class; |
33 |
|
|
$class_path =~ s{::}{/}g; |
34 |
dpavlin |
705 |
$class_path = "lib/$class_path.pm"; |
35 |
dpavlin |
679 |
|
36 |
dpavlin |
1089 |
my $test_path; |
37 |
dpavlin |
679 |
|
38 |
dpavlin |
1089 |
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 |
|
|
die "no test_path from $class" unless $test_path; |
50 |
|
|
|
51 |
dpavlin |
682 |
die qq|class "$class" exists as |, $self->path_size($class_path) if -e $class_path; |
52 |
|
|
die qq|class "$class" test exists as |, $self->path_size($test_path) if -e $test_path; |
53 |
dpavlin |
679 |
|
54 |
|
|
warn "## path $class_path"; |
55 |
|
|
$self->mkbasepath( $class_path ); |
56 |
|
|
|
57 |
|
|
my $skeleton = 'Frey::Skeleton'; |
58 |
|
|
|
59 |
|
|
my $code = read_file( 'lib/Frey/Skeleton.pm' ); |
60 |
|
|
$code =~ s{$skeleton}{$class}g; |
61 |
|
|
write_file( $class_path, $code ); |
62 |
dpavlin |
682 |
warn "# created class $class at ", $self->path_size($class_path); |
63 |
dpavlin |
679 |
system "svk add $class_path" if $self->svk_add; |
64 |
|
|
|
65 |
|
|
$code = read_file( 't/30-frey-skeleton.t' ); |
66 |
|
|
$code =~ s{$skeleton}{$class}g; |
67 |
|
|
write_file( $test_path, $code ); |
68 |
dpavlin |
680 |
chmod 0755, $test_path; |
69 |
dpavlin |
682 |
warn "# created class $class at ", $self->path_size($test_path); |
70 |
dpavlin |
679 |
system "svk add $test_path" if $self->svk_add; |
71 |
|
|
|
72 |
dpavlin |
680 |
my $html |
73 |
dpavlin |
682 |
= qq|Created class "$class" at | |
74 |
|
|
. $self->path_size($class_path) |
75 |
|
|
. qq| and test | |
76 |
|
|
. $self->path_size($test_path) |
77 |
dpavlin |
680 |
; |
78 |
|
|
|
79 |
dpavlin |
703 |
$html = $self->html_links( $html ); |
80 |
dpavlin |
680 |
|
81 |
|
|
return $html; |
82 |
dpavlin |
679 |
} |
83 |
|
|
|
84 |
|
|
=head1 DESCRIPTION |
85 |
|
|
|
86 |
|
|
Create Frey class based on L<Frey::Skeleton>. |
87 |
|
|
|
88 |
|
|
Blame this on my Smalltalk influence, because creating classes there is just |
89 |
|
|
interface and message sends wrapped in GUI, but that makes Smalltalk books |
90 |
|
|
much harder to read... |
91 |
|
|
|
92 |
|
|
On the other hand, having handy class creator within L<Frey> makes sense. |
93 |
|
|
|
94 |
|
|
=cut |
95 |
|
|
|
96 |
|
|
1; |