1- import { getPythonExecutionDetails } from '../extensions/python'
1+ import { Progress } from 'vscode'
2+ import {
3+ getPythonExecutionDetails ,
4+ isActivePythonEnvGlobal
5+ } from '../extensions/python'
26import { findPythonBin , getDefaultPython , installPackages } from '../python'
37import { ConfigKey , getConfigValue } from '../vscode/config'
48import { getFirstWorkspaceFolder } from '../vscode/workspaceFolders'
@@ -16,25 +20,59 @@ export const findPythonBinForInstall = async (): Promise<
1620 )
1721}
1822
23+ const getProcessGlobalArgs = ( isGlobal : boolean ) => ( isGlobal ? [ '--user' ] : [ ] )
24+
25+ const installPackageAndIncrementProgress = ( {
26+ root,
27+ pythonBinPath,
28+ isGlobalEnv,
29+ progress,
30+ incrementAmount,
31+ packageName,
32+ successMessage
33+ } : {
34+ root : string
35+ pythonBinPath : string
36+ isGlobalEnv : boolean
37+ progress : Progress < { message : string ; amount : number } >
38+ incrementAmount : number
39+ packageName : string
40+ successMessage : string
41+ } ) =>
42+ Toast . runCommandAndIncrementProgress (
43+ async ( ) => {
44+ await installPackages (
45+ root ,
46+ pythonBinPath ,
47+ ...getProcessGlobalArgs ( isGlobalEnv ) ,
48+ packageName
49+ )
50+ return successMessage
51+ } ,
52+ progress ,
53+ incrementAmount
54+ )
55+
1956const showUpgradeProgress = (
2057 root : string ,
21- pythonBinPath : string
58+ pythonBinPath : string ,
59+ isGlobalEnv : boolean
2260) : Thenable < unknown > =>
2361 Toast . showProgress ( 'Upgrading DVC' , async progress => {
2462 progress . report ( { increment : 0 } )
2563
2664 progress . report ( { increment : 25 , message : 'Updating packages...' } )
2765
2866 try {
29- await Toast . runCommandAndIncrementProgress (
30- async ( ) => {
31- await installPackages ( root , pythonBinPath , 'dvc' )
32- return 'Upgraded successfully'
33- } ,
67+ await installPackageAndIncrementProgress ( {
68+ incrementAmount : 75 ,
69+ isGlobalEnv,
70+ packageName : 'dvc' ,
3471 progress,
35- 75
36- )
37-
72+ pythonBinPath,
73+ root,
74+ successMessage : 'Upgraded successfully'
75+ } )
3876 return Toast . delayProgressClosing ( )
3977 } catch ( error : unknown ) {
4078 return Toast . reportProgressError ( error , progress )
@@ -43,33 +81,36 @@ const showUpgradeProgress = (
4381
4482const showInstallProgress = (
4583 root : string ,
46- pythonBinPath : string
84+ pythonBinPath : string ,
85+ isGlobalEnv : boolean
4786) : Thenable < unknown > =>
4887 Toast . showProgress ( 'Installing packages' , async progress => {
4988 progress . report ( { increment : 0 } )
5089
5190 try {
52- await Toast . runCommandAndIncrementProgress (
53- async ( ) => {
54- await installPackages ( root , pythonBinPath , 'dvclive' )
55- return 'DVCLive Installed'
56- } ,
91+ await installPackageAndIncrementProgress ( {
92+ incrementAmount : 25 ,
93+ isGlobalEnv,
94+ packageName : 'dvclive' ,
5795 progress,
58- 25
59- )
96+ pythonBinPath,
97+ root,
98+ successMessage : 'DVCLive Installed'
99+ } )
60100 } catch ( error : unknown ) {
61101 return Toast . reportProgressError ( error , progress )
62102 }
63103
64104 try {
65- await Toast . runCommandAndIncrementProgress (
66- async ( ) => {
67- await installPackages ( root , pythonBinPath , 'dvc' )
68- return 'DVC Installed'
69- } ,
105+ await installPackageAndIncrementProgress ( {
106+ incrementAmount : 75 ,
107+ isGlobalEnv,
108+ packageName : 'dvc' ,
70109 progress,
71- 75
72- )
110+ pythonBinPath,
111+ root,
112+ successMessage : 'DVC Installed'
113+ } )
73114
74115 return Toast . delayProgressClosing ( )
75116 } catch ( error : unknown ) {
@@ -78,10 +119,17 @@ const showInstallProgress = (
78119 } )
79120
80121const getArgsAndRunCommand = async (
81- command : ( root : string , pythonBinPath : string ) => Thenable < unknown >
122+ isPythonExtensionUsed : boolean ,
123+ command : (
124+ root : string ,
125+ pythonBinPath : string ,
126+ isGlobalEnv : boolean
127+ ) => Thenable < unknown >
82128) : Promise < unknown > => {
83129 const pythonBinPath = await findPythonBinForInstall ( )
84130 const root = getFirstWorkspaceFolder ( )
131+ const isPythonEnvGlobal =
132+ isPythonExtensionUsed && ( await isActivePythonEnvGlobal ( ) )
85133
86134 if ( ! root ) {
87135 return Toast . showError (
@@ -95,13 +143,17 @@ const getArgsAndRunCommand = async (
95143 )
96144 }
97145
98- return command ( root , pythonBinPath )
146+ return command ( root , pythonBinPath , ! ! isPythonEnvGlobal )
99147}
100148
101- export const autoInstallDvc = ( ) : Promise < unknown > => {
102- return getArgsAndRunCommand ( showInstallProgress )
149+ export const autoInstallDvc = (
150+ isPythonExtensionUsed : boolean
151+ ) : Promise < unknown > => {
152+ return getArgsAndRunCommand ( isPythonExtensionUsed , showInstallProgress )
103153}
104154
105- export const autoUpgradeDvc = ( ) : Promise < unknown > => {
106- return getArgsAndRunCommand ( showUpgradeProgress )
155+ export const autoUpgradeDvc = (
156+ isPythonExtensionUsed : boolean
157+ ) : Promise < unknown > => {
158+ return getArgsAndRunCommand ( isPythonExtensionUsed , showUpgradeProgress )
107159}
0 commit comments