Last active
February 23, 2017 10:14
-
-
Save Songmu/11643592e98c9ccc0dfb4e524e956b0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.014; | |
use warnings; | |
use utf8; | |
use autodie; | |
use JSON::PP qw/encode_json/; | |
my $os = shift || 'darwin'; | |
my $cmd = { | |
darwin => 'brew list --versions', | |
redhat => 'yum list installed', | |
debian => 'dpkg -l', | |
}->{$os} or die 'unsupported os specified'; | |
my @lines = split /\n/, `$cmd`; | |
my @packages; | |
for my $line (@lines) { | |
my $info = parse_packge($os, $line); | |
push @packages, $info if $info; | |
} | |
say encode_json { | |
packages => \@packages, | |
}; | |
sub parse_packge { | |
my ($os, $line) = @_; | |
if ($os eq 'darwin') { | |
# zsh 5.0.5 5.2 5.3.1 | |
my @stuff = split /\s+/, $line; | |
return { | |
name => $stuff[0], | |
version => $stuff[-1], | |
}; | |
} | |
elsif ($os eq 'debian') { | |
# ii zsh-common 5.0.7-5 all architecture independent files for Zsh | |
my @stuff = split /\s+/, $line; | |
return unless $stuff[0] eq 'ii'; | |
return { | |
name => $stuff[1], | |
version => $stuff[2], | |
architecture => $stuff[3], | |
}; | |
} | |
elsif ($os eq 'redhat') { | |
# zsh.x86_64 4.2.6-9.el5 installed | |
# zip.x86_64 3.0-1.el6_7.1 @base | |
my @stuff = split /\s+/, $line; | |
return unless scalar(@stuff) == 3; | |
return unless sub { | |
my $l = shift; | |
return 1 if $l eq 'installed'; | |
return 1 if $l =~ /^\@/; | |
return undef; | |
}->($stuff[2]); | |
my @package_and_arch = split /\./, $stuff[0]; | |
my $arch = pop @package_and_arch; | |
my $package = join '.', @package_and_arch; | |
return if $arch !~ /^(i[3-6]86|k6|athlon|x86_64|noarch|ppc|alpha|sparc)$/; | |
return { | |
name => $package, | |
version => $stuff[1], | |
architecture => $arch, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment