Skip to content

Commit 0d0ea55

Browse files
authored
Merge pull request #155 from rage/cache
Cache
2 parents a6bbfda + dc8dc50 commit 0d0ea55

File tree

12 files changed

+65
-96
lines changed

12 files changed

+65
-96
lines changed

plugins/csharp/src/cs_test_result.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use tmc_langs_framework::TestResult;
77
/// Test result from the C# test runner.
88
#[derive(Debug, Deserialize)]
99
#[serde(rename_all = "PascalCase")]
10+
#[allow(clippy::upper_case_acronyms)]
1011
pub struct CSTestResult {
1112
pub name: String,
1213
pub passed: bool,

plugins/csharp/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl From<CSharpError> for TmcError {
3232
}
3333

3434
// conversion from plugin error to a tmc result
35-
impl<T> Into<Result<T, TmcError>> for CSharpError {
36-
fn into(self) -> Result<T, TmcError> {
37-
Err(TmcError::Plugin(Box::new(self)))
35+
impl<T> From<CSharpError> for Result<T, TmcError> {
36+
fn from(from: CSharpError) -> Self {
37+
Err(TmcError::Plugin(Box::new(from)))
3838
}
3939
}

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();

plugins/java/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl From<JavaError> for TmcError {
4747
}
4848

4949
// conversion from plugin error to a tmc result
50-
impl<T> Into<Result<T, TmcError>> for JavaError {
51-
fn into(self) -> Result<T, TmcError> {
52-
Err(TmcError::Plugin(Box::new(self)))
50+
impl<T> From<JavaError> for Result<T, TmcError> {
51+
fn from(from: JavaError) -> Self {
52+
Err(TmcError::Plugin(Box::new(from)))
5353
}
5454
}

plugins/java/src/java_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) trait JavaPlugin: LanguagePlugin {
4747
self.create_run_result_file(project_root_path, timeout, compile_result)?;
4848
let result = self.parse_test_result(&test_result);
4949
file_util::remove_file(&test_result.test_results)?;
50-
Ok(result?)
50+
result
5151
}
5252

5353
/// Parses test results.

plugins/java/src/maven_plugin.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use tmc_langs_framework::{
1717
};
1818
use tmc_langs_util::file_util;
1919

20-
const MVN_ARCHIVE: &[u8] = include_bytes!("../deps/apache-maven-3.6.3-bin.tar.gz");
20+
const MVN_ARCHIVE: &[u8] = include_bytes!("../deps/apache-maven-3.8.1-bin.tar.gz");
21+
const MVN_PATH_IN_ARCHIVE: &str = "apache-maven-3.8.1"; // the name of the base directory in the maven archive
22+
const MVN_VERSION: &str = "3.8.1";
2123

2224
pub struct MavenPlugin {
2325
jvm: Jvm,
@@ -52,17 +54,40 @@ impl MavenPlugin {
5254
#[cfg(not(windows))]
5355
let mvn_exec = "mvn";
5456

55-
let mvn_exec_path = tmc_path
56-
.join("apache-maven-3.6.3")
57-
.join("bin")
58-
.join(mvn_exec);
59-
if !mvn_exec_path.exists() {
57+
let mvn_path = tmc_path.join("apache-maven");
58+
let mvn_version_path = mvn_path.join("VERSION");
59+
60+
let needs_update = if mvn_version_path.exists() {
61+
let version_contents = file_util::read_file_to_string(&mvn_version_path)?;
62+
MVN_VERSION != version_contents
63+
} else {
64+
true
65+
};
66+
67+
if needs_update {
68+
if mvn_path.exists() {
69+
file_util::remove_dir_all(&mvn_path)?;
70+
}
71+
// TODO: remove this bit eventually, this is just to clean up the old maven cachce that had the version in the name
72+
let old_path = tmc_path.join("apache-maven-3.6.3");
73+
if old_path.exists() {
74+
file_util::remove_dir_all(old_path)?;
75+
}
76+
6077
log::debug!("extracting bundled tar");
6178
let tar = GzDecoder::new(Cursor::new(MVN_ARCHIVE));
6279
let mut tar = Archive::new(tar);
6380
tar.unpack(&tmc_path)
64-
.map_err(|e| JavaError::JarWrite(tmc_path, e))?;
81+
.map_err(|e| JavaError::JarWrite(tmc_path.clone(), e))?;
82+
83+
log::debug!("renaming extracted archive to apache-maven");
84+
file_util::rename(tmc_path.join(MVN_PATH_IN_ARCHIVE), &mvn_path)?;
85+
86+
log::debug!("writing bundle version data");
87+
file_util::write_to_file(MVN_VERSION.as_bytes(), &mvn_version_path)?;
6588
}
89+
90+
let mvn_exec_path = mvn_path.join("bin").join(mvn_exec);
6691
Ok(mvn_exec_path.as_os_str().to_os_string())
6792
}
6893
}
@@ -324,7 +349,7 @@ mod test {
324349
std::env::set_var("PATH", "");
325350
let cmd = MavenPlugin::get_mvn_command().unwrap();
326351
let expected = format!(
327-
"tmc{0}apache-maven-3.6.3{0}bin{0}mvn",
352+
"tmc{0}apache-maven-3.8.1{0}bin{0}mvn",
328353
std::path::MAIN_SEPARATOR
329354
);
330355
assert!(cmd.to_string_lossy().ends_with(&expected))

plugins/make/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl From<MakeError> for TmcError {
3838
}
3939

4040
// conversion from plugin error to a tmc result
41-
impl<T> Into<Result<T, TmcError>> for MakeError {
42-
fn into(self) -> Result<T, TmcError> {
43-
Err(TmcError::Plugin(Box::new(self)))
41+
impl<T> From<MakeError> for Result<T, TmcError> {
42+
fn from(from: MakeError) -> Self {
43+
Err(TmcError::Plugin(Box::new(from)))
4444
}
4545
}

plugins/python3/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ impl From<PythonError> for TmcError {
3939
}
4040

4141
// conversion from plugin error to a tmc result
42-
impl<T> Into<Result<T, TmcError>> for PythonError {
43-
fn into(self) -> Result<T, TmcError> {
44-
Err(TmcError::Plugin(Box::new(self)))
42+
impl<T> From<PythonError> for Result<T, TmcError> {
43+
fn from(from: PythonError) -> Self {
44+
Err(TmcError::Plugin(Box::new(from)))
4545
}
4646
}

plugins/r/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl From<RError> for TmcError {
2424
}
2525

2626
// conversion from plugin error to a tmc result
27-
impl<T> Into<Result<T, TmcError>> for RError {
28-
fn into(self) -> Result<T, TmcError> {
29-
Err(TmcError::Plugin(Box::new(self)))
27+
impl<T> From<RError> for Result<T, TmcError> {
28+
fn from(from: RError) -> Self {
29+
Err(TmcError::Plugin(Box::new(from)))
3030
}
3131
}

0 commit comments

Comments
 (0)