codesite2git: initial commit
This commit is contained in:
parent
be2354c9b8
commit
76341bb409
153
codesite2git
Executable file
153
codesite2git
Executable file
@ -0,0 +1,153 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
require __DIR__.'/lib/bootstrap.php';
|
||||
|
||||
class CProjectGitExporter extends CProject {
|
||||
|
||||
function __construct(string $projdirname, string $projname) {
|
||||
CProject::__construct($projdirname, $projname);
|
||||
|
||||
$this->allowText2Html = false; // Don't produce HTML when processing the download block
|
||||
}
|
||||
|
||||
function ExportAsGit(string $dest) {
|
||||
|
||||
// Create destination directory
|
||||
if (is_dir($dest)) {
|
||||
throw new Exception("Destination path '{$dest}' already exists");
|
||||
}
|
||||
mkdir($dest);
|
||||
shell_exec('cd '. escapeshellarg($dest).' && git init');
|
||||
|
||||
// Parse download information blocks
|
||||
$this->filterLongDescArea();
|
||||
|
||||
// Prepare to start modifying the generated README.md file
|
||||
$lines = explode("\n", $this->longdesc);
|
||||
$lines = array_merge(["# ".$this->projname, ""], $lines);
|
||||
|
||||
// Modify some lines to standard markdown format:
|
||||
foreach($lines as $i => $line) {
|
||||
|
||||
// Convert ==HEADERS== to ## Headers
|
||||
if (strlen($line) > 0 && $line[0] == '=') {
|
||||
$tmp = rtrim($line, '=');
|
||||
$indent = substr_count($tmp, '=');
|
||||
|
||||
// Normalise the capitalisation for header text
|
||||
$header_text = substr($tmp, $indent);
|
||||
if ($header_text != "TODO" && $header_text != "FIXME" && $header_text != "WARNING") {
|
||||
$header_text = ucwords(strtolower($header_text));
|
||||
}
|
||||
|
||||
// raise markdown-equivalent header level by 1 to compensate for new header
|
||||
$lines[$i] = $line = str_repeat('#', $indent + 1).' '.$header_text;
|
||||
}
|
||||
|
||||
// Convert multiline single-backtick to multi-backtick
|
||||
// Heuristic: if there is only a single backtick in the line
|
||||
if (substr_count($line, '`') == 1) {
|
||||
$lines[$i] = $line = str_replace('`', "\n```\n", $line);
|
||||
}
|
||||
|
||||
// If the previous line started with a hyphen, and this line didn't, we need an
|
||||
// extra line separator
|
||||
if ($i > 0 && strlen($line) > 0 &&
|
||||
$line[0] != '-' && strlen($lines[$i-1]) > 0 && $lines[$i-1][0] == '-'
|
||||
) {
|
||||
$lines[$i] = $line = "\n".$line;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy all images to a doc/ subdirectory
|
||||
mkdir($dest.'/doc');
|
||||
foreach($this->images as $img) {
|
||||
copy($this->dir.$img, $dest.'/doc/'.$img);
|
||||
}
|
||||
|
||||
// Copy all downloads to a dist-archive/ subdirectory
|
||||
|
||||
if (count($this->downloads) > 0) {
|
||||
|
||||
mkdir($dest.'/dist-archive');
|
||||
foreach($this->downloads as $file) {
|
||||
copy($this->dir.$file, $dest.'/dist-archive/'.$file);
|
||||
}
|
||||
|
||||
if (! $this->downloads_section_was_replaced) {
|
||||
// Add our own downloads section
|
||||
$lines[] = $this->renderDownloadsBlock($this->downloads, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Save final README
|
||||
file_put_contents($dest.'/README.md', implode("\n", $lines));
|
||||
|
||||
// Extra properties file
|
||||
$ctime = $this->lastupdate - ($this->lifespan * 3600); // Lifespans are measured in hours
|
||||
file_put_contents($dest.'/.legacy-codesite.toml', "# Converted with codesite2git
|
||||
|
||||
project_name=\"".$this->projname."\"
|
||||
short_description=\"".$this->shortdesc."\"
|
||||
written_in_lang=\"".$this->subtag."\"
|
||||
topics=".json_encode($this->tags)."
|
||||
ctime=".$ctime."
|
||||
mtime=".$this->lastupdate."
|
||||
|
||||
");
|
||||
|
||||
// Git commit everything
|
||||
// Once for the meta with ctime; once for all files with the mtime
|
||||
$command = (
|
||||
'cd '. escapeshellarg($dest).' && '.
|
||||
'git add .legacy-codesite.toml ; '.
|
||||
'GIT_COMMITTER_DATE="'.date(DATE_ISO8601, $ctime).'" git commit -m "initial meta commit" --date '.escapeshellarg(date(DATE_ISO8601, $ctime)).' ; '.
|
||||
'git add -A ; '.
|
||||
'GIT_COMMITTER_DATE="'.date(DATE_ISO8601, $this->lastupdate).'" git commit -m "commit all archived files" --date '.escapeshellarg(date(DATE_ISO8601, $this->lastupdate))
|
||||
);
|
||||
echo $command."\n";
|
||||
shell_exec($command);
|
||||
|
||||
}
|
||||
|
||||
public function renderDownloadsBlock($render_downloads, $include_header=false) { // override
|
||||
if (! count($render_downloads)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ret = "\n";
|
||||
if ($include_header) {
|
||||
$ret .= "## Download\n\n";
|
||||
}
|
||||
foreach($render_downloads as $filename) {
|
||||
$ret .= "- [⬇️ {$filename}](raw/branch/master/dist-archive/{$filename}) ";
|
||||
$ret .= "*(".fbytes(filesize($this->dir.$filename)).")*\n";
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function codesite2git(string $projdirname, string $projname, string $dest) {
|
||||
|
||||
// Parse existing project
|
||||
$c = new CProjectGitExporter($projdirname, $projname);
|
||||
var_dump($c);
|
||||
|
||||
$c->ExportAsGit($dest);
|
||||
|
||||
}
|
||||
|
||||
function main(array $argv) {
|
||||
if (count($argv) != 5) {
|
||||
die("Usage: codesite2git CODESITE_ROOT_PATH PROJECT_DIR_NAME PROJECT_REAL_NAME DEST_DIR\n");
|
||||
}
|
||||
|
||||
$config = setup_vars($argv[1]);
|
||||
|
||||
codesite2git($argv[2], $argv[3], $argv[4]);
|
||||
}
|
||||
|
||||
main($_SERVER['argv']);
|
@ -2,7 +2,7 @@
|
||||
|
||||
class CProject {
|
||||
|
||||
private $dir;
|
||||
protected $dir;
|
||||
|
||||
public $projname;
|
||||
public $shortdesc = '(no description)';
|
||||
@ -10,11 +10,11 @@ class CProject {
|
||||
public $lastupdate = 0;
|
||||
public $numreleases = 0;
|
||||
|
||||
private $longdesc = '';
|
||||
private $prefix_html = '';
|
||||
private $images = array();
|
||||
private $downloads = array();
|
||||
private $downloads_hashes = array();
|
||||
protected $longdesc = '';
|
||||
protected $prefix_html = '';
|
||||
protected $images = array();
|
||||
protected $downloads = array();
|
||||
protected $downloads_hashes = array();
|
||||
|
||||
public $downloads_section_was_replaced = false;
|
||||
|
||||
@ -26,6 +26,8 @@ class CProject {
|
||||
|
||||
protected $go_get_target = '';
|
||||
protected $git_repo = '';
|
||||
|
||||
protected $allowText2Html = true;
|
||||
|
||||
public function __construct($dirname, $projname) {
|
||||
$this->dir = BASEDIR.'data/'.$dirname.'/';
|
||||
@ -235,7 +237,9 @@ class CProject {
|
||||
|
||||
$this->longdesc = substr($this->longdesc, 0, strlen($this->longdesc)-1); // Strip the extra NL we added
|
||||
|
||||
$this->longdesc = text2html($this->longdesc);
|
||||
if ($this->allowText2Html) {
|
||||
$this->longdesc = text2html($this->longdesc);
|
||||
}
|
||||
foreach($known_tags as $tag_name => $tag_idx) {
|
||||
$this->longdesc = str_replace(
|
||||
'${{TAG_'.$tag_idx.'}}',
|
||||
@ -244,7 +248,9 @@ class CProject {
|
||||
);
|
||||
}
|
||||
|
||||
$this->longdesc = str_replace("</ul>\n<br />", "</ul>", $this->longdesc);
|
||||
if ($this->allowText2Html) {
|
||||
$this->longdesc = str_replace("</ul>\n<br />", "</ul>", $this->longdesc);
|
||||
}
|
||||
|
||||
// Skip displaying the global downloads area
|
||||
// This flag also indicates that the content has been pre-HTMLified
|
||||
|
Reference in New Issue
Block a user