From a7c706326b5c8aa7dd015b798ab102501378ee32 Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Wed, 22 Feb 2017 00:25:09 -0600 Subject: [PATCH] write source link automatically from GiHub git repository #121 (#143) --- .gitignore | 3 +- SourceLink.Create.GitHub/CreateTask.cs | 49 +++++++---- SourceLink.Create.GitHub/Process.cs | 4 +- .../SourceLink.Create.GitHub.csproj | 4 +- .../SourceLink.Create.GitHub.targets | 9 +- SourceLink.sln | 19 ----- build.ps1 | 4 +- dotnet-sourcelink-git/Program.cs | 85 +++++++++++++++++-- .../dotnet-sourcelink-git.csproj | 8 +- dotnet-sourcelink/Program.cs | 32 +++++-- .../{Json.cs => SourceLinkJson.cs} | 0 dotnet-sourcelink/dotnet-sourcelink.csproj | 4 +- 12 files changed, 159 insertions(+), 62 deletions(-) rename dotnet-sourcelink/{Json.cs => SourceLinkJson.cs} (100%) diff --git a/.gitignore b/.gitignore index 778d701..afcdbc6 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ Tfs/lib/*.dll lib/*.dll .vs/ packages.config -.vscode/launch.json \ No newline at end of file +.vscode/launch.json +launchSettings.json \ No newline at end of file diff --git a/SourceLink.Create.GitHub/CreateTask.cs b/SourceLink.Create.GitHub/CreateTask.cs index 1ac88fd..004bc96 100644 --- a/SourceLink.Create.GitHub/CreateTask.cs +++ b/SourceLink.Create.GitHub/CreateTask.cs @@ -8,11 +8,10 @@ namespace SourceLink.Create.GitHub { public class CreateTask : MSBuildTask { - public string Url { get; set; } + public string GitDirectory { get; set; } - public string Repo { get; set; } + public string Url { get; set; } - [Required] public string[] Sources { get; set; } [Required] @@ -33,28 +32,48 @@ DataReceivedEventHandler LogMessageHander(MessageImportance importance) public override bool Execute() { - var repo = Repo; - if (String.IsNullOrEmpty(repo)) + var url = Url; + var gitOption = String.IsNullOrEmpty(GitDirectory) ? "" : " -d \"" + GitDirectory + "\""; + + if (String.IsNullOrEmpty(url)) { - var originCmd = Process.RunAndGetOutput("dotnet", "sourcelink-git origin"); + var originCmd = Process.RunAndGetOutput("dotnet", "sourcelink-git origin" + gitOption); if (originCmd.ExitCode != 0 || originCmd.OutputLines.Count != 1) { Log.LogMessage(MessageImportance.High, "unable to get repository origin"); return false; } - repo = originCmd.OutputLines[0]; + var origin = originCmd.OutputLines[0]; + url = GetRepoUrl(origin); } - var args = new StringBuilder(); - args.Append("sourcelink-git create"); + var sbArgs = new StringBuilder(); + sbArgs.Append("sourcelink-git create" + gitOption); + sbArgs.Append(" -u " + url); + sbArgs.Append(" -f \"" + File + "\""); + if (Sources != null) { + foreach (var source in Sources) + sbArgs.Append(" -s \"" + source + "\""); + } + var args = sbArgs.ToString(); - var exit = Process.Run("dotnet", args.ToString(), - outputHandler: LogMessageHander(MessageImportance.Normal), - errorHandler: LogMessageHander(MessageImportance.Normal) - ); + var create = Process.RunAndGetOutput("dotnet", args); + if(create.ExitCode != 0) + { + Log.LogMessage(MessageImportance.High, "dotnet " + args); + foreach (var line in create.OutputLines) + Log.LogMessage(MessageImportance.High, line); + Log.LogError("exit code " + create.ExitCode + " when running: dotnet " + args); + } + else + { + Log.LogMessage(MessageImportance.Normal, "dotnet " + args); + foreach (var line in create.OutputLines) + Log.LogMessage(MessageImportance.Normal, line); + } SourceLink = File; - return exit == 0; + return !Log.HasLoggedErrors; } public static string GetRepoUrl(string origin) @@ -66,7 +85,7 @@ public static string GetRepoUrl(string origin) } origin = origin.Replace(".git", ""); var uri = new Uri(origin); - return "https://raw.githubusercontent.com" + uri.LocalPath + "/{0}/*"; + return "https://raw.githubusercontent.com" + uri.LocalPath + "/{commit}/*"; } } diff --git a/SourceLink.Create.GitHub/Process.cs b/SourceLink.Create.GitHub/Process.cs index ecb7210..a7fb071 100644 --- a/SourceLink.Create.GitHub/Process.cs +++ b/SourceLink.Create.GitHub/Process.cs @@ -38,7 +38,9 @@ public static int Run(string filename, string arguments = "", string workingDire p.WaitForExit(); return p.ExitCode; } - } catch (Exception e) { + } + catch (Exception e) + { return e.HResult; } } diff --git a/SourceLink.Create.GitHub/SourceLink.Create.GitHub.csproj b/SourceLink.Create.GitHub/SourceLink.Create.GitHub.csproj index a6be00a..bc6a757 100644 --- a/SourceLink.Create.GitHub/SourceLink.Create.GitHub.csproj +++ b/SourceLink.Create.GitHub/SourceLink.Create.GitHub.csproj @@ -8,11 +8,11 @@ Cameron Taggart https://opensource.org/licenses/MIT https://ctaggart.github.io/SourceLink/SourceLink128.jpg - https://ctaggart.github.io/SourceLink/ + https://github.com/ctaggart/SourceLink sourcelink pdb symbols git sourceindexing debugging sourceserver build SourceLink: Source Code On Demand - Source Link your Portable PDB files to allow source code to be downloaded on demand from the source code repository host + Source Link your Portable PDB files to allow source code to be downloaded on demand diff --git a/SourceLink.Create.GitHub/SourceLink.Create.GitHub.targets b/SourceLink.Create.GitHub/SourceLink.Create.GitHub.targets index 0ebe9f9..19a5410 100644 --- a/SourceLink.Create.GitHub/SourceLink.Create.GitHub.targets +++ b/SourceLink.Create.GitHub/SourceLink.Create.GitHub.targets @@ -12,11 +12,14 @@ $(BaseIntermediateOutputPath)sourcelink.json - - + + + + + + - \ No newline at end of file diff --git a/SourceLink.sln b/SourceLink.sln index 5091f8b..f9ed60d 100644 --- a/SourceLink.sln +++ b/SourceLink.sln @@ -7,27 +7,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .gitattributes = .gitattributes .gitignore = .gitignore appveyor.yml = appveyor.yml - build.fsx = build.fsx build.ps1 = build.ps1 - docs\tools\generate.fsx = docs\tools\generate.fsx - paket.dependencies = paket.dependencies - paket.lock = paket.lock README.md = README.md - RELEASE_NOTES.md = RELEASE_NOTES.md - EndProjectSection -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{D3E73D0A-292F-463C-B834-F8FCD9AA36BD}" - ProjectSection(SolutionItems) = preProject - docs\content\exe.md = docs\content\exe.md - docs\content\fake.md = docs\content\fake.md - docs\tools\generate.fsx = docs\tools\generate.fsx - docs\content\github.md = docs\content\github.md - docs\content\how-it-works.md = docs\content\how-it-works.md - docs\content\index.md = docs\content\index.md - docs\tools\templates\template.cshtml = docs\tools\templates\template.cshtml - docs\content\tfs.md = docs\content\tfs.md - docs\content\users.md = docs\content\users.md - docs\content\visualstudio.md = docs\content\visualstudio.md EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-sourcelink", "dotnet-sourcelink\dotnet-sourcelink.csproj", "{68D40DAC-77EF-426A-979D-1CC4723CB2A1}" diff --git a/build.ps1 b/build.ps1 index 5108ef6..018584a 100644 --- a/build.ps1 +++ b/build.ps1 @@ -1,5 +1,5 @@ $version = '2.0.0' # the version under development, update after a release -$versionSuffix = '-a044' # manually incremented for local builds +$versionSuffix = '-a057' # manually incremented for local builds function isVersionTag($tag){ $v = New-Object Version @@ -36,5 +36,5 @@ bash .\build-rename.sh # testing on local nuget feed if (-not $env:appveyor){ - copy .\bin\*$version$versionSuffix.nupkg C:\dotnet\nupkg\ + Copy-Item .\bin\*$version$versionSuffix.nupkg C:\dotnet\nupkg\ } \ No newline at end of file diff --git a/dotnet-sourcelink-git/Program.cs b/dotnet-sourcelink-git/Program.cs index eea43ea..d246233 100644 --- a/dotnet-sourcelink-git/Program.cs +++ b/dotnet-sourcelink-git/Program.cs @@ -3,6 +3,7 @@ using LibGit2Sharp; using System.Collections.Generic; using System.IO; +using Newtonsoft.Json; namespace SourceLink.Git { public class Program @@ -24,8 +25,16 @@ static int Main(string[] args) app.ShowHelp(); return 0; } - app.Execute(args); - return 0; + + try + { + return app.Execute(args); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + return -1; + } } public static void PrintRepo(CommandLineApplication command) @@ -85,21 +94,73 @@ public static void PrintOrigin(CommandLineApplication command) public static void Create(CommandLineApplication command) { command.Description = "creates the Source Link JSON file"; - var dirOption = command.Option("-d|--dir ", "the directory to look for the repository", CommandOptionType.SingleValue); - var fileOption = command.Option("-f|--file ", "file to write", CommandOptionType.SingleValue); + var dirOption = command.Option("-d|--dir ", "the directory to look for the git repository", CommandOptionType.SingleValue); + var fileOption = command.Option("-f|--file ", "the sourcelink.json file to write", CommandOptionType.SingleValue); + var embedOption = command.Option("-e|--embed ", "the sourcelink.embed file to write", CommandOptionType.SingleValue); var urlOption = command.Option("-u|--url ", "URL for downloading the source files, use {0} for commit and * for path", CommandOptionType.SingleValue); + var sourceOption = command.Option("-s|--source ", "source file to verify checksum in git repository", CommandOptionType.MultipleValue); + command.HelpOption("-h|--help"); command.OnExecute(() => { + var dir = "./"; + if (dirOption.HasValue()) + dir = dirOption.Value(); - // get commit + var repoPath = FindGitRepo(dir); + if (repoPath == null) + { + Console.Error.WriteLine("repository not found at or above " + dir); + return 1; + } - // TODO write actual json - using (var sw = System.IO.File.CreateText(fileOption.Value())) + if (!fileOption.HasValue()) { - sw.WriteLine("{\"documents\": { \"C:\\\\Users\\\\camer\\\\cs\\\\sourcelink-test\\\\*\" : \"https://raw.githubusercontent.com/ctaggart/sourcelink-test/b5012a98bed12f6704cb942e92ba34ccdbd920d8/*\" }}"); + Console.Error.WriteLine("--file option required"); + return 2; } + var file = fileOption.Value(); + + if (!urlOption.HasValue()) + { + Console.Error.WriteLine("--url option required"); + return 3; + } + var url = urlOption.Value(); + var commit = GetCommit(repoPath); + url = url.Replace("{commit}", commit); + + // TODO test checksums + //if (sourceOption.HasValue()) + //{ + // var n = sourceOption.Values.Count; + + // if (embedOption.HasValue()) + // { + // var embed = embedOption.Value(); + + // using (var sw = new StreamWriter(File.OpenWrite(file))) + // { + // sw.Write("a.cs;b.cs;"); + // } + // } + //} + + var json = new SourceLinkJson + { + documents = new Dictionary + { + { string.Format("{0}{1}{2}", repoPath, Path.DirectorySeparatorChar, '*'), url }, + } + }; + + using (var sw = new StreamWriter(File.OpenWrite(file))) + { + var js = new JsonSerializer(); + js.Serialize(sw, json); + } + return 0; }); } @@ -146,5 +207,13 @@ public static string GetOrigin(string repoPath) return null; } + public static string GetCommit(string repoPath) + { + using (var repo = new Repository(repoPath)) + { + return repo.Head.Tip.Sha; + } + } + } } \ No newline at end of file diff --git a/dotnet-sourcelink-git/dotnet-sourcelink-git.csproj b/dotnet-sourcelink-git/dotnet-sourcelink-git.csproj index f2d779d..dead6dc 100644 --- a/dotnet-sourcelink-git/dotnet-sourcelink-git.csproj +++ b/dotnet-sourcelink-git/dotnet-sourcelink-git.csproj @@ -7,15 +7,19 @@ Cameron Taggart https://opensource.org/licenses/MIT https://ctaggart.github.io/SourceLink/SourceLink128.jpg - https://ctaggart.github.io/SourceLink/ + https://github.com/ctaggart/SourceLink sourcelink pdb symbols git sourceindexing debugging sourceserver build SourceLink: Source Code On Demand - Source Link your Portable PDB files to allow source code to be downloaded on demand from the source code repository host + Source Link your Portable PDB files to allow source code to be downloaded on demand + + + + \ No newline at end of file diff --git a/dotnet-sourcelink/Program.cs b/dotnet-sourcelink/Program.cs index e2977da..65555c3 100644 --- a/dotnet-sourcelink/Program.cs +++ b/dotnet-sourcelink/Program.cs @@ -34,8 +34,16 @@ public static int Main(string[] args) app.ShowHelp(); return 0; } - app.Execute(args); - return 0; + + try + { + return app.Execute(args); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return -1; + } } public static void PrintJson(CommandLineApplication command) @@ -126,7 +134,9 @@ public static void PrintUrls(CommandLineApplication command) { Console.WriteLine("{0} {1} {2} {3}", toHex(doc.Hash), HashAlgorithmGuids.GetName(doc.HashAlgorithm), LanguageGuids.GetName(doc.Language), doc.Name); Console.WriteLine(doc.Url); - } else { + } + else + { missingDocs.Add(doc); } } @@ -174,10 +184,14 @@ public static void TestUrls(CommandLineApplication command) { Console.WriteLine("{0} {1} {2} {3}", toHex(doc.Hash), HashAlgorithmGuids.GetName(doc.HashAlgorithm), LanguageGuids.GetName(doc.Language), doc.Name); Console.WriteLine(doc.Url); - } else { + } + else + { erroredDocs.Add(doc); } - } else { + } + else + { missingDocs.Add(doc); } } @@ -276,7 +290,9 @@ public static string GetUrl(string file, SourceLinkJson json) var url = json.documents[key]; var path = m.Groups[1].Value.Replace(@"\", "/"); return url.Replace("*", path); - } else { + } + else + { if (!key.Equals(file, StringComparison.Ordinal)) continue; return json.documents[key]; } @@ -332,7 +348,9 @@ static void HashUrl(HttpClient hc, Document doc) doc.UrlHash = ha.ComputeHash(stream); } } - } else { + } + else + { doc.Error = "url failed " + rsp.StatusCode + ": " + rsp.ReasonPhrase; } } diff --git a/dotnet-sourcelink/Json.cs b/dotnet-sourcelink/SourceLinkJson.cs similarity index 100% rename from dotnet-sourcelink/Json.cs rename to dotnet-sourcelink/SourceLinkJson.cs diff --git a/dotnet-sourcelink/dotnet-sourcelink.csproj b/dotnet-sourcelink/dotnet-sourcelink.csproj index 75445a1..c13a9b5 100644 --- a/dotnet-sourcelink/dotnet-sourcelink.csproj +++ b/dotnet-sourcelink/dotnet-sourcelink.csproj @@ -7,12 +7,12 @@ Cameron Taggart https://opensource.org/licenses/MIT https://ctaggart.github.io/SourceLink/SourceLink128.jpg - https://ctaggart.github.io/SourceLink/ + https://github.com/ctaggart/SourceLink sourcelink pdb symbols git sourceindexing debugging sourceserver build SourceLink: Source Code On Demand - Source Link your Portable PDB files to allow source code to be downloaded on demand from the source code repository host + Source Link your Portable PDB files to allow source code to be downloaded on demand