예제 #1
0
파일: Managed.cs 프로젝트: imacgee/Core
        /// <summary>
        /// 使用指定的扩展构建器生成扩展实现
        /// </summary>
        /// <param name="name">扩展名</param>
        /// <returns>扩展实现</returns>
        protected virtual TInterface MakeExtend(string name)
        {
            var extend = GetExtend(name)();

            OnResolving?.Invoke(extend);
            OnAfterResolving?.Invoke(extend);

            return(extend);
        }
        /// <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);
        }