Initial commit master 0.001
authorMarius Gavrilescu <marius@ieval.ro>
Thu, 31 Aug 2017 23:05:05 +0000 (02:05 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Thu, 31 Aug 2017 23:05:05 +0000 (02:05 +0300)
Changes [new file with mode: 0644]
MANIFEST [new file with mode: 0644]
Makefile.PL [new file with mode: 0644]
README [new file with mode: 0644]
lib/Android/ADB.pm [new file with mode: 0644]
lib/Android/ADB/Device.pm [new file with mode: 0644]
t/Android-ADB.t [new file with mode: 0644]

diff --git a/Changes b/Changes
new file mode 100644 (file)
index 0000000..9a22867
--- /dev/null
+++ b/Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension Android::ADB.
+
+0.001 2017-09-01T00:05+01:00  Thu Aug 31 01:16:32 2017
+ - Initial release
diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..8d38ac4
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,7 @@
+Changes
+Makefile.PL
+MANIFEST
+README
+t/Android-ADB.t
+lib/Android/ADB.pm
+lib/Android/ADB/Device.pm
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..72df034
--- /dev/null
@@ -0,0 +1,22 @@
+use 5.014000;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+       NAME              => 'Android::ADB',
+       VERSION_FROM      => 'lib/Android/ADB.pm',
+       ABSTRACT_FROM     => 'lib/Android/ADB.pm',
+       AUTHOR            => 'Marius Gavrilescu <marius@ieval.ro>',
+       MIN_PERL_VERSION  => '5.14.0',
+       LICENSE           => 'perl',
+       SIGN              => 1,
+       PREREQ_PM         => {
+               qw/Class::Accessor 0
+                  File::Slurp     0/,
+       },
+       META_ADD         => {
+               dynamic_config => 0,
+               resources      => {
+                       repository   => 'https://git.ieval.ro/?p=android-adb.git',
+               },
+       }
+);
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..1381e80
--- /dev/null
+++ b/README
@@ -0,0 +1,31 @@
+Android-ADB version 0.001
+=========================
+
+This module is a minimal wrapper over the Android Debug Bridge
+(adb) command for manipulating Android devices.
+
+INSTALLATION
+
+To install this module type the following:
+
+   perl Makefile.PL
+   make
+   make test
+   make install
+
+DEPENDENCIES
+
+This module requires these other modules and libraries:
+
+* Class::Accessor
+* File::Slurp
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2017 by Marius Gavrilescu
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.24.2 or,
+at your option, any later version of Perl 5 you may have available.
+
+
diff --git a/lib/Android/ADB.pm b/lib/Android/ADB.pm
new file mode 100644 (file)
index 0000000..9c0eb99
--- /dev/null
@@ -0,0 +1,189 @@
+package Android::ADB;
+
+use 5.014000;
+use strict;
+use warnings;
+
+our $VERSION = '0.001';
+
+use Android::ADB::Device;
+use Carp;
+use File::Slurp;
+use IPC::Open2;
+
+sub new {
+       my ($class, %args) = @_;
+       $args{path} //= $ENV{ADB};
+       $args{path} //= 'adb';
+       $args{args} //= [];
+       bless \%args, $class
+}
+
+sub run {
+       my ($self, @args) = @_;
+       my ($out, $in);
+       my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : ();
+       my $pid = open2 $out, $in, $self->{path}, @{$self->{args}}, @args;
+       my $result = read_file $out;
+       close $out;
+       close $in;
+       waitpid $pid, 0 or croak "$!";
+       $result;
+}
+
+sub start_server { shift->run('start-server') }
+sub kill_server  { shift->run('kill-server') }
+
+sub connect      { shift->run('connect', @_) }
+sub disconnect   { shift->run('disconnect', @_) }
+
+sub devices      {
+       my @devices = split '\n', shift->run('devices', '-l');
+       my @result;
+       for (@devices) {
+               next if /^List of devices/;
+               next unless / /;
+               push @result, Android::ADB::Device->new(split)
+       }
+       @result
+}
+
+sub set_device {
+       my ($self, $device) = @_;
+       $self->{device_serial} = $device->serial;
+}
+
+sub wait_for_device   { shift->run('wait-for-device') }
+sub get_state         { shift->run('get-state') }
+sub get_serialno      { shift->run('get-serialno') }
+sub get_devpath       { shift->run('get-devpath') }
+sub remount           { shift->run('remount') }
+sub reboot            { shift->run('reboot', @_) }
+sub reboot_bootloader { shift->run('reboot-bootloader') }
+sub root              { shift->run('root') }
+sub usb               { shift->run('usb') }
+sub tcpip             { shift->run('tcpip', @_) }
+
+sub push {
+       my ($self, $local, $remote) = @_;
+       $self->run(push => $local, $remote)
+}
+
+sub pull {
+       my ($self, $remote, $local) = @_;
+       $self->run(push => $remote, $local)
+}
+
+sub pull_archive {
+       my ($self, $remote, $local) = @_;
+       $self->run(push => '-a', $remote, $local)
+}
+
+sub shell { shift->run(shell => @_) }
+
+1;
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+Android::ADB - thin wrapper over the 'adb' command
+
+=head1 SYNOPSIS
+
+  use Android::ADB;;
+  my $adb = Android::ADB->new(path => '/opt/android/platform-tools/adb');
+  my @devices = $adb->devices;
+  $adb->set_device($devices[0]);
+  $adb->push('file.txt', '/sdcard/');
+  sleep 10;
+  $adb->reboot('recovery');
+
+=head1 DESCRIPTION
+
+This module is a minimal wrapper over the Android Debug Bridge
+(C<adb>) command for manipulating Android devices.
+
+Methods die on non-zero exit code and return the text printed by the
+C<adb> command. The available methods are:
+
+=over
+
+=item Android::ADB->B<new>([I<args>])
+
+Create a new Android::ADB object. The available arguments are C<path>,
+the path to the C<adb> executable (defaults to the value of the
+environment variable C<ADB> or the string C<adb>) and C<args>, an
+arrayref of arguments passed to every adb command (defaults to []).
+
+=item $adb->B<devices>
+
+Returns a list of L<Android::ADB::Device> objects representing
+connected devices.
+
+=item $adb->B<set_device>(I<$device>)
+
+Takes an L<Android::ADB::Device> and directs all further commands to
+that device by passing C<-s serialno> to every command.
+
+=item $adb->B<run>(I<$command>, [I<@args>])
+
+Run an arbitrary ADB command and return its output.
+
+=item $adb->B<start_server>
+
+=item $adb->B<kill_server>
+
+=item $adb->B<connect>(I<$host_and_port>)
+
+=item $adb->B<disconnect>([I<$host_and_port>])
+
+=item $adb->B<wait_for_device>
+
+=item $adb->B<get_state>
+
+=item $adb->B<get_serialno>
+
+=item $adb->B<get_devpath>
+
+=item $adb->B<remount>
+
+=item $adb->B<reboot>([I<$where>])
+
+=item $adb->B<reboot_bootloader>
+
+=item $adb->B<root>
+
+=item $adb->B<usb>
+
+=item $adb->B<tcpip>(I<$port>)
+
+=item $adb->B<push>(I<$local>, I<$remote>)
+
+=item $adb->B<pull>(I<$remote>, I<$local>)
+
+=item $adb->B<shell>(I<@args>)
+
+Analogues of the respective adb commands.
+
+=item $adb->B<pull_archive>(I<$remote>, I<$local>)
+
+Same as C<adb pull -a $remote $local>.
+
+=back
+
+=head1 AUTHOR
+
+Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2017 by Marius Gavrilescu
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.24.2 or,
+at your option, any later version of Perl 5 you may have available.
+
+
+=cut
diff --git a/lib/Android/ADB/Device.pm b/lib/Android/ADB/Device.pm
new file mode 100644 (file)
index 0000000..6dde6b3
--- /dev/null
@@ -0,0 +1,63 @@
+package Android::ADB::Device;
+
+use 5.014000;
+use strict;
+use warnings;
+
+use parent qw/Class::Accessor::Fast/;
+
+our $VERSION = '0.001';
+
+sub new {
+       my ($class, $serial, $state, @attrs) = @_;
+       my %attrs = map { split ':', $_, 2 } @attrs;
+       bless { serial => $serial, state => $state, %attrs }, $class
+}
+
+__PACKAGE__->mk_ro_accessors(qw/serial state usb product model device/);
+
+1;
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+Android::ADB::Device - information about an Android device
+
+=head1 SYNOPSIS
+
+  use Android::ADB;
+  my @devices = $adb->devices;
+  say $devices[0]->serial;
+  say $devices[0]->state; # e.g. offline, bootloader, sideload, or device
+
+  # The available attributes depend on your device
+  say $devices[0]->usb;     # e.g. 2-1
+  say $devices[0]->product; # e.g. angler
+  say $devices[0]->model;   # e.g. MI_MAX
+  say $devices[0]->device;  # e.g. angler
+
+=head1 DESCRIPTION
+
+Information about an Android device in form of a blessed hash with a
+few accessors. See SYNPOSIS for a list of accessors.
+
+=head1 SEE ALSO
+
+L<Android::ADB>
+
+=head1 AUTHOR
+
+Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2017 by Marius Gavrilescu
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.24.2 or,
+at your option, any later version of Perl 5 you may have available.
+
+
+=cut
diff --git a/t/Android-ADB.t b/t/Android-ADB.t
new file mode 100644 (file)
index 0000000..f3b8346
--- /dev/null
@@ -0,0 +1,6 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+BEGIN { use_ok('Android::ADB') };
This page took 0.018105 seconds and 4 git commands to generate.