view growi-update.pl @ 0:77db1db8cf44 default tip

add growi-update.pl
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Wed, 12 Aug 2020 08:33:44 +0900
parents
children
line wrap: on
line source

#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw/getcwd/;


my $growi_version_text = 'current_growi_version.txt';
my $latest_version_url = 'https://github.com/weseek/growi/releases/latest';
my $growi_dir          = 'growi';
my $update_growi_sh    = 'update.sh';

my $latest_version = fetch_latest_version($latest_version_url);
print "latest: $latest_version\n";

my $current_version = read_growi_version_txt($growi_version_text);

if ($current_version eq $latest_version) {
  exit 0;
}

update_growi($growi_dir, $update_growi_sh);

overwrrite_growi_version_txt($growi_version_text, $latest_version);


sub read_growi_version_txt {
  my $file = shift;
  open my $fh, '<', $file;
  my $version = <$fh>;
  chomp $version;
  close $fh;
  return $version;
}

sub overwrrite_growi_version_txt {
  my ($file, $version) = @_;
  open my $fh, '>', $file;
  print $fh "$version\n";
  close $fh;
}


sub fetch_latest_version {
  my $url = shift;
  my $response = `curl $url`; #using curl command because perl core http module can't use https

  # response == <html><body>You are being <a href="https://github.com/weseek/growi/releases/tag/v4.1.2">redirected</a>.</body></html>

  unless ($response =~ m|tag/v([\d\.]+)"|) {
    die 'did not fetch latest version';
  }

  my $latest_version = $1;

  return $latest_version;
}

sub update_growi {
  my ($dir, $shell_script) = @_;
  my $current_dir = getcwd();
  chdir $dir;
  system("./$shell_script");
  chdir $current_dir;
}