Skip to content

Commit a2d328d

Browse files
authored
Merge pull request #180 from rage/refresh-sandbox
return sandbox image from refresh
2 parents 12f021b + 3b9cef9 commit a2d328d

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

tmc-langs-plugins/src/lib.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,36 @@ pub fn extract_project_overwrite(
5050
/// See `LanguagePlugin::compress_project`.
5151
// TODO: clean up
5252
pub fn compress_project_to_zip(path: &Path) -> Result<Vec<u8>, PluginError> {
53-
match get_language_plugin_type(path)? {
54-
PluginType::CSharp => Ok(tmc_zip::zip_student_files(
53+
match get_language_plugin_type(path) {
54+
Some(PluginType::CSharp) => Ok(tmc_zip::zip_student_files(
5555
<CSharpPlugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
5656
path,
5757
)?),
58-
PluginType::Make => Ok(tmc_zip::zip_student_files(
58+
Some(PluginType::Make) => Ok(tmc_zip::zip_student_files(
5959
<MakePlugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
6060
path,
6161
)?),
62-
PluginType::Maven => Ok(tmc_zip::zip_student_files(
62+
Some(PluginType::Maven) => Ok(tmc_zip::zip_student_files(
6363
<MavenPlugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
6464
path,
6565
)?),
66-
PluginType::NoTests => Ok(tmc_zip::zip_student_files(
66+
Some(PluginType::NoTests) => Ok(tmc_zip::zip_student_files(
6767
<NoTestsPlugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
6868
path,
6969
)?),
70-
PluginType::Python3 => Ok(tmc_zip::zip_student_files(
70+
Some(PluginType::Python3) => Ok(tmc_zip::zip_student_files(
7171
<Python3Plugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
7272
path,
7373
)?),
74-
PluginType::R => Ok(tmc_zip::zip_student_files(
74+
Some(PluginType::R) => Ok(tmc_zip::zip_student_files(
7575
<RPlugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
7676
path,
7777
)?),
78-
PluginType::Ant => Ok(tmc_zip::zip_student_files(
78+
Some(PluginType::Ant) => Ok(tmc_zip::zip_student_files(
7979
<AntPlugin as LanguagePlugin>::StudentFilePolicy::new(path)?,
8080
path,
8181
)?),
82+
None => Err(PluginError::PluginNotFound(path.to_path_buf())),
8283
}
8384
}
8485

@@ -113,7 +114,7 @@ pub enum PluginType {
113114
Ant,
114115
}
115116

116-
pub fn get_language_plugin_type(path: &Path) -> Result<PluginType, PluginError> {
117+
pub fn get_language_plugin_type(path: &Path) -> Option<PluginType> {
117118
let plugin_type = if NoTestsPlugin::is_exercise_type_correct(path) {
118119
log::info!(
119120
"Detected project at {} as {}",
@@ -165,21 +166,22 @@ pub fn get_language_plugin_type(path: &Path) -> Result<PluginType, PluginError>
165166
);
166167
PluginType::Ant
167168
} else {
168-
return Err(PluginError::PluginNotFound(path.to_path_buf()));
169+
return None;
169170
};
170-
Ok(plugin_type)
171+
Some(plugin_type)
171172
}
172173

173174
// Get language plugin for the given path.
174175
pub fn get_language_plugin(path: &Path) -> Result<Plugin, PluginError> {
175-
let plugin = match get_language_plugin_type(path)? {
176-
PluginType::NoTests => Plugin::NoTests(NoTestsPlugin::new()),
177-
PluginType::CSharp => Plugin::CSharp(CSharpPlugin::new()),
178-
PluginType::Make => Plugin::Make(MakePlugin::new()),
179-
PluginType::Python3 => Plugin::Python3(Python3Plugin::new()),
180-
PluginType::R => Plugin::R(RPlugin::new()),
181-
PluginType::Maven => Plugin::Maven(MavenPlugin::new()?),
182-
PluginType::Ant => Plugin::Ant(AntPlugin::new()?),
176+
let plugin = match get_language_plugin_type(path) {
177+
Some(PluginType::NoTests) => Plugin::NoTests(NoTestsPlugin::new()),
178+
Some(PluginType::CSharp) => Plugin::CSharp(CSharpPlugin::new()),
179+
Some(PluginType::Make) => Plugin::Make(MakePlugin::new()),
180+
Some(PluginType::Python3) => Plugin::Python3(Python3Plugin::new()),
181+
Some(PluginType::R) => Plugin::R(RPlugin::new()),
182+
Some(PluginType::Maven) => Plugin::Maven(MavenPlugin::new()?),
183+
Some(PluginType::Ant) => Plugin::Ant(AntPlugin::new()?),
184+
None => return Err(PluginError::PluginNotFound(path.to_path_buf())),
183185
};
184186
Ok(plugin)
185187
}

tmc-langs/src/course_refresher.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::io::Write;
88
use std::path::{Path, PathBuf};
99
use std::time::Duration;
1010
use tmc_langs_framework::{TmcCommand, TmcProjectYml};
11+
use tmc_langs_plugins::PluginType;
1112
use tmc_langs_util::file_util;
1213
use walkdir::WalkDir;
1314

@@ -34,6 +35,7 @@ pub struct RefreshExercise {
3435
points: Vec<String>,
3536
#[serde(skip)]
3637
path: PathBuf,
38+
sandbox_image: String,
3739
tmcproject_yml: Option<TmcProjectYml>,
3840
}
3941

@@ -300,18 +302,43 @@ fn get_exercises(
300302
let exercise_path = course_clone_path.join(&exercise_dir);
301303
let points = super::get_available_points(&exercise_path)?;
302304

305+
let sandbox_image = if let Some(image_override) = tmcproject_yml
306+
.as_ref()
307+
.and_then(|y| y.sandbox_image.as_ref())
308+
{
309+
image_override.clone()
310+
} else {
311+
get_default_sandbox_image(&exercise_path)?.to_string()
312+
};
313+
303314
Ok(RefreshExercise {
304315
name,
305316
points,
306317
checksum,
307318
path: exercise_dir,
319+
sandbox_image,
308320
tmcproject_yml,
309321
})
310322
})
311323
.collect::<Result<_, LangsError>>()?;
312324
Ok(exercises)
313325
}
314326

327+
fn get_default_sandbox_image(path: &Path) -> Result<&'static str, LangsError> {
328+
let url = match tmc_langs_plugins::get_language_plugin_type(&path) {
329+
Some(PluginType::CSharp) => "eu.gcr.io/moocfi-public/tmc-sandbox-csharp:latest",
330+
Some(PluginType::Make) => "eu.gcr.io/moocfi-public/tmc-sandbox-make:latest",
331+
Some(PluginType::Maven) | Some(PluginType::Ant) => {
332+
"eu.gcr.io/moocfi-public/tmc-sandbox-java:latest"
333+
}
334+
Some(PluginType::NoTests) => "eu.gcr.io/moocfi-public/tmc-sandbox-python:latest", // doesn't really matter, just use Python image
335+
Some(PluginType::Python3) => "eu.gcr.io/moocfi-public/tmc-sandbox-python:latest",
336+
Some(PluginType::R) => "eu.gcr.io/moocfi-public/tmc-sandbox-r:latest",
337+
None => return Err(LangsError::NoPlugin),
338+
};
339+
Ok(url)
340+
}
341+
315342
fn calculate_checksum(exercise_dir: &Path) -> Result<String, LangsError> {
316343
let mut digest = Context::new();
317344

tmc-langs/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub enum LangsError {
5858
SettingsCannotContainNull,
5959
#[error("The number given was too high: {0}")]
6060
SettingNumberTooHigh(serde_json::Number),
61+
#[error("Failed to detect exercise language")]
62+
NoPlugin,
6163

6264
#[error("Cache path {0} was invalid. Not a valid UTF-8 string or did not contain a cache version after a dash")]
6365
InvalidCachePath(PathBuf),

tmc-langs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ pub fn prepare_stub(exercise_path: &Path, dest_path: &Path) -> Result<(), LangsE
876876
submission_processing::prepare_stub(&exercise_path, dest_path)?;
877877

878878
// The Ant plugin needs some additional files to be copied over.
879-
if let PluginType::Ant = tmc_langs_plugins::get_language_plugin_type(exercise_path)? {
879+
if let Some(PluginType::Ant) = tmc_langs_plugins::get_language_plugin_type(exercise_path) {
880880
AntPlugin::copy_tmc_junit_runner(dest_path).map_err(|e| TmcError::Plugin(Box::new(e)))?;
881881
}
882882
Ok(())

0 commit comments

Comments
 (0)