diff --git a/AddActionsWorkflow/Commands/MyCommand.cs b/AddActionsWorkflow/Commands/MyCommand.cs index aa869b3..257e9e2 100644 --- a/AddActionsWorkflow/Commands/MyCommand.cs +++ b/AddActionsWorkflow/Commands/MyCommand.cs @@ -12,17 +12,18 @@ namespace AddActionsWorkflow; internal sealed class MyCommand : BaseCommand { string finaleWorkflowname = ""; + string branchName = "main"; protected override async Task ExecuteAsync(OleMenuCmdEventArgs e) { var dirInfo = new DirectoryInfo((await VS.Solutions.GetCurrentSolutionAsync()).FullPath); var slnDir = dirInfo.Parent.FullName; - // try to get the repo root - string repoRoot = await GetGitRootDirAsync(slnDir); - // create the workflow file with options var options = await General.GetLiveInstanceAsync(); + + // try to get the repo root + string repoRoot = await GetGitRootDirAsync(slnDir, options.UseCurrentBranchName); var workflowCreated = await CreateWorkflowTemplateAsync(repoRoot, options); if (workflowCreated) @@ -68,7 +69,7 @@ internal async Task CreateWorkflowTemplateAsync(string workingDirectory, G var stdErrBuffer = new StringBuilder(); var result = await Cli.Wrap("dotnet") - .WithArguments($"new workflow -n {finaleWorkflowname} --no-update-check {overwriteFile}") + .WithArguments($"new workflow -n {finaleWorkflowname} -b {branchName} --no-update-check {overwriteFile}") .WithWorkingDirectory(workingDirectory) .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer)) .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer)) @@ -83,7 +84,7 @@ internal async Task CreateWorkflowTemplateAsync(string workingDirectory, G return created; } - internal async Task GetGitRootDirAsync(string workingDirectory) + internal async Task GetGitRootDirAsync(string workingDirectory, bool useCurrentBranch) { await VS.StatusBar.ShowMessageAsync("Establishing git root directory..."); var rootGitDir = workingDirectory; @@ -105,8 +106,32 @@ internal async Task GetGitRootDirAsync(string workingDirectory) { rootGitDir = stdOut; rootGitDir = rootGitDir.Replace('/', '\\').Replace("\n", ""); + + if (useCurrentBranch) await GetCurrentBranchNameAsync(workingDirectory); } return rootGitDir; } + + internal async Task GetCurrentBranchNameAsync(string workingDirectory) + { + var stdOutBuffer = new StringBuilder(); + var stdErrBuffer = new StringBuilder(); + + var result = await Cli.Wrap("git") + .WithArguments("branch --show-current") + .WithWorkingDirectory(workingDirectory) + .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer)) + .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer)) + .WithValidation(CommandResultValidation.None) + .ExecuteAsync(); + + var stdOut = stdOutBuffer.ToString(); + var stdErr = stdErrBuffer.ToString(); + + if (result.ExitCode == 0) + { + branchName = stdOut; + } + } } diff --git a/AddActionsWorkflow/Options/General.cs b/AddActionsWorkflow/Options/General.cs index 69cf0cd..25add0a 100644 --- a/AddActionsWorkflow/Options/General.cs +++ b/AddActionsWorkflow/Options/General.cs @@ -12,8 +12,8 @@ public class General : BaseOptionModel [Category("Generator")] [DisplayName("Default file name")] [Description("The base name of the workflow (.yaml) file to be generated")] - [DefaultValue("build-")] - public string DefaultName { get; set; } = "build-"; + [DefaultValue("build")] + public string DefaultName { get; set; } = "build"; [Category("Generator")] [DisplayName("Randomize file name")] @@ -32,5 +32,11 @@ public class General : BaseOptionModel [Description("The Solution Items folder to add these to in the Visual Studio solution")] [DefaultValue("Solution Items")] public string SolutionFolderName { get; set; } = "Solution Items"; + + [Category("Generator")] + [DisplayName("Current branch")] + [Description("Will use the current branch name or 'main' if false")] + [DefaultValue(true)] + public bool UseCurrentBranchName { get; set; } = true; } }