Exemplo n.º 1
0
 private void Resolver_OnResolving(object sender, NugetAssemblyResolvingEventArgs e)
 {
     // try globs instead
     {
         var assemblyDefinition = GetModificationAssemblies().FirstOrDefault(x => x.Assembly.Name.Name == e.Name);
         if (assemblyDefinition?.Assembly != null)
         {
             e.FileLocation = assemblyDefinition.Path;
             return;
         }
     }
 }
        /// <summary>
        /// Resolves a package from nuget using a specified name and version.
        /// </summary>
        /// <param name="name">Name of the package to find</param>
        /// <param name="version">Version of the package</param>
        /// <returns>File path if the nuget package is found</returns>
        protected string ResolvePackage(string name, Version version)
        {
            var args = new NugetAssemblyResolvingEventArgs()
            {
                Name    = name,
                Version = version
            };

            OnResolving?.Invoke(this, args);

            if (!String.IsNullOrWhiteSpace(args.FileLocation))
            {
                return(args.FileLocation);
            }

            // search locally
            foreach (var file in Directory.EnumerateFiles(Environment.CurrentDirectory, name + ".*"))
            {
                var extension = Path.GetExtension(file).ToLower();
                if (new[] { ".exe", ".dll" }.Contains(extension))
                {
                    Console.WriteLine($" * Found {name} locally");
                    return(file);
                }
            }

            // remote search
            SemanticVersion vers    = SemanticVersion.ParseOptionalVersion($"{version.Major}.{version.Minor}.*");
            IPackage        package = ResolvePackage(name, vers);

            if (package != null)
            {
                string installPath = packageManager.PathResolver.GetInstallPath(package);
                string libPath     = Path.Combine(installPath, "lib", "net45", name + ".dll");

                if (File.Exists(libPath))
                {
                    OnResolved?.Invoke(this, new NugetAssemblyResolvedEventArgs()
                    {
                        FilePath = libPath
                    });
                    return(libPath);
                }
                else
                {
                    var files = Directory.EnumerateFiles(installPath, "*.dll", SearchOption.AllDirectories);
                    if (files.Count() == 1)
                    {
                        libPath = files.Single();
                        OnResolved?.Invoke(this, new NugetAssemblyResolvedEventArgs()
                        {
                            FilePath = libPath
                        });
                        return(libPath);
                    }
                }
            }
            //else
            //{
            //}

            return(null);
        }