@@ -112,6 +112,12 @@ const ONE_WEEK: TimeDelta = TimeDelta::weeks(1);
112112/// [`clean`]: PackageCache::clean
113113#[ derive( Debug ) ]
114114pub struct PackageCache {
115+ /// Path to an R executable
116+ r : PathBuf ,
117+
118+ /// Library paths to consider
119+ library_paths : Vec < PathBuf > ,
120+
115121 /// On disk cache directory root
116122 cache_root : file_lock:: Filesystem ,
117123
@@ -144,7 +150,7 @@ struct Metadata {
144150}
145151
146152impl PackageCache {
147- pub fn new ( ) -> anyhow:: Result < Self > {
153+ pub fn new ( r : PathBuf , library_paths : Vec < PathBuf > ) -> anyhow:: Result < Self > {
148154 let cache_root = file_lock:: Filesystem :: new ( crate :: fs:: sources_dir ( ) ?) ;
149155 cache_root. create_dir ( ) ?;
150156
@@ -160,6 +166,8 @@ impl PackageCache {
160166 let cache_root_lock = cache_root. open_ro_shared_create ( LOCK_FILENAME ) ?;
161167
162168 Ok ( Self {
169+ r,
170+ library_paths,
163171 cache_root,
164172 cache_root_lock,
165173 source_unavailable : RwLock :: new ( HashSet :: new ( ) ) ,
@@ -170,13 +178,8 @@ impl PackageCache {
170178 ///
171179 /// May spawn an R subprocess or download from CRAN (in a blocking manner) to
172180 /// generate the sources, so keep that in mind when calling this.
173- pub fn get < P : AsRef < Path > , Q : AsRef < Path > > (
174- & self ,
175- package : & str ,
176- r : P ,
177- library_paths : & [ Q ] ,
178- ) -> Option < PathBuf > {
179- match self . get_result ( package, r, library_paths) {
181+ pub fn get ( & self , package : & str ) -> Option < PathBuf > {
182+ match self . get_result ( package) {
180183 Ok ( Some ( sources) ) => Some ( sources) ,
181184 Ok ( None ) => None ,
182185 Err ( err) => {
@@ -186,13 +189,8 @@ impl PackageCache {
186189 }
187190 }
188191
189- fn get_result < P : AsRef < Path > , Q : AsRef < Path > > (
190- & self ,
191- package : & str ,
192- r : P ,
193- library_paths : & [ Q ] ,
194- ) -> anyhow:: Result < Option < PathBuf > > {
195- let Some ( package) = InstalledPackage :: find ( package, library_paths) ? else {
192+ fn get_result ( & self , package : & str ) -> anyhow:: Result < Option < PathBuf > > {
193+ let Some ( package) = InstalledPackage :: find ( package, & self . library_paths ) ? else {
196194 // Not even installed
197195 return Ok ( None ) ;
198196 } ;
@@ -216,9 +214,9 @@ impl PackageCache {
216214 // Write path
217215 let result = if matches ! ( package. description( ) . priority, Some ( Priority :: Base ) ) {
218216 // R version to download is the same as the base package version
219- self . try_populate_base ( & package. description ( ) . version , library_paths)
217+ self . try_populate_base ( & package. description ( ) . version , & self . library_paths )
220218 } else {
221- self . try_populate ( & package, r, library_paths)
219+ self . try_populate ( & package, & self . r , & self . library_paths )
222220 } ;
223221
224222 match result {
@@ -539,22 +537,3 @@ impl PackageCache {
539537 Ok ( ( ) )
540538 }
541539}
542-
543- // // For local testing
544- // #[cfg(test)]
545- // mod tests {
546- // use std::path::PathBuf;
547- //
548- // use crate::PackageCache;
549- //
550- // #[test]
551- // fn testit() {
552- // let r_script_path = PathBuf::from("/usr/local/bin/Rscript");
553- // let r_libpaths = vec![
554- // PathBuf::from("/Users/davis/Library/R/arm64/4.5/library"),
555- // PathBuf::from("/Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library"),
556- // ];
557- // let cache = PackageCache::new(r_script_path, r_libpaths).unwrap();
558- // cache.get("utils");
559- // }
560- // }
0 commit comments