/
Base.pm
334 lines (246 loc) · 7.55 KB
/
Base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package App::Parrot::Create::Base;
use v5.14;
use Moose::Role;
use App::Parrot::Create::Type;
use Template;
use Method::Signatures;
use File::Spec;
use File::Path;
use File::Temp;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
requires 'init';
has 'name' => (
is => 'rw',
isa => 'Str',
required => 1,
predicate => 'has_name',
default => 'name'
);
has 'build_system' => (
isa => 'Build_Systems',
is => 'rw',
required => 1,
predicate => 'has_build_system',
default => 'Perl 5'
);
has 'test_system' => (
isa => 'Test_Systems',
is => 'rw',
required => 1,
predicate => 'has_test_system',
default => 'Perl 5'
);
has 'archive_path' => (
isa => 'Str',
is => 'rw',
predicate => 'has_archive_path',
trigger => sub {
my $self = shift;
unless (-f $self->archive_path) {
die "File $self->archive_path doesn't found\n";
}
}
);
has 'template' => (
isa => 'Str',
is => 'rw',
predicate => 'has_template',
trigger => sub {
my $self = shift;
unless (-f $self->template) {
die "File $self->template doesn't found\n";
}
}
);
method get_template() {
my ($volume, $dir, $file) = File::Spec->splitpath($self->template);
my $filedir = File::Spec->catpath($volume, $dir);
return ($filedir, $file);
}
method generate_template($template_dir,$template_file) {
my $config = {
INCLUDE_PATH => $template_dir,
EVAL_PERL => 1,
};
my $tt = Template->new($config);
my $objects = {
WINXED => App::Parrot::Create::Type::WINXED,
NQP => App::Parrot::Create::Type::NQP,
PERL5 => App::Parrot::Create::Type::PERL5,
PIR => App::Parrot::Create::Type::PIR,
ROSELLA_WINXED => App::Parrot::Create::Type::ROSELLA_WINXED,
ROSELLA_NQP => App::Parrot::Create::Type::ROSELLA_NQP,
object => $self,
};
my $output;
$tt->process($template_file, $objects, \$output)
|| die $tt->error;
return $output;
}
method generate_project($content_template) {
my @lines_template = split(/\n/, $content_template);
my $time = time;
my $tmp_base = File::Temp->newdir("app-parrot-create-XXXXXXX");
my $filepath = "$tmp_base/$time/".$self->name;
my $fh;
while (@lines_template) {
my $line_template = shift @lines_template;
last if ($line_template =~ /^__END__$/);
#TODO: Change normal for human
if ($line_template =~ /^__(.*)__$/) {
my ($volume, $dir, $file) = File::Spec->splitpath("$filepath/$1");
my $filedir = File::Spec->catpath($volume, $dir);
unless (-d $filedir) {
say "creating $filedir";
mkpath( [ $filedir ], 0, 0777 );
}
say "creating $filedir$file";
if ($fh) {
close $fh;
undef $fh;
}
open $fh, '>', "$filedir/$file" or die $!;
}
elsif($fh) {
say $fh $line_template;
}
}
close($fh) if($fh);
my $project_path = "$tmp_base/$time/";
my $archive_path = $self->generate_archive($project_path);
return $archive_path;
}
method generate_archive($path_to_project) {
my $zip = Archive::Zip->new();
my $dir_member = $zip->addTree("$path_to_project/");
my $time = time;
#TODO: Change file path
unless ( $zip->writeToFileNamed("/tmp/$time-".$self->name.".zip") == AZ_OK ) {
die 'write error';
}
$self->archive_path("/tmp/$time-".$self->name.".zip");
return $self->archive_path;
}
method generate() {
my $content_template = $self->generate_template($self->get_template());
my $archive_path = $self->generate_project($content_template);
return $archive_path;
}
1;
__END__
=pod
=head1 NAME
App::Parrot::Create::Base - Moose role for app-parrot-create web project.
=head1 SYNOPSIS
package Foo;
use Moose;
with 'App::Parrot::Create::Base';
sub init {
#need to set base options
$self->name(...);
$self->build_system(...);
$self->test_system(...);
#and other if you will create
}
#... which allows
my $bar = Foo->new();
$bar->init(...);
=head1 REQUIRED FIELDS
name - Name High Level Language or Library for Parrot VM.
It's a String type.
#set the name
$self->name(...)
#check the name
if ($self->has_name){
...
}
build_system - Build system based on one of the languages which providing Parrot VM.
It's a String Constant type from App::Parrot::Create::Type package.
#set the build system
$self->build_system(App::Parrot::Create::Type::WINXED)
#check the build system
if ($self->has_build_system){
...
}
test_system - Test system based on one of the languages which providing Parrot VM.
It's a String Constant type from App::Parrot::Create::Type package.
#set the test system
$self->test_system(App::Parrot::Create::Type::WINXED)
#check the test system
if ($self->has_test_system){
...
}
=head1 FIELDS
archive_path - contain a file path to project zip.
It's a String type.
template - contain a file path on template files. It's reading from config file.
It's a String type.
=head1 METHODS
=head3 get_template
=head4 Description:
get a full path on current file template;
=cut
=head4 Args:
None;
=cut
=head4 Retuns:
@array;
=cut
=head4
$array[0] - folder; $array[1] - file;
=cut
=head3 generate_template
=head4 Description:
generate template content from template file;
=cut
=head4 Args:
$template_dir,
$template_file;
=cut
=head4 Retuns:
$template_content;
=cut
=head3 generate_project
=head4 Description:
creating a skeleton of project from template content and packing it.
=cut
=head4 Args:
$template_content;
=cut
=head4 Retuns:
$archive_path;
=cut
=head3 generate_archive
=head4 Description:
packing a directory content into zip archive.
=cut
=head4 Args:
$path_to_dir;
=cut
=head4 Retuns:
$archive_path;
=cut
=head3 generate
=head4 Description:
wrapper which is calling generate_template and generate_project methods.
=cut
=head4 Args:
None;
=cut
=head4 Retuns:
$archive_path;
=cut
=head1 DESCRIPTION
This module built on Moose::Role module. It's including a base methods and fields, which is the same for Parrot:HLL and App::Parrot::Create::Library.
If you want to use this role you will need to realization an init method. In this method you will need to set a required fields.
It are name, build_system, test_system.
=head1 BUGS
If you find bugs please report us.
https://github.com/letolabs/app-parrot-create
=head1 AUTHORS
Jonathan Leto duke@leto.net
Denis Boyun denisboyun@gmail.com
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2013, Parrot Foundation.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0
=cut