refactor(git-provider): Basic manager class implementation and dummy Set-GitRepo command
This commit is contained in:
parent
6193d30029
commit
dedea9213b
1 changed files with 100 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
using System.Management.Automation;
|
using System.Management.Automation;
|
||||||
using SQLite;
|
using SQLite;
|
||||||
|
|
||||||
|
|
@ -17,23 +18,114 @@ public class NewGitRepoCommand : PSCmdlet
|
||||||
HelpMessage = "Reference name for the repo")]
|
HelpMessage = "Reference name for the repo")]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public NewGitRepoCommand()
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{nameof(NewGitRepoCommand)} init");
|
||||||
|
var gm = GitManager.Instance;
|
||||||
|
}
|
||||||
|
|
||||||
protected override void BeginProcessing()
|
protected override void BeginProcessing()
|
||||||
{
|
{
|
||||||
using var conn = new SQLiteConnection("./test.db");
|
//using var conn = new SQLiteConnection("./test.db");
|
||||||
|
|
||||||
var pwd = this.SessionState.Path.CurrentLocation.Path;
|
var pwd = this.SessionState.Path.CurrentLocation.Path;
|
||||||
WriteObject("Checking if current directory is a git repository...");
|
WriteObject("Checking if current directory is a git repository...");
|
||||||
var ps = new ProcessStartInfo( //$"git -C \"{pwd}\" rev-parse --show-toplevel")
|
|
||||||
"git", ["rev-parse", "--show-toplevel"])
|
var repoFolfder = IsGitRepo(pwd);
|
||||||
{
|
|
||||||
RedirectStandardOutput = true,
|
|
||||||
RedirectStandardError = true,
|
|
||||||
WorkingDirectory = pwd
|
|
||||||
};
|
|
||||||
var a = Process.Start(ps);
|
|
||||||
Console.WriteLine(a.StandardOutput.ReadToEnd());
|
|
||||||
Console.WriteLine(a.StandardError.ReadToEnd());
|
|
||||||
|
|
||||||
base.BeginProcessing();
|
base.BeginProcessing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ParsedGitFolderDetails? IsGitRepo(string path)
|
||||||
|
{
|
||||||
|
var ps = new ProcessStartInfo("git",
|
||||||
|
["rev-parse", "--show-toplevel"])
|
||||||
|
{
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true,
|
||||||
|
WorkingDirectory = path
|
||||||
|
};
|
||||||
|
|
||||||
|
// If the user doesn't have git on their path, this will throw an exception that I don't have to do anything
|
||||||
|
// special with, it'll be unhandled and powershell will handle it
|
||||||
|
var gitProcess = Process.Start(ps);
|
||||||
|
|
||||||
|
// This probably shouldn't be possible? Not really sure of the conditions where the process could be started
|
||||||
|
// but return null, but I'm going to consider that unrecoverable error territory
|
||||||
|
if (gitProcess is null)
|
||||||
|
{
|
||||||
|
throw new Exception("git failed to start");
|
||||||
|
}
|
||||||
|
|
||||||
|
gitProcess.WaitForExit();
|
||||||
|
|
||||||
|
if (!gitProcess.StandardOutput.EndOfStream)
|
||||||
|
{
|
||||||
|
var directory = gitProcess.StandardOutput.ReadToEnd();
|
||||||
|
// Gotta trim what we get as it might already have a newline character at the end
|
||||||
|
var dirInfo = new DirectoryInfo(directory.Trim());
|
||||||
|
|
||||||
|
var repoFolderInfo = new ParsedGitFolderDetails
|
||||||
|
{
|
||||||
|
Directory = dirInfo.FullName,
|
||||||
|
Folder = dirInfo.Name,
|
||||||
|
};
|
||||||
|
|
||||||
|
return repoFolderInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gitProcess.StandardError.EndOfStream)
|
||||||
|
{
|
||||||
|
var errorAsException = new Exception(gitProcess.StandardError.ReadToEnd());
|
||||||
|
WriteError(new ErrorRecord(errorAsException, "git-not-found", ErrorCategory.FromStdErr, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The directory details of the directory returned from git rev-parse --show-toplevel
|
||||||
|
/// </summary>
|
||||||
|
private class ParsedGitFolderDetails
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The full path to the top level folder containing a git repository
|
||||||
|
/// </summary>
|
||||||
|
public string Directory { get; set; } = null!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The last folder name of the directory
|
||||||
|
/// </summary>
|
||||||
|
public string Folder { get; set; } = null!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Cmdlet(VerbsCommon.Set, Noun)]
|
||||||
|
public class SetGitRepoCommand : PSCmdlet
|
||||||
|
{
|
||||||
|
private const string Noun = "GitRepo";
|
||||||
|
|
||||||
|
public SetGitRepoCommand()
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{nameof(NewGitRepoCommand)} init");
|
||||||
|
var a = GitManager.Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void BeginProcessing()
|
||||||
|
{
|
||||||
|
base.BeginProcessing();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: better name for this
|
||||||
|
public class GitManager
|
||||||
|
{
|
||||||
|
private static readonly Lazy<GitManager> GitManagerInstance = new(() => new GitManager());
|
||||||
|
public static GitManager Instance => GitManagerInstance.Value;
|
||||||
|
|
||||||
|
private GitManager()
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{nameof(GitManager)} init");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue