Custom addon repo name option added.

This commit is contained in:
Andrew Manning 2016-05-08 09:26:06 -04:00
parent f73a74967e
commit 174484a51c
4 changed files with 72 additions and 21 deletions

View File

@ -1356,6 +1356,7 @@ class Admin extends \Zotlabs\Web\Controller {
'$post' => 'admin/plugins/addrepo', '$post' => 'admin/plugins/addrepo',
'$desc' => t('Enter the public git repository URL of the plugin repo.'), '$desc' => t('Enter the public git repository URL of the plugin repo.'),
'$repoURL' => array('repoURL', t('Plugin repo git URL'), '', ''), '$repoURL' => array('repoURL', t('Plugin repo git URL'), '', ''),
'$repoName' => array('repoName', t('Custom repo name'), '', '', t('(optional)')),
'$submit' => t('Download Plugin Repo') '$submit' => t('Download Plugin Repo')
) )
); );
@ -1709,11 +1710,36 @@ class Admin extends \Zotlabs\Web\Controller {
require_once('library/markdown.php'); require_once('library/markdown.php');
if(array_key_exists('repoURL',$_REQUEST)) { if(array_key_exists('repoURL',$_REQUEST)) {
require __DIR__ . '/../../library/PHPGit.autoload.php'; // Load PHPGit dependencies require __DIR__ . '/../../library/PHPGit.autoload.php'; // Load PHPGit dependencies
logger('Repo URL submitted: ' . $_REQUEST['repoURL']);
$repoURL = $_REQUEST['repoURL']; $repoURL = $_REQUEST['repoURL'];
$git = new GitRepo('sys', $repoURL, true); $extendDir = __DIR__ . '/../../store/git/sys/extend';
$addonDir = $extendDir.'/addon';
if(!file_exists($extendDir)) {
if(!mkdir($extendDir, 0770, true)) {
logger('Error creating extend folder: ' . $extendDir);
json_return_and_die(array('message' => 'Error creating extend folder: ' . $extendDir, 'success' => false));
} else {
if(!symlink(__DIR__ . '/../../extend/addon', $addonDir)) {
logger('Error creating symlink to addon folder: ' . $addonDir);
json_return_and_die(array('message' => 'Error creating symlink to addon folder: ' . $addonDir, 'success' => false));
}
}
}
$repoName = null;
if(array_key_exists('repoName',$_REQUEST)) {
$repoName = $_REQUEST['repoName'];
logger('repoName: ' . $repoName);
} else {
$repoName = GitRepo::getRepoNameFromURL($repoURL);
}
if(!$repoName) {
logger('Invalid git repo');
json_return_and_die(array('message' => 'Invalid git repo', 'success' => false));
}
$repoDir = $addonDir.'/'.$repoName;
// clone the repo if new automatically
$git = new GitRepo('sys', $repoURL, true, $repoName, $repoDir);
$repo = $git->probeRepo($git->path); $repo = $git->probeRepo();
$repo['readme'] = $repo['manifest'] = null; $repo['readme'] = $repo['manifest'] = null;
foreach ($git->git->tree('master') as $object) { foreach ($git->git->tree('master') as $object) {
if ($object['type'] == 'blob' && (strtolower($object['file']) === 'readme.md' || strtolower($object['file']) === 'readme')) { if ($object['type'] == 'blob' && (strtolower($object['file']) === 'readme.md' || strtolower($object['file']) === 'readme')) {

View File

@ -15,16 +15,39 @@ class GitRepo {
public $url = null; public $url = null;
public $name = null; public $name = null;
public $path = null; private $path = null;
private $repoID = null; private $repoID = null;
private $channel = null; private $channel = null;
public $git = null; public $git = null;
private $repoBasePath = null; private $repoBasePath = null;
function __construct($channel = 'sys', $url = null, $clone = false, $name = null) { function __construct($channel = 'sys', $url = null, $clone = false, $name = null, $path = null) {
if($channel === 'sys' && ! is_site_admin()) {
logger('Only admin can use channel sys');
return null;
}
$this->repoBasePath = __DIR__ . '/../../store/git'; $this->repoBasePath = __DIR__ . '/../../store/git';
$this->channel = $channel; $this->channel = $channel;
$this->git = new PHPGit(); $this->git = new PHPGit();
// Allow custom path for repo in the case of , for example
if($path) {
//if(mkdir($path, 0770, true)) {
$this->path = $path;
//} else {
// logger('Error creating GitRepo. Path not created.');
// return null;
//}
} else {
$this->path = $this->repoBasePath . "/" . $this->channel . "/" . $this->name;
}
if ($this->isValidGitRepoURL($url)) {
$this->url = $url;
}
if ($name) { if ($name) {
$this->name = $name; $this->name = $name;
} else { } else {
@ -34,14 +57,17 @@ class GitRepo {
logger('Error creating GitRepo. No repo name found.'); logger('Error creating GitRepo. No repo name found.');
return null; return null;
} }
$this->path = $this->repoBasePath . "/" . $this->channel . "/" . $this->name;
if (file_exists($this->path)) { if (is_dir($this->path)) {
// ignore the $url input if it exists // ignore the $url input if it exists
// TODO: Check if the path is either empty or is a valid git repo and error if not
$this->git->setRepository($this->path); $this->git->setRepository($this->path);
// TODO: get repo metadata // TODO: get repo metadata
} else if ($url && validate_url($url) && $this->isValidGitRepoURL($url)) { return;
$this->url = $url; }
$this->repoID = random_string();
if ($this->url) {
//$this->repoID = random_string();
// create the folder and clone the repo at url to that folder if $clone is true // create the folder and clone the repo at url to that folder if $clone is true
if ($clone) { if ($clone) {
if (mkdir($this->path, 0770, true)) { if (mkdir($this->path, 0770, true)) {
@ -69,7 +95,7 @@ class GitRepo {
} }
public function setRepoPath($directory) { public function setRepoPath($directory) {
if (file_exists($directory)) { if (is_dir($directory)) {
$this->path->$directory; $this->path->$directory;
$this->git->setRepository($directory); $this->git->setRepository($directory);
return true; return true;
@ -88,17 +114,14 @@ class GitRepo {
} }
public function cloneRepo() { public function cloneRepo() {
if (validate_url($this->url) && $this->isValidGitRepoURL($this->url) && file_exists($this->path)) { if (validate_url($this->url) && $this->isValidGitRepoURL($this->url) && is_dir($this->path)) {
return $this->git->clone($this->url, $this->path); return $this->git->clone($this->url, $this->path);
} }
} }
public static function probeRepo($dir) { public function probeRepo() {
if (!file_exists($dir)) { $git = $this->git;
return null; logger('probeRepo path: ' . $this->path);
}
$git = new PHPGit();
$git->setRepository($dir);
$repo = array(); $repo = array();
$repo['remote'] = $git->remote(); $repo['remote'] = $git->remote();
$repo['branches'] = $git->branch(['all' => true]); $repo['branches'] = $git->branch(['all' => true]);
@ -107,7 +130,7 @@ class GitRepo {
} }
public static function isValidGitRepoURL($url) { public static function isValidGitRepoURL($url) {
if (strrpos(parse_url($url, PHP_URL_PATH), '.')) { if (validate_url($url) && strrpos(parse_url($url, PHP_URL_PATH), '.')) {
return true; return true;
} else { } else {
return false; return false;

View File

@ -54,9 +54,10 @@
function adminPluginsAddRepo() { function adminPluginsAddRepo() {
var repoURL = $('#id_repoURL').val(); var repoURL = $('#id_repoURL').val();
var repoName = $('#id_repoName').val();
$('#chat-rotator').spin('tiny'); $('#chat-rotator').spin('tiny');
$.post( $.post(
"/admin/plugins/addrepo", {repoURL: repoURL}, "/admin/plugins/addrepo", {repoURL: repoURL, repoName: repoName},
function(response) { function(response) {
$('#chat-rotator').spin(false); $('#chat-rotator').spin(false);
if (response.success) { if (response.success) {

View File

@ -2,6 +2,7 @@
<p class="descriptive-text">{{$desc}}</p> <p class="descriptive-text">{{$desc}}</p>
{{include file="field_input.tpl" field=$repoURL}} {{include file="field_input.tpl" field=$repoURL}}
{{include file="field_input.tpl" field=$repoName}}
<div class="btn-group pull-right"> <div class="btn-group pull-right">
<button id="add-plugin-repo-submit" class="btn btn-primary" type="submit" name="submit" onclick="adminPluginsAddRepo(); return false;">{{$submit}}</button> <button id="add-plugin-repo-submit" class="btn btn-primary" type="submit" name="submit" onclick="adminPluginsAddRepo(); return false;">{{$submit}}</button>
</div> </div>