composer updater && Added .updateignore

This commit is contained in:
Kanti 2014-11-11 08:38:00 +01:00
commit 643cda0302
11 changed files with 231 additions and 193 deletions

View file

@ -2,6 +2,6 @@
"require": { "require": {
"michelf/php-markdown": "1.4.*@dev", "michelf/php-markdown": "1.4.*@dev",
"suin/php-rss-writer": ">=1.0", "suin/php-rss-writer": ">=1.0",
"kanti/hub-updater": "dev-master" "kanti/hub-updater": "0.1"
} }
} }

21
composer.lock generated
View file

@ -4,24 +4,24 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "09a845c7ac1f579f79cdd0196a86199d", "hash": "d7bc647b7c26ac62864fbe9199d1565a",
"packages": [ "packages": [
{ {
"name": "kanti/hub-updater", "name": "kanti/hub-updater",
"version": "dev-master", "version": "0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/Kanti/hub-updater.git", "url": "https://github.com/Kanti/hub-updater.git",
"reference": "fd965ed3524720d70a0cdddbd95e83f536c9b24f" "reference": "fc70ff8ad8e68db921914106e5f4b56557358dfb"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/Kanti/hub-updater/zipball/fd965ed3524720d70a0cdddbd95e83f536c9b24f", "url": "https://api.github.com/repos/Kanti/hub-updater/zipball/fc70ff8ad8e68db921914106e5f4b56557358dfb",
"reference": "fd965ed3524720d70a0cdddbd95e83f536c9b24f", "reference": "fc70ff8ad8e68db921914106e5f4b56557358dfb",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=5.4.0" "php": ">=5.3.0"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -29,7 +29,7 @@
"Kanti\\": "" "Kanti\\": ""
} }
}, },
"notification-url": "https://packagist.org/downloads/", "notification-url": "http://packagist.org/downloads/",
"license": [ "license": [
"GPL-2.0" "GPL-2.0"
], ],
@ -41,8 +41,8 @@
"role": "Developer" "role": "Developer"
} }
], ],
"description": "Simple Github Updater for Web Projects [PHP]", "description": "Simple Github Updater for Web Projects",
"time": "2014-10-31 18:20:52" "time": "2014-11-11 07:32:09"
}, },
{ {
"name": "michelf/php-markdown", "name": "michelf/php-markdown",
@ -145,8 +145,7 @@
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": {
"michelf/php-markdown": 20, "michelf/php-markdown": 20
"kanti/hub-updater": 20
}, },
"prefer-stable": false, "prefer-stable": false,
"platform": [], "platform": [],

2
vendor/autoload.php vendored
View file

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer' . '/autoload_real.php'; require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit181c8f31ce07c1aa29ef78a7a9b430c3::getLoader(); return ComposerAutoloaderInit94770e161b9a4024b163320059f87e51::getLoader();

View file

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInit181c8f31ce07c1aa29ef78a7a9b430c3 class ComposerAutoloaderInit94770e161b9a4024b163320059f87e51
{ {
private static $loader; private static $loader;
@ -19,9 +19,9 @@ class ComposerAutoloaderInit181c8f31ce07c1aa29ef78a7a9b430c3
return self::$loader; return self::$loader;
} }
spl_autoload_register(array('ComposerAutoloaderInit181c8f31ce07c1aa29ef78a7a9b430c3', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInit94770e161b9a4024b163320059f87e51', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(); self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit181c8f31ce07c1aa29ef78a7a9b430c3', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInit94770e161b9a4024b163320059f87e51', 'loadClassLoader'));
$map = require __DIR__ . '/autoload_namespaces.php'; $map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) { foreach ($map as $namespace => $path) {
@ -44,7 +44,7 @@ class ComposerAutoloaderInit181c8f31ce07c1aa29ef78a7a9b430c3
} }
} }
function composerRequire181c8f31ce07c1aa29ef78a7a9b430c3($file) function composerRequire94770e161b9a4024b163320059f87e51($file)
{ {
require $file; require $file;
} }

View file

@ -1,44 +1,4 @@
[ [
{
"name": "kanti/hub-updater",
"version": "dev-master",
"version_normalized": "9999999-dev",
"source": {
"type": "git",
"url": "https://github.com/Kanti/hub-updater.git",
"reference": "fd965ed3524720d70a0cdddbd95e83f536c9b24f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Kanti/hub-updater/zipball/fd965ed3524720d70a0cdddbd95e83f536c9b24f",
"reference": "fd965ed3524720d70a0cdddbd95e83f536c9b24f",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"time": "2014-10-31 18:20:52",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"Kanti\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-2.0"
],
"authors": [
{
"name": "Kanti",
"email": "git@kanti.de",
"homepage": "https://kanti.de",
"role": "Developer"
}
],
"description": "Simple Github Updater for Web Projects [PHP]"
},
{ {
"name": "michelf/php-markdown", "name": "michelf/php-markdown",
"version": "dev-lib", "version": "dev-lib",
@ -138,5 +98,45 @@
"rss", "rss",
"writer" "writer"
] ]
},
{
"name": "kanti/hub-updater",
"version": "0.1",
"version_normalized": "0.1.0.0",
"source": {
"type": "git",
"url": "https://github.com/Kanti/hub-updater.git",
"reference": "fc70ff8ad8e68db921914106e5f4b56557358dfb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Kanti/hub-updater/zipball/fc70ff8ad8e68db921914106e5f4b56557358dfb",
"reference": "fc70ff8ad8e68db921914106e5f4b56557358dfb",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"time": "2014-11-11 07:32:09",
"type": "library",
"installation-source": "source",
"autoload": {
"psr-4": {
"Kanti\\": ""
}
},
"notification-url": "http://packagist.org/downloads/",
"license": [
"GPL-2.0"
],
"authors": [
{
"name": "Kanti",
"email": "git@kanti.de",
"homepage": "https://kanti.de",
"role": "Developer"
}
],
"description": "Simple Github Updater for Web Projects"
} }
] ]

View file

@ -0,0 +1 @@
README.md

View file

@ -2,31 +2,38 @@
namespace Kanti; namespace Kanti;
class CacheOneFile { class CacheOneFile
{
protected $fileName = ""; protected $fileName = "";
protected $holdTime = 43200; //12h protected $holdTime = 43200; //12h
public function __construct($fileName, $holdTime = 43200) { public function __construct($fileName, $holdTime = 43200)
{
$this->fileName = $fileName; $this->fileName = $fileName;
$this->holdTime = $holdTime; $this->holdTime = $holdTime;
} }
public function is() { public function is()
if (!file_exists($this->fileName)) {
return false; if (! file_exists($this->fileName)) {
if (filemtime($this->fileName) < ( time() - $this->holdTime )) {
unlink($this->fileName);
return false; return false;
} }
if (filemtime($this->fileName) < ( time() - $this->holdTime )) {
unlink($this->fileName);
return false;
}
return true; return true;
} }
public function get() { public function get()
{
return file_get_contents($this->fileName); return file_get_contents($this->fileName);
} }
public function set($content) { public function set($content)
{
file_put_contents($this->fileName, $content); file_put_contents($this->fileName, $content);
} }
} }

View file

@ -2,88 +2,85 @@
namespace Kanti; namespace Kanti;
class HubUpdater { class HubUpdater
{
protected $options = [ protected $options = array(
"cacheFile" => "downloadInfo.json", "cacheFile" => "downloadInfo.json",
"versionFile" => "installedVersion.json", "versionFile" => "installedVersion.json",
"zipFile" => "tmpZipFile.zip", "zipFile" => "tmpZipFile.zip",
"updateignore" => ".updateignore",
"name" => "",
"branch" => "master", "name" => "",
"cache" => "cache/", "branch" => "master",
"save" => "", "cache" => "cache/",
"prerelease" => false, "save" => "",
]; "prerelease" => false,
);
protected $allRelease = [];
protected $newestRelease = []; protected $allRelease = array();
protected $newestRelease = array();
protected $streamContext = null; protected $streamContext = null;
public function __construct($option) { public function __construct($option)
if(is_array($option)) {
{ if (is_array($option)) {
if(! isset($option['name'])) if (! isset($option['name'])) {
{ throw new Exception('No Name in Option Set');
throw new Exception('No Name in Option Set'); }
} $this->options = $option + $this->options;
$this->options = $option + $this->options; } elseif (is_string($option)) {
} $this->options['name'] = $option;
else if(is_string($option)) } else {
{ throw new Exception('No Option Set');
$this->options['name'] = $option; }
}
else $this->options['save'] = rtrim($this->options['save'], '/');
{ if ($this->options['save'] !== '') {
throw new Exception('No Option Set'); $this->options['save'] .= '/';
} if (!file_exists($this->options['save'])) {
mkdir($this->options['save']);
$this->options['cache'] = rtrim($this->options['cache'],'/'); }
if($this->options['cache'] !== ''){ }
$this->options['cache'] .= '/'; $this->options['cache'] = $this->options['save'] . rtrim($this->options['cache'], '/');
if (!file_exists($this->options['cache'])) { if ($this->options['cache'] !== '') {
mkdir($this->options['cache']); $this->options['cache'] .= '/';
} if (!file_exists($this->options['cache'])) {
} mkdir($this->options['cache']);
$this->options['save'] = rtrim($this->options['save'],'/'); }
if($this->options['save'] !== ''){ }
$this->options['save'] .= '/';
if (!file_exists($this->options['save'])) { $this->cachedInfo = new CacheOneFile($this->options['cache'] . $this->options['cacheFile']);
mkdir($this->options['save']);
}
}
$this->cachedInfo = new CacheOneFile($this->options['cache'] . $this->options['cacheFile']);
$this->streamContext = stream_context_create( $this->streamContext = stream_context_create(
array( array(
'http' => array( 'http' => array(
'header' => "User-Agent: Awesome-Update-My-Self-" . $this->options['name'] . "\r\n 'header' => "User-Agent: Awesome-Update-My-Self-" . $this->options['name'] . "\r\n
Accept: application/vnd.github.v3+json", Accept: application/vnd.github.v3+json",
), ),
'ssl' => array( 'ssl' => array(
'cafile' => dirname(__FILE__) . '/ca_bundle.crt', 'cafile' => dirname(__FILE__) . '/ca_bundle.crt',
'verify_peer' => true, 'verify_peer' => true,
), ),
) )
); );
$this->streamContext2 = stream_context_create( $this->streamContext2 = stream_context_create(
array( array(
'http' => array( 'http' => array(
'header' => "User-Agent: Awesome-Update-My-Self-" . $this->options['name'] . "\r\n", 'header' => "User-Agent: Awesome-Update-My-Self-" . $this->options['name'] . "\r\n",
), ),
'ssl' => array( 'ssl' => array(
'cafile' => dirname(__FILE__) . '/ca_bundle.crt', 'cafile' => dirname(__FILE__) . '/ca_bundle.crt',
'verify_peer' => true, 'verify_peer' => true,
), ),
) )
); );
$this->allRelease = $this->getRemoteInfos(); $this->allRelease = $this->getRemoteInfos();
} }
protected function getRemoteInfos() { protected function getRemoteInfos()
{
$path = "https://api.github.com/repos/" . $this->options['name'] ."/releases"; $path = "https://api.github.com/repos/" . $this->options['name'] ."/releases";
if ($this->cachedInfo->is()) { if ($this->cachedInfo->is()) {
$fileContent = $this->cachedInfo->get(); $fileContent = $this->cachedInfo->get();
} else { } else {
if (!in_array('https', stream_get_wrappers())) { if (!in_array('https', stream_get_wrappers())) {
@ -95,103 +92,143 @@ class HubUpdater {
return array(); return array();
} }
$json = json_decode($fileContent, true); $json = json_decode($fileContent, true);
if(isset($json['message'])) if (isset($json['message'])) {
{ $json = array();
$json = []; }
}
$fileContent = json_encode($json, JSON_PRETTY_PRINT); $fileContent = json_encode($json, JSON_PRETTY_PRINT);
$this->cachedInfo->set($fileContent); $this->cachedInfo->set($fileContent);
return $json; return $json;
} }
return json_decode($fileContent, true); return json_decode($fileContent, true);
} }
public function able() { public function able()
if (!in_array('https', stream_get_wrappers())) {
if (!in_array('https', stream_get_wrappers())) {
return false; return false;
if (empty($this->allRelease)) }
if (empty($this->allRelease)) {
return false; return false;
}
foreach($this->allRelease as $release)
{ foreach ($this->allRelease as $release) {
if(!$this->options['prerelease'] && $release['prerelease']) if (!$this->options['prerelease'] && $release['prerelease']) {
continue; continue;
if($this->options['branch'] !== $release['target_commitish']) }
continue; if ($this->options['branch'] !== $release['target_commitish']) {
$this->newestRelease = $release; continue;
break; }
} $this->newestRelease = $release;
break;
}
if (file_exists($this->options['cache'] . $this->options['versionFile'])) { if (file_exists($this->options['cache'] . $this->options['versionFile'])) {
$fileContent = file_get_contents($this->options['cache'] . $this->options['versionFile']); $fileContent = file_get_contents($this->options['cache'] . $this->options['versionFile']);
$current = json_decode($fileContent, true); $current = json_decode($fileContent, true);
if (isset($current['id']) && $current['id'] == $this->newestRelease['id']) if (isset($current['id']) && $current['id'] == $this->newestRelease['id']) {
return false; return false;
if (isset($current['tag_name']) && $current['tag_name'] == $this->newestRelease['tag_name']) }
if (isset($current['tag_name']) && $current['tag_name'] == $this->newestRelease['tag_name']) {
return false; return false;
}
} }
return true; return true;
} }
public function update() { public function update()
{
if ($this->able()) { if ($this->able()) {
if ($this->download($this->newestRelease['zipball_url'] )) { if ($this->download($this->newestRelease['zipball_url'])) {
if ($this->unZip()) { if ($this->unZip()) {
unlink($this->options['cache'] . $this->options['zipFile']); unlink($this->options['cache'] . $this->options['zipFile']);
file_put_contents($this->options['cache'] . $this->options['versionFile'], json_encode(array( file_put_contents($this->options['cache'] . $this->options['versionFile'], json_encode(array(
"id" => $this->newestRelease['id'], "id" => $this->newestRelease['id'],
"tag_name" => $this->newestRelease['tag_name'] "tag_name" => $this->newestRelease['tag_name']
), JSON_PRETTY_PRINT)); ), JSON_PRETTY_PRINT));
return true; return true;
} }
} }
} }
return false; return false;
} }
protected function download($url) { protected function download($url)
{
$file = @fopen($url, 'r', false, $this->streamContext2); $file = @fopen($url, 'r', false, $this->streamContext2);
if ($file == false) if ($file == false) {
return false; return false;
file_put_contents(dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $this->options['cache'] . $this->options['zipFile'], $file); }
fclose($file); file_put_contents(
dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $this->options['cache'] . $this->options['zipFile'],
$file
);
fclose($file);
return true; return true;
} }
protected function unZip() { protected function unZip()
{
$path = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $this->options['cache'] . $this->options['zipFile']; $path = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $this->options['cache'] . $this->options['zipFile'];
$updateIgnore = array();
$zip = new \ZipArchive; if (file_exists($this->options['updateignore'])) {
$updateIgnore = file($this->options['updateignore']);
foreach($updateIgnore as &$ignore) {
$ignore = $this->options['save'] . trim($ignore);
}
}
$zip = new \ZipArchive();
if ($zip->open($path) === true) { if ($zip->open($path) === true) {
$cutLength = strlen($zip->getNameIndex(0)); $cutLength = strlen($zip->getNameIndex(0));
for ($i = 1; $i < $zip->numFiles; $i++) {//iterate throw the Zip for ($i = 1; $i < $zip->numFiles; $i++) {//iterate throw the Zip
$fileName = $zip->getNameIndex($i); $name = $this->options['save'] . substr($zip->getNameIndex($i), $cutLength);
$stat = $zip->statIndex($i);
if ($stat["crc"] == 0) { $do = true;
$dirName = $this->options['save'] . substr($fileName, $cutLength);
if (!file_exists($dirName)) { foreach($updateIgnore as $ignore) {
mkdir($dirName); if (substr($name, 0, strlen($ignore)) == $ignore) {
$do = false;
break;
}
}
if ($do) {
$stat = $zip->statIndex($i);
if ($stat["crc"] == 0) {
if (!file_exists($name)) {
mkdir($name);
}
} else {
copy("zip://" . $path . "#" . $zip->getNameIndex($i), $name);
} }
} else {
copy("zip://" . $path . "#" . $fileName, $this->options['save'] . substr($fileName, $cutLength));
} }
} }
$zip->close(); $zip->close();
return true; return true;
} else { } else {
return false; return false;
} }
} }
public function printOne() { public function printOne()
{
$string = "<h3>Updated to<h3>"; $string = "<h3>Updated to<h3>";
$string .= "<h2>[" . $this->newestRelease['tag_name'] . "] " . $this->newestRelease['name'] . "</h2>\n"; $string .= "<h2>[" . $this->newestRelease['tag_name'] . "] " . $this->newestRelease['name'] . "</h2>\n";
$string .= "<p>" . $this->newestRelease['body'] . "</p>\n"; $string .= "<p>" . $this->newestRelease['body'] . "</p>\n";
return $string; return $string;
} }
public function getName() { public function getName()
{
return $this->newestRelease['tag_name']; return $this->newestRelease['tag_name'];
} }
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "kanti/hub-updater", "name": "kanti/hub-updater",
"description": "Simple Github Updater for Web Projects [PHP]", "description": "Simple Github Updater for Web Projects",
"license": "GPL-2.0", "license": "GPL-2.0",
"authors": [ "authors": [
{ {
@ -17,7 +17,7 @@
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"require": { "require": {
"php": ">=5.4.0" "php": ">=5.3.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View file

@ -2,4 +2,4 @@
require 'vendor/autoload.php'; require 'vendor/autoload.php';
$updater = new \Kanti\HubUpdater('kanti/test'); $updater = new \Kanti\HubUpdater('kanti/test');
$updater->update(); $updater->update();

View file

@ -1,26 +1,20 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
$updater = new \Kanti\HubUpdater([ $updater = new \Kanti\HubUpdater(array(
"name" => 'kanti/test',//Repository to watch "name" => 'kanti/test',//Repository to watch
"branch" => 'master',//wich branch to watch "branch" => 'master',//wich branch to watch
"cache" => 'cache/',//were to put the caching stuff "cache" => 'cache/',//were to put the caching stuff
"save" => 'save/',//there to put the downloaded Version[default ./] "save" => 'save/',//there to put the downloaded Version[default ./]
"prerelease" => true,//accept prereleases? "prerelease" => true,//accept prereleases?
]); ));
if($updater->able()) if ($updater->able()) {
{ if (isset($_GET['update'])) {
if(isset($_GET['update']))
{
$updater->update(); $updater->update();
echo '<p>updated :)</p>'; echo '<p>updated :)</p>';
} else {
echo '<a href="?update">Update Me</a>'; //only update if they klick this link
} }
else } else {
{
echo '<a href="?update">Update Me</a>'; //only update if they klick this link
}
}
else
{
echo '<p>uptodate :)</p>'; echo '<p>uptodate :)</p>';
} }