public Manifest ConvertToManifest(ManifestOnDisk manifestOnDisk, IDependencies dependencies)
        {
            if (manifestOnDisk == null)
            {
                return(null);
            }

            var manifest = new Manifest(dependencies)
            {
                Version            = manifestOnDisk.Version,
                DefaultDestination = manifestOnDisk.DefaultDestination,
                DefaultProvider    = manifestOnDisk.DefaultProvider,
            };

            var libraryStateConverter = new LibraryStateToFileConverter(manifest.DefaultProvider, manifest.DefaultDestination);

            if (manifestOnDisk.Libraries != null)
            {
                foreach (LibraryInstallationStateOnDisk lod in manifestOnDisk.Libraries)
                {
                    manifest.AddLibrary(libraryStateConverter.ConvertToLibraryInstallationState(lod));
                }
            }

            return(manifest);
        }
Example #2
0
        public async Task SaveAsync_Success()
        {
            var manifest = new Manifest(_dependencies);

            IProvider provider     = _dependencies.GetProvider("cdnjs");
            var       desiredState = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.min.js" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            Assert.IsTrue(result.Success);

            manifest.AddLibrary(desiredState);
            await manifest.SaveAsync(_filePath, CancellationToken.None).ConfigureAwait(false);

            Manifest newManifest = await Manifest.FromFileAsync(_filePath, _dependencies, CancellationToken.None).ConfigureAwait(false);

            Assert.IsTrue(File.Exists(_filePath));
            Assert.AreEqual(manifest.Libraries.Count(), newManifest.Libraries.Count());
            Assert.AreEqual(manifest.Version, newManifest.Version);
        }
Example #3
0
        public async Task UninstallAsync_Success()
        {
            var manifest            = new Manifest(_dependencies);
            CancellationToken token = CancellationToken.None;

            IProvider provider     = _dependencies.GetProvider("cdnjs");
            var       desiredState = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.js", "jquery.min.js" }
            };

            manifest.AddLibrary(desiredState);
            await manifest.RestoreAsync(token);

            string file1 = Path.Combine(_projectFolder, "lib", "jquery.js");
            string file2 = Path.Combine(_projectFolder, "lib", "jquery.min.js");

            Assert.IsTrue(File.Exists(file1));
            Assert.IsTrue(File.Exists(file2));

            manifest.Uninstall(desiredState.LibraryId, (file) => { _hostInteraction.DeleteFile(file); });

            Assert.IsFalse(File.Exists(file1));
            Assert.IsFalse(File.Exists(file2));
        }
Example #4
0
        public async Task UninstallAsync_Success()
        {
            var manifest            = new Manifest(_dependencies);
            CancellationToken token = CancellationToken.None;

            IProvider provider     = _dependencies.GetProvider("cdnjs");
            var       desiredState = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.js", "jquery.min.js" }
            };

            manifest.AddVersion("1.0");
            manifest.AddLibrary(desiredState);
            IEnumerable <ILibraryOperationResult> results = await manifest.RestoreAsync(token);

            string file1 = Path.Combine(_projectFolder, "lib", "jquery.js");
            string file2 = Path.Combine(_projectFolder, "lib", "jquery.min.js");

            Assert.IsTrue(File.Exists(file1));
            Assert.IsTrue(File.Exists(file2));
            Assert.IsTrue(results.Count() == 1);
            Assert.IsTrue(results.First().Success);

            ILibraryOperationResult uninstallResult = await manifest.UninstallAsync(desiredState.LibraryId, (file) => _hostInteraction.DeleteFilesAsync(file, token), token);

            Assert.IsFalse(File.Exists(file1));
            Assert.IsFalse(File.Exists(file2));
            Assert.IsTrue(results.Count() == 1);
            Assert.IsTrue(results.First().Success);
        }
Example #5
0
        public async Task CleanAsync_Success()
        {
            var manifest            = new Manifest(_dependencies);
            CancellationToken token = CancellationToken.None;

            IProvider provider = _dependencies.GetProvider("cdnjs");
            var       state1   = new LibraryInstallationState
            {
                Name            = "jquery",
                Version         = "3.1.1",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.js", "jquery.min.js" }
            };

            var state2 = new LibraryInstallationState
            {
                Name            = "knockout",
                Version         = "3.4.2",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "knockout-min.js" }
            };

            manifest.AddVersion("1.0");
            manifest.AddLibrary(state1);
            manifest.AddLibrary(state2);
            await manifest.RestoreAsync(token);

            string file1 = Path.Combine(_projectFolder, "lib", "jquery.js");
            string file2 = Path.Combine(_projectFolder, "lib", "jquery.min.js");
            string file3 = Path.Combine(_projectFolder, "lib", "knockout-min.js");

            Assert.IsTrue(File.Exists(file1));
            Assert.IsTrue(File.Exists(file2));
            Assert.IsTrue(File.Exists(file3));

            IEnumerable <ILibraryOperationResult> results = await manifest.CleanAsync((file) => _hostInteraction.DeleteFilesAsync(file, token), token);

            Assert.IsFalse(File.Exists(file1));
            Assert.IsFalse(File.Exists(file2));
            Assert.IsFalse(File.Exists(file3));
            Assert.IsTrue(results.Count() == 2);
            Assert.IsTrue(results.All(r => r.Success));
        }
Example #6
0
        public async Task CleanAsync_Success()
        {
            var manifest            = new Manifest(_dependencies);
            CancellationToken token = CancellationToken.None;

            IProvider provider = _dependencies.GetProvider("cdnjs");
            var       state1   = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.js", "jquery.min.js" }
            };

            var state2 = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "knockout-min.js" }
            };

            manifest.AddVersion("1.0");
            manifest.AddLibrary(state1);
            manifest.AddLibrary(state2);
            await manifest.RestoreAsync(token);

            string file1 = Path.Combine(_projectFolder, "lib", "jquery.js");
            string file2 = Path.Combine(_projectFolder, "lib", "jquery.min.js");
            string file3 = Path.Combine(_projectFolder, "lib", "knockout-min.js");

            Assert.IsTrue(File.Exists(file1));
            Assert.IsTrue(File.Exists(file2));
            Assert.IsTrue(File.Exists(file3));

            manifest.Clean((file) => { _hostInteraction.DeleteFile(file); });

            Assert.IsFalse(File.Exists(file1));
            Assert.IsFalse(File.Exists(file2));
            Assert.IsFalse(File.Exists(file3));
        }
        private async void InstallPackageAsync()
        {
            try
            {
                ILibrary selectedPackage = SelectedPackage;
                _isInstalling = true;
                InstallPackageCommand.CanExecute(null);
                Manifest manifest = await Manifest.FromFileAsync(_configFileName, _deps, CancellationToken.None).ConfigureAwait(false);

                string targetPath = _targetPath;

                if (!string.IsNullOrEmpty(_configFileName))
                {
                    Uri configContainerUri = new Uri(_configFileName, UriKind.Absolute);
                    Uri targetUri          = new Uri(targetPath, UriKind.Absolute);
                    targetPath = configContainerUri.MakeRelativeUri(targetUri).ToString();
                }

                manifest.AddLibrary(new LibraryInstallationState
                {
                    LibraryId       = PackageId,
                    ProviderId      = selectedPackage.ProviderId,
                    DestinationPath = targetPath,
                    Files           = SelectedFiles.ToList()
                });

                await manifest.SaveAsync(_configFileName, CancellationToken.None).ConfigureAwait(false);

                EnvDTE.Project project = VsHelpers.DTE.SelectedItems.Item(1)?.ProjectItem?.ContainingProject;
                project?.AddFileToProject(_configFileName);

                await LibraryHelpers.RestoreAsync(_configFileName).ConfigureAwait(false);

                _dispatcher.Invoke(() =>
                {
                    _closeDialog(true);
                });
            }
            catch { }
        }
Example #8
0
        private async void ExecuteAsync(object sender, EventArgs e)
        {
            Telemetry.TrackUserTask("installdialogopened");

            CancellationToken token      = CancellationToken.None;
            Project           project    = VsHelpers.DTE.SelectedItems.Item(1).ProjectItem.ContainingProject;
            string            rootFolder = project.GetRootFolder();

            string   configFilePath = Path.Combine(rootFolder, Constants.ConfigFileName);
            var      dependencies   = Dependencies.FromConfigFile(configFilePath);
            Manifest manifest       = await Manifest.FromFileAsync(configFilePath, dependencies, token);

            string itemFolder               = VsHelpers.DTE.SelectedItems.Item(1).ProjectItem.Properties.Item("FullPath").Value.ToString();
            string relativeFolder           = PackageUtilities.MakeRelative(rootFolder, itemFolder).Replace(Path.DirectorySeparatorChar, '/').Trim('/');
            ILibraryInstallationState state = GetLibraryToInstall(relativeFolder);

            manifest.AddLibrary(state);
            await manifest.SaveAsync(configFilePath, token);

            project.AddFileToProject(configFilePath);
            await LibraryHelpers.RestoreAsync(configFilePath);
        }
Example #9
0
        private async Task InstallPackageAsync()
        {
            try
            {
                bool isLibraryInstallationStateValid = await IsLibraryInstallationStateValidAsync().ConfigureAwait(false);

                if (isLibraryInstallationStateValid)
                {
                    ILibrary selectedPackage = SelectedPackage;
                    InstallPackageCommand.CanExecute(null);
                    Manifest manifest = await Manifest.FromFileAsync(_configFileName, _deps, CancellationToken.None).ConfigureAwait(false);

                    string targetPath = _targetPath;

                    if (!string.IsNullOrEmpty(_configFileName))
                    {
                        Uri configContainerUri = new Uri(_configFileName, UriKind.Absolute);
                        Uri targetUri          = new Uri(targetPath, UriKind.Absolute);
                        targetPath = configContainerUri.MakeRelativeUri(targetUri).ToString();
                    }

                    if (String.IsNullOrEmpty(manifest.Version))
                    {
                        manifest.Version = Manifest.SupportedVersions.Max().ToString();
                    }

                    (string name, string version) = LibraryIdToNameAndVersionConverter.Instance.GetLibraryNameAndVersion(PackageId, SelectedProvider.Id);
                    LibraryInstallationState libraryInstallationState = new LibraryInstallationState
                    {
                        Name            = name,
                        Version         = version,
                        ProviderId      = selectedPackage.ProviderId,
                        DestinationPath = InstallationFolder.DestinationFolder,
                    };

                    _isInstalling = true;

                    // When "Include all files" option is checked, we don't want to write out the files to libman.json.
                    // We will only list the files when user chose to install specific files.
                    if (LibraryFilesToInstall == FileSelectionType.ChooseSpecificFilesToInstall)
                    {
                        libraryInstallationState.Files = SelectedFiles.ToList();
                    }

                    manifest.AddLibrary(libraryInstallationState);

                    await Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    if (!File.Exists(_configFileName))
                    {
                        await manifest.SaveAsync(_configFileName, CancellationToken.None);

                        if (_project != null)
                        {
                            await _project.AddFileToProjectAsync(_configFileName);
                        }
                    }

                    RunningDocumentTable rdt = new RunningDocumentTable(Shell.ServiceProvider.GlobalProvider);
                    string configFilePath    = Path.GetFullPath(_configFileName);

                    IVsTextBuffer textBuffer = rdt.FindDocument(configFilePath) as IVsTextBuffer;

                    _dispatcher.Invoke(() =>
                    {
                        _closeDialog(true);
                    });

                    // The file isn't open. So we'll write to disk directly
                    if (textBuffer == null)
                    {
                        manifest.AddLibrary(libraryInstallationState);

                        await manifest.SaveAsync(_configFileName, CancellationToken.None).ConfigureAwait(false);

                        Telemetry.TrackUserTask("Invoke-RestoreFromAddClientLibrariesDialog");
                        await _libraryCommandService.RestoreAsync(_configFileName, CancellationToken.None).ConfigureAwait(false);
                    }
                    else
                    {
                        // libman.json file is open, so we will write to the textBuffer.
                        InsertIntoTextBuffer(textBuffer, libraryInstallationState, manifest);

                        // Save manifest file so we can restore library files.
                        rdt.SaveFileIfDirty(configFilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(InstallPackageAsync), ex);
            }
        }
        private async void InstallPackageAsync()
        {
            try
            {
                ILibrary selectedPackage = SelectedPackage;
                _isInstalling = true;
                InstallPackageCommand.CanExecute(null);
                Manifest manifest = await Manifest.FromFileAsync(_configFileName, _deps, CancellationToken.None).ConfigureAwait(false);

                string targetPath = _targetPath;

                if (!string.IsNullOrEmpty(_configFileName))
                {
                    Uri configContainerUri = new Uri(_configFileName, UriKind.Absolute);
                    Uri targetUri          = new Uri(targetPath, UriKind.Absolute);
                    targetPath = configContainerUri.MakeRelativeUri(targetUri).ToString();
                }

                if (String.IsNullOrEmpty(manifest.Version))
                {
                    manifest.Version = Manifest.SupportedVersions.Max().ToString();
                }

                LibraryInstallationState libraryInstallationState = new LibraryInstallationState
                {
                    LibraryId       = PackageId,
                    ProviderId      = selectedPackage.ProviderId,
                    DestinationPath = InstallationFolder.DestinationFolder,
                };

                // When "Include all files" option is checked, we don't want to write out the files to libman.json.
                // We will only list the files when user chose to install specific files.
                if (LibraryFilesToInstall == FileSelectionType.ChooseSpecificFilesToInstall)
                {
                    libraryInstallationState.Files = SelectedFiles.ToList();
                }

                manifest.AddLibrary(libraryInstallationState);

                await Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                EnvDTE.Project project = VsHelpers.DTE.SelectedItems.Item(1)?.ProjectItem?.ContainingProject;
                project?.AddFileToProjectAsync(_configFileName);

                RunningDocumentTable rdt = new RunningDocumentTable(Shell.ServiceProvider.GlobalProvider);
                string configFilePath    = Path.GetFullPath(_configFileName);

                IVsTextBuffer textBuffer = rdt.FindDocument(configFilePath) as IVsTextBuffer;

                _dispatcher.Invoke(() =>
                {
                    _closeDialog(true);
                });

                // The file isn't open. So we'll write to disk directly
                if (textBuffer == null)
                {
                    manifest.AddLibrary(libraryInstallationState);

                    await manifest.SaveAsync(_configFileName, CancellationToken.None).ConfigureAwait(false);

                    await _libraryCommandService.RestoreAsync(_configFileName, CancellationToken.None).ConfigureAwait(false);
                }
                else
                {
                    // libman.json file is open, so we will write to the textBuffer.
                    InsertIntoTextBuffer(textBuffer, libraryInstallationState);

                    // Save manifest file so we can restore library files.
                    rdt.SaveFileIfDirty(configFilePath);
                }
            }
            catch { }
        }