File Coverage

blib/lib/App/FileComposer.pm
Criterion Covered Total %
statement 38 55 69.0
branch 4 14 28.5
condition n/a
subroutine 12 12 100.0
pod 0 7 0.0
total 54 88 61.3


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