@@ -17,24 +17,28 @@ static _APP_NAME: &str = "pypoetry";
1717pub struct Config {
1818 pub virtualenvs_in_project : Option < bool > ,
1919 pub virtualenvs_path : PathBuf ,
20+ pub cache_dir : Option < PathBuf > ,
2021 pub file : Option < PathBuf > ,
2122}
2223
2324impl Config {
2425 fn new (
2526 file : Option < PathBuf > ,
2627 virtualenvs_path : PathBuf ,
28+ cache_dir : Option < PathBuf > ,
2729 virtualenvs_in_project : Option < bool > ,
2830 ) -> Self {
2931 trace ! (
30- "Poetry config file => {:?}, virtualenv.path => {:?}, virtualenvs_in_project => {:?}" ,
32+ "Poetry config file => {:?}, virtualenv.path => {:?}, cache_dir => {:?}, virtualenvs_in_project => {:?}" ,
3133 file,
3234 virtualenvs_path,
35+ cache_dir,
3336 virtualenvs_in_project
3437 ) ;
3538 Config {
3639 file,
3740 virtualenvs_path,
41+ cache_dir,
3842 virtualenvs_in_project,
3943 }
4044 }
@@ -54,44 +58,79 @@ impl Config {
5458
5559fn create_config ( file : Option < PathBuf > , env : & EnvVariables ) -> Option < Config > {
5660 let cfg = file. clone ( ) . and_then ( |f| parse ( & f) ) ;
61+ let cache_dir = get_cache_dir ( & cfg, env) ;
62+ let virtualenvs_path_from_env_var = env
63+ . poetry_virtualenvs_path
64+ . clone ( )
65+ . map ( |p| resolve_virtualenvs_path ( & p, & cache_dir) ) ;
66+
5767 if let Some ( virtualenvs_path) = & cfg. clone ( ) . and_then ( |cfg| cfg. virtualenvs_path ) {
58- let mut virtualenvs_path = virtualenvs_path. clone ( ) ;
5968 trace ! ( "Poetry virtualenvs path => {:?}" , virtualenvs_path) ;
60- if virtualenvs_path
61- . to_string_lossy ( )
62- . to_lowercase ( )
63- . contains ( "{cache-dir}" )
64- {
65- if let Some ( cache_dir) = & get_default_cache_dir ( env) {
66- virtualenvs_path = PathBuf :: from (
67- virtualenvs_path
68- . to_string_lossy ( )
69- . replace ( "{cache-dir}" , cache_dir. to_string_lossy ( ) . as_ref ( ) ) ,
70- ) ;
71- trace ! (
72- "Poetry virtualenvs path after replacing cache-dir => {:?}" ,
73- virtualenvs_path
74- ) ;
75- }
76- }
69+ let virtualenvs_path = resolve_virtualenvs_path ( & virtualenvs_path. clone ( ) , & cache_dir) ;
7770
7871 return Some ( Config :: new (
7972 file. clone ( ) ,
80- virtualenvs_path. clone ( ) ,
73+ // Give preference to the virtualenvs path from the env var
74+ virtualenvs_path_from_env_var. unwrap_or ( virtualenvs_path. clone ( ) ) ,
75+ cache_dir,
8176 cfg. and_then ( |cfg| cfg. virtualenvs_in_project ) ,
8277 ) ) ;
8378 }
8479
85- get_default_cache_dir ( env)
86- . map ( |cache_dir| Config :: new ( file, cache_dir. join ( "virtualenvs" ) , None ) )
80+ // Give preference to the virtualenvs path from the env var
81+ if let Some ( virtualenvs_path_from_env_var) = virtualenvs_path_from_env_var {
82+ if virtualenvs_path_from_env_var. exists ( ) {
83+ return Some ( Config :: new (
84+ file,
85+ virtualenvs_path_from_env_var,
86+ cache_dir,
87+ cfg. and_then ( |cfg| cfg. virtualenvs_in_project ) ,
88+ ) ) ;
89+ }
90+ }
91+
92+ cache_dir
93+ . map ( |cache_dir| Config :: new ( file, cache_dir. join ( "virtualenvs" ) , Some ( cache_dir) , None ) )
94+ }
95+
96+ /// Replaces {cache-dir} in virtualenvs path with the cache dir
97+ fn resolve_virtualenvs_path ( virtualenvs_path : & Path , cache_dir : & Option < PathBuf > ) -> PathBuf {
98+ if virtualenvs_path
99+ . to_string_lossy ( )
100+ . to_lowercase ( )
101+ . contains ( "{cache-dir}" )
102+ {
103+ if let Some ( cache_dir) = & cache_dir {
104+ let virtualenvs_path = PathBuf :: from (
105+ virtualenvs_path
106+ . to_string_lossy ( )
107+ . replace ( "{cache-dir}" , cache_dir. to_string_lossy ( ) . as_ref ( ) ) ,
108+ ) ;
109+ trace ! (
110+ "Poetry virtualenvs path after replacing cache-dir => {:?}" ,
111+ virtualenvs_path
112+ ) ;
113+ return virtualenvs_path;
114+ }
115+ }
116+ virtualenvs_path. to_path_buf ( )
87117}
88118/// Maps to DEFAULT_CACHE_DIR in poetry
89- fn get_default_cache_dir ( env : & EnvVariables ) -> Option < PathBuf > {
119+ fn get_cache_dir ( cfg : & Option < ConfigToml > , env : & EnvVariables ) -> Option < PathBuf > {
120+ // Cache dir in env variables takes precedence
90121 if let Some ( cache_dir) = env. poetry_cache_dir . clone ( ) {
91- Some ( cache_dir)
92- } else {
93- Platformdirs :: new ( _APP_NAME. into ( ) , false ) . user_cache_path ( )
122+ if cache_dir. is_dir ( ) {
123+ return Some ( cache_dir) ;
124+ }
125+ }
126+ // Check cache dir in config.
127+ if let Some ( cache_dir) = cfg. as_ref ( ) . and_then ( |cfg| cfg. cache_dir . clone ( ) ) {
128+ if cache_dir. is_dir ( ) {
129+ return Some ( cache_dir) ;
130+ }
94131 }
132+
133+ Platformdirs :: new ( _APP_NAME. into ( ) , false ) . user_cache_path ( )
95134}
96135
97136/// Maps to CONFIG_DIR in poetry
@@ -105,7 +144,7 @@ fn get_config_dir(env: &EnvVariables) -> Option<PathBuf> {
105144 Platformdirs :: new ( _APP_NAME. into ( ) , true ) . user_config_path ( )
106145}
107146
108- fn find_config_file ( env : & EnvVariables ) -> Option < PathBuf > {
147+ pub fn find_config_file ( env : & EnvVariables ) -> Option < PathBuf > {
109148 let config_dir = get_config_dir ( env) ?;
110149 let file = config_dir. join ( "config.toml" ) ;
111150 if file. exists ( ) {
0 commit comments