示例#1
0
        public static async Task <IPythonInterpreterFactory> CreateAndAddFactory(
            IServiceProvider site,
            IInterpreterRegistryService registry,
            IInterpreterOptionsService options,
            PythonProjectNode project,
            IPythonWorkspaceContext 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);
                }
            }
        }
 public async Task ChangeFactoryAsync(IPythonInterpreterFactory factory)
 {
     await _pythonWorkspace.SetInterpreterFactoryAsync(factory);
 }