refactoring

This commit is contained in:
2018-09-07 16:19:30 +02:00
parent 27480c0290
commit 110deb867a
7 changed files with 80 additions and 84 deletions

View File

@ -1,68 +0,0 @@
using System.Diagnostics;
using System.IO;
namespace Yavsc.Server.Models.IT.SourceCode
{
public abstract class GitBatch : Batch<GitRepositoryReference>
{
public GitBatch()
{
// git -c color.status=always status
// | ~/bin/ansi2html.sh --bg=dark --palette=xterm > ../test.html
}
ProcessStartInfo CreateAnsiFilter
(GitRepositoryReference input, params string [] args )
{
var pStart = new ProcessStartInfo("git", string.Join(" ", args));
if (args[0]=="clone")
pStart.WorkingDirectory = WorkingDir;
else
pStart.WorkingDirectory = Path.Combine( WorkingDir, input.Path);
return pStart;
}
protected ProcessStartInfo CreateProcessStart(string args)
{
return new ProcessStartInfo("git", args)
{ WorkingDirectory = WorkingDir };
}
bool Clone (GitRepositoryReference input)
{
var pStart = CreateProcessStart( $"clone -b {input.Branch} {input.Url} {input.Path}");
pStart.WorkingDirectory = WorkingDir;
var proc = Process.Start(pStart);
proc.WaitForExit();
return proc.ExitCode == 0;
}
bool Pull (GitRepositoryReference input)
{
LogPath = Path.Combine( WorkingDir, "git.log");
var pStart = new ProcessStartInfo("git", "pull");
pStart.WorkingDirectory = Path.Combine(WorkingDir,input.Path);
pStart.RedirectStandardOutput = true;
using (var mem = new MemoryStream())
{
using (var memWriter = new StreamWriter(mem))
{
var proc = Process.Start(pStart);
using (var memReader = new StreamReader(mem)) {
while (!proc.StandardOutput.EndOfStream)
memWriter.Write(proc.StandardOutput.Read());
proc.WaitForExit();
}
bool ok = proc.ExitCode==0;
ResultHandler(ok);
return ok;
}
}
}
}
}

View File

@ -0,0 +1,53 @@
// // GitClone.cs
// /*
// paul 21/06/2018 11:27 20182018 6 21
// */
using System.Diagnostics;
using System.IO;
using System;
namespace Yavsc.Server.Models.IT.SourceCode
{
public class GitClone : SingleCmdProjectBatch
{
public GitClone(string repo) : base(repo,"git")
{
}
public override void Launch(Project input)
{
if (input==null) throw new ArgumentNullException("input");
LogPath = $"{input.Name}.git-clone.ansi.log";
// TODO honor Args property
// Model annotations => input.Repository!=null => input.Name == input.Repository.Path
var prjPath = Path.Combine(WorkingDir, input.Name);
var repoInfo = new DirectoryInfo(prjPath);
var gitCmd = repoInfo.Exists ? "pull" : "clone --depth=1";
var cloneStart = new ProcessStartInfo
( _cmdPath, $"{gitCmd} {input.Repository.Url} {input.Repository.Path}" )
{
WorkingDirectory = WorkingDir,
RedirectStandardOutput = true,
UseShellExecute = false
};
// TODO make `.ansi.log` a defined constant.
var logFile = new FileInfo
( Path.Combine
( _repositoryRootPath, $"{input.Name}.ansi.log" ));
using (var stream = logFile.Create())
using (var writer = new StreamWriter(stream))
{
var process = Process.Start(cloneStart);
// TODO publish the starting log url ...
while (!process.HasExited)
{
if (process.StandardOutput.Peek() > -1)
writer.WriteLine(process.StandardOutput.ReadLine());
}
}
if (ResultHandler!=null) ResultHandler(true);
}
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Diagnostics;
using System.IO;
namespace Yavsc.Server.Models.IT.SourceCode
{
public class ProjectBuild : SingleCmdProjectBatch
{
public ProjectBuild(string repoRoot): base(repoRoot, "make")
{
}
public override void Launch(Project input)
{
if (input==null) throw new ArgumentNullException("input");
LogPath = $"{input.Name}.{_cmdPath}.ansi.log";
// TODO honor Args property
// Model annotations => input.Repository!=null => input.Name == input.Repository.Path
var prjPath = Path.Combine(WorkingDir, input.Name);
var repoInfo = new DirectoryInfo(prjPath);
var args = string.Join(" ", Args);
var cloneStart = new ProcessStartInfo
( _cmdPath, args )
{
WorkingDirectory = prjPath,
RedirectStandardOutput = true,
UseShellExecute = false
};
// TODO make `.ansi.log` a defined constant.
var logFile = new FileInfo
( Path.Combine
( _repositoryRootPath, $"{input.Name}.ansi.log" ));
using (var stream = logFile.Create())
using (var writer = new StreamWriter(stream))
{
var process = Process.Start(cloneStart);
// TODO announce ...
while (!process.HasExited)
{
if (process.StandardOutput.Peek() > -1)
writer.WriteLine(process.StandardOutput.ReadLine());
}
}
if (ResultHandler!=null) ResultHandler(true);
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.IO;
namespace Yavsc.Server.Models.IT.SourceCode
{
public abstract class SingleCmdProjectBatch : Batch<Project>
{
protected string _repositoryRootPath;
protected string _cmdPath ;
public SingleCmdProjectBatch (string repoRoot, string cmdPath)
{
_cmdPath = cmdPath;
_repositoryRootPath = repoRoot;
WorkingDir = _repositoryRootPath;
var fie = new DirectoryInfo(WorkingDir);
if (!fie.Exists)
throw new Exception ($"This directory doesn't exist: {WorkingDir},\nand cannot be used as a repository.");
}
}
}