public async Task RestorePackages( BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { DotNetProject projectToReload = GetProjectToReloadAfterRestore(project); var changedLock = await RestorePackagesInternal(project, cancellationToken); if (projectToReload != null) { // Need to ensure transitive project references are refreshed if only the single // project is reloaded since they will still be out of date. await ReloadProject(projectToReload, changedLock, refreshTransitiveReferences : true); } else if (changedLock != null) { LockFileChanged = true; await Runtime.RunInMainThread(() => { FileService.NotifyFileChanged(changedLock); // Restoring a single project so ensure references are refreshed for // transitive project references. NotifyProjectReferencesChanged(project, includeTransitiveProjectReferences: true); }); } }
public void TestBuildIntegratedNuGetPackageSpecNameMatchesFilePath() { // Arrange var packageIdentity = new PackageIdentity("packageA", new NuGetVersion("1.0.0")); using (var randomTestPackageSourcePath = TestFileSystemUtility.CreateRandomTestFolder()) using (var randomPackagesFolderPath = TestFileSystemUtility.CreateRandomTestFolder()) using (var randomProjectFolderPath = TestFileSystemUtility.CreateRandomTestFolder()) { var randomConfig = Path.Combine(randomProjectFolderPath, "project.json"); var token = CancellationToken.None; CreateConfigJson(randomConfig); var projectTargetFramework = NuGetFramework.Parse("netcore50"); var testNuGetProjectContext = new TestNuGetProjectContext(); var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem( projectTargetFramework, testNuGetProjectContext, randomProjectFolderPath, "msbuildName"); var projectFilePath = Path.Combine(randomProjectFolderPath, "fileName.csproj"); var buildIntegratedProject = new BuildIntegratedNuGetProject(randomConfig, projectFilePath, msBuildNuGetProjectSystem); // Assert Assert.Equal(projectFilePath, buildIntegratedProject.MSBuildProjectPath); Assert.Equal("fileName", buildIntegratedProject.ProjectName); Assert.Equal("fileName", buildIntegratedProject.PackageSpec.Name); } }
public static IReadOnlyList <PackageIdentity> GetOrderedProjectDependencies( BuildIntegratedNuGetProject buildIntegratedProject) { var results = new List <PackageIdentity>(); var lockFilePath = ProjectJsonPathUtilities.GetLockFilePath(buildIntegratedProject.JsonConfigPath); var lockFileFormat = new LockFileFormat(); // Read the lock file to find the full closure of dependencies if (File.Exists(lockFilePath)) { var lockFile = lockFileFormat.Read(lockFilePath); var dependencies = new HashSet <PackageDependencyInfo>(PackageIdentity.Comparer); foreach (var target in lockFile.Targets) { foreach (var targetLibrary in target.Libraries) { var identity = new PackageIdentity(targetLibrary.Name, targetLibrary.Version); var dependency = new PackageDependencyInfo(identity, targetLibrary.Dependencies); dependencies.Add(dependency); } } // Sort dependencies var sortedDependencies = SortPackagesByDependencyOrder(dependencies); results.AddRange(sortedDependencies); } return(results); }
/// <summary> /// Restore a build integrated project and update the lock file /// </summary> public static async Task <RestoreResult> RestoreAsync( BuildIntegratedNuGetProject project, ExternalProjectReferenceContext context, IEnumerable <SourceRepository> sources, string effectiveGlobalPackagesFolder, Action <SourceCacheContext> cacheContextModifier, CancellationToken token) { using (var cacheContext = new SourceCacheContext()) { cacheContextModifier(cacheContext); var providers = RestoreCommandProviders.Create(effectiveGlobalPackagesFolder, sources, cacheContext, context.Logger); // Restore var result = await RestoreAsync( project, project.PackageSpec, context, providers, token); // Throw before writing if this has been canceled token.ThrowIfCancellationRequested(); // Write out the lock file and msbuild files await result.CommitAsync(context.Logger, token); return(result); } }
//returns the lock file, if it changed async Task <string> RestorePackagesInternal( BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { var now = DateTime.UtcNow; Action <SourceCacheContext> cacheContextModifier = c => c.MaxAge = now; var spec = await MonoDevelopDependencyGraphRestoreUtility.GetSolutionRestoreSpec(solutionManager, project, context, cancellationToken); context.AddToCache(spec); RestoreResult restoreResult = await DependencyGraphRestoreUtility.RestoreProjectAsync( solutionManager, project, context, new RestoreCommandProvidersCache(), cacheContextModifier, sourceRepositories, Guid.NewGuid(), context.Logger, cancellationToken); if (restoreResult.Success) { if (!object.Equals(restoreResult.LockFile, restoreResult.PreviousLockFile)) { return(restoreResult.LockFilePath); } } else { ReportRestoreError(restoreResult); } return(null); }
public static async Task ExecuteInitPs1ScriptsAsync( BuildIntegratedNuGetProject project, IEnumerable <PackageIdentity> packages, FallbackPackagePathResolver pathResolver, INuGetProjectContext projectContext) { // Find all dependencies in sorted order var sortedPackages = await BuildIntegratedProjectUtility.GetOrderedProjectPackageDependencies(project); // Keep track of the packages that need to be executed. var packagesToExecute = new HashSet <PackageIdentity>(packages, PackageIdentity.Comparer); // Use the ordered packages to run init.ps1 for the specified packages. foreach (var package in sortedPackages) { if (packagesToExecute.Remove(package)) { var packageInstallPath = pathResolver.GetPackageDirectory(package.Id, package.Version); if (packageInstallPath == null) { continue; } await project.ExecuteInitScriptAsync( package, packageInstallPath, projectContext, throwOnFailure : false); } } }
//returns the lock file, if it changed async Task <string> RestorePackagesInternal( BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { var nugetPaths = NuGetPathContext.Create(settings); RestoreResult restoreResult = await BuildIntegratedRestoreUtility.RestoreAsync( project, context, sourceRepositories, nugetPaths.UserPackageFolder, nugetPaths.FallbackPackageFolders, cancellationToken); if (restoreResult.Success) { if (!object.Equals(restoreResult.LockFile, restoreResult.PreviousLockFile)) { return(restoreResult.LockFilePath); } } else { ReportRestoreError(restoreResult); } return(null); }
/// <summary> /// Restore without writing the lock file /// </summary> internal static Task <RestoreResultPair> PreviewRestoreAsync( ISolutionManager solutionManager, BuildIntegratedNuGetProject project, PackageSpec packageSpec, DependencyGraphCacheContext context, RestoreCommandProvidersCache providerCache, Action <SourceCacheContext> cacheContextModifier, IEnumerable <SourceRepository> sources, ISettings settings, ILogger log, CancellationToken token) { return(PreviewRestoreAsync( solutionManager, project, packageSpec, context, providerCache, cacheContextModifier, sources, userPackagesPath: null, settings: settings, log: log, token: token)); }
public Task RestorePackages( BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { ProjectRestored = project; return(Task.FromResult(0)); }
public NuGetProject AddBuildIntegratedProject(string projectName = null, NuGetFramework projectTargetFramework = null) { if (GetNuGetProject(projectName) != null) { throw new ArgumentException("Project with " + projectName + " already exists"); } var packagesFolder = Path.Combine(SolutionDirectory, PackagesFolder); projectName = string.IsNullOrEmpty(projectName) ? Guid.NewGuid().ToString() : projectName; var projectFullPath = Path.Combine(SolutionDirectory, projectName); Directory.CreateDirectory(projectFullPath); var projectJsonPath = Path.Combine(projectFullPath, "project.json"); CreateConfigJson(projectJsonPath); projectTargetFramework = projectTargetFramework ?? NuGetFramework.Parse("net46"); var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(projectTargetFramework, new TestNuGetProjectContext(), projectFullPath, projectName); var projectFilePath = Path.Combine(projectFullPath, $"{msBuildNuGetProjectSystem.ProjectName}.csproj"); NuGetProject nuGetProject = new BuildIntegratedNuGetProject(projectJsonPath, projectFilePath, msBuildNuGetProjectSystem); NuGetProjects.Add(nuGetProject); return(nuGetProject); }
public Task <bool> IsRestoreRequired(BuildIntegratedNuGetProject project) { var pathResolver = new VersionFolderPathResolver(packagesFolder); var projects = new BuildIntegratedNuGetProject[] { project }; return(BuildIntegratedRestoreUtility.IsRestoreRequired(projects, pathResolver, context)); }
public static async Task MigrateAsync( BuildIntegratedNuGetProject project) { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var dteProjectFullName = project.MSBuildProjectPath; var projectJsonFilePath = ProjectJsonPathUtilities.GetProjectConfigPath(Path.GetDirectoryName(project.MSBuildProjectPath), Path.GetFileNameWithoutExtension(project.MSBuildProjectPath)); if (!File.Exists(projectJsonFilePath)) { throw new FileNotFoundException(string.Format(Strings.Error_FileNotExists, projectJsonFilePath)); } var packageSpec = JsonPackageSpecReader.GetPackageSpec( Path.GetFileNameWithoutExtension(project.MSBuildProjectPath), projectJsonFilePath); if (packageSpec == null) { throw new InvalidOperationException( string.Format(Strings.Error_InvalidJson, projectJsonFilePath)); } await MigrateDependenciesAsync(project, packageSpec); var buildProject = EnvDTEProjectUtility.AsMSBuildEvaluationProject(dteProjectFullName); MigrateRuntimes(packageSpec, buildProject); RemoveProjectJsonReference(buildProject, projectJsonFilePath); await CreateBackupAsync(project, projectJsonFilePath); }
private static async Task MigrateDependenciesAsync(BuildIntegratedNuGetProject project, PackageSpec packageSpec) { ThreadHelper.ThrowIfNotOnUIThread(); if (packageSpec.TargetFrameworks.Count > 1) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Strings.Error_MultipleFrameworks, project.MSBuildProjectPath)); } var dependencies = new List <LibraryDependency>(); foreach (var targetFramework in packageSpec.TargetFrameworks) { dependencies.AddRange(targetFramework.Dependencies); } dependencies.AddRange(packageSpec.Dependencies); foreach (var dependency in dependencies) { await project.ProjectServices.References.AddOrUpdatePackageReferenceAsync(dependency, CancellationToken.None); } }
/// <summary> /// Find the list of parent projects which directly or indirectly reference the child project. /// </summary> public static async Task <IReadOnlyList <BuildIntegratedNuGetProject> > GetParentProjectsInClosure( ISolutionManager solutionManager, BuildIntegratedNuGetProject target, ExternalProjectReferenceContext referenceContext) { var projects = solutionManager.GetNuGetProjects().OfType <BuildIntegratedNuGetProject>().ToList(); return(await GetParentProjectsInClosure(projects, target, referenceContext)); }
static void NotifyProjectReferencesChanged(BuildIntegratedNuGetProject project) { var buildIntegratedProject = project as IBuildIntegratedNuGetProject; if (buildIntegratedProject != null) { buildIntegratedProject.NotifyProjectReferencesChanged(); } }
static void NotifyProjectReferencesChanged(BuildIntegratedNuGetProject project) { var bips = project as BuildIntegratedProjectSystem; if (bips != null) { bips.Project.RefreshProjectBuilder(); bips.Project.DotNetProject.NotifyModified("References"); } }
async Task CollectPackagesForBuildIntegratedProjectAsync( BuildIntegratedNuGetProject project, ISet <PackageIdentity> finishedPackages, IList <PackageItem> installedPackages, CancellationToken token) { token.ThrowIfCancellationRequested(); var lockFile = await getLockFileOrNullAsync(project); if (lockFile == null) { return; } if (lockFile.Libraries == null || lockFile.Libraries.Count == 0) { return; } FallbackPackagePathResolver fppr; if ((lockFile?.PackageFolders?.Count ?? 0) != 0) { // The user packages folder is always the first package folder. Subsequent package folders are always // fallback package folders. var packageFolders = lockFile .PackageFolders .Select(lockFileItem => lockFileItem.Path); var userPackageFolder = packageFolders.First(); var fallbackPackageFolders = packageFolders.Skip(1); fppr = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders); } else { var pathContext = NuGetPathContext.Create(settings); fppr = new FallbackPackagePathResolver(pathContext); } foreach (var package in BuildIntegratedProjectUtility.GetOrderedLockFilePackageDependencies(lockFile)) { if (!finishedPackages.Contains(package)) { var installPath = fppr.GetPackageDirectory(package.Id, package.Version); if (!string.IsNullOrEmpty(installPath)) { installedPackages.Add(new PackageItem(package, installPath)); finishedPackages.Add(package); } } } }
DotNetProject GetProjectToReloadAfterRestore(BuildIntegratedNuGetProject project) { var dotNetCoreNuGetProject = project as DotNetCoreNuGetProject; if (dotNetCoreNuGetProject?.ProjectRequiresReloadAfterRestore() == true) { return(dotNetCoreNuGetProject.DotNetProject); } return(null); }
public RestoreNuGetPackagesInNuGetIntegratedProject( DotNetProject project, BuildIntegratedNuGetProject nugetProject, IMonoDevelopSolutionManager solutionManager) { this.project = project; this.nugetProject = nugetProject; packageManagementEvents = PackageManagementServices.PackageManagementEvents; packageRestorer = new MonoDevelopBuildIntegratedRestorer(solutionManager); }
public RestoreNuGetPackagesInDotNetCoreProject(DotNetProject project) { this.project = project; packageManagementEvents = PackageManagementServices.PackageManagementEvents; var solutionManager = PackageManagementServices.Workspace.GetSolutionManager(project.ParentSolution); nugetProject = solutionManager.GetNuGetProject(new DotNetProjectProxy(project)) as BuildIntegratedNuGetProject; packageRestorer = new MonoDevelopBuildIntegratedRestorer(solutionManager); }
/// <summary> /// Read lock file /// </summary> public static async Task <LockFile> GetLockFileOrNull(BuildIntegratedNuGetProject buildIntegratedProject) { var lockFilePath = await buildIntegratedProject.GetAssetsFilePathOrNullAsync(); if (lockFilePath == null) { return(null); } return(GetLockFileOrNull(lockFilePath)); }
static void NotifyProjectReferencesChanged( BuildIntegratedNuGetProject project, bool includeTransitiveProjectReferences) { var buildIntegratedProject = project as IBuildIntegratedNuGetProject; if (buildIntegratedProject != null) { buildIntegratedProject.NotifyProjectReferencesChanged(includeTransitiveProjectReferences); } }
public async Task RestoreAsync(BuildIntegratedNuGetProject project) { try { var projectDirectory = Path.GetDirectoryName(project.MSBuildProjectPath); var result = await Cmder.RunAsync("dotnet", projectDirectory, "restore"); } catch (Exception ex) { Debug.WriteLine(ex); } }
public Task <bool> IsRestoreRequired(BuildIntegratedNuGetProject project) { var nugetPaths = NuGetPathContext.Create(settings); var packageFolderPaths = new List <string>(); packageFolderPaths.Add(nugetPaths.UserPackageFolder); packageFolderPaths.AddRange(nugetPaths.FallbackPackageFolders); var projects = new BuildIntegratedNuGetProject[] { project }; return(BuildIntegratedRestoreUtility.IsRestoreRequired(projects, packageFolderPaths, context)); }
/// <summary> /// Orders all package dependencies in a project. /// Project must be restored. /// </summary> public static async Task <IReadOnlyList <PackageIdentity> > GetOrderedProjectPackageDependencies( BuildIntegratedNuGetProject buildIntegratedProject) { var lockFile = await GetLockFileOrNull(buildIntegratedProject); if (lockFile != null) { return(GetOrderedLockFilePackageDependencies(lockFile)); } return(new List <PackageIdentity>()); }
/// <summary> /// Restore without writing the lock file /// </summary> internal static async Task <RestoreResult> RestoreAsync( BuildIntegratedNuGetProject project, PackageSpec packageSpec, ExternalProjectReferenceContext context, RestoreCommandProviders providers, CancellationToken token) { // Restoring packages var logger = context.Logger; logger.LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.BuildIntegratedPackageRestoreStarted, project.ProjectName)); using (var request = new RestoreRequest(packageSpec, providers, logger, disposeProviders: false)) { request.MaxDegreeOfConcurrency = PackageManagementConstants.DefaultMaxDegreeOfParallelism; request.LockFileVersion = await GetLockFileVersion(project, context); // Add the existing lock file if it exists var lockFilePath = ProjectJsonPathUtilities.GetLockFilePath(project.JsonConfigPath); request.LockFilePath = lockFilePath; request.ExistingLockFile = LockFileUtilities.GetLockFile(lockFilePath, logger); // Find the full closure of project.json files and referenced projects var projectReferences = await project.GetProjectReferenceClosureAsync(context); request.ExternalProjects = projectReferences.ToList(); token.ThrowIfCancellationRequested(); var command = new RestoreCommand(request); // Execute the restore var result = await command.ExecuteAsync(token); // Report a final message with the Success result if (result.Success) { logger.LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.BuildIntegratedPackageRestoreSucceeded, project.ProjectName)); } else { logger.LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.BuildIntegratedPackageRestoreFailed, project.ProjectName)); } return(result); } }
private async Task <IVsPathContext> GetPathContextFromAssetsFileAsync( BuildIntegratedNuGetProject buildIntegratedProject, CancellationToken token) { token.ThrowIfCancellationRequested(); var lockFile = await _getLockFileOrNullAsync(buildIntegratedProject); if ((lockFile?.PackageFolders?.Count ?? 0) == 0) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_LockFileError)); } // The user packages folder is always the first package folder. Subsequent package folders are always // fallback package folders. var packageFolders = lockFile .PackageFolders .Select(lockFileItem => lockFileItem.Path) .ToList(); var userPackageFolder = packageFolders[0]; var fallbackPackageFolders = packageFolders.Skip(1); if (lockFile.Libraries == null || lockFile.Libraries.Count == 0) { return(new VsPathContext(userPackageFolder, fallbackPackageFolders, _telemetryProvider)); } var fppr = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders); var trie = new PathLookupTrie <string>(); foreach (var pid in lockFile .Libraries .Where(l => l.Type == LibraryType.Package) .Select(l => new PackageIdentity(l.Name, l.Version))) { var packageInstallPath = fppr.GetPackageDirectory(pid.Id, pid.Version); if (string.IsNullOrEmpty(packageInstallPath)) { throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid)); } trie[packageInstallPath] = packageInstallPath; } return(new VsIndexedPathContext( userPackageFolder, fallbackPackageFolders, trie, _telemetryProvider)); }
public Task <bool> IsRestoreRequired(BuildIntegratedNuGetProject project) { var pathContext = NuGetPathContext.Create(settings); var packageFolderPaths = new List <string> (); packageFolderPaths.Add(pathContext.UserPackageFolder); packageFolderPaths.AddRange(pathContext.FallbackPackageFolders); var pathResolvers = packageFolderPaths.Select(path => new VersionFolderPathResolver(path)); var packagesChecked = new HashSet <PackageIdentity> (); return(project.IsRestoreRequired(pathResolvers, packagesChecked, context)); }
public RestoreNuGetPackagesInNuGetIntegratedProject ( DotNetProject project, BuildIntegratedNuGetProject nugetProject, IMonoDevelopSolutionManager solutionManager) { this.project = project; this.nugetProject = nugetProject; packageManagementEvents = PackageManagementServices.PackageManagementEvents; packageRestorer = new MonoDevelopBuildIntegratedRestorer ( solutionManager.CreateSourceRepositoryProvider (), solutionManager.Settings); }
public RestoreNuGetPackagesInNuGetIntegratedProject( DotNetProject project, BuildIntegratedNuGetProject nugetProject, IMonoDevelopSolutionManager solutionManager) { this.project = project; this.nugetProject = nugetProject; packageManagementEvents = PackageManagementServices.PackageManagementEvents; packageRestorer = new MonoDevelopBuildIntegratedRestorer( solutionManager.CreateSourceRepositoryProvider(), solutionManager.Settings); }
public RestoreNuGetPackagesInNuGetIntegratedProject( DotNetProject project, BuildIntegratedNuGetProject nugetProject, IMonoDevelopSolutionManager solutionManager, bool restoreTransitiveProjectReferences = false) : this( new DotNetProjectProxy(project), nugetProject, solutionManager, new MonoDevelopBuildIntegratedRestorer(solutionManager), restoreTransitiveProjectReferences) { }
public async Task RestorePackages ( BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { var changedLock = await RestorePackagesInternal (project, cancellationToken); if (changedLock != null) { await Runtime.RunInMainThread (() => { FileService.NotifyFileChanged (changedLock); NotifyProjectReferencesChanged (project); }); } }
public Task<bool> IsRestoreRequired (BuildIntegratedNuGetProject project) { var nugetPaths = NuGetPathContext.Create (settings); var packageFolderPaths = new List<string>(); packageFolderPaths.Add (nugetPaths.UserPackageFolder); packageFolderPaths.AddRange (nugetPaths.FallbackPackageFolders); var projects = new BuildIntegratedNuGetProject[] { project }; return BuildIntegratedRestoreUtility.IsRestoreRequired (projects, packageFolderPaths, context); }
static void NotifyProjectReferencesChanged (BuildIntegratedNuGetProject project) { var bips = project as BuildIntegratedProjectSystem; if (bips != null) { bips.Project.RefreshProjectBuilder (); bips.Project.DotNetProject.NotifyModified ("References"); } }
//returns the lock file, if it changed async Task<string> RestorePackagesInternal ( BuildIntegratedNuGetProject project, CancellationToken cancellationToken) { var nugetPaths = NuGetPathContext.Create (settings); RestoreResult restoreResult = await BuildIntegratedRestoreUtility.RestoreAsync ( project, context, sourceRepositories, nugetPaths.UserPackageFolder, nugetPaths.FallbackPackageFolders, cancellationToken); if (restoreResult.Success) { if (!object.Equals (restoreResult.LockFile, restoreResult.PreviousLockFile)) { return restoreResult.LockFilePath; } } else { ReportRestoreError (restoreResult); } return null; }