blib/lib/VIM/Packager/Command/Init.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 169 CC 18 SCAR 41.7
CC
18
Cov
0%
run
110.0
create_dir_skeleton
90.0
options
2.0
CRAP 64.4
1 package VIM::Packager::Command::Init;
2 use warnings;
3 use strict;
4 use VIM::Packager;
5 use File::Path;
6 use File::Spec;
7 use DateTime;
8 use base qw(App::CLI::Command);
9  
10 =head1 NAME
11  
12 VIM::Packager::Command::Init - create vim package skelton
13  
14 =head1 init
15  
16 =head2 SYNOPSIS
17  
18 $ vim-packager init \
19 --name=ohai \
20 --type=plugin \
21 --author=Cornelius \
22 --email=cornelius.howl@gmail.com
23  
24 or just skip author name and email option if you have L</Autho Info File>
25  
26 $ vim-packager init \
27 --name=ohai \
28 --type=plugin \
29  
30 # optional
31 --dirs
32 --dirs=basic
33 --dirs=full
34 --dirs=auto # default
35  
36 even skip plugin name
37  
38 $ cd your-plugin.vim
39 $ vim-packager init --type plugin
40  
41 the plugin name will be C<your-plugin.vim>
42  
43 =head2 OPTIONS
44  
45 =over 4
46  
47 =item --name=[name] | -n
48  
49 =item --type=[type] | -t
50  
51 =item --author=[author] | -a
52  
53 =item --email=[email] | -e
54  
55 =item --migrate | -m
56  
57 =back
58  
59 =head2 Author Info File
60  
61 it locates at F<~/.vim-author>.
62  
63 the format is:
64  
65 author: Your Name
66 email: cornelius.howl @ delete-me.gmail.com
67  
68 =cut
69  
70 sub options {
71 (
72 'n|name=s' => 'name',
73 'v|verbose' => 'verbose',
74 'm|migration' => 'migration',
75 't|type=s' => 'type',
76 'a|author=s' => 'author',
77 'e|email=s' => 'email',
78 'd|dirs=s' => 'dirs',
79 );
80 }
81  
82 sub run {
83 my ( $self, @args ) = @_;
84  
85 my $plugin_name;
86 my $author;
87 my $email;
88  
89 $plugin_name ||= $self->{name};
90 unless( $plugin_name ) {
91 my $cwd;
92 chomp($cwd = `pwd`);
93 $cwd =~ s{/$}{};
94 my @s = split "/", $cwd;
95 $plugin_name = pop @s;
96 $self->{name} = $plugin_name;
97 }
98  
99 my $info_file = File::Spec->join( $ENV{HOME} , '.vim-author' ) ;
100 if( -e $info_file ) {
101 say "Found author info file";
102 # found author information file
103 open FH , "<" , $info_file or die $@;
104 while( <FH> ) {
105 chomp( $self->{author} = $_ ) if s/^author:\s+//;
106 chomp( $self->{email} = $_ ) if s/^email:\s+//;
107 }
108 say " Author: " . $self->{author};
109 say " Email: " . $self->{email};
110 close FH;
111 }
112  
113 unless( $self->{author} and $self->{email} ) {
114 say "Please specify --author and --email";
115 return;
116 }
117  
118 # migrate dirs
119 if( $self->{migrate} ) {
120 File::Path::mkpath [ 'vimlib' ];
121 my @known_dir_names = qw(autoload indent syntax colors doc plugin ftplugin after ftdetect);
122 for ( @known_dir_names ) {
123 if( -e $_ ) {
124 say "$_ directory found , migrate $_ into vimlib/ ";
125 rename $_ , File::Spec->join( 'vimlib', $_ );
126 }
127 }
128 }
129  
130 $self->create_dir_skeleton() if $self->{dirs};
131  
132 # if we have doc directory , create a basic doc skeleton
133 $self->create_doc_skeleton()
134 if -e File::Spec->join('vimlib' , 'doc') ;
135  
136 # create meta file skeleton
137 $self->create_meta_skeleton( );
138  
139 $self->create_readme_skeleton();
140 }
141  
142 sub create_readme_skeleton {
143 my $self = shift;
144 my $cmd = shift;
145
146 say "Creating README";
147  
148 open README , ">" , "README";
149 print README ""; # XXX
150 close README;
151  
152 }
153  
154 sub create_doc_skeleton {
155 my $self = shift;
156 my $name = $self->{name};
157  
158 say "Creating doc skeleton.";
159  
160 open DOC , ">" , File::Spec->join( 'vimlib', 'doc' , "$name.txt" );
161 print DOC <<END;
162  
163 *$name.txt* Plugin for ....
164  
165 $name *$name*
166 Last Change: @{[ DateTime->now ]}
167  
168 Version 0.0.0
169 Copyright (C) yourname
170 License: MIT license {{{
171 Permission is hereby granted, free of charge, to any person obtaining
172 a copy of this software and associated documentation files (the
173 "Software"), to deal in the Software without restriction, including
174 without limitation the rights to use, copy, modify, merge, publish,
175 distribute, sublicense, and/or sell copies of the Software, and to
176 permit persons to whom the Software is furnished to do so, subject to
177 the following conditions:
178  
179 The above copyright notice and this permission notice shall be included
180 in all copies or substantial portions of the Software.
181  
182 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
183 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
184 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEME and associated documentation files (the
185 "Software"), to deal in the Software without restriction, including
186 without limitation the rights to use, copy, modify, merge, publish,
187 distribute, sublicense, and/or sell copies of the Software, and to
188 permit persons to whom
189 }}}
190  
191 CONTENTS *$name-contents*
192  
193 |$name-description| Description
194 |$name-usage| Usage
195 |$name-key-mappings| Key mappings
196 |$name-variables| Variables
197 |$name-contact| Contact
198  
199 For Vim version 7.0 or later.
200 This plugin only works if 'compatible' is not set.
201 {Vi does not have any of these features.}
202  
203 ==============================================================================
204 DESCRIPTION *$name-description*
205  
206 *$name* is a plugin to provide way to ...
207  
208 ==============================================================================
209 USAGE *$name-usage*
210  
211  
212 ==============================================================================
213 KEY MAPPINGS *$name-key-mappings*
214  
215 xx_key *xx_key*
216 key description here
217  
218 ==============================================================================
219 VARIABLES *$name-variables*
220  
221 g:xxx_variable
222 Your variable here
223  
224 ==============================================================================
225 CHANGELOG *$name-changelog*
226  
227 ==============================================================================
228 CONTACT *$name-contact*
229  
230  
231 ==============================================================================
232 Document skeleton generated by VIM::Packager.
233  
234 vim:tw=78:ts=8:ft=help:norl:fen:fdl=0:fdm=marker:
235 END
236 close DOC;
237 }
238  
239 =head2 create_dir_skeleton
240  
241  
242  
243 =cut
244  
245 sub create_dir_skeleton {
246 my $self = shift;
247 say "Creating directories.";
248 if( $self->{dirs} eq 'basic' ) {
249 File::Path::mkpath [
250 map { File::Spec->join( 'vimlib' , $_ ) } qw(autoload syntax plugin ftplugin ftdetect doc)
251 ],1;
252 }
253 elsif( $self->{dirs} eq 'full' ) {
254 File::Path::mkpath [
255 map { File::Spec->join( 'vimlib' , $_ ) } qw(autoload syntax indent colors plugin ftplugin ftdetect compiler doc)
256 ],1;
257 }
258 elsif( $self->{dirs} eq 'auto' ) {
259  
260 # create direcotries by type
261 my $type = $self->{type};
262 if( $type ) {
263 if ( $type eq 'syntax' ) {
264 File::Path::mkpath [
265 map { File::Spec->join( 'vimlib' , $_ ) } qw(syntax indent)
266 ],1;
267 }
268 elsif( $type eq 'colors' ) {
269 File::Path::mkpath [
270 map { File::Spec->join( 'vimlib' , $_ ) } qw(colors)
271 ],1;
272 }
273 elsif( $type eq 'plugin' ) {
274 File::Path::mkpath [
275 map { File::Spec->join( 'vimlib' , $_ ) } qw(plugin doc autoload)
276 ],1;
277 }
278 elsif( $type eq 'ftplugin' ) {
279 File::Path::mkpath [
280 map { File::Spec->join( 'vimlib' , $_ ) } qw(ftplugin doc autoload)
281 ],1;
282 }
283 }
284 # or we just create some common directories
285 else {
286 # create basic skeleton directories
287 File::Path::mkpath [
288 map { File::Spec->join( 'vimlib' , $_ ) } qw(autoload plugin doc)
289 ],1;
290 }
291  
292 }
293  
294  
295 }
296  
297 sub create_meta_skeleton {
298 my $self = shift;
299 say "Writing META.";
300  
301 open FH, ">", "META";
302 print FH <<END;
303 \n=name @{[ $self->{name} ]}
304 \n=author @{[ $self->{author} ]}
305 \n=email @{[ $self->{email} ]}
306 \n=type @{[ $self->{type} || '[ script type ]' ]}
307 \n=version_from [File]
308 \n=vim_version >= 7.2
309 \n=dependency
310  
311 [name] >= [version]
312  
313 [name]
314 | autoload/libperl.vim | http://github.com/c9s/libperl.vim/raw/master/autoload/libperl.vim
315 | plugin/yours.vim | http://ohlalallala.com/yours.vim
316 \n=script
317  
318 # your script files here
319  
320 \n=repository git://....../
321  
322 END
323 close FH;
324  
325 }
326  
327 1;