| 1 | package Dist::Zilla::Plugin::JSAN::GitHubDocs; | ||
| 2 | BEGIN { | ||
| 3 | $Dist::Zilla::Plugin::JSAN::GitHubDocs::VERSION = '0.06'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: a plugin for Dist::Zilla which updates the 'gh-pages' branch after each release | ||
| 7 | |||
| 8 | use Moose; | ||
| 9 | |||
| 10 | use Archive::Tar; | ||
| 11 | use Git::Wrapper; | ||
| 12 | use File::Temp; | ||
| 13 | use Path::Class; | ||
| 14 | use Cwd qw(abs_path); | ||
| 15 | |||
| 16 | with 'Dist::Zilla::Role::AfterRelease'; | ||
| 17 | with 'Dist::Zilla::Role::Git::DirtyFiles'; | ||
| 18 | |||
| 19 | |||
| 20 | has 'extract' => ( | ||
| 21 | isa => 'Str', | ||
| 22 | is => 'rw', | ||
| 23 | default => 'doc/html' | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has 'push_to' => ( | ||
| 28 | isa => 'Str', | ||
| 29 | is => 'rw', | ||
| 30 | default => 'origin' | ||
| 31 | ); | ||
| 32 | |||
| 33 | |||
| 34 | has 'redirect_prefix' => ( | ||
| 35 | isa => 'Str', | ||
| 36 | is => 'rw', | ||
| 37 | default => 'doc/html' | ||
| 38 | ); | ||
| 39 | |||
| 40 | |||
| 41 | sub dist_name_as_url { | ||
| 42 | my ($self) = @_; | ||
| 43 | |||
| 44 | return join '/', (split /-/, $self->zilla->name); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub after_release { | ||
| 49 | my ($self, $archive) = @_; | ||
| 50 | |||
| 51 | my $git = Git::Wrapper->new('.'); | ||
| 52 | my $gh_exists = eval { $git->rev_parse( '--verify', '-q', 'gh-pages' ); 1; }; | ||
| 53 | |||
| 54 | |||
| 55 | my @dirty_files = $self->list_dirty_files($git); | ||
| 56 | |||
| 57 | if (@dirty_files) { | ||
| 58 | $self->log_fatal("There are dirty files in the repo: [ @dirty_files ] - can't update gh-pages branch"); | ||
| 59 | } | ||
| 60 | |||
| 61 | $self->log("Updating `gh-pages` branch"); | ||
| 62 | |||
| 63 | |||
| 64 | # setting up the temporary git repo | ||
| 65 | |||
| 66 | my $temp_dir = File::Temp->newdir(); | ||
| 67 | my $git_gh_pages = Git::Wrapper->new( $temp_dir . ''); | ||
| 68 | |||
| 69 | $git_gh_pages->init('-q'); | ||
| 70 | |||
| 71 | $git_gh_pages->remote('add', 'src', abs_path('.')); | ||
| 72 | $git_gh_pages->fetch(qw(-q src)); | ||
| 73 | |||
| 74 | |||
| 75 | if ($gh_exists) { | ||
| 76 | $git_gh_pages->checkout('remotes/src/gh-pages'); | ||
| 77 | } else { | ||
| 78 | $git_gh_pages->symbolic_ref('HEAD', 'refs/heads/gh-pages'); | ||
| 79 | |||
| 80 | my $index_file = file($temp_dir, 'index.html'); | ||
| 81 | |||
| 82 | my $fh = $index_file->openw(); | ||
| 83 | |||
| 84 | my $redirect_url = $self->redirect_prefix . '/' . $self->dist_name_as_url . '.html'; | ||
| 85 | |||
| 86 | print $fh <<INDEX | ||
| 87 | |||
| 88 | <!DOCTYPE html> | ||
| 89 | <html> | ||
| 90 | <head> | ||
| 91 | <meta http-equiv="refresh" content="0;url=$redirect_url"> | ||
| 92 | </head> | ||
| 93 | |||
| 94 | <body> | ||
| 95 | </body> | ||
| 96 | </html> | ||
| 97 | |||
| 98 | INDEX | ||
| 99 | ; | ||
| 100 | $fh->close(); | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | # exracting the relevant files from tarball | ||
| 105 | |||
| 106 | my $extract = $self->extract; | ||
| 107 | $extract =~ s!^/!!; | ||
| 108 | |||
| 109 | $extract = qr/^$extract/; | ||
| 110 | |||
| 111 | my $next = Archive::Tar->iter($archive . ''); | ||
| 112 | |||
| 113 | while (my $file = $next->()) { | ||
| 114 | |||
| 115 | my @extract_path = split '/', $file->full_path; | ||
| 116 | |||
| 117 | shift @extract_path; | ||
| 118 | |||
| 119 | my $extract_path = join '/', @extract_path; | ||
| 120 | |||
| 121 | if ($extract_path =~ $extract) { | ||
| 122 | $file->extract( $temp_dir . '/' . $extract_path ) or warn "Extraction failed"; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | # pushing updates | ||
| 127 | |||
| 128 | $git_gh_pages->add('.'); | ||
| 129 | |||
| 130 | # non-zero exit status if no files has been changed in docs | ||
| 131 | eval { | ||
| 132 | $git_gh_pages->commit('-m', '"gh-pages" branch update'); | ||
| 133 | }; | ||
| 134 | |||
| 135 | if ($gh_exists) { | ||
| 136 | $git_gh_pages->checkout('-b', 'gh-pages'); | ||
| 137 | } | ||
| 138 | |||
| 139 | $git_gh_pages->push('src', 'gh-pages'); | ||
| 140 | |||
| 141 | $git->push($self->push_to, 'gh-pages'); | ||
| 142 | |||
| 143 | $self->log("`gh-pages` branch has been successfully updated"); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | __PACKAGE__->meta->make_immutable; | ||
| 149 | no Moose; | ||
| 150 | |||
| 151 | 1; | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | __END__ |