Skip to content

Commit

Permalink
Added current branch name [release]
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed Feb 18, 2022
1 parent dcb3344 commit 2f7511e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
35 changes: 30 additions & 5 deletions AddActionsWorkflow/Commands/MyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ namespace AddActionsWorkflow;
internal sealed class MyCommand : BaseCommand<MyCommand>
{
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)
Expand Down Expand Up @@ -68,7 +69,7 @@ internal async Task<bool> 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))
Expand All @@ -83,7 +84,7 @@ internal async Task<bool> CreateWorkflowTemplateAsync(string workingDirectory, G
return created;
}

internal async Task<String> GetGitRootDirAsync(string workingDirectory)
internal async Task<String> GetGitRootDirAsync(string workingDirectory, bool useCurrentBranch)
{
await VS.StatusBar.ShowMessageAsync("Establishing git root directory...");
var rootGitDir = workingDirectory;
Expand All @@ -105,8 +106,32 @@ internal async Task<String> 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;
}
}
}
10 changes: 8 additions & 2 deletions AddActionsWorkflow/Options/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class General : BaseOptionModel<General>
[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")]
Expand All @@ -32,5 +32,11 @@ public class General : BaseOptionModel<General>
[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;
}
}

1 comment on commit 2f7511e

@timheuer
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #9

Please sign in to comment.