{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/cli/pulls/comments/47129922","id":47129922,"diff_hunk":"@@ -0,0 +1,217 @@\n+// Copyright (c) .NET Foundation and contributors. All rights reserved.\n+// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n+\n+using System;\n+using System.Collections.Generic;\n+using System.IO;\n+using System.Linq;\n+using System.Reflection.Metadata;\n+using System.Reflection.PortableExecutable;\n+using System.Security.Cryptography;\n+using System.Text;\n+using System.Xml.Linq;\n+using Microsoft.DotNet.ProjectModel.Compilation;\n+\n+namespace Microsoft.DotNet.Tools.Compiler\n+{\n+    internal class BindingRedirectGenerator\n+    {\n+        private const int TokenLength = 8;\n+        private const string Namespace = \"urn:schemas-microsoft-com:asm.v1\";\n+\n+        private static XName ConfigurationElementName = XName.Get(\"configuration\");\n+        private static XName RuntimeElementName = XName.Get(\"runtime\");\n+        private static XName AssemblyBindingElementName = XName.Get(\"assemblyBinding\", Namespace);\n+        private static XName DependentAssemblyElementName = XName.Get(\"dependentAssembly\", Namespace);\n+        private static XName AssemblyIdentityElementName = XName.Get(\"assemblyIdentity\", Namespace);\n+        private static XName BindingRedirectElementName = XName.Get(\"bindingRedirect\", Namespace);\n+\n+        private static XName NameAttributeName = XName.Get(\"name\");\n+        private static XName PublicKeyTokenAttributeName = XName.Get(\"publicKeyToken\");\n+        private static XName CultureAttributeName = XName.Get(\"culture\");\n+        private static XName OldVersionAttributeName = XName.Get(\"oldVersion\");\n+        private static XName NewVersionAttributeName = XName.Get(\"newVersion\");\n+\n+        private readonly SHA1 _sha1 = SHA1.Create();\n+\n+        public XDocument Generate(IEnumerable<LibraryExport> dependencies)\n+        {\n+            var document = new XDocument(\n+                new XElement(ConfigurationElementName,\n+                    new XElement(RuntimeElementName,\n+                        new XElement(AssemblyBindingElementName,\n+                            CollectRedirects(dependencies).Select(GetDependentAssembly)\n+                            )\n+                        )\n+                    )\n+                );\n+            return document;\n+        }\n+\n+        private XElement GetDependentAssembly(AssemblyRedirect redirect)\n+        {\n+            var culture = string.IsNullOrEmpty(redirect.From.Culture) ? \"neutral\" : redirect.From.Culture;\n+\n+            return new XElement(DependentAssemblyElementName,\n+                new XElement(AssemblyIdentityElementName,\n+                    new XAttribute(NameAttributeName, redirect.From.Name),\n+                    new XAttribute(PublicKeyTokenAttributeName, redirect.From.PublicKeyToken),\n+                    new XAttribute(CultureAttributeName, culture)\n+                    ),\n+                new XElement(BindingRedirectElementName,\n+                    new XAttribute(OldVersionAttributeName, redirect.From.Version),\n+                    new XAttribute(NewVersionAttributeName, redirect.To.Version)\n+                    )\n+                );\n+        }\n+\n+        private AssemblyRedirect[] CollectRedirects(IEnumerable<LibraryExport> dependencies)\n+        {\n+            var allRuntimeAssemblies = dependencies.SelectMany(d => d.RuntimeAssemblies).Select(GetAssemblyInfo).ToArray();\n+            var assemblyLookup = allRuntimeAssemblies.ToDictionary(r => r.Identity.ToLookupKey());\n+\n+            var redirectAssemblies = new HashSet<AssemblyRedirect>();\n+            foreach (var assemblyReferenceInfo in allRuntimeAssemblies)\n+            {\n+                foreach (var referenceIdentity in assemblyReferenceInfo.References)\n+                {\n+                    AssemblyReferenceInfo targetAssemblyIdentity;\n+                    if (assemblyLookup.TryGetValue(referenceIdentity.ToLookupKey(), out targetAssemblyIdentity)\n+                        && targetAssemblyIdentity.Identity.Version != referenceIdentity.Version)\n+                    {\n+                        if (targetAssemblyIdentity.Identity.PublicKeyToken != null)\n+                        {\n+                            redirectAssemblies.Add(new AssemblyRedirect()\n+                            {\n+                                From = referenceIdentity,\n+                                To = targetAssemblyIdentity.Identity\n+                            });\n+                        }\n+                    }\n+                }\n+            }\n+\n+            return redirectAssemblies.ToArray();\n+        }\n+\n+        private AssemblyReferenceInfo GetAssemblyInfo(LibraryAsset arg)\n+        {\n+            using (var peReader = new PEReader(File.OpenRead(arg.ResolvedPath)))\n+            {\n+                var metadataReader = peReader.GetMetadataReader();\n+\n+                var definition = metadataReader.GetAssemblyDefinition();\n+\n+                var publicKey = metadataReader.GetBlobBytes(definition.PublicKey);\n+                var publicKeyToken = GetPublicKeyToken(publicKey);\n+                \n+                var identity = new AssemblyIdentity(\n+                    metadataReader.GetString(definition.Name),\n+                    definition.Version,\n+                    publicKeyToken,\n+                    metadataReader.GetString(definition.Culture)\n+                );\n+\n+                var references = new List<AssemblyIdentity>(metadataReader.AssemblyReferences.Count);\n+\n+                foreach (var assemblyReferenceHandle in metadataReader.AssemblyReferences)\n+                {\n+                    var assemblyReference = metadataReader.GetAssemblyReference(assemblyReferenceHandle);\n+                    references.Add(new AssemblyIdentity(\n+                        metadataReader.GetString(assemblyReference.Name),\n+                        assemblyReference.Version,\n+                        GetPublicKeyToken(metadataReader.GetBlobBytes(assemblyReference.PublicKeyOrToken)),\n+                        metadataReader.GetString(assemblyReference.Culture)\n+                    ));\n+                }\n+\n+                return new AssemblyReferenceInfo(identity, references.ToArray());\n+            }\n+        }\n+\n+        private string GetPublicKeyToken(byte[] bytes)\n+        {\n+            if (bytes.Length == 0)\n+            {\n+                return null;\n+            }\n+\n+            byte[] token;\n+            if (bytes.Length == TokenLength)\n+            {\n+                token = bytes;\n+            }\n+            else\n+            {\n+                token = new byte[TokenLength];\n+                var sha1 = _sha1.ComputeHash(bytes);\n+                Array.Copy(sha1, sha1.Length - TokenLength, token, 0, TokenLength);\n+                Array.Reverse(token);\n+            }\n+\n+            StringBuilder hex = new StringBuilder(TokenLength * 2);","path":"src/Microsoft.DotNet.Tools.Compiler/Resources/BindingRedirectGenerator.cs","position":152,"original_position":152,"commit_id":"1fa242148c7b1e5b64ee9b8247d3498d3ce082df","original_commit_id":"1fa242148c7b1e5b64ee9b8247d3498d3ce082df","user":{"login":"davidfowl","id":95136,"avatar_url":"https://avatars.githubusercontent.com/u/95136?v=3","gravatar_id":"","url":"https://api.github.com/users/davidfowl","html_url":"https://github.com/davidfowl","followers_url":"https://api.github.com/users/davidfowl/followers","following_url":"https://api.github.com/users/davidfowl/following{/other_user}","gists_url":"https://api.github.com/users/davidfowl/gists{/gist_id}","starred_url":"https://api.github.com/users/davidfowl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidfowl/subscriptions","organizations_url":"https://api.github.com/users/davidfowl/orgs","repos_url":"https://api.github.com/users/davidfowl/repos","events_url":"https://api.github.com/users/davidfowl/events{/privacy}","received_events_url":"https://api.github.com/users/davidfowl/received_events","type":"User","site_admin":false},"body":"Var","created_at":"2015-12-09T18:24:44Z","updated_at":"2015-12-09T18:24:44Z","html_url":"https://github.com/dotnet/cli/pull/455#discussion_r47129922","pull_request_url":"https://api.github.com/repos/dotnet/cli/pulls/455","_links":{"self":{"href":"https://api.github.com/repos/dotnet/cli/pulls/comments/47129922"},"html":{"href":"https://github.com/dotnet/cli/pull/455#discussion_r47129922"},"pull_request":{"href":"https://api.github.com/repos/dotnet/cli/pulls/455"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/cli/pulls/455","id":53153779,"html_url":"https://github.com/dotnet/cli/pull/455","diff_url":"https://github.com/dotnet/cli/pull/455.diff","patch_url":"https://github.com/dotnet/cli/pull/455.patch","issue_url":"https://api.github.com/repos/dotnet/cli/issues/455","number":455,"state":"open","locked":false,"title":"Generate binding redirect for Desktop CLR ","user":{"login":"pakrym","id":1697911,"avatar_url":"https://avatars.githubusercontent.com/u/1697911?v=3","gravatar_id":"","url":"https://api.github.com/users/pakrym","html_url":"https://github.com/pakrym","followers_url":"https://api.github.com/users/pakrym/followers","following_url":"https://api.github.com/users/pakrym/following{/other_user}","gists_url":"https://api.github.com/users/pakrym/gists{/gist_id}","starred_url":"https://api.github.com/users/pakrym/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pakrym/subscriptions","organizations_url":"https://api.github.com/users/pakrym/orgs","repos_url":"https://api.github.com/users/pakrym/repos","events_url":"https://api.github.com/users/pakrym/events{/privacy}","received_events_url":"https://api.github.com/users/pakrym/received_events","type":"User","site_admin":false},"body":"https://github.com/dotnet/cli/issues/397\r\n\r\n@davidfowl @anurse ","created_at":"2015-12-09T17:49:57Z","updated_at":"2015-12-09T18:24:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e42f882eb348eb6671ed94dbee085b83507f2c0b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/cli/pulls/455/commits","review_comments_url":"https://api.github.com/repos/dotnet/cli/pulls/455/comments","review_comment_url":"https://api.github.com/repos/dotnet/cli/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/cli/issues/455/comments","statuses_url":"https://api.github.com/repos/dotnet/cli/statuses/1fa242148c7b1e5b64ee9b8247d3498d3ce082df","head":{"label":"dotnet:pakrym/bindings","ref":"pakrym/bindings","sha":"1fa242148c7b1e5b64ee9b8247d3498d3ce082df","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":43568107,"name":"cli","full_name":"dotnet/cli","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/cli","description":"This repo contains the .NET Core command-line (CLI) tools, used for building .NET Core apps and libraries through your development flow (compiling, NuGet package management, running, testing, ...).","fork":false,"url":"https://api.github.com/repos/dotnet/cli","forks_url":"https://api.github.com/repos/dotnet/cli/forks","keys_url":"https://api.github.com/repos/dotnet/cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/cli/teams","hooks_url":"https://api.github.com/repos/dotnet/cli/hooks","issue_events_url":"https://api.github.com/repos/dotnet/cli/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/cli/events","assignees_url":"https://api.github.com/repos/dotnet/cli/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/cli/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/cli/tags","blobs_url":"https://api.github.com/repos/dotnet/cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/cli/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/cli/languages","stargazers_url":"https://api.github.com/repos/dotnet/cli/stargazers","contributors_url":"https://api.github.com/repos/dotnet/cli/contributors","subscribers_url":"https://api.github.com/repos/dotnet/cli/subscribers","subscription_url":"https://api.github.com/repos/dotnet/cli/subscription","commits_url":"https://api.github.com/repos/dotnet/cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/cli/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/cli/merges","archive_url":"https://api.github.com/repos/dotnet/cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/cli/downloads","issues_url":"https://api.github.com/repos/dotnet/cli/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/cli/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/cli/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/cli/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/cli/releases{/id}","created_at":"2015-10-02T18:03:15Z","updated_at":"2015-12-09T13:52:04Z","pushed_at":"2015-12-09T18:18:31Z","git_url":"git://github.com/dotnet/cli.git","ssh_url":"git@github.com:dotnet/cli.git","clone_url":"https://github.com/dotnet/cli.git","svn_url":"https://github.com/dotnet/cli","homepage":"http://dotnet.github.io/","size":14369,"stargazers_count":137,"watchers_count":137,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":187,"forks":34,"open_issues":187,"watchers":137,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"1a2601345529650501a98a7f9d99c59a8da1ae59","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":43568107,"name":"cli","full_name":"dotnet/cli","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/cli","description":"This repo contains the .NET Core command-line (CLI) tools, used for building .NET Core apps and libraries through your development flow (compiling, NuGet package management, running, testing, ...).","fork":false,"url":"https://api.github.com/repos/dotnet/cli","forks_url":"https://api.github.com/repos/dotnet/cli/forks","keys_url":"https://api.github.com/repos/dotnet/cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/cli/teams","hooks_url":"https://api.github.com/repos/dotnet/cli/hooks","issue_events_url":"https://api.github.com/repos/dotnet/cli/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/cli/events","assignees_url":"https://api.github.com/repos/dotnet/cli/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/cli/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/cli/tags","blobs_url":"https://api.github.com/repos/dotnet/cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/cli/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/cli/languages","stargazers_url":"https://api.github.com/repos/dotnet/cli/stargazers","contributors_url":"https://api.github.com/repos/dotnet/cli/contributors","subscribers_url":"https://api.github.com/repos/dotnet/cli/subscribers","subscription_url":"https://api.github.com/repos/dotnet/cli/subscription","commits_url":"https://api.github.com/repos/dotnet/cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/cli/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/cli/merges","archive_url":"https://api.github.com/repos/dotnet/cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/cli/downloads","issues_url":"https://api.github.com/repos/dotnet/cli/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/cli/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/cli/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/cli/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/cli/releases{/id}","created_at":"2015-10-02T18:03:15Z","updated_at":"2015-12-09T13:52:04Z","pushed_at":"2015-12-09T18:18:31Z","git_url":"git://github.com/dotnet/cli.git","ssh_url":"git@github.com:dotnet/cli.git","clone_url":"https://github.com/dotnet/cli.git","svn_url":"https://github.com/dotnet/cli","homepage":"http://dotnet.github.io/","size":14369,"stargazers_count":137,"watchers_count":137,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":187,"forks":34,"open_issues":187,"watchers":137,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/cli/pulls/455"},"html":{"href":"https://github.com/dotnet/cli/pull/455"},"issue":{"href":"https://api.github.com/repos/dotnet/cli/issues/455"},"comments":{"href":"https://api.github.com/repos/dotnet/cli/issues/455/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/cli/pulls/455/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/cli/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/cli/pulls/455/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/cli/statuses/1fa242148c7b1e5b64ee9b8247d3498d3ce082df"}}}}