Skip to content

Commit 4abc92d

Browse files
committed
improved caching for C# runner
1 parent 50fedf6 commit 4abc92d

File tree

1 file changed

+13
-70
lines changed

1 file changed

+13
-70
lines changed

plugins/csharp/src/plugin.rs

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use walkdir::WalkDir;
1818
use zip::ZipArchive;
1919

2020
const TMC_CSHARP_RUNNER: &[u8] = include_bytes!("../deps/tmc-csharp-runner-1.1.1.zip");
21+
const TMC_CSHARP_RUNNER_VERSION: &str = "1.1.1";
2122

2223
#[derive(Default)]
2324
pub struct CSharpPlugin {}
@@ -27,40 +28,6 @@ impl CSharpPlugin {
2728
Self {}
2829
}
2930

30-
/// Verifies that the runner directory matches the contents of the zip.
31-
/// Note: does not check for extra files not in the zip.
32-
fn runner_needs_to_be_extracted(target: &Path) -> Result<bool, CSharpError> {
33-
log::debug!("verifying C# runner integrity at {}", target.display());
34-
35-
// no need to check the zip contents if the directory doesn't even exist
36-
if !target.exists() {
37-
return Ok(true);
38-
}
39-
40-
let mut zip = ZipArchive::new(Cursor::new(TMC_CSHARP_RUNNER))?;
41-
for i in 0..zip.len() {
42-
let file = zip.by_index(i)?;
43-
if file.is_file() {
44-
let target_file_path = target.join(Path::new(file.name()));
45-
if !target_file_path.exists() {
46-
return Ok(true); // new file in zip, need to extract
47-
}
48-
49-
let target_bytes = file_util::read_file(target_file_path)?;
50-
let zip_file_path = PathBuf::from(file.name());
51-
let zip_bytes: Vec<u8> = file
52-
.bytes()
53-
.collect::<Result<Vec<_>, _>>()
54-
.map_err(|e| FileError::FileRead(zip_file_path, e))?;
55-
56-
if target_bytes != zip_bytes {
57-
return Ok(true); // bytes changed, need to extract
58-
}
59-
}
60-
}
61-
Ok(false)
62-
}
63-
6431
/// Extracts the bundled tmc-csharp-runner to the given path.
6532
fn extract_runner_to_dir(target: &Path) -> Result<(), CSharpError> {
6633
log::debug!("extracting C# runner to {}", target.display());
@@ -94,12 +61,23 @@ impl CSharpPlugin {
9461
match dirs::cache_dir() {
9562
Some(cache_dir) => {
9663
let runner_dir = cache_dir.join("tmc").join("tmc-csharp-runner");
97-
if Self::runner_needs_to_be_extracted(&runner_dir)? {
64+
let version_path = runner_dir.join("VERSION");
65+
66+
let needs_update = if version_path.exists() {
67+
let version = file_util::read_file_to_string(&version_path)?;
68+
version != TMC_CSHARP_RUNNER_VERSION
69+
} else {
70+
true
71+
};
72+
73+
if needs_update {
74+
log::debug!("updating the cached C# runner");
9875
if runner_dir.exists() {
9976
// clear the directory if it exists
10077
file_util::remove_dir_all(&runner_dir)?;
10178
}
10279
Self::extract_runner_to_dir(&runner_dir)?;
80+
file_util::write_to_file(TMC_CSHARP_RUNNER_VERSION.as_bytes(), version_path)?;
10381
}
10482
Ok(runner_dir)
10583
}
@@ -444,41 +422,6 @@ mod test {
444422
temp
445423
}
446424

447-
#[test]
448-
fn runner_needs_to_be_extracted() {
449-
init();
450-
451-
// replace a file's content with garbage
452-
let temp = tempfile::TempDir::new().unwrap();
453-
CSharpPlugin::extract_runner_to_dir(temp.path()).unwrap();
454-
std::fs::write(
455-
temp.path().join("TestMyCode.CSharp.Bootstrap.exe"),
456-
b"garbage",
457-
)
458-
.unwrap();
459-
assert!(CSharpPlugin::runner_needs_to_be_extracted(&temp.path()).unwrap());
460-
461-
// remove a file
462-
let temp = tempfile::TempDir::new().unwrap();
463-
CSharpPlugin::extract_runner_to_dir(temp.path()).unwrap();
464-
std::fs::remove_file(temp.path().join("TestMyCode.CSharp.Bootstrap.exe")).unwrap();
465-
assert!(CSharpPlugin::runner_needs_to_be_extracted(&temp.path()).unwrap());
466-
}
467-
468-
#[test]
469-
fn runner_does_not_need_to_be_extracted() {
470-
init();
471-
472-
// no changes
473-
let temp = tempfile::TempDir::new().unwrap();
474-
CSharpPlugin::extract_runner_to_dir(temp.path()).unwrap();
475-
assert!(!CSharpPlugin::runner_needs_to_be_extracted(&temp.path()).unwrap());
476-
477-
// new file added
478-
file_to(&temp, "new_file", "stuff");
479-
assert!(!CSharpPlugin::runner_needs_to_be_extracted(&temp.path()).unwrap());
480-
}
481-
482425
#[test]
483426
fn extracts_runner_to_dir() {
484427
init();

0 commit comments

Comments
 (0)