From 110deb867aee89be27911f539b6f1e61b6317244 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Fri, 7 Sep 2018 16:19:30 +0200 Subject: [PATCH] refactoring --- Yavsc.Server/Models/IT/SourceCode/GitBatch.cs | 68 ------------------- .../Models/IT/SourceCode}/GitClone.cs | 18 ++--- .../Models/IT/SourceCode/ProjectBuild.cs | 50 ++++++++++++++ .../IT/SourceCode/SingleCmdProjectBatch.cs | 21 ++++++ test/Mandatory/ServerSideFixture.cs | 2 - test/Mandatory/YavscMandatory.cs | 2 +- test/project.json | 3 +- 7 files changed, 80 insertions(+), 84 deletions(-) delete mode 100644 Yavsc.Server/Models/IT/SourceCode/GitBatch.cs rename {Yavsc/Services => Yavsc.Server/Models/IT/SourceCode}/GitClone.cs (77%) create mode 100644 Yavsc.Server/Models/IT/SourceCode/ProjectBuild.cs create mode 100644 Yavsc.Server/Models/IT/SourceCode/SingleCmdProjectBatch.cs diff --git a/Yavsc.Server/Models/IT/SourceCode/GitBatch.cs b/Yavsc.Server/Models/IT/SourceCode/GitBatch.cs deleted file mode 100644 index d961e0cc..00000000 --- a/Yavsc.Server/Models/IT/SourceCode/GitBatch.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System.Diagnostics; -using System.IO; - -namespace Yavsc.Server.Models.IT.SourceCode -{ - public abstract class GitBatch : Batch - { - - 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; - } - } - } - } -} \ No newline at end of file diff --git a/Yavsc/Services/GitClone.cs b/Yavsc.Server/Models/IT/SourceCode/GitClone.cs similarity index 77% rename from Yavsc/Services/GitClone.cs rename to Yavsc.Server/Models/IT/SourceCode/GitClone.cs index e24e1441..bc21d9c0 100644 --- a/Yavsc/Services/GitClone.cs +++ b/Yavsc.Server/Models/IT/SourceCode/GitClone.cs @@ -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 + 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) diff --git a/Yavsc.Server/Models/IT/SourceCode/ProjectBuild.cs b/Yavsc.Server/Models/IT/SourceCode/ProjectBuild.cs new file mode 100644 index 00000000..2bad4b29 --- /dev/null +++ b/Yavsc.Server/Models/IT/SourceCode/ProjectBuild.cs @@ -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); + } + + } +} \ No newline at end of file diff --git a/Yavsc.Server/Models/IT/SourceCode/SingleCmdProjectBatch.cs b/Yavsc.Server/Models/IT/SourceCode/SingleCmdProjectBatch.cs new file mode 100644 index 00000000..1252fcc7 --- /dev/null +++ b/Yavsc.Server/Models/IT/SourceCode/SingleCmdProjectBatch.cs @@ -0,0 +1,21 @@ +using System; +using System.IO; + +namespace Yavsc.Server.Models.IT.SourceCode +{ + public abstract class SingleCmdProjectBatch : Batch + { + 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."); + } + + } +} \ No newline at end of file diff --git a/test/Mandatory/ServerSideFixture.cs b/test/Mandatory/ServerSideFixture.cs index edf5d7ac..ac72ed45 100644 --- a/test/Mandatory/ServerSideFixture.cs +++ b/test/Mandatory/ServerSideFixture.cs @@ -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 { diff --git a/test/Mandatory/YavscMandatory.cs b/test/Mandatory/YavscMandatory.cs index 3d495b5e..4dfebb2a 100755 --- a/test/Mandatory/YavscMandatory.cs +++ b/test/Mandatory/YavscMandatory.cs @@ -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 { diff --git a/test/project.json b/test/project.json index 9edb8503..77a249b8 100644 --- a/test/project.json +++ b/test/project.json @@ -29,7 +29,8 @@ "wwwroot", "node_modules", "bower_components", - "contrib" + "contrib", + "testingrepo" ], "tooling": { "defaultNamespace": "test"