| 1 | package Hubot::Creator; | ||
| 2 | $Hubot::Creator::VERSION = '0.2.8'; | ||
| 3 | use Moose; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | use Cwd 'cwd'; | ||
| 7 | use File::Copy (); | ||
| 8 | use File::Copy::Recursive 'dircopy'; | ||
| 9 | use File::Path 'mkpath'; | ||
| 10 | use File::ShareDir 'dist_dir'; | ||
| 11 | use File::Spec::Functions 'catfile'; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | has 'path' => ( is => 'ro', isa => 'Str', default => './hubot', ); | ||
| 15 | |||
| 16 | sub copy { | ||
| 17 | my ( $self, $src, $dst ) = @_; | ||
| 18 | |||
| 19 | print "Copying $src -> $dst\n"; | ||
| 20 | File::Copy::copy( $src, $dst ); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub run { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | my $path = $self->path; | ||
| 27 | |||
| 28 | print "Create a hubot install at $path\n"; | ||
| 29 | |||
| 30 | my $dist_dir = try { | ||
| 31 | File::ShareDir::dist_dir('Hubot'); | ||
| 32 | } | ||
| 33 | catch { | ||
| 34 | warn "not installed `Hubot` module"; # ignore $_ | ||
| 35 | cwd() . '/share'; | ||
| 36 | }; | ||
| 37 | |||
| 38 | mkpath( catfile( $path, 'bin' ) ); | ||
| 39 | mkpath( catfile( $path, 'lib/Hubot/Adapter' ) ); | ||
| 40 | mkpath( catfile( $path, 'lib/Hubot/Scripts' ) ); | ||
| 41 | |||
| 42 | my @modules = qw( | ||
| 43 | Hubot/EventEmitter.pm | ||
| 44 | Hubot/Scripts/help.pm | ||
| 45 | Hubot/Scripts/ascii.pm | ||
| 46 | Hubot/Scripts/shorten.pm | ||
| 47 | Hubot/Scripts/tweet.pm | ||
| 48 | Hubot/Scripts/roles.pm | ||
| 49 | Hubot/Response.pm | ||
| 50 | Hubot/Robot.pm | ||
| 51 | Hubot/User.pm | ||
| 52 | Hubot/Adapter.pm | ||
| 53 | Hubot/TextListener.pm | ||
| 54 | Hubot/Listener.pm | ||
| 55 | Hubot/Adapter/Irc.pm | ||
| 56 | Hubot/Adapter/Shell.pm | ||
| 57 | Hubot/Adapter/Campfire.pm | ||
| 58 | Hubot/Message.pm | ||
| 59 | Hubot/Brain.pm | ||
| 60 | Hubot/Creator.pm | ||
| 61 | Hubot.pm | ||
| 62 | ); | ||
| 63 | |||
| 64 | my $installed = $dist_dir =~ /share$/ ? 0 : 1; | ||
| 65 | my $path_prefix = $installed ? '' : cwd() . '/lib'; | ||
| 66 | |||
| 67 | for my $module (@modules) { | ||
| 68 | if ($installed) { | ||
| 69 | next unless require $module; | ||
| 70 | } | ||
| 71 | |||
| 72 | my ( $src, $dst ) = ( | ||
| 73 | $installed ? $INC{"$module"} : "$path_prefix/$module", | ||
| 74 | catfile( "$path", 'lib', $module ) | ||
| 75 | ); | ||
| 76 | $self->copy( $src, $dst ); | ||
| 77 | } | ||
| 78 | |||
| 79 | dircopy( catfile( $dist_dir, 'lib' ), catfile( $path, 'lib' ), ); | ||
| 80 | |||
| 81 | my @files = qw( | ||
| 82 | .gitignore | ||
| 83 | Procfile | ||
| 84 | README.md | ||
| 85 | cpanfile | ||
| 86 | hubot-scripts.json | ||
| 87 | ); | ||
| 88 | |||
| 89 | for my $file (@files) { | ||
| 90 | my ( $src, $dst ) | ||
| 91 | = ( catfile("$dist_dir/$file"), catfile("$path/$file") ); | ||
| 92 | $self->copy( $src, $dst ); | ||
| 93 | } | ||
| 94 | |||
| 95 | my @bins = qw( | ||
| 96 | bin/hubot | ||
| 97 | ); | ||
| 98 | |||
| 99 | for my $file (@bins) { | ||
| 100 | my ( $src, $dst ) | ||
| 101 | = ( catfile("$dist_dir/$file"), catfile("$path/$file") ); | ||
| 102 | $self->copy( $src, $dst ); | ||
| 103 | chmod 0755, "$dst"; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | __PACKAGE__->meta->make_immutable; | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | =pod | ||
| 112 | |||
| 113 | =encoding utf-8 | ||
| 114 | |||
| 115 | =head1 NAME | ||
| 116 | |||
| 117 | Hubot::Creator - deployable package builder for C<hubot> | ||
| 118 | |||
| 119 | =head1 VERSION | ||
| 120 | |||
| 121 | version 0.2.8 | ||
| 122 | |||
| 123 | =head1 SYNOPSIS | ||
| 124 | |||
| 125 | Hubot::Creator->new(path => '/path/to/hubot')->run; | ||
| 126 | |||
| 127 | =head1 AUTHOR | ||
| 128 | |||
| 129 | Hyungsuk Hong <hshong@perl.kr> | ||
| 130 | |||
| 131 | =head1 COPYRIGHT AND LICENSE | ||
| 132 | |||
| 133 | This software is copyright (c) 2013 by Hyungsuk Hong. | ||
| 134 | |||
| 135 | This is free software; you can redistribute it and/or modify it under | ||
| 136 | the same terms as the Perl 5 programming language system itself. | ||
| 137 | |||
| 138 | =cut |