【问题标题】:Using libgit2sharp with LINQPad?将 libgit2sharp 与 LINQPad 一起使用?
【发布时间】:2013-06-07 11:32:03
【问题描述】:

我有带有 NuGet 的 LINQPad 版本,我添加了 libgit2sharp 但这依赖于另一个(本机)dll。

我试过了:

  • 将它们复制到我的系统目录中。
  • 将它们放在我已添加到路径中的单独目录中。
  • 将它们放在 LINQPads 插件目录中。
  • 当我运行查询时将它们复制到Assembly.GetExecutingAssembly().Location

我只是想用这个库读取日志并将它作为 LINQPad 中的 sn-p 会很整洁,但我想如果其他所有方法都失败了,我可以将它变成一个控制台应用程序。

有人用过带有 LINQPad 的 libgit2sharp,能解释一下如何让它们一起玩得更好吗?

【问题讨论】:

  • LinqPad 有错误吗?我不太确定您遇到了什么问题。
  • 是的,无法加载 DLL 'git2-9d9fff3'(我在测试中复制的那个)

标签: linqpad libgit2sharp


【解决方案1】:

将 dll 放入路径中的文件夹或将 dll 的位置添加到路径中应该可以(并且对我有用)

也许您的更改尚未生效。尝试关闭并重新打开 LinqPad,或者如果失败,请注销并重新进入 Windows。

【讨论】:

  • 我觉得自己很愚蠢。我将 dll 复制到我一直保存 UnxUtils、sysinternals 二进制文件等的路径目录之一。只是假设 LINQPad 会找到它,如果这有效但需要重新启动 LINQPad。非常感谢。
【解决方案2】:

编辑:LINQPad 再次支持开箱即用的 LibGit2Sharp(从 LINQPad 5.10.02 开始 - 在撰写本文时处于测试阶段)。 现在不需要变通方法。

不幸的是,这在 LibGit2Sharp v0.22 中再次被破坏。此软件包不再包含位于 /libs 中 LibGit2Sharp.dll 下的子文件夹中的本机二进制文件。相反,它们位于一个单独的依赖 NuGet 包中,该包依赖于修补项目文件。由于 LINQPad 不使用项目文件,因此失败。

您可以通过添加其他答案中建议的初始化方法来解决此问题,以使用本机文件夹位置填充 PATH 变量。以下代码无需修改即可在具有当前 LibGit2Sharp 的任何机器上运行:

void Main()
{
   ... your query here...
}

static UserQuery()
{
   const string pathEnvVariable = "PATH";
   char slash = Path.DirectorySeparatorChar;
   char pathSep = Path.PathSeparator;

   // The correct native binary file is located under a folder ending in
   // "windows\x86" or "windows\amd64" or "win7-x86\native" or "win7-x64\native".
   // This may change in later LibGit2Sharp releases.
   string nativeStem1 = $"{slash}windows{slash}{(IntPtr.Size == 8 ? "amd64" : "x86")}";
   string nativeStem2 = $"{(IntPtr.Size == 8 ? "-x64" : "-x86")}{slash}native";

   // Locate the root folder in the NuGet package. This contains folders for the
   // main package (LibGit2Sharp) plus dependencies (LibGit2Sharp.NativeBinaries).
   var nugetRoot = new FileInfo (typeof (Repository).Assembly.Location)
      .Directory.Parent.Parent.Parent;

   var nativeBinaryPath = nugetRoot
      .GetFiles ("*.dll", SearchOption.AllDirectories)
      .Single (d => d.DirectoryName.EndsWith (nativeStem1, StringComparison.OrdinalIgnoreCase) ||
                    d.DirectoryName.EndsWith (nativeStem2, StringComparison.OrdinalIgnoreCase))
      .Directory
      .FullName;

   string currentPaths = Environment.GetEnvironmentVariable (pathEnvVariable);

   if (!(pathSep + currentPaths + pathSep).ToUpperInvariant().Contains
      (pathSep + nativeBinaryPath.ToUpperInvariant() + pathSep))
   {
      Environment.SetEnvironmentVariable (pathEnvVariable, currentPaths + pathSep + nativeBinaryPath);
   }
}

请注意,您不需要显式调用此方法,因为它位于静态构造函数中。

另一种解决方法是在 NuGet 上发布另一个 LibGit2Sharp 包,该包在 LibGit2Sharp.dll 所在的预期位置中包含本机二进制文件(NativeBinaries\x86\git2-785d8c4.dll 和 NativeBinaries\amd86\git2-785d8c4.dll ) - 以下载 LibGit2Sharp 0.21.0.176 为例。

【讨论】:

  • 谢谢乔。出于某种原因,我的 LinqPad 似乎正在运行来自 TEMP 文件夹的查询。这意味着它与InvalidOperationException: Sequence contains more than one matching element 一起爆炸。获取 NuGet 根目录时。当我将此行更改为此它可以工作:var nugetRoot = new DirectoryInfo(Path.Combine(localAppData, "LINQPad", "NuGet.FW46"));
  • 这又坏了?我正在尝试将 LINQPad 5.36.03 与 LibGit2Sharp 0.26 一起使用并获得DllNotFoundException
  • 是的,最新的 libgit2sharp 在 LINQPad 5 中永久损坏。解决方法是下载 0.25.4 或更早版本。好消息是它将在 .NET Core 下的 LINQPad 6 中再次运行。
【解决方案3】:

了不起的@mbx 站了起来,helped us make this happen

感谢他,LibGit2Sharp NuGet 包 (v0.15.0) 的下一个版本将直接在 LinqPad 中运行。

更新:

LibGit2Sharp NuGet v0.14.1 包包含此修复程序,现在是 available for download

【讨论】:

  • LibGit2Sharp NuGet 包的 0.22.0 版在 LINQPad 5.08.01 版中不起作用。
【解决方案4】:

我最终在运行时添加了相关路径:

void SetUpNativeBinaries(){
    var appDataLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    var packageVersion = "LibGit2Sharp.0.14.0.0";// may read from PackageInfo
    var processorArchitecture = IntPtr.Size==8?"amd64":"x86";
    var libgitNativeBinaryPath = Path.Combine(appDataLocal, "LINQPad", "NuGet", "LibGit2Sharp", packageVersion, "NativeBinaries", processorArchitecture);
    libgitNativeBinaryPath.Dump();

    var pathEnvVariable = "PATH";
    var currentPaths = Environment.GetEnvironmentVariable(pathEnvVariable);
    var combinedPaths = String.Format(
        System.Globalization.CultureInfo.InvariantCulture,
        "{0}{1}{2}",
        libgitNativeBinaryPath,
        Path.PathSeparator,
        currentPaths);

    Environment.SetEnvironmentVariable(pathEnvVariable, combinedPaths);
}

将其保存为“C#程序”即可立即开始:

void Main()
{
    SetUpNativeBinaries();
    var repoWorkingDir = @"C:\temp\libgit2_repo";
    var repo = new Repository(repoWorkingDir);

    repo.Config.Dump();
    repo.Branches.Dump();
    repo.Index.RetrieveStatus().Dump();
    //...
}

更新 There is a closed issue and the fix is shipped with since release 0.14.1.0

【讨论】:

    【解决方案5】:

    对于 LibGit2Sharp NuGet 包的 0.22.0 版和 LINQPad 的 5.08.01 版,NuGet 包和 mbx's answer 都不适合我。

    我将mbx的回答中的方法修改为如下:

    void SetUpLibGit2SharpNativeBinaries()
    {
        string libGit2SharpNativeBinariesPath = @"C:\Users\kevitt\AppData\Local\LINQPad\NuGet.FW46\LibGit2Sharp\LibGit2Sharp.NativeBinaries.1.0.129\libgit2\windows\x86";
    
        string pathEnvVariable = "PATH";
        string currentPaths = Environment.GetEnvironmentVariable(pathEnvVariable);
    
        IEnumerable<string> paths = currentPaths.Split(';');
    
        if (!paths.Contains(libGit2SharpNativeBinariesPath))
        {
            var combinedPaths = String.Format(
                System.Globalization.CultureInfo.InvariantCulture,
                "{0}{1}{2}",
                libGit2SharpNativeBinariesPath,
                Path.PathSeparator,
                currentPaths);
    
            Environment.SetEnvironmentVariable(pathEnvVariable, combinedPaths);
        }
    }
    

    请注意,这几乎肯定不适用于其他任何人,因为您需要在分配给字符串变量 libGit2SharpNativeBinariesPath 的值中用您自己的至少替换我的 Windows 登录名。

    另外,libGit2SharpNativeBinariesPath(对应于 mbx 代码中的 libgitNativeBinaryPath 字符串)现在似乎不是可以(容易)在 mbx 代码中生成的东西,因此您可能需要自己确定路径。

    【讨论】:

    • %LOCALAPPDATA% 替换C:\Users\kevitt\AppData\Local 应该使脚本更通用。不过,您仍然会被硬编码到固定版本的LibGit2Sharp.NativeBinaries 上。我刚刚将%LOCALAPPDATA%\LINQPad\NuGet.FW46\LibGit2Sharp\LibGit2Sharp.NativeBinaries.1.0.129\libgit2\windows\x86 添加到我的PATH 中,这似乎成功了。
    • @KevinKuszyk 我实际上尝试了您的建议,但它不起作用。介意自己试试吗?
    • 所以现在我很困惑。这上周有效,但今天又坏了。我会进行更多调查,并在有更新时发布更新。
    • 我正在取得进展。似乎%LOCALAPPDATA% 在 LinqPad 中不起作用,但 var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) 可以。
    • @KevinKuszyk 正如 Joe Albahari 在his comment 中对您回答这个问题时提到的那样,他将在 LINQPad 中“修复”这个问题;更多详情here.
    【解决方案6】:

    为我工作:将 git2-785d8c4.dll 复制到与 LibGit2Sharp.dll 相同的目录并重新启动 LINQPad。

    copy %LOCALAPPDATA%\LINQPad\NuGet.FW46\LibGit2Sharp\LibGit2Sharp.NativeBinaries.2.0.267\runtimes\win-x86\native\git2-785d8c4.dll %LOCALAPPDATA%\LINQPad\NuGet.FW46\LibGit2Sharp\LibGit2Sharp.0.26.0\lib\net46
    

    (LibGit2Sharp.0.26.0,LINQPad 5.36.03)

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 2014-08-06
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多