Example #1
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");
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// This method initializes some of the common methods or classes that will be used in installing the packages.
        /// </summary>
        /// <param name="packageSource">The package source of the package ids to be installed</param>
        private void Init(string packageSource)
        {
            IEnumerable <Lazy <INuGetResourceProvider> > providers = Repository.Provider.GetCoreV3();
            PackageSource pSource = new PackageSource(packageSource);

            this.SourceRepository = new SourceRepository(pSource, providers);

            //path to packages folder. Create the folder if it does not exist and download the packages there.
            if (ProjectHelper.CheckIfSolutionAndProjectFilesAreInSameFolder(this.project.DirectoryPath))
            {
                this.RootPath = Directory.CreateDirectory(this.project.DirectoryPath + "/packages").FullName;
            }
            else
            {
                this.RootPath = Directory.CreateDirectory(Directory.GetParent(this.project.DirectoryPath).FullName + "/packages").FullName;
            }

            this.DefaultSettings = Settings.LoadDefaultSettings(this.RootPath);
            PackageSourceProvider packageSourceProvider = new PackageSourceProvider(this.DefaultSettings);

            this.SourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, providers);
        }