Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
write source link automatically from GiHub git repository #121 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctaggart authored Feb 22, 2017
1 parent beb0b2c commit a7c7063
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Tfs/lib/*.dll
lib/*.dll
.vs/
packages.config
.vscode/launch.json
.vscode/launch.json
launchSettings.json
49 changes: 34 additions & 15 deletions SourceLink.Create.GitHub/CreateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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}/*";
}

}
Expand Down
4 changes: 3 additions & 1 deletion SourceLink.Create.GitHub/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions SourceLink.Create.GitHub/SourceLink.Create.GitHub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<Authors>Cameron Taggart</Authors>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageIconUrl>https://ctaggart.github.io/SourceLink/SourceLink128.jpg</PackageIconUrl>
<PackageProjectUrl>https://ctaggart.github.io/SourceLink/</PackageProjectUrl>
<PackageProjectUrl>https://github.com/ctaggart/SourceLink</PackageProjectUrl>
<PackageTags>sourcelink pdb symbols git sourceindexing debugging sourceserver build</PackageTags>
<Description>
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
</Description>
</PropertyGroup>

Expand Down
9 changes: 6 additions & 3 deletions SourceLink.Create.GitHub/SourceLink.Create.GitHub.targets
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
<SourceLinkFile Condition="'$(SourceLinkFile)' == ''">$(BaseIntermediateOutputPath)sourcelink.json</SourceLinkFile>
</PropertyGroup>

<Target Name="SourceLinkCreate" BeforeTargets="CoreCompile">
<SourceLink.Create.GitHub.CreateTask Url="$(SourceLinkUrl)" Repo="$(SourceLinkRepo)" Sources="@(Compile)" File="$(SourceLinkFile)">
<ItemGroup>
<SourceLinkSources Condition="'@(SourceLinkSources)' == ''" Include="@(Compile)" Exclude="@(EmbeddedFiles)" />
</ItemGroup>

<Target Name="SourceLinkCreate">
<SourceLink.Create.GitHub.CreateTask GitDirectory="$(SourceLinkGitDirectory)" Url="$(SourceLinkUrl)" File="$(SourceLinkFile)" Sources="@(SourceLinkSources)">
<Output TaskParameter="SourceLink" PropertyName="SourceLink" />
</SourceLink.Create.GitHub.CreateTask>
<Message Text="SourceLink = $(SourceLink)" Importance="high" />
</Target>

</Project>
19 changes: 0 additions & 19 deletions SourceLink.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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\
}
85 changes: 77 additions & 8 deletions dotnet-sourcelink-git/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LibGit2Sharp;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace SourceLink.Git {
public class Program
Expand All @@ -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)
Expand Down Expand Up @@ -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 <directory>", "the directory to look for the repository", CommandOptionType.SingleValue);
var fileOption = command.Option("-f|--file <file>", "file to write", CommandOptionType.SingleValue);
var dirOption = command.Option("-d|--dir <directory>", "the directory to look for the git repository", CommandOptionType.SingleValue);
var fileOption = command.Option("-f|--file <file>", "the sourcelink.json file to write", CommandOptionType.SingleValue);
var embedOption = command.Option("-e|--embed <file>", "the sourcelink.embed file to write", CommandOptionType.SingleValue);
var urlOption = command.Option("-u|--url <url>", "URL for downloading the source files, use {0} for commit and * for path", CommandOptionType.SingleValue);
var sourceOption = command.Option("-s|--source <url>", "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, string>
{
{ 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;
});
}
Expand Down Expand Up @@ -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;
}
}

}
}
8 changes: 6 additions & 2 deletions dotnet-sourcelink-git/dotnet-sourcelink-git.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
<Authors>Cameron Taggart</Authors>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageIconUrl>https://ctaggart.github.io/SourceLink/SourceLink128.jpg</PackageIconUrl>
<PackageProjectUrl>https://ctaggart.github.io/SourceLink/</PackageProjectUrl>
<PackageProjectUrl>https://github.com/ctaggart/SourceLink</PackageProjectUrl>
<PackageTags>sourcelink pdb symbols git sourceindexing debugging sourceserver build</PackageTags>
<Description>
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
</Description>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\dotnet-sourcelink\SourceLinkJson.cs" Link="SourceLinkJson.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="LibGit2Sharp.Portable" Version="0.24.10" />
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
</Project>
32 changes: 25 additions & 7 deletions dotnet-sourcelink/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
File renamed without changes.
Loading

0 comments on commit a7c7063

Please sign in to comment.