File Coverage

blib/lib/App/Antigen.pm
Criterion Covered Total %
statement 15 56 26.7
branch 0 6 0.0
condition n/a
subroutine 5 13 38.4
pod 6 6 100.0
total 26 81 32.1


line stmt bran cond sub pod time code
1             package App::Antigen;
2              
3 1     1   18921 use Moo;
  1         13392  
  1         7  
4 1     1   2044 use MooX::Options;
  1         1175  
  1         12  
5 1     1   211359 use YAML::Tiny;
  1         3853  
  1         49  
6 1     1   662 use Path::Tiny;
  1         9098  
  1         62  
7 1     1   430 use IPC::System::Simple qw/ system /;
  1         7302  
  1         543  
8              
9             our $VERSION = '0.001';
10              
11             =head1 NAME
12              
13             App::Antigen - Plugin Manager for Zsh
14              
15             =head1 SYNOPSIS
16              
17             use App::Antigen;
18              
19             my $app = App::Antigen->new_with_options( plugins => \@plugins );
20             $app->run;
21              
22             =head1 DESCRIPTION
23              
24             App::Antigen is the underlying code for the antigen-perl tool, which is used
25             for managing Zsh plugins. This module is still under development, and so the
26             interface is subject to change as new features are added, and bugs are found.
27              
28             =head2 Todo
29              
30             There are many things which are still to do in this, including supporting
31             upgrades to the plugins as hosted on github, as well as adding support for
32             other targets such as normal git repos, tarball downloads, and local files and
33             folders. As said before, this module is still under development, and may change
34             entirely without warning.
35              
36             =head2 Attributes
37              
38             These are the attributes provided (using MooX::Options). These can also be put
39             in the configuration file - see L
40              
41             =head3 output
42              
43             This it the output folder into which all the plugin repositories and code will
44             be put. Defaults to $HOME/.antigen-perl
45              
46             =cut
47              
48             option 'output' => (
49             is => 'lazy',
50             format => 's',
51             short => 'o',
52             default => sub { File::Spec->catfile( $ENV{HOME}, '.antigen-perl' ) },
53             doc => 'Directory for all Antigen-Perl output files',
54             );
55              
56             =head3 repo
57              
58             This is the folder where all the repositories will be stored. Defaults to
59             $HOME/.antigen-perl/repos
60              
61             =cut
62              
63             option 'repo' => (
64             is => 'lazy',
65             format => 's',
66             short => 'r',
67 0     0     builder => sub { File::Spec->catfile( $_[0]->output, 'repos' ) },
68             doc => 'Directory for Antigen-Perl repos',
69             );
70              
71             =head3 output_file
72              
73             This is the file which will contain all the calls to the various plugins for
74             zsh to load. Defaults to $HOME/.antigen-perl/antigen-perl.zsh
75              
76             =cut
77              
78             option 'output_file' => (
79             is => 'lazy',
80             format => 's',
81             short => 'f',
82 0     0     builder => sub { File::Spec->catfile( $_[0]->output, 'antigen-perl.zsh' ) },
83             doc => 'Final output file for sourcing',
84             );
85              
86             =head3 plugins
87              
88             This contains an array of hashrefs of the plugins, with the keys as the method/place to
89             get the plugins from. Currently only accepts one method for getting the
90             plugins, github. An example plugin config:
91              
92             my $plugins = [
93             github => "TBSliver/zsh-theme-steeef",
94             github => "TBSliver/zsh-plugin-extract"
95             ];
96              
97             =cut
98              
99             has 'plugins' => (
100             is => 'ro',
101             required => 1,
102             );
103              
104             =head2 Methods
105              
106             These are the various methods which are provided, either for internal use or
107             for basic usage.
108              
109             =head3 run
110              
111             This is the main method of App::Antigen, and when called will actually build
112             the entire plugin structure, according to the plugin options specified.
113              
114             =cut
115              
116             sub run {
117 0     0 1   my $self = shift;
118              
119 0           my @plugin_dirs;
120              
121 0           for my $plugin ( @{ $self->plugins } ) {
  0            
122 0 0         if ( exists $plugin->{ github } ) {
123 0           push @plugin_dirs, $self->github_cmd( $plugin->{ github } );
124             }
125             }
126              
127 0           my @plugin_files;
128              
129 0           for my $plugin_dir ( @plugin_dirs ) {
130 0           push @plugin_files, $self->find_plugin( $plugin_dir );
131             }
132              
133 0           $self->write_output_file( \@plugin_files, \@plugin_dirs );
134              
135 0           print "To actually use the plugins, make sure you have the following line at the bottom of your ~/.zshrc:\n\n";
136 0           print " source " . $self->output_file . "\n\n\n";
137             }
138              
139             =head3 gen_github_url
140              
141             This function generates the github repository URL as required for getting the
142             plugins.
143              
144             =cut
145              
146             sub gen_github_url {
147 0     0 1   my ( $self, $repo ) = @_;
148              
149 0           return sprintf( "https://github.com/%s.git", $repo );
150             }
151              
152             =head3 gen_plugin_target
153              
154             This function performs a regex on the github url, replacing all colons (:) with
155             '-COLON-', and all slashes (/) with '-SLASH-'. This is then used as the folder
156             name for the github target.
157              
158             =cut
159              
160             sub gen_plugin_target {
161 0     0 1   my ( $self, $repo ) = @_;
162              
163 0           $repo =~ s/:/-COLON-/g;
164 0           $repo =~ s/\//-SLASH-/g;
165              
166 0           return File::Spec->catfile( $self->repo, $repo );
167             }
168              
169             =head3 github_cmd
170              
171             This function pulls together the github url and target folder, and actually
172             performs the git command using a call out to system.
173              
174             =cut
175              
176             sub github_cmd {
177 0     0 1   my ( $self, $repo ) = @_;
178              
179 0           my $url = $self->gen_github_url( $repo );
180 0           my $output_file = $self->gen_plugin_target( $url );
181              
182 0 0         if ( -d $output_file ) {
183 0           print "skipping existing plugin $repo\n";
184             } else {
185 0           system ( 'git', 'clone', '--recursive', '--', $url, $output_file );
186             }
187              
188 0           return $output_file;
189             }
190              
191             =head3 find_plugin
192              
193             This finds all the plugins inside the repo directory with a file extension of
194             *.plugin.zsh and addes them to the plugin list. This will find every occurance
195             of a file with that plugin extension.
196              
197             =cut
198              
199             sub find_plugin {
200 0     0 1   my ( $self, $dir ) = @_;
201              
202 0           my @plugins;
203              
204 0           my $iter = path( $dir )->iterator;
205 0           while (my $path = $iter->() ) {
206 0 0         push ( @plugins, $path->stringify ) if $path =~ /\.plugin\.zsh$/;
207             }
208              
209 0           return @plugins;
210             }
211              
212             =head3 write_output_file
213              
214             This takes all the plugins found with the correct extension, and puts them in a
215             single file ready to be added to your .zshrc
216              
217             =cut
218              
219             sub write_output_file {
220 0     0 1   my ( $self, $plugins, $directories ) = @_;
221              
222 0           my $file = path( $self->output_file );
223              
224 0           my @lines = (
225             "# Generated by Script antigen-perl\n"
226             );
227              
228 0           push @lines, map { "source " . $_ . "\n" } @$plugins;
  0            
229 0           push @lines, map { "fpath+=" . $_ . "\n" } @$directories;
  0            
230              
231 0           $file->spew( join "", @lines );
232             }
233              
234             =head1 AUTHOR
235              
236             Tom Bloor Etom.bloor@googlemail.comE
237              
238             =head1 COPYRIGHT
239              
240             Copyright 2014- Tom Bloor
241              
242             =head1 LICENSE
243              
244             This library is free software; you can redistribute it and/or modify
245             it under the same terms as Perl itself.
246              
247             =head1 SEE ALSO
248              
249             L, L
250              
251             =cut
252              
253             1;