-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpan.rb
140 lines (104 loc) · 3.26 KB
/
cpan.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# using CPAN as backend
require 'nokogiri'
require 'net/http'
repository.do {
if Packo.protected?
CLI.warn 'Run packo repository in unprotected mode (packo-repository) or CPAN may not work'
end
def versionify (version)
Versionomy.parse(version.gsub(/undef|version/i, '0').sub(/^[.v]/, ''))
end
def each_package (&block)
warn; cpan
`perl -MCPAN -e 'print join("\\n", map {$_->{ID}."-".$_->{RO}->{CPAN_VERSION}} CPAN::Shell->expand("Module", "/./"))'`.each_line {|line|
CLI.info "Parsing `#{line.strip}`" if System.env[:VERBOSE]
whole, name, version = line.strip.match(/^((?:\w+::)*\w+)-([^\-]+)$/).to_a
next unless whole
begin
versionify(version)
rescue Versionomy::Errors::ParseError => e
version.sub!(e.message.match(/Extra characters: "(.*?)"/).to_a.last, '')
begin
versionify(version)
rescue Versionomy::Errors::ParseError
version = '0'
end
end
block.call Package.new(
tags: ['perl', 'cpan'],
name: name,
version: versionify(version)
)
}
if $? != 0
raise RuntimeError.new 'CPAN failed'
end
end
def each_dependency (package, &block)
self.get_deps(package.name).each {|name|
block.call Package.new(
tags: ['perl', 'cpan'],
name: name,
version: nil
)
}
end
def has? (package)
!`perl -MCPAN -e 'CPAN::Shell->i("#{package.name}");'`.
include?("No objects found of any type for argument #{package.name}")
end
def install (package)
cpan('-i', package.name)
package = package.clone
package.contents = Packo.contents(filesystem.bin.cpan_files.execute(package.name).lines)
package.dependencies = Package::Dependencies.new(package)
self.dependencies(package).each {|dep|
package.dependencies << Package::Dependency.new(dep.to_hash)
}
package
end
# protected
memoize
def perl_version
`perl -MConfig -e 'print $Config{version};'`
end
def get_deps (package)
Nokogiri::XML(Net::HTTP.get(URI.parse("http://deps.cpantesters.org/?xml=1;module=%s;perl=%s;os=any%%20OS;pureperl=0" %
[package, perl_version].map {|s| URI.encode(s) }))).xpath('//cpandeps/dependency').select {|node|
node.xpath(node.path + '/depth').text == '1'
}.map {|node|
node.xpath(node.path + '/module').text
}
end
def cpan (*args)
if Packo.user?
root = File.join(ENV['HOME'], '.cpan')
lock = File.join(root, '.lock')
File.unlink(lock) if File.file?(lock)
if !File.file?(File.join(root, 'CPAN', 'MyConfig.pm'))
`echo -e "yes\nyes" | cpan`
end
end
Packo.sh('cpan', *args) if args.length > 0
end
}
__END__
---
--- bin/cpan_files ---
#! /usr/bin/env perl
use strict;
use warnings;
use ExtUtils::Installed;
use List::Util qw(first);
$\ = "\n";
$ARGV[0] or die "Usage: $0 Module::Name";
my $mod = $ARGV[0];
die "Does not look like a module name"
unless $mod =~ m{^\w+(::\w+)*$};
my $inst = ExtUtils::Installed->new();
die "Can't find module $mod using .packlist files"
unless first { $_ eq $mod } ( $inst->modules );
foreach my $item ( sort( $inst->files($mod) ) ) {
print $item
}
print $inst->packlist($mod)->packlist_file();