blib/lib/App/FileComposer.pm

filter: Statementstmt 64.4% 38 / 59 Branchbran 25.0% 4 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 6 totaltotal 55.9% 52 / 93 SCAR 29.2
CC
10
Cov
56%
load
13.8
write
13.6
get_filename
2.0
CRAP 18.5
↑ Summary Build »
1 package App::FileComposer;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #// Constructor
11 3 has 'filename', is => 'rw', isa => 'Str';
12 3 has 'origin', is => 'rw', isa => 'Str', default => $ENV{"HOME"} . "/.app-filecomposer";
13  
14 =head1 NAME
15  
16 App::FileComposer - Dumps pre defined scripts!
17  
18 =head1 VERSION
19  
20 Version 0.01
21  
22 =cut
23  
24 3 our $VERSION = '0.01';
25  
26  
27 =head1 SYNOPSIS
28 Inside your module:
29  
30 use App::FileComposer;
31  
32 my $foo = App::FileComposer->new(filename => foo.c);
33 $foo->load();
34  
35 $foo->write();
36 ...
37  
38  
39 =head1 DESCRIPTION
40  
41 This module is an internal implemantation of a CLI Tool called mkscript
42 but, if you wish you can use its internal functions as a module in your script..
43  
44 App::FileComposer looks for Code samples inside some defined directory and use their data
45 to write new ones. It saves a lot of time from having to write the same initial lines of
46 code every time ...
47 instead you can define your own samples and load whatever is inside them into a new file
48  
49  
50 =head1 SUBROUTINES/METHODS
51  
52  
53 =head1
54  
55 =head1 set_filename()
56  
57 Change the filename defined in the new method, very Useful in case of Bad filename
58 errors..
59  
60  
61 =head1 get_filename()
62  
63 Get the Current filename passed to new...
64  
65  
66 =head1 set_sourcePath()
67  
68 If you wish to change the local of the sample files, define here
69 the default directory is: /home/user/.samples
70  
71  
72 =head1 get_sourcePath()
73  
74 Get the Current samples dir
75  
76  
77 =head1 load()
78  
79 load the default samples directory, it dies if does not exists
80  
81 =head1 write()
82  
83 Once the file is loaded through load(), you can write..
84 write() will dump the file in ./ (The current working directory)
85  
86  
87 =cut
88  
89  
90  
91  
92 =head1 AUTHOR
93  
94 Ariel Vieira, C<< <ariel.vieira at yandex.com> >>
95  
96 =head1 BUGS
97  
98 github: L<https://github.com/ariDevelops>
99  
100 Please report any bugs or feature requests to C<bug-app-filecomposer at rt.cpan.org>, or through
101 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-FileComposer>. I will be notified, and then you'll
102 automatically be notified of progress on your bug as I make changes.
103  
104  
105  
106  
107 =head1 SUPPORT
108  
109 You can find documentation for this module with the perldoc command.
110  
111 perldoc App::FileComposer
112  
113  
114 You can also look for information at:
115  
116 =over 4
117  
118 =item * RT: CPAN's request tracker (report bugs here)
119  
120 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=App-FileComposer>
121  
122 =item * CPAN Ratings
123  
124 L<https://cpanratings.perl.org/d/App-FileComposer>
125  
126 =item * Search CPAN
127  
128 L<https://metacpan.org/release/App-FileComposer>
129  
130 =back
131  
132  
133 =head1 ACKNOWLEDGEMENTS
134  
135  
136 =head1 LICENSE AND COPYRIGHT
137  
138 This software is Copyright (c) 2023 by Ariel Vieira.
139  
140 This is free software, licensed under:
141  
142 The GNU General Public License, Version 2, June 1991
143  
144  
145 =cut
146  
147  
148  
149 #// setters & getters
150  
151 sub set_filename {
152 1 ▶ my ($self, $newname) = @_;
Subroutine set_filename: called CC=1 SCAR=0.0
Pod undocumented
153 1 $self->{'filename'} = $newname;
154 }
155  
156 sub set_sourcePath {
157 1 ▶ my($self, $path) = @_;
Subroutine set_sourcePath: called CC=1 SCAR=0.0
Pod undocumented
158 1 $self->{'origin'} = $path;
159 }
160  
161 sub get_filename {
162 0 ▶ my $self = shift;
Subroutine get_filename: not called CC=1 SCAR=6.9
Pod undocumented
163 0 return $self->{'filename'};
164 }
165  
166 sub get_sourcePath {
167 0 ▶ my $self = shift;
Subroutine get_sourcePath: not called CC=1 SCAR=6.9
Pod undocumented
168 0 return $self->{'origin'};
169 }
170  
171  
172  
173 #// core methods
174  
175  
176 sub load {
177 3 ▶ my $self = shift;
Subroutine load: called CC=6 SCAR=26.2
Pod undocumented
178 3 my $origin = $self->{'origin'};
179 3 my $filename = $self->{'filename'};
180
181 ### Block user from supplying bad filenames
182 croak BRIGHT_RED
183 'Bad Filename attribute FileComposer->new(filename => \'foo.pl\')', RESET ,
184 "\n you must supply extensions like: <name.pl>, <name.py>, <name.sh>\n"
185 3 ▶ unless $self->{'filename'} =~ /^.+(\.\w+)\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"filename"} =~ /^.+(\.\w+)\b/i 1 2 3
186
187  
188
189  
190 ### isolate the extension in $1
191 2 $filename =~ m{^.+(\.[a-z]+)}i;
192 2 my $extension = $1;
193
194 ### Search the file we want
195 2 ▶ opendir DIRHANDLE, $origin
Branch: 1 branch
BranchTrueFalseTotal
opendir DIRHANDLE, $origin or croak(BRIGHT_RED(), " The $origin directory does not exist\n" . "run in Terminal: \$ mkdir $origin or mkscript --reconf", RESET()) 2 0 2
196 or croak BRIGHT_RED,
197 " The $origin directory does not exist\n".
198 "run in Terminal: \$ mkdir $origin or mkscript --reconf", RESET;
199
200
201 #// define a flag
202 0 our $i_found;
203
204 0 ▶ while(readdir DIRHANDLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readdir DIRHANDLE)) 0 0 0
205 0 ▶ next unless /$extension\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless /$extension\b/i 0 0 0
206  
207 #// flag
208 0 $i_found = $_; # the sample file we want !
209
210 0 }close(DIRHANDLE);
211
212
213
214 0 ▶ if ($i_found) { return $i_found; }
Branch: 1 branch
BranchTrueFalseTotal
if ($App::FileComposer::i_found) { } 0 0 0
215 else{
216
217 ###stop the code if don't find the extension we want
218 0 die BRIGHT_RED,
219 "No sample file in $origin containing extension $extension \n",
220 RESET;
221 }
222
223 }
224  
225  
226  
227 sub write {
228 1 ▶ my ($self, $where) = @_;
Subroutine write: called CC=5 SCAR=26.1
Pod undocumented
229 1 my $origin = $self->{'origin'};
230 1 my $filename = $self->{'filename'};
231 #// flag
232  
233 1 our $i_found;
234
235 #// dies if we have not the file in $i_found
236 1 ▶ croak '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $App::FileComposer::i_found 1 0 1
237 The source guidelines were not loaded internally,
238 you forgot to load them..
239 Set: $obj->load() method in your code before use write()
240 ' unless defined $i_found;
241  
242  
243  
244 {
245  
246 #// define a temp file
247 0 my $temp = "temp.$$";
248  
249 #// copy data in sample file to the temp file
250 #// after that, rename it
251 0 ▶ open INPUT , '<', "$origin/$i_found"
Branch: 1 branch
BranchTrueFalseTotal
open INPUT, "<", "$origin/$App::FileComposer::i_found" or die "error: $! \n" 0 0 0
252 or die "error: $! \n";
253  
254 0 my @load_file_in_mem = <INPUT>;
255 0 close(INPUT);
256  
257 0 ▶ open OUTPUT, '>>', "./$temp"
Branch: 1 branch
BranchTrueFalseTotal
open OUTPUT, ">>", "./$temp" or die "cannot write to $temp, error: $!\n" 0 0 0
258 or die "cannot write to $temp, error: $!\n";
259
260 0 print OUTPUT $_ for @load_file_in_mem;
261 0 rename $temp, $filename;
262  
263 0 close(OUTPUT);
264
265 }
266  
267
268 }
269  
270  
271  
272 3 1; # End of App::FileComposer
↑ Summary Build »