private async Task ApplyCustomAsync()
        {
            IPythonInterpreterFactory factory = null;

            if (RegisterCustomEnv)
            {
                Version version;
                if (!Version.TryParse(VersionName, out version))
                {
                    version = null;
                }

                factory = await CustomEnv.CreateCustomEnv(
                    RegistryService,
                    OptionsService,
                    PrefixPath,
                    InterpreterPath,
                    WindowsInterpreterPath,
                    PathEnvironmentVariable,
                    InterpreterArchitecture.TryParse(ArchitectureName ?? ""),
                    version,
                    Description
                    );
            }
            else
            {
                Debug.Assert(SelectedProject != null, "Project is null, UI should not have allowed this");
                if (SelectedProject != null)
                {
                    Version version;
                    if (!Version.TryParse(VersionName, out version))
                    {
                        version = null;
                    }

                    if (SelectedProject.Node != null)
                    {
                        factory = SelectedProject.Node.AddMSBuildEnvironment(
                            RegistryService,
                            PrefixPath,
                            InterpreterPath,
                            WindowsInterpreterPath,
                            PathEnvironmentVariable,
                            version,
                            InterpreterArchitecture.TryParse(ArchitectureName ?? ""),
                            Description
                            );
                    }
                    else if (SelectedProject.Workspace != null)
                    {
                        await SelectedProject.Workspace.SetInterpreterAsync(InterpreterPath);
                    }
                }
            }

            if (factory != null)
            {
                if (SelectedProject != null)
                {
                    if (SelectedProject.Node != null)
                    {
                        SelectedProject.Node.AddInterpreter(factory.Configuration.Id);
                        if (SetAsCurrent)
                        {
                            SelectedProject.Node.SetInterpreterFactory(factory);
                        }
                    }
                    else if (SelectedProject.Workspace != null)
                    {
                        await SelectedProject.Workspace.SetInterpreterFactoryAsync(factory);
                    }
                }

                if (SetAsDefault)
                {
                    OptionsService.DefaultInterpreter = factory;
                }
            }
        }
Exemplo n.º 2
0
        public static async Task <IPythonInterpreterFactory> CreateAndAddFactory(
            IServiceProvider site,
            IInterpreterRegistryService registry,
            IInterpreterOptionsService options,
            PythonProjectNode project,
            IWorkspace workspace,
            string path,
            IPythonInterpreterFactory baseInterp,
            bool registerAsCustomEnv,
            string customEnvName,
            bool preferVEnv = false
            )
        {
            if (site == null)
            {
                throw new ArgumentNullException(nameof(site));
            }

            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (baseInterp == null)
            {
                throw new ArgumentNullException(nameof(baseInterp));
            }

            if (preferVEnv)
            {
                await CreateWithVEnv(site, baseInterp, path);
            }
            else
            {
                await CreateWithVirtualEnv(site, baseInterp, path);
            }

            if (registerAsCustomEnv)
            {
                GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                var factory = await CustomEnv.CreateCustomEnv(
                    registry,
                    options,
                    path,
                    interpExe,
                    winterpExe,
                    pathVar,
                    baseInterp.Configuration.Architecture,
                    baseInterp.Configuration.Version,
                    customEnvName
                    );

                if (factory != null)
                {
                    if (project != null)
                    {
                        project.AddInterpreter(factory.Configuration.Id);
                    }
                    else if (workspace != null)
                    {
                        await workspace.SetInterpreterFactoryAsync(factory);
                    }
                }

                return(factory);
            }
            else
            {
                if (project != null)
                {
                    return(project.AddVirtualEnvironment(registry, path, baseInterp));
                }
                else if (workspace != null)
                {
                    // In workspaces, always store the path to the virtual env's python.exe
                    GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                    var workspaceFactoryProvider = site.GetComponentModel().GetService <WorkspaceInterpreterFactoryProvider>();
                    using (workspaceFactoryProvider?.SuppressDiscoverFactories(forceDiscoveryOnDispose: true)) {
                        var relativeInterpExe = PathUtils.GetRelativeFilePath(workspace.Location, interpExe);
                        await workspace.SetInterpreterAsync(relativeInterpExe);
                    }

                    var factory = workspaceFactoryProvider?
                                  .GetInterpreterFactories()
                                  .FirstOrDefault(f => PathUtils.IsSamePath(f.Configuration.InterpreterPath, interpExe));
                    return(factory);
                }
                else
                {
                    return(null);
                }
            }
        }
Exemplo n.º 3
0
        public static async Task <IPythonInterpreterFactory> CreateAndAddFactory(
            IServiceProvider site,
            IInterpreterRegistryService registry,
            IInterpreterOptionsService options,
            PythonProjectNode project,
            string path,
            IPythonInterpreterFactory baseInterp,
            bool registerAsCustomEnv,
            string customEnvName,
            bool preferVEnv = false
            )
        {
            if (site == null)
            {
                throw new ArgumentNullException(nameof(site));
            }

            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (baseInterp == null)
            {
                throw new ArgumentNullException(nameof(baseInterp));
            }

            if (preferVEnv)
            {
                await CreateWithVEnv(site, baseInterp, path);
            }
            else
            {
                await CreateWithVirtualEnv(site, baseInterp, path);
            }

            if (registerAsCustomEnv)
            {
                GetVirtualEnvConfig(path, baseInterp, out string interpExe, out string winterpExe, out string pathVar);

                var factory = await CustomEnv.CreateCustomEnv(
                    registry,
                    options,
                    path,
                    interpExe,
                    winterpExe,
                    pathVar,
                    baseInterp.Configuration.Architecture,
                    baseInterp.Configuration.Version,
                    customEnvName
                    );

                if (project != null && factory != null)
                {
                    project.AddInterpreter(factory.Configuration.Id);
                }

                return(factory);
            }
            else
            {
                if (project != null)
                {
                    return(project.AddVirtualEnvironment(registry, path, baseInterp));
                }
                else
                {
                    return(null);
                }
            }
        }