Skip to content

Commit

Permalink
GitTools#203 Added the option to index pdb files using srctool
Browse files Browse the repository at this point in the history
Use srctool to obtain a list of source files from the pdb and filter out files not found in git or with an invalid extension
  • Loading branch information
raymondwu1 authored and JamieR committed Sep 7, 2021
1 parent e6b6c2e commit b95bc78
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 13 deletions.
67 changes: 67 additions & 0 deletions src/GitLink/Helpers/SrcToolHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// <copyright file="SrcToolHelper.cs" company="CatenaLogic">
// Copyright (c) 2014 - 2016 CatenaLogic. All rights reserved.
// </copyright>

namespace GitLink
{
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Catel;
using Catel.Logging;
using GitTools.Git;

internal static class SrcToolHelper
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

internal static List<string> GetSourceFiles(string srcToolFilePath, string projectPdbFile)
{
Argument.IsNotNullOrWhitespace(() => projectPdbFile);
List<string> sources = new List<string>();

var processStartInfo = new ProcessStartInfo(srcToolFilePath)
{
Arguments = string.Format("-r \"{0}\"", projectPdbFile),
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
};

using (var process = new Process())
{
process.OutputDataReceived += (s, e) =>
{
if (e.Data != null)
{
var sourceFile = e.Data.ToLower();

if (Linker.ValidExtension(sourceFile))
{
var repositoryDirectory = GitDirFinder.TreeWalkForGitDir(Path.GetDirectoryName(sourceFile));

if (repositoryDirectory != null)
{
sources.Add(sourceFile);
}
}
}
};

process.EnableRaisingEvents = true;
process.StartInfo = processStartInfo;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();

var processExitCode = process.ExitCode;
if (processExitCode != 0)
{
throw Log.ErrorAndCreateException<GitLinkException>("SrcTool exited with unexpected error code '{0}'", processExitCode);
}
}

return sources;
}
}
}
2 changes: 2 additions & 0 deletions src/GitLink/LinkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public struct LinkOptions

public bool IndexAllDepotFiles { get; set; }

public bool IndexWithSrcTool { get; set; }

public string IntermediateOutputPath { get; set; }
}
}
40 changes: 27 additions & 13 deletions src/GitLink/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static class Linker
private static readonly string UrlEncodedFileNamePlaceHolder = Uri.EscapeUriString("{urlencoded_filename}");
private static readonly string RevisionPlaceholder = Uri.EscapeUriString("{revision}");
private static readonly string PdbStrExePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "pdbstr.exe");
private static readonly string SrcToolExePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "srctool.exe");
private static readonly string[] ExtensionsToIgnore = new string[] { ".g.cs" };
private static IReadOnlyList<string> _sourceFilesList = null;

Expand Down Expand Up @@ -83,11 +84,18 @@ public static class Linker
}
else
{
_sourceFilesList = GetSourceFilesFromPdb(pdbPath, !options.SkipVerify);
if (options.IndexWithSrcTool)
{
_sourceFilesList = SrcToolHelper.GetSourceFiles(SrcToolExePath, pdbPath);
}
else
{
_sourceFilesList = GetSourceFilesFromPdb(pdbPath, !options.SkipVerify);
}

if (!_sourceFilesList.Any())
{
Log.Error($"No source files were found in the PDB: {pdbPath}. If your PDB is native you should use the -a option.");
Log.Error($"No source files were found in the PDB: {pdbPath}. If your PDB is native you could use the -a or -t option.");
return false;
}

Expand Down Expand Up @@ -309,17 +317,7 @@ private static List<string> GetSourceFilesFromDepot(string repositoryDirectory)
{
sourceFiles = from file in Directory.GetFiles(repo.Info.WorkingDirectory, "*.*", SearchOption.AllDirectories)
where !repo.Ignore.IsPathIgnored(file)
let ext = Path.GetExtension(file)
where string.Equals(ext, ".cs", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".cpp", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".c", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".cc", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".cxx", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".c++", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".h", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".hh", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".inl", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".hpp", StringComparison.OrdinalIgnoreCase)
where ValidExtension(file)
select file;
}

Expand Down Expand Up @@ -391,5 +389,21 @@ private static string ReplaceSlashes(IProvider provider, string relativePathForU

return relativePathForUrl;
}

public static Boolean ValidExtension(string sourceFile)
{
var ext = Path.GetExtension(sourceFile);

return string.Equals(ext, ".cs", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".cpp", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".c", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".cc", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".cxx", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".c++", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".h", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".hh", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".inl", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".hpp", StringComparison.OrdinalIgnoreCase);
}
}
}

0 comments on commit b95bc78

Please sign in to comment.