Skip to content

Commit

Permalink
Changing libs for git
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed Jul 15, 2023
1 parent a7d33b1 commit 6249f4d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 92 deletions.
16 changes: 11 additions & 5 deletions AddActionsWorkflow/AddActionsWorkflow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,22 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CliWrap">
<Version>3.5.0</Version>
<Version>3.6.4</Version>
</PackageReference>
<PackageReference Include="Community.VisualStudio.Toolkit.17">
<Version>17.0.507</Version>
</PackageReference>
<PackageReference Include="Community.VisualStudio.VSCT" Version="16.0.29.6" PrivateAssets="all" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.5.4074">
<PackageReference Include="LibGit2Sharp">
<Version>0.27.2</Version>
</PackageReference>
<PackageReference Include="LibGit2Sharp.NativeBinaries">
<Version>2.0.320</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.6.2164">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.492" ExcludeAssets="Runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
Expand Down
1 change: 1 addition & 0 deletions AddActionsWorkflow/AddActionsWorkflowPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace AddActionsWorkflow;
[Guid(PackageGuids.AddActionsWorkflowString)]
[ProvideOptionPage(typeof(OptionsProvider.GeneralOptions), "GitHub Actions Workflow", "General", 0, 0, true)]
[ProvideProfile(typeof(OptionsProvider.GeneralOptions), "GitHub Actions Workflow", "General", 0, 0, true)]
[ProvideBindingPath]
public sealed class AddActionsWorkflowPackage : ToolkitPackage
{
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
Expand Down
59 changes: 17 additions & 42 deletions AddActionsWorkflow/Commands/AddWorkflowCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AddActionsWorkflow.Options;
using CliWrap;
using Microsoft;
using LibGit2Sharp;
using System.Diagnostics;
using System.IO;
using System.Text;
Expand All @@ -11,7 +11,7 @@ namespace AddActionsWorkflow;
[Command(PackageIds.AddWorkflowCommand)]
internal sealed class AddWorkflowCommand : BaseCommand<AddWorkflowCommand>
{
string finaleWorkflowname = "";
string finaleWorkflowname = string.Empty;
string branchName = "main";

protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
Expand All @@ -23,7 +23,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
var options = await General.GetLiveInstanceAsync();

// try to get the repo root
string repoRoot = await GetGitRootDirAsync(slnDir, options.UseCurrentBranchName);
string repoRoot = await GetGitRootDirAsync(dirInfo.FullName, options.UseCurrentBranchName);
var workflowCreated = await CreateWorkflowTemplateAsync(repoRoot, options);

if (workflowCreated)
Expand Down Expand Up @@ -88,50 +88,25 @@ internal async Task<String> GetGitRootDirAsync(string workingDirectory, bool use
{
await VS.StatusBar.ShowMessageAsync("Establishing git root directory...");
var rootGitDir = workingDirectory;
var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();

var result = await Cli.Wrap("git")
.WithArguments("rev-parse --show-toplevel")
.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)
while (!Directory.Exists(Path.Combine(rootGitDir, ".git")))
{
rootGitDir = stdOut;
rootGitDir = rootGitDir.Replace('/', '\\').Replace("\n", "");

if (useCurrentBranch) await GetCurrentBranchNameAsync(workingDirectory);
rootGitDir = Path.GetFullPath(Path.Combine(rootGitDir, ".."));
}

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)
try
{
using (var repo = new Repository(rootGitDir))
{
if (useCurrentBranch) branchName = repo.Head.FriendlyName;
rootGitDir = repo.Info.WorkingDirectory;
}
}
catch (Exception ex)
{
branchName = stdOut;
Debug.WriteLine(ex.Message);
}

return rootGitDir;
}
}
42 changes: 20 additions & 22 deletions AddActionsWorkflow/Commands/LaunchRemoteUrlCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using AddActionsWorkflow.Options;
using CliWrap;
using System;
using System.Collections.Generic;
using LibGit2Sharp;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AddActionsWorkflow.Commands;

Expand All @@ -17,29 +13,31 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
// get the repo URI
var dirInfo = new DirectoryInfo((await VS.Solutions.GetCurrentSolutionAsync()).FullPath);
var slnDir = dirInfo.Parent.FullName;

string path = dirInfo.FullName;

while (!Directory.Exists(Path.Combine(path, ".git")))
{
path = Path.GetFullPath(Path.Combine(path, ".."));
}

var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();
var remoteUri = string.Empty;

var result = await Cli.Wrap("git")
.WithArguments("remote get-url origin --push")
.WithWorkingDirectory(slnDir)
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
.WithValidation(CommandResultValidation.None)
.ExecuteAsync();

var stdOut = stdOutBuffer.ToString();
var stdErr = stdErrBuffer.ToString();

if (result.ExitCode == 0)
try
{
_ = Process.Start(stdOut);
}
else
using (var repo = new Repository(path))
{
var headRemote = repo.Head.RemoteName;
var remote = repo.Network.Remotes.FirstOrDefault(r => r.Name == headRemote);
remoteUri = remote.Url;
}
_ = Process.Start(remoteUri);
}
catch (Exception ex)
{
var argError = new UriFormatException(stdErr);
var argError = new UriFormatException(ex.Message);
await argError.LogAsync();
}
}
Expand Down
1 change: 1 addition & 0 deletions AddActionsWorkflow/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

[assembly: AssemblyVersion(Vsix.Version)]
[assembly: AssemblyFileVersion(Vsix.Version)]
[assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\LibGit2Sharp.dll")]

namespace System.Runtime.CompilerServices
{
Expand Down
2 changes: 1 addition & 1 deletion AddActionsWorkflow/source.extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed partial class Vsix
public const string Name = "Add Actions Workflow";
public const string Description = @"Adds a GitHub Actions workflow to the solution";
public const string Language = "en-US";
public const string Version = "1.0.0";
public const string Version = "1.0.1";
public const string Author = "Tim Heuer";
public const string Tags = "github, actions, devops, workflow";
}
Expand Down
44 changes: 22 additions & 22 deletions AddActionsWorkflow/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="AddActionsWorkflow.727729af-a64d-474f-b03e-aaddf3b5dff6" Version="1.0.0" Language="en-US" Publisher="Tim Heuer" />
<DisplayName>Add Actions Workflow</DisplayName>
<Description xml:space="preserve">Adds a GitHub Actions workflow to the solution</Description>
<MoreInfo>https://github.com/timheuer/AddActionsWorkflow</MoreInfo>
<License>LICENSE.txt</License>
<GettingStartedGuide>https://github.com/timheuer/AddActionsWorkflow</GettingStartedGuide>
<Icon>Resources\Icon.png</Icon>
<PreviewImage>Resources\Icon.png</PreviewImage>
<Tags>github, actions, devops, workflow</Tags>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[17.0, 18.0)">
<ProductArchitecture>amd64</ProductArchitecture>
</InstallationTarget>
</Installation>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[17.0,)" DisplayName="Visual Studio core editor" />
</Prerequisites>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
</Assets>
<Metadata>
<Identity Id="AddActionsWorkflow.727729af-a64d-474f-b03e-aaddf3b5dff6" Version="1.0.1" Language="en-US" Publisher="Tim Heuer" />
<DisplayName>Add Actions Workflow</DisplayName>
<Description xml:space="preserve">Adds a GitHub Actions workflow to the solution</Description>
<MoreInfo>https://github.com/timheuer/AddActionsWorkflow</MoreInfo>
<License>LICENSE.txt</License>
<GettingStartedGuide>https://github.com/timheuer/AddActionsWorkflow</GettingStartedGuide>
<Icon>Resources\Icon.png</Icon>
<PreviewImage>Resources\Icon.png</PreviewImage>
<Tags>github, actions, devops, workflow</Tags>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[17.0, 18.0)">
<ProductArchitecture>amd64</ProductArchitecture>
</InstallationTarget>
</Installation>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[17.0,)" DisplayName="Visual Studio core editor" />
</Prerequisites>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
</Assets>
</PackageManifest>

0 comments on commit 6249f4d

Please sign in to comment.