Browse Source

Les liens vers les bonnes sources pour 'voir en ligne'

2.6
Matthieu Marcillaud 3 years ago
parent
commit
505801ac59
  1. 47
      src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/Finder.php
  2. 19
      src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/Git.php
  3. 24
      src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/None.php
  4. 2
      src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/Svn.php
  5. 65
      src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/VcsInformationsAbstract.php
  6. 38
      src/autodoc/Plugin/Core/Compiler/Pass/VcsLinkBuilder.php
  7. 4
      src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/DefaultViewer.php
  8. 29
      src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Gitea.php
  9. 39
      src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Github.php
  10. 39
      src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Gitlab.php
  11. 5
      src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Redmine.php
  12. 5
      src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Trac.php

47
src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/Finder.php

@ -0,0 +1,47 @@
<?php
/**
* autodoc
*
* Permet l'extraction des informations VCS d'un répertoire
*/
namespace autodoc\Plugin\Core\Compiler\Pass\VcsInformations;
/**
* Extraction d'informations d'un dossier utilisant un Vcs
**/
class Finder
{
/**
* Retourne la description git ou svn d’un répertoire
*
* @param string $root Chemin racine le plus bas (on ne cherche pas en deça)
* @param string $dir Répertoire depuis la racine (on cherche un .git ou .svn en remontant vers root)
* @return VcsInformationsAbstract
*/
public static function find($root, $dir) {
// liste des répertoires déjà analysés
static $dirs = array();
$_dir = rtrim($root . $dir, '/') . '/';
if (!isset($dirs[$_dir])) {
if (file_exists($_dir . '.git')) {
$vcs = new Git();
$vcs->extractWithDirectory($_dir);
} elseif (file_exists($_dir . '.svn')) {
$vcs = new Svn();
$vcs->extractWithDirectory($_dir);
} elseif ($dir) {
$vcs = clone self::find($root, dirname($dir));
$vcs->setInnerPath(ltrim($vcs->getInnerPath() . '/' . basename($dir), '/'));
} else {
// on a tout épuisé jusqu’à root sans trouver de .git ou .svn
$vcs = new None();
$vcs->extractWithDirectory($_dir);
}
$dirs[$dir] = $vcs;
}
return $dirs[$dir];
}
}

19
src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/Git.php

@ -2,7 +2,7 @@
/**
* autodoc
*
* Permet l'extraction des informations SVN d'un répertoire
* Permet l'extraction des informations Git d'un répertoire
*/
namespace autodoc\Plugin\Core\Compiler\Pass\VcsInformations;
@ -12,11 +12,6 @@ namespace autodoc\Plugin\Core\Compiler\Pass\VcsInformations;
**/
class Git extends VcsInformationsAbstract
{
/**
* Données XML de l'extraction
* @var SimpleXMLElement|null
**/
private $xml;
/**
* {@inheritDoc}
@ -45,6 +40,7 @@ class Git extends VcsInformationsAbstract
list($root, $path) = explode('/', $path, 2);
$root = $protocol . '://' . $root;
}
$path = substr($path, 0, -4); // remove .git
$infos['root'] = $root;
$infos['path'] = $path;
@ -53,14 +49,9 @@ class Git extends VcsInformationsAbstract
# git.spip.net
$this->setUrlRoot( $infos['root'] );
# SPIP/spip/
$this->setPath( $infos['path'] );
$this->setProjectPath( $infos['path'] );
# master
$this->setBranch( $infos['branche'] );
}
}
/**
* {@inheritDoc}
**/
public function clear() {
parent::clear();
}
}

24
src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/None.php

@ -0,0 +1,24 @@
<?php
/**
* autodoc
*
* Permet l'extraction des informations d'un répertoire
*/
namespace autodoc\Plugin\Core\Compiler\Pass\VcsInformations;
/**
* Extraction d'informations d'un dossier utilisant un Vcs
**/
class None extends VcsInformationsAbstract
{
/**
* {@inheritDoc}
**/
public function extractWithDirectory($dir) {
$this->clear();
$this->setUrlPath( $dir );
$this->setUrlRoot( $dir );
}
}

2
src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/Svn.php

@ -39,7 +39,7 @@ class Svn extends VcsInformationsAbstract
# svn://trac.rezo.net/spip
$this->setUrlRoot( $svninfo->entry->repository->root );
# spip/
$this->setPath( substr($this->getUrlPath(), strlen($this->getUrlRoot()) + 1) );
$this->setProjectPath( substr($this->getUrlPath(), strlen($this->getUrlRoot()) + 1) );
$this->setXML = $svninfo;
}
}

65
src/autodoc/Plugin/Core/Compiler/Pass/VcsInformations/VcsInformationsAbstract.php

@ -31,7 +31,19 @@ abstract class VcsInformationsAbstract
* Chemin depuis la racine du repository
* @var string
**/
private $path;
private $projectPath;
/**
* Chemin depuis la racine du repository
* @var string
**/
private $innerPath;
/**
* Branche utilisée
* @var string
**/
private $branch;
/**
* Type du VCS
@ -57,6 +69,8 @@ abstract class VcsInformationsAbstract
public function clear() {
$this->urlRoot = null;
$this->urlPath = null;
$this->projectPath = null;
$this->innerPath = null;
}
@ -97,20 +111,57 @@ abstract class VcsInformationsAbstract
}
/**
* Retourne le chemin complet de l'élément depuis la racine du repository
* Retourne le chemin complet de l'élément depuis la racine jusqu’au projet
*
* @return string
**/
public function getProjectPath() {
return $this->projectPath;
}
/**
* Définit le chemin de l'élément depuis la racine jusqu’au projet
*
* @param string $path
**/
public function setProjectPath($path) {
$this->projectPath = $path;
}
/**
* Retourne le chemin du projet au répertoire en cours
*
* @return string
**/
public function getInnerPath() {
return $this->innerPath;
}
/**
* Définit le chemin du projet au répertoire en cours
*
* @param string $path
**/
public function setInnerPath($path) {
$this->innerPath = $path;
}
/**
* Retourne la branche utilisée
*
* @return string
**/
public function getPath() {
return $this->path;
public function getBranch() {
return $this->branch;
}
/**
* Définit le chemin complet de l'élément depuis la racine du repository
* Définit la branche utilisée
*
* @param string $path
**/
public function setPath($path) {
$this->path = $path;
public function setBranch($branch) {
$this->branch = $branch;
}
}

38
src/autodoc/Plugin/Core/Compiler/Pass/VcsLinkBuilder.php

@ -72,7 +72,7 @@ class VcsLinkBuilder implements CompilerPassInterface
$dir = '';
}
$vcs_infos = $this->getVcsInformationsFromDirectory($root . $dir);
$vcs_infos = VcsInformations\Finder::find($root, $dir);
$vcs_viewer = $this->getVcsViewer($vcs_infos);
$vcs_viewer->setFilename($file->getName());
@ -160,36 +160,6 @@ class VcsLinkBuilder implements CompilerPassInterface
/**
* Obtenir les infos d'un repostory
*
* @note
* Uniquement SVN actuellement
*
* @param string $dir
* @return VcsInformationsAbstract
**/
public function getVcsInformationsFromDirectory($dir) {
// liste des répertoires déjà analysés
static $dirs = array();
$dir = rtrim($dir, '/') . '/';
if (!isset($dirs[$dir])) {
if (file_exists($dir . '.git')) {
$vcs = new VcsInformations\Git();
} else {
$vcs = new VcsInformations\Svn();
}
$vcs->extractWithDirectory($dir);
$dirs[$dir] = $vcs;
}
return $dirs[$dir];
}
/**
* Obtenir un calculateur d'URL des sources des fichiers en ligne
*
@ -225,6 +195,12 @@ class VcsLinkBuilder implements CompilerPassInterface
static $connus = array(
'svn://zone.spip.org/spip-zone' => array('trac', 'https://zone.spip.org/trac/spip-zone'),
'svn://trac.rezo.net/spip' => array('redmine', 'https://core.spip.net/projects/spip'),
'https://git.spip.net' => array('gitea', 'https://git.spip.net'),
'git@git.spip.net' => array('gitea', 'https://git.spip.net'),
'https://github.com' => array('github', 'https://github.com'),
'git@github.com' => array('github', 'https://github.com'),
'https://gitlab.com' => array('gitlab', 'https://gitlab.com'),
'git@gitlab.com' => array('gitlab', 'https://gitlab.com'),
);
$url_repo = (string) $url_repo;
if (isset($connus[$url_repo])) {

4
src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/DefaultViewer.php

@ -56,7 +56,9 @@ class DefaultViewer
* @return string URL
**/
public function getUrl() {
$url = rtrim($this->getUrlViewerRoot() . $this->getVcs()->getPath(), '/');
$url = $this->getUrlViewerRoot();
$url = rtrim($url . $this->getVcs()->getProjectPath(), '/');
$url = rtrim($url . $this->getVcs()->getInnerPath(), '/');
if ($this->getFilename()) $url .= '/' . $this->getFilename();
if ($this->getLine()) $url .= '#L' . $this->getLine();
return $url;

29
src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Gitea.php

@ -0,0 +1,29 @@
<?php
/**
* autodoc
*
* Permettre le calcul d'URL vers un Gitea d'une source issues d'un Vcs
*/
namespace autodoc\Plugin\Core\Compiler\Pass\VcsViewer;
/**
* Calcul d'URL pour visualiser des fichiers sources en ligne sur un Gitea
**/
class Gitea extends DefaultViewer
{
/**
* {@inheritDoc}
**/
public function getUrl() {
$vcs = $this->getVcs();
$url = $this->getUrlViewerRoot();
$url = rtrim($url . '/' . $vcs->getProjectPath(), '/');
$url .= '/src/branch/';
$url .= $vcs->getBranch() . '/';
$url = rtrim($url . $vcs->getInnerPath(), '/');
if ($this->getFilename()) $url .= '/' . $this->getFilename();
if ($this->getLine()) $url .= '#L' . $this->getLine();
return $url;
}
}

39
src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Github.php

@ -0,0 +1,39 @@
<?php
/**
* autodoc
*
* Permettre le calcul d'URL vers un Github d'une source issues d'un Vcs
*/
namespace autodoc\Plugin\Core\Compiler\Pass\VcsViewer;
/**
* Calcul d'URL pour visualiser des fichiers sources en ligne sur un Github
**/
class Github extends DefaultViewer
{
/**
* {@inheritDoc}
**/
public function getUrl() {
// https://github.com/marcimat/bigup/tree/master/saisies
// https://github.com/marcimat/bigup/blob/master/saisies/bigup.html
$vcs = $this->getVcs();
$url = $this->getUrlViewerRoot();
$url = rtrim($url . '/' . $vcs->getProjectPath(), '/');
if ($this->isDirFilename()) {
$url .= '/tree/';
} else {
$url .= '/blob/';
}
$url = rtrim($url . $vcs->getBranch(), '/');
$url = rtrim($url . '/' . $vcs->getInnerPath(), '/');
if ($this->getFilename()) $url .= '/' . $this->getFilename();
if ($this->getLine()) $url .= '#L' . $this->getLine();
return $url;
}
protected function isDirFilename() {
return false === strpos($this->getFilename(), '.');
}
}

39
src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Gitlab.php

@ -0,0 +1,39 @@
<?php
/**
* autodoc
*
* Permettre le calcul d'URL vers un Gitlab d'une source issues d'un Vcs
*/
namespace autodoc\Plugin\Core\Compiler\Pass\VcsViewer;
/**
* Calcul d'URL pour visualiser des fichiers sources en ligne sur un Gitlab
**/
class Gitlab extends DefaultViewer
{
/**
* {@inheritDoc}
**/
public function getUrl() {
// https://gitlab.com/magraine/depublication/-/tree/master/prive
// https://gitlab.com/magraine/depublication/-/blob/master/depublication_fonctions.php
$vcs = $this->getVcs();
$url = $this->getUrlViewerRoot();
$url = rtrim($url . '/' . $vcs->getProjectPath(), '/');
if ($this->isDirFilename()) {
$url .= '/-/tree/';
} else {
$url .= '/-/blob/';
}
$url = rtrim($url . $vcs->getBranch(), '/');
$url = rtrim($url . '/' . $vcs->getInnerPath(), '/');
if ($this->getFilename()) $url .= '/' . $this->getFilename();
if ($this->getLine()) $url .= '#L' . $this->getLine();
return $url;
}
protected function isDirFilename() {
return false === strpos($this->getFilename(), '.');
}
}

5
src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Redmine.php

@ -16,7 +16,10 @@ class Redmine extends DefaultViewer
* {@inheritDoc}
**/
public function getUrl() {
$url = rtrim($this->getUrlViewerRoot() . '/repository/entry/' . $this->getVcs()->getPath(), '/');
$vcs = $this->getVcs();
$url = $this->getUrlViewerRoot() . '/repository/entry/';
$url = rtrim($url . $vcs->getProjectPath(), '/');
$url = rtrim($url . $vcs->getInnerPath(), '/');
if ($this->getFilename()) $url .= '/' . $this->getFilename();
if ($this->getLine()) $url .= '#L' . $this->getLine();
return $url;

5
src/autodoc/Plugin/Core/Compiler/Pass/VcsViewer/Trac.php

@ -16,7 +16,10 @@ class Trac extends DefaultViewer
* {@inheritDoc}
**/
public function getUrl() {
$url = rtrim($this->getUrlViewerRoot() . '/browser/' . $this->getVcs()->getPath(), '/');
$vcs = $this->getVcs();
$url = $this->getUrlViewerRoot() . '/browser/';
$url = rtrim($url . $vcs->getProjectPath(), '/');
$url = rtrim($url . $vcs->getInnerPath(), '/');
if ($this->getFilename()) $url .= '/' . $this->getFilename();
if ($this->getLine()) $url .= '#L' . $this->getLine();
return $url;

Loading…
Cancel
Save