1
1
use std:: path:: { Path , PathBuf } ;
2
+ use std:: sync:: LazyLock ;
2
3
3
4
use anyhow:: Result ;
4
5
use rusqlite:: Connection ;
@@ -29,6 +30,28 @@ pub enum Error {
29
30
Git ( #[ from] crate :: git:: Error ) ,
30
31
}
31
32
33
+ static STORE_HOME : LazyLock < Option < PathBuf > > = LazyLock :: new ( || {
34
+ if let Some ( path) = std:: env:: var_os ( EnvVars :: PRE_COMMIT_HOME ) {
35
+ debug ! (
36
+ path = %path. to_string_lossy( ) ,
37
+ "Loading store from PRE_COMMIT_HOME" ,
38
+ ) ;
39
+ Some ( path. into ( ) )
40
+ } else if let Some ( path) = std:: env:: var_os ( EnvVars :: XDG_CACHE_HOME ) {
41
+ let path = PathBuf :: from ( path) . join ( "pre-commit" ) ;
42
+ debug ! (
43
+ path = %path. to_string_lossy( ) ,
44
+ "Loading store from XDG_CACHE_HOME" ,
45
+ ) ;
46
+ Some ( path)
47
+ } else {
48
+ let home = home:: home_dir ( ) ?;
49
+ let path = home. join ( ".cache" ) . join ( "pre-commit" ) ;
50
+ debug ! ( path = %path. display( ) , "Loading store from ~/.cache" ) ;
51
+ Some ( path)
52
+ }
53
+ } ) ;
54
+
32
55
/// A store for managing repos.
33
56
#[ derive( Debug ) ]
34
57
pub struct Store {
@@ -38,25 +61,9 @@ pub struct Store {
38
61
39
62
impl Store {
40
63
pub fn from_settings ( ) -> Result < Self , Error > {
41
- if let Some ( path) = std:: env:: var_os ( EnvVars :: PRE_COMMIT_HOME ) {
42
- debug ! (
43
- path = %path. to_string_lossy( ) ,
44
- "Loading store from PRE_COMMIT_HOME" ,
45
- ) ;
46
- return Ok ( Self :: from_path ( path) ) ;
47
- } else if let Some ( path) = std:: env:: var_os ( EnvVars :: XDG_CACHE_HOME ) {
48
- let path = PathBuf :: from ( path) . join ( "pre-commit" ) ;
49
- debug ! (
50
- path = %path. to_string_lossy( ) ,
51
- "Loading store from XDG_CACHE_HOME" ,
52
- ) ;
53
- return Ok ( Self :: from_path ( path) ) ;
54
- }
55
-
56
- let home = home:: home_dir ( ) . ok_or ( Error :: HomeNotFound ) ?;
57
- let path = home. join ( ".cache" ) . join ( "pre-commit" ) ;
58
- debug ! ( path = %path. display( ) , "Loading store from ~/.cache" ) ;
59
- Ok ( Self :: from_path ( path) )
64
+ Ok ( Self :: from_path (
65
+ STORE_HOME . as_ref ( ) . ok_or ( Error :: HomeNotFound ) ?,
66
+ ) )
60
67
}
61
68
62
69
pub fn from_path ( path : impl Into < PathBuf > ) -> Self {
@@ -267,8 +274,26 @@ impl Store {
267
274
LockedFile :: acquire ( self . path . join ( ".lock" ) , "store" ) . await
268
275
}
269
276
270
- pub fn uv_path ( & self ) -> PathBuf {
271
- self . path . join ( "tools" ) . join ( "uv" )
277
+ /// The path to the tool directory in the store.
278
+ pub fn tools_path ( & self , tool : ToolBucket ) -> PathBuf {
279
+ self . path . join ( tool. as_str ( ) )
280
+ }
281
+ }
282
+
283
+ #[ derive( Copy , Clone ) ]
284
+ pub enum ToolBucket {
285
+ Uv ,
286
+ Python ,
287
+ Node ,
288
+ }
289
+
290
+ impl ToolBucket {
291
+ pub fn as_str ( & self ) -> & str {
292
+ match self {
293
+ ToolBucket :: Uv => "uv" ,
294
+ ToolBucket :: Python => "python" ,
295
+ ToolBucket :: Node => "node" ,
296
+ }
272
297
}
273
298
}
274
299
0 commit comments