sub
trim_comment {
my
$c
=
shift
;
$c
=~ s/
return
$c
;
}
sub
trim {
my
$c
=
shift
;
$c
=~ s/^\s*//;
$c
=~ s/\s*$//;
return
$c
;
}
use
constant
META_FILES
=> [
'VIMMETA'
,
'META'
,
'VIMMETA.yml'
];
sub
new {
bless
{} ,
shift
}
sub
meta {
my
$self
=
shift
;
return
$self
->{meta} ||= {}; }
sub
read_metafile {
my
$self
=
shift
;
my
$file
=
$self
->find_meta_file();
die
'Can not found META file'
unless
-e
$file
;
open
my
$fh
,
"<"
,
$file
;
$self
->
read
(
$fh
);
close
$fh
;
return
$self
->meta;
}
sub
find_meta_file {
my
$files
= META_FILES;
for
(
@$files
) {
return
$_
if
-e
$_
;
}
}
sub
read
{
my
$self
=
shift
;
my
$fh
=
shift
;
my
@lines
= <
$fh
>;
my
$cur_section
;
my
%sections
= ();
for
(
@lines
) {
chomp
;
$_
= trim trim_comment(
$_
);
next
if
blank(
$_
);
if
( /^=(\w+)(?:\s+(.*?))?$/ ) {
$cur_section
= $1;
$sections
{
$cur_section
} = $2
if
$2;
next
;
}
push
@{
$sections
{
$cur_section
} } ,
$_
;
}
for
my
$sec
(
keys
%sections
) {
my
$lines
=
$sections
{
$sec
};
my
$dispatch
=
'__'
.
$sec
;
if
(
$self
->can(
$dispatch
) ) {
$self
->
$dispatch
(
$lines
);
}
else
{
$self
->{meta}->{
$sec
} =
$lines
;
}
}
}
my
$package_re
=
'[0-9a-zA-Z._-]+'
;
sub
blank {
my
$c
=
shift
;
return
$c
=~ /^\s*$/;
}
sub
_get_value {
my
$cur
=
shift
;
my
(
$v
) = (
$cur
=~ /^=\w+\s+(.*)$/ ) ;
return
$v
;
}
sub
__name {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{name} =
$value
;
}
sub
__email {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{email} =
$value
;
}
sub
__author {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{author} =
$value
;
}
sub
__version {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{version} =
$value
;
}
sub
__abstract {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{abstract} =
$value
;
}
sub
__type {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{type} =
$value
;
}
sub
__version_from {
my
(
$self
,
$version_file
) =
@_
;
$self
->meta->{version_from} =
$version_file
;
open
FH,
"<"
,
$version_file
;
my
@lines
= <FH>;
close
FH;
for
(
@lines
) {
if
( /^
"=VERSION ([0-9.]+)$/ or /^"
\s
*Version
:\s*([0-9.]+)$/i ) {
$self
->meta->{version} = $1;
return
;
}
}
print
"Warning: Can not found version, you should declare your version in your vim script.\n"
;
print
"For example \n"
;
print
" \" Version: 0.3 \n"
;
}
sub
__install_dirs {
my
(
$self
,
$lines
) =
@_
;
$self
->meta->{install_dirs} = [];
for
(
@$lines
) {
$_
= trim
$_
;
push
@{
$self
->meta->{install_dirs} },
$_
if
$_
;
}
}
sub
__dependency {
my
(
$self
,
$lines
) =
@_
;
$self
->meta->{dependency} = [];
my
%pkgs
= ();
my
$cur_name
;
for
(
@$lines
) {
if
( m{^ (
$package_re
) \s+ ([=<>]{1,2}) \s+ ([0-9.]+) }x ) {
my
(
$name
,
$op
,
$version
) = ( $1, $2, $3 );
$cur_name
=
$name
;
$pkgs
{
$name
} = {
name
=>
$name
,
op
=>
$op
,
version
=>
$version
,
};
next
;
}
elsif
( m{^(
$package_re
)$} ) {
$cur_name
= $1;
$pkgs
{
$cur_name
} = [];
next
;
}
elsif
( m{^\|\s*(.*?)\s*\|\s*(\S+)} ) {
my
(
$target
,
$from
) = ( $1, $2 );
push
@{
$pkgs
{
$cur_name
} } , {
from
=>
$from
,
target
=>
$target
};
}
my
$git_repo
=
$_
;
$pkgs
{
$cur_name
}->{git_repo} =
$git_repo
;
}
}
$self
->meta->{dependency} = [
map
( { {
name
=>
$_
,
required_files
=>
$pkgs
{
$_
} } }
grep
{
ref
(
$pkgs
{
$_
} ) eq
'ARRAY'
}
keys
%pkgs
),
map
( {
$pkgs
{
$_
} }
grep
{
ref
(
$pkgs
{
$_
} ) ne
'ARRAY'
}
keys
%pkgs
),
];
}
sub
__script {
my
(
$self
,
$lines
) =
@_
;
$self
->meta->{script} =
$lines
;
}
sub
__repository {
my
(
$self
,
$value
) =
@_
;
$self
->meta->{repository} =
$value
;
}
sub
__vim_version {
my
(
$self
,
$v
) =
@_
;
my
(
$op
,
$version
) =
$v
=~ m/^([<=>]{1,2})\s+([0-9.-a-z]+)/;
$self
->meta->{vim_version} = {
op
=>
$op
,
version
=>
$version
,
};
}
1;