File Coverage

blib/lib/App/FileComposer.pm
Criterion Covered Total %
statement 34 55 61.8
branch 4 14 28.5
condition n/a
subroutine 10 12 83.3
pod 0 6 0.0
total 48 87 55.1


line stmt bran cond sub pod time code
1             package App::FileComposer;
2              
3 3     3   210999 use warnings;
  3         31  
  3         100  
4 3     3   17 use strict;
  3         4  
  3         66  
5 3     3   18 use feature 'say';
  3         5  
  3         459  
6 3     3   2069 use Term::ANSIColor qw(:constants);
  3         27783  
  3         3302  
7 3     3   1969 use Moose;
  3         1435252  
  3         19  
8 3     3   23637 use Carp qw(croak);
  3         11  
  3         2990  
9              
10             #// Constructor
11             has 'filename', is => 'rw', isa => 'Str';
12             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             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     1 0 1756 my ($self, $newname) = @_;
153 1         4 $self->{'filename'} = $newname;
154             }
155              
156             sub set_sourcePath {
157 1     1 0 1524 my($self, $path) = @_;
158 1         6 $self->{'origin'} = $path;
159             }
160              
161             sub get_filename {
162 0     0 0 0 my $self = shift;
163 0         0 return $self->{'filename'};
164             }
165              
166             sub get_sourcePath {
167 0     0 0 0 my $self = shift;
168 0         0 return $self->{'origin'};
169             }
170              
171              
172              
173             #// core methods
174              
175              
176             sub load {
177 3     3 0 3185 my $self = shift;
178 3         7 my $origin = $self->{'origin'};
179 3         7 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 100       44 unless $self->{'filename'} =~ /^.+(\.\w+)\b/i;
186            
187              
188            
189              
190             ### isolate the extension in $1
191 2         21 $filename =~ m{^.+(\.[a-z]+)}i;
192 2         18 my $extension = $1;
193            
194             ### Search the file we want
195 2 50       228 opendir DIRHANDLE, $origin
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         0 our $i_found;
203            
204 0         0 while(readdir DIRHANDLE) {
205 0 0       0 next unless /$extension\b/i;
206              
207             #// flag
208 0         0 $i_found = $_; # the sample file we want !
209            
210 0         0 }close(DIRHANDLE);
211            
212            
213            
214 0 0       0 if ($i_found) { return $i_found; }
  0         0  
215             else{
216            
217             ###stop the code if don't find the extension we want
218 0         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     1 0 1076 my ($self, $where) = @_;
229 1         3 my $origin = $self->{'origin'};
230 1         3 my $filename = $self->{'filename'};
231             #// flag
232              
233 1         2 our $i_found;
234            
235             #// dies if we have not the file in $i_found
236 1 50       120 croak '
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.$$";
  0            
248              
249             #// copy data in sample file to the temp file
250             #// after that, rename it
251 0 0         open INPUT , '<', "$origin/$i_found"
252             or die "error: $! \n";
253              
254 0           my @load_file_in_mem = <INPUT>;
255 0           close(INPUT);
256              
257 0 0         open OUTPUT, '>>', "./$temp"
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             1; # End of App::FileComposer