FileProjectItem AddFileToProject(string include)
        {
            var projectItem = new FileProjectItem(projectHelper.Project, ItemType.Compile, include);

            projectHelper.AddProjectItem(projectItem);
            return(projectItem);
        }
示例#2
0
        /// <summary>
        /// This methods updates the project's .csproj file. It adds all the installed packages to the .csproj file.
        /// </summary>
        /// <param name="packageReader">The <see cref="PackageReaderBase"/> to use in reading the nuget package</param>
        /// <param name="installedPath">The folder path where the package was installed or downloaded to</param>
        /// <param name="packageToInstall">The package to install with all its dependencies.</param>
        /// <param name="nuGetFramework">The <see cref="NuGetFramework"/></param>
        private void UpdateProjectFile(
            PackageReaderBase packageReader,
            string installedPath,
            SourcePackageDependencyInfo packageToInstall,
            NuGetFramework nuGetFramework)
        {
            IEnumerable <FrameworkSpecificGroup> dllItems = packageReader.GetLibItems();

            if (dllItems != null)
            {
                FrameworkReducer frameworkReducer   = new FrameworkReducer();
                NuGetFramework   nearest            = frameworkReducer.GetNearest(nuGetFramework, dllItems.Select(x => x.TargetFramework));
                string           targetDllFramework = dllItems
                                                      .Where(x => x.TargetFramework.Equals(nearest))
                                                      .SelectMany(x => x.Items).Where(x => x.Contains(".dll")).FirstOrDefault();

                if (!string.IsNullOrEmpty(targetDllFramework) && !string.IsNullOrEmpty(installedPath))
                {
                    string      targetDllFrameworkPath = Path.Combine(installedPath, targetDllFramework.ToString());
                    string      dllName = Assembly.LoadFile(targetDllFrameworkPath).FullName;
                    string      dllNameWithProcessArchitecture = dllName + "," + " processorArchitecture=MSIL";
                    ProjectItem item = this.project.GetItems("Reference").FirstOrDefault(a => a.EvaluatedInclude.Contains(packageToInstall.Id));
                    if (item == null)
                    {
                        if (ProjectHelper.CheckIfSolutionAndProjectFilesAreInSameFolder(this.project.DirectoryPath))
                        {
                            ProjectItem projectItem = this.project.AddItem("Reference", dllNameWithProcessArchitecture).FirstOrDefault();
                            projectItem.HasMetadata("HintPath");
                            projectItem.SetMetadataValue("HintPath", $"packages\\{packageToInstall.Id}.{ packageToInstall.Version}\\{targetDllFramework.Replace(@"/", "\\")}".Replace(@"\\", "//"));
                        }
                        else
                        {
                            ProjectItem projectItem = this.project.AddItem("Reference", dllNameWithProcessArchitecture).FirstOrDefault();
                            projectItem.SetMetadataValue("HintPath", $"..\\packages\\{packageToInstall.Id}.{ packageToInstall.Version}\\{targetDllFramework.Replace(@"/", "\\")}".Replace(@"\\", "//"));
                        }

                        this.project.Save();
                    }

                    ProjectItem dataAnnotationsRef = this.project.GetItems("Reference").FirstOrDefault(a => a.EvaluatedInclude.Contains("System.ComponentModel.DataAnnotations"));
                    if (dataAnnotationsRef == null)
                    {
                        ProjectHelper.AddProjectItem(this.project, "Reference", "System.ComponentModel.DataAnnotations");
                    }

                    ProjectItem packageConfigRef = this.project.GetItems("None").FirstOrDefault(a => a.EvaluatedInclude.Contains("packages.config"));
                    if (packageConfigRef == null)
                    {
                        ProjectHelper.AddProjectItem(this.project, "None", "packages.config");
                    }
                }
            }
        }