コード例 #1
0
ファイル: PackageExtensions.cs プロジェクト: ymotton/KRuntime
 /// <summary>
 /// Returns true if a package has no content that applies to a project.
 /// </summary>
 public static bool HasProjectContent(this IPackage package)
 {
     // Note that while checking for both AssemblyReferences and LibFiles seems redundant,
     // there are tests that directly inject items into the AssemblyReferences collection
     // without having those files represented in the Lib folder.  We keep the redundant
     // check to play it on the safe side.
     return package.FrameworkAssemblies.Any() ||
            package.AssemblyReferences.Any() ||
            package.GetContentFiles().Any() ||
            package.GetLibFiles().Any() ||
            package.GetBuildFiles().Any();
 }
コード例 #2
0
        private void RemovePackageReferenceFromProject(IPackage package)
        {
            if (IsNuGetAwareProject())
            {
                RemovePackageReferenceFromNuGetAwareProject(package);
                return;
            }

            string packageFullName = package.GetFullName();

            Logger.Log(MessageLevel.Info, NuGetResources.Log_BeginRemovePackageReference, packageFullName, Project.ProjectName);

            PackageOperationEventArgs args = CreateOperation(package);

            OnPackageReferenceRemoving(args);

            if (args.Cancel)
            {
                return;
            }


            // Get other packages
            IEnumerable <IPackage> otherPackages = from p in LocalRepository.GetPackages()
                                                   where p.Id != package.Id
                                                   select p;

            // Get other references
            var otherAssemblyReferences = from p in otherPackages
                                          let assemblyReferences = GetFilteredAssembliesToDelete(p)
                                                                   from assemblyReference in assemblyReferences ?? Enumerable.Empty <IPackageAssemblyReference>() // This can happen if package installed left the project in a bad state
                                                                   select assemblyReference;

            // Get content files from other packages
            // Exclude transform files since they are treated specially
            var otherContentFiles = from p in otherPackages
                                    from file in GetCompatibleInstalledItemsForPackage(p.Id, p.GetContentFiles(), NetPortableProfileTable.Default)
                                    where !IsTransformFile(file.Path)
                                    select file;

            // Get the files and references for this package, that aren't in use by any other packages so we don't have to do reference counting
            var assemblyReferencesToDelete = GetFilteredAssembliesToDelete(package)
                                             .Except(otherAssemblyReferences, PackageFileComparer.Default);

            var contentFilesToDelete = GetCompatibleInstalledItemsForPackage(package.Id, package.GetContentFiles(), NetPortableProfileTable.Default)
                                       .Except(otherContentFiles, PackageFileComparer.Default);

            var buildFilesToDelete = GetCompatibleInstalledItemsForPackage(package.Id, package.GetBuildFiles(), NetPortableProfileTable.Default);

            // Delete the content files
            Project.DeleteFiles(contentFilesToDelete, otherPackages, _fileTransformers);

            // Remove references
            foreach (IPackageAssemblyReference assemblyReference in assemblyReferencesToDelete)
            {
                Project.RemoveReference(assemblyReference.Name);
            }

            // remove the <Import> statement from projects for the .targets and .props files
            foreach (var importFile in buildFilesToDelete)
            {
                string fullImportFilePath = Path.Combine(PathResolver.GetInstallPath(package), importFile.Path);
                Project.RemoveImport(fullImportFilePath);
            }

            // Remove package to the repository
            LocalRepository.RemovePackage(package);


            Logger.Log(MessageLevel.Info, NuGetResources.Log_SuccessfullyRemovedPackageReference, packageFullName, Project.ProjectName);
            OnPackageReferenceRemoved(args);
        }
コード例 #3
0
        protected virtual void ExtractPackageFilesToProject(IPackage package)
        {
            // BUG 491: Installing a package with incompatible binaries still does a partial install.
            // Resolve assembly references and content files first so that if this fails we never do anything to the project
            List <IPackageAssemblyReference>  assemblyReferences  = Project.GetCompatibleItemsCore(package.AssemblyReferences).ToList();
            List <FrameworkAssemblyReference> frameworkReferences = Project.GetCompatibleItemsCore(package.FrameworkAssemblies).ToList();
            List <IPackageFile> contentFiles = Project.GetCompatibleItemsCore(package.GetContentFiles()).ToList();
            List <IPackageFile> buildFiles   = Project.GetCompatibleItemsCore(package.GetBuildFiles()).ToList();

            // If the package doesn't have any compatible assembly references or content files,
            // throw, unless it's a meta package.
            if (assemblyReferences.Count == 0 && frameworkReferences.Count == 0 && contentFiles.Count == 0 && buildFiles.Count == 0 &&
                (package.FrameworkAssemblies.Any() || package.AssemblyReferences.Any() || package.GetContentFiles().Any() || package.GetBuildFiles().Any()))
            {
                // for portable framework, we want to show the friendly short form (e.g. portable-win8+net45+wp8) instead of ".NETPortable, Profile=Profile104".
                FrameworkName targetFramework       = Project.TargetFramework;
                string        targetFrameworkString = targetFramework.IsPortableFramework()
                                                    ? VersionUtility.GetShortFrameworkName(targetFramework)
                                                    : targetFramework != null?targetFramework.ToString() : null;

                throw new InvalidOperationException(
                          String.Format(CultureInfo.CurrentCulture,
                                        NuGetResources.UnableToFindCompatibleItems, package.GetFullName(), targetFrameworkString));
            }

            // IMPORTANT: this filtering has to be done AFTER the 'if' statement above,
            // so that we don't throw the exception in case the <References> filters out all assemblies.
            FilterAssemblyReferences(assemblyReferences, package.PackageAssemblyReferences);

            try
            {
                // Log target framework info for debugging
                LogTargetFrameworkInfo(package, assemblyReferences, contentFiles, buildFiles);

                // Add content files
                Project.AddFiles(contentFiles, _fileTransformers);

                // Add the references to the reference path
                foreach (IPackageAssemblyReference assemblyReference in assemblyReferences)
                {
                    if (assemblyReference.IsEmptyFolder())
                    {
                        continue;
                    }

                    // Get the physical path of the assembly reference
                    string referencePath         = Path.Combine(PathResolver.GetInstallPath(package), assemblyReference.Path);
                    string relativeReferencePath = PathUtility.GetRelativePath(Project.Root, referencePath);

                    if (Project.ReferenceExists(assemblyReference.Name))
                    {
                        Project.RemoveReference(assemblyReference.Name);
                    }

                    Project.AddReference(relativeReferencePath);
                }

                // Add GAC/Framework references
                foreach (FrameworkAssemblyReference frameworkReference in frameworkReferences)
                {
                    if (!Project.ReferenceExists(frameworkReference.AssemblyName))
                    {
                        Project.AddFrameworkReference(frameworkReference.AssemblyName);
                    }
                }

                foreach (var importFile in buildFiles)
                {
                    string fullImportFilePath = Path.Combine(PathResolver.GetInstallPath(package), importFile.Path);
                    Project.AddImport(
                        fullImportFilePath,
                        importFile.Path.EndsWith(".props", StringComparison.OrdinalIgnoreCase) ? ProjectImportLocation.Top : ProjectImportLocation.Bottom);
                }
            }
            finally
            {
                if (_packageReferenceRepository != null)
                {
                    // save the used project's framework if the repository supports it.
                    _packageReferenceRepository.AddPackage(package.Id, package.Version, package.DevelopmentDependency, Project.TargetFramework);
                }
                else
                {
                    // Add package to local repository in the finally so that the user can uninstall it
                    // if any exception occurs. This is easier than rolling back since the user can just
                    // manually uninstall things that may have failed.
                    // If this fails then the user is out of luck.
                    LocalRepository.AddPackage(package);
                }
            }
        }
コード例 #4
0
 bool PackageHasMSBuildTargetsFile(IPackage package)
 {
     return(package.GetBuildFiles().Any());
 }
コード例 #5
0
		bool PackageHasMSBuildTargetsFile (IPackage package)
		{
			return package.GetBuildFiles ().Any ();
		}
コード例 #6
0
 public static bool HasProjectContent(this IPackage package) =>
 (package.FrameworkAssemblies.Any <FrameworkAssemblyReference>() || (package.AssemblyReferences.Any <IPackageAssemblyReference>() || (package.GetContentFiles().Any <IPackageFile>() || (package.GetLibFiles().Any <IPackageFile>() || package.GetBuildFiles().Any <IPackageFile>()))));
コード例 #7
0
        protected virtual void ExtractPackageFilesToProject(IPackage package)
        {
            List <IPackageAssemblyReference>  assemblyReferences = this.Project.GetCompatibleItemsCore <IPackageAssemblyReference>(package.AssemblyReferences).ToList <IPackageAssemblyReference>();
            List <FrameworkAssemblyReference> list2 = this.Project.GetCompatibleItemsCore <FrameworkAssemblyReference>(package.FrameworkAssemblies).ToList <FrameworkAssemblyReference>();
            List <IPackageFile> contentFiles        = this.Project.GetCompatibleItemsCore <IPackageFile>(package.GetContentFiles()).ToList <IPackageFile>();
            List <IPackageFile> buildFiles          = this.Project.GetCompatibleItemsCore <IPackageFile>(package.GetBuildFiles()).ToList <IPackageFile>();

            if ((assemblyReferences.Count == 0) && ((list2.Count == 0) && ((contentFiles.Count == 0) && ((buildFiles.Count == 0) && (package.FrameworkAssemblies.Any <FrameworkAssemblyReference>() || (package.AssemblyReferences.Any <IPackageAssemblyReference>() || (package.GetContentFiles().Any <IPackageFile>() || package.GetBuildFiles().Any <IPackageFile>())))))))
            {
                FrameworkName targetFramework = this.Project.TargetFramework;
                string        str             = targetFramework.IsPortableFramework() ? VersionUtility.GetShortFrameworkName(targetFramework) : targetFramework?.ToString();
                object[]      args            = new object[] { package.GetFullName(), str };
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, NuGetResources.UnableToFindCompatibleItems, args));
            }
            this.FilterAssemblyReferences(assemblyReferences, package.PackageAssemblyReferences);
            try
            {
                this.LogTargetFrameworkInfo(package, assemblyReferences, contentFiles, buildFiles);
                this.Project.AddFiles(contentFiles, this._fileTransformers);
                foreach (IPackageAssemblyReference reference in assemblyReferences)
                {
                    if (!reference.IsEmptyFolder())
                    {
                        string str2         = Path.Combine(this.PathResolver.GetInstallPath(package), reference.Path);
                        string relativePath = PathUtility.GetRelativePath(this.Project.Root, str2);
                        if (this.Project.ReferenceExists(reference.Name))
                        {
                            this.Project.RemoveReference(reference.Name);
                        }
                        this.Project.AddReference(relativePath);
                    }
                }
                foreach (FrameworkAssemblyReference reference2 in list2)
                {
                    if (!this.Project.ReferenceExists(reference2.AssemblyName))
                    {
                        this.Project.AddFrameworkReference(reference2.AssemblyName);
                    }
                }
                foreach (IPackageFile file in buildFiles)
                {
                    string targetFullPath = Path.Combine(this.PathResolver.GetInstallPath(package), file.Path);
                    this.Project.AddImport(targetFullPath, file.Path.EndsWith(".props", StringComparison.OrdinalIgnoreCase) ? ProjectImportLocation.Top : ProjectImportLocation.Bottom);
                }
            }
            finally
            {
                if (this._packageReferenceRepository != null)
                {
                    this._packageReferenceRepository.AddPackage(package.Id, package.Version, package.DevelopmentDependency, this.Project.TargetFramework);
                }
                else
                {
                    this.LocalRepository.AddPackage(package);
                }
            }
        }