Commit cb4e58c3 authored by Nathan Bean's avatar Nathan Bean
Browse files

Started a windows service

parent 3583c269
......@@ -13,32 +13,64 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeAnalyzers", "CodeAnalyz
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsService", "WindowsService\WindowsService.csproj", "{1E5A0004-1529-451B-847E-78DB0E17DA72}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{EF995F7A-CF49-47D1-8701-67DBFE4C111A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D1378935-7645-4214-89C6-AC85A186AED4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Debug|x64.ActiveCfg = Debug|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Debug|x64.Build.0 = Debug|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Release|Any CPU.Build.0 = Release|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Release|x64.ActiveCfg = Release|Any CPU
{D1378935-7645-4214-89C6-AC85A186AED4}.Release|x64.Build.0 = Release|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Debug|x64.ActiveCfg = Debug|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Debug|x64.Build.0 = Debug|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Release|Any CPU.Build.0 = Release|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Release|x64.ActiveCfg = Release|Any CPU
{3D4234CD-711F-4B9F-AA9D-061E5ACCD8CD}.Release|x64.Build.0 = Release|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Debug|x64.ActiveCfg = Debug|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Debug|x64.Build.0 = Debug|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Release|Any CPU.Build.0 = Release|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Release|x64.ActiveCfg = Release|Any CPU
{BD88E4EE-1C2B-4EE6-952A-F9F63D40D199}.Release|x64.Build.0 = Release|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Debug|x64.ActiveCfg = Debug|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Debug|x64.Build.0 = Debug|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Release|Any CPU.Build.0 = Release|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Release|x64.ActiveCfg = Release|Any CPU
{65E28062-2B9A-4BB5-B546-449C4CB915E0}.Release|x64.Build.0 = Release|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Debug|x64.ActiveCfg = Debug|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Debug|x64.Build.0 = Debug|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Release|Any CPU.Build.0 = Release|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Release|x64.ActiveCfg = Release|Any CPU
{1E5A0004-1529-451B-847E-78DB0E17DA72}.Release|x64.Build.0 = Release|Any CPU
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Debug|x64.ActiveCfg = Debug|x64
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Debug|x64.Build.0 = Debug|x64
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Release|Any CPU.Build.0 = Release|Any CPU
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Release|x64.ActiveCfg = Release|x64
{EF995F7A-CF49-47D1-8701-67DBFE4C111A}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace KSU.CS.Pendant.Server.Helpers
{
public class GithubHelper
{
public static async Task<string> CloneAndCheckout(string repoUrl, string path, string branch)
{
var psi = new ProcessStartInfo() {
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
using var process = Process.Start(psi);
using StreamWriter writer = process.StandardInput;
if (writer.BaseStream.CanWrite)
{
writer.WriteLine($"git clone {repoUrl} {path}");
writer.WriteLine($"cd {path}");
writer.WriteLine($"git checkout {branch}");
}
Console.WriteLine(process.StandardOutput.ReadToEnd());
await process.WaitForExitAsync();
return path;
}
}
}
using System;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
namespace KSU.CS.Pendant.Server.Helpers
{
public static class ProcessAsyncHelper
{
public static async Task<ProcessResult> ExecuteShellCommand(string command, string arguments, int timeout)
{
var result = new ProcessResult();
using (var process = new Process())
{
// If you run bash-script on Linux it is possible that ExitCode can be 255.
// To fix it you can try to add '#!/bin/bash' header to the script.
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
var outputBuilder = new StringBuilder();
var outputCloseEvent = new TaskCompletionSource<bool>();
process.OutputDataReceived += (s, e) =>
{
// The output stream has been closed i.e. the process has terminated
if (e.Data == null)
{
outputCloseEvent.SetResult(true);
}
else
{
outputBuilder.AppendLine(e.Data);
}
};
var errorBuilder = new StringBuilder();
var errorCloseEvent = new TaskCompletionSource<bool>();
process.ErrorDataReceived += (s, e) =>
{
// The error stream has been closed i.e. the process has terminated
if (e.Data == null)
{
errorCloseEvent.SetResult(true);
}
else
{
errorBuilder.AppendLine(e.Data);
}
};
bool isStarted;
try
{
isStarted = process.Start();
}
catch (Exception error)
{
// Usually it occurs when an executable file is not found or is not executable
result.Completed = true;
result.ExitCode = -1;
result.Output = error.Message;
isStarted = false;
}
if (isStarted)
{
// Reads the output stream first and then waits because deadlocks are possible
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Creates task to wait for process exit using timeout
var waitForExit = WaitForExitAsync(process, timeout);
// Create task to wait for process exit and closing all output streams
var processTask = Task.WhenAll(waitForExit, outputCloseEvent.Task, errorCloseEvent.Task);
// Waits process completion and then checks it was not completed by timeout
if (await Task.WhenAny(Task.Delay(timeout), processTask) == processTask && waitForExit.Result)
{
result.Completed = true;
result.ExitCode = process.ExitCode;
// Adds process output if it was completed with error
if (process.ExitCode != 0)
{
result.Output = $"{outputBuilder}{errorBuilder}";
}
}
else
{
try
{
// Kill hung process
process.Kill();
}
catch
{
}
}
}
}
return result;
}
private static Task<bool> WaitForExitAsync(Process process, int timeout)
{
return Task.Run(() => process.WaitForExit(timeout));
}
public struct ProcessResult
{
public bool Completed;
public int? ExitCode;
public string Output;
}
}
}
using System;
using System.IO;
using LibGit2Sharp;
using KSU.CS.Pendant.Server.Helpers;
namespace WindowsService
{
......@@ -9,7 +9,8 @@ namespace WindowsService
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Repository.Clone("git@github.com:ksu-cis/webhook-test-zombiepaladin.git", "C:\\Users\\nhbean\\Desktop\\Test");
GithubHelper.CloneAndCheckout("git@github.com:ksu-cis/webhook-test-zombiepaladin.git", "C:\\Users\\nhbean\\Desktop\\Test", "main");
//LibGit2Sharp.Repository.Clone("git@github.com:ksu-cis/webhook-test-zombiepaladin.git", "C:\\Users\\nhbean\\Desktop\\Test");
}
}
}
......@@ -5,8 +5,4 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibGit2Sharp-SSH" Version="1.0.22" />
</ItemGroup>
</Project>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment