Пример #1
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 { }
        }