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 { }
        }
        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 { }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        private void RebuildPackageTree(InstallablePackage package)
        {
            if (package != Package)
            {
                return;
            }

            DisplayRoots = null;

            if (package == null)
            {
                return;
            }

            bool             canUpdateInstallStatusValue = false;
            Func <bool>      canUpdateInstallStatus      = () => canUpdateInstallStatusValue;
            HashSet <string> selectedFiles = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            PackageItem      root          = new PackageItem(this, null, selectedFiles)
            {
                CanUpdateInstallStatus = canUpdateInstallStatus,
                ItemType  = PackageItemType.Folder,
                Name      = RootFolderName,
                IsChecked = false
            };

            PackageItem packageItem = new PackageItem(this, root, selectedFiles)
            {
                CanUpdateInstallStatus = canUpdateInstallStatus,
                Name      = package.Name,
                ItemType  = PackageItemType.Folder,
                IsChecked = false
            };

            //The node that children will be added to
            PackageItem realParent = root;
            //The node that will be set as the parent of the child nodes
            PackageItem virtualParent = packageItem;

            if (IncludePackageName)
            {
                realParent    = packageItem;
                root.Children = new[] { packageItem };
            }

            foreach (string file in package.Files)
            {
                string[]    parts                = file.Split('/');
                PackageItem currentRealParent    = realParent;
                PackageItem currentVirtualParent = virtualParent;

                for (int i = 0; i < parts.Length; ++i)
                {
                    bool isFolder = i != parts.Length - 1;

                    if (isFolder)
                    {
                        PackageItem next = currentRealParent.Children.FirstOrDefault(x => x.ItemType == PackageItemType.Folder && string.Equals(x.Name, parts[i]));

                        if (next == null)
                        {
                            next = new PackageItem(this, currentVirtualParent, selectedFiles)
                            {
                                CanUpdateInstallStatus = canUpdateInstallStatus,
                                Name      = parts[i],
                                ItemType  = PackageItemType.Folder,
                                IsChecked = false
                            };

                            List <PackageItem> children = new List <PackageItem>(currentRealParent.Children)
                            {
                                next
                            };

                            children.Sort((x, y) => x.ItemType == y.ItemType ? StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name) : y.ItemType == PackageItemType.Folder ? 1 : -1);

                            currentRealParent.Children = children;

                            if (currentVirtualParent != currentRealParent)
                            {
                                currentVirtualParent.Children = children;
                            }
                        }

                        currentRealParent    = next;
                        currentVirtualParent = next;
                    }
                    else
                    {
                        PackageItem next = new PackageItem(this, currentVirtualParent, selectedFiles)
                        {
                            CanUpdateInstallStatus = canUpdateInstallStatus,
                            FullPath  = file,
                            Name      = parts[i],
                            ItemType  = PackageItemType.File,
                            IsChecked = false,
                        };

                        List <PackageItem> children = new List <PackageItem>(currentRealParent.Children)
                        {
                            next
                        };

                        children.Sort((x, y) => x.ItemType == y.ItemType ? StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name) : y.ItemType == PackageItemType.Folder ? -1 : 1);

                        currentRealParent.Children = children;

                        if (currentVirtualParent != currentRealParent)
                        {
                            currentVirtualParent.Children = children;
                        }

                        next.IsMain = string.Equals(package.MainFile, file, StringComparison.OrdinalIgnoreCase);
                    }
                }
            }

            SetNodeOpenStates(root);

            Dispatcher.Invoke(() =>
            {
                if (package == Package)
                {
                    canUpdateInstallStatusValue = true;
                    _packageItem  = packageItem;
                    DisplayRoots  = new[] { root };
                    SelectedFiles = selectedFiles;
                    InstallPackageCommand.CanExecute(null);
                }
            });
        }