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

@ -2,28 +2,22 @@
// /*
// paul 21/06/2018 11:27 20182018 6 21
// */
using Yavsc.Server.Models.IT.SourceCode;
using Yavsc.Server.Models.IT;
using System.Diagnostics;
using System.IO;
using System;
namespace Yavsc.Lib
namespace Yavsc.Server.Models.IT.SourceCode
{
public class GitClone : Batch<Project>
public class GitClone : SingleCmdProjectBatch
{
string _repositoryRootPath;
string gitPath="git";
public GitClone(string repoRoot)
public GitClone(string repo) : base(repo,"git")
{
_repositoryRootPath = repoRoot;
}
}
public override void Launch(Project input)
{
if (input==null) throw new ArgumentNullException("input");
WorkingDir = _repositoryRootPath;
LogPath = $"{input.Name}.git-clone.ansi.log";
// TODO honor Args property
// Model annotations => input.Repository!=null => input.Name == input.Repository.Path
@ -32,7 +26,7 @@ namespace Yavsc.Lib
var gitCmd = repoInfo.Exists ? "pull" : "clone --depth=1";
var cloneStart = new ProcessStartInfo
( gitPath, $"{gitCmd} {input.Repository.Url} {input.Repository.Path}" )
( _cmdPath, $"{gitCmd} {input.Repository.Url} {input.Repository.Path}" )
{
WorkingDirectory = WorkingDir,
RedirectStandardOutput = true,
@ -46,7 +40,7 @@ namespace Yavsc.Lib
using (var writer = new StreamWriter(stream))
{
var process = Process.Start(cloneStart);
// TODO announce ...
// TODO publish the starting log url ...
while (!process.HasExited)
{
if (process.StandardOutput.Peek() > -1)

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.");
}
}
}

View File

@ -8,8 +8,6 @@ using Yavsc.Services;
using Yavsc;
using Xunit;
using Npgsql;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Storage.Internal;
namespace test
{

View File

@ -20,8 +20,8 @@ using Yavsc.Helpers;
using Microsoft.Data.Entity;
using Xunit.Abstractions;
using System.IO;
using Yavsc.Lib;
using System.Linq;
using Yavsc.Server.Models.IT.SourceCode;
namespace test
{

View File

@ -29,7 +29,8 @@
"wwwroot",
"node_modules",
"bower_components",
"contrib"
"contrib",
"testingrepo"
],
"tooling": {
"defaultNamespace": "test"