From 637d1dc54e6bf37df4aede0d8fe93f8b779e37d5 Mon Sep 17 00:00:00 2001 From: Daniel Martinez Date: Tue, 30 Mar 2021 23:57:16 +0300 Subject: [PATCH] added plugintype so compress-project doesn't need to construct a plugin --- tmc-langs-plugins/src/lib.rs | 67 +++++++++++++++++++++++------------- tmc-langs/src/lib.rs | 4 +-- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/tmc-langs-plugins/src/lib.rs b/tmc-langs-plugins/src/lib.rs index 6d401e157aa..6afdbeacc83 100644 --- a/tmc-langs-plugins/src/lib.rs +++ b/tmc-langs-plugins/src/lib.rs @@ -54,33 +54,32 @@ pub fn extract_project_overwrite( /// See `LanguagePlugin::compress_project`. // TODO: clean up pub fn compress_project(path: &Path) -> Result, PluginError> { - let plugin = get_language_plugin(path)?; - match plugin { - Plugin::CSharp(_) => Ok(tmc_zip::zip( + match get_language_plugin_type(path)? { + PluginType::CSharp => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), - Plugin::Make(_) => Ok(tmc_zip::zip( + PluginType::Make => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), - Plugin::Maven(_) => Ok(tmc_zip::zip( + PluginType::Maven => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), - Plugin::NoTests(_) => Ok(tmc_zip::zip( + PluginType::NoTests => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), - Plugin::Python3(_) => Ok(tmc_zip::zip( + PluginType::Python3 => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), - Plugin::R(_) => Ok(tmc_zip::zip( + PluginType::R => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), - Plugin::Ant(_) => Ok(tmc_zip::zip( + PluginType::Ant => Ok(tmc_zip::zip( ::StudentFilePolicy::new(path)?, path, )?), @@ -108,52 +107,59 @@ pub enum Plugin { Ant(AntPlugin), } -// Get language plugin for the given path. -pub fn get_language_plugin(path: &Path) -> Result { - if NoTestsPlugin::is_exercise_type_correct(path) { +pub enum PluginType { + CSharp, + Make, + Maven, + NoTests, + Python3, + R, + Ant, +} + +pub fn get_language_plugin_type(path: &Path) -> Result { + let plugin_type = if NoTestsPlugin::is_exercise_type_correct(path) { log::info!( "Detected project at {} as {}", path.display(), NoTestsPlugin::PLUGIN_NAME ); - Ok(Plugin::NoTests(NoTestsPlugin::new())) + PluginType::NoTests } else if CSharpPlugin::is_exercise_type_correct(path) { - let csharp = CSharpPlugin::new(); log::info!( "Detected project at {} as {}", path.display(), CSharpPlugin::PLUGIN_NAME ); - Ok(Plugin::CSharp(csharp)) + PluginType::CSharp } else if MakePlugin::is_exercise_type_correct(path) { - let make = MakePlugin::new(); log::info!( "Detected project at {} as {}", path.display(), MakePlugin::PLUGIN_NAME ); - Ok(Plugin::Make(make)) + PluginType::Make } else if Python3Plugin::is_exercise_type_correct(path) { log::info!( "Detected project at {} as {}", path.display(), Python3Plugin::PLUGIN_NAME ); - Ok(Plugin::Python3(Python3Plugin::new())) + PluginType::Python3 } else if RPlugin::is_exercise_type_correct(path) { log::info!( "Detected project at {} as {}", path.display(), RPlugin::PLUGIN_NAME ); - Ok(Plugin::R(RPlugin::new())) + PluginType::R } else if MavenPlugin::is_exercise_type_correct(path) { log::info!( "Detected project at {} as {}", path.display(), MavenPlugin::PLUGIN_NAME ); - Ok(Plugin::Maven(MavenPlugin::new()?)) + PluginType::Maven } else if AntPlugin::is_exercise_type_correct(path) { // TODO: currently, ant needs to be last because any project with src and test are recognized as ant log::info!( @@ -161,8 +167,23 @@ pub fn get_language_plugin(path: &Path) -> Result { path.display(), AntPlugin::PLUGIN_NAME ); - Ok(Plugin::Ant(AntPlugin::new()?)) + PluginType::Ant } else { - Err(PluginError::PluginNotFound(path.to_path_buf())) - } + return Err(PluginError::PluginNotFound(path.to_path_buf())); + }; + Ok(plugin_type) +} + +// Get language plugin for the given path. +pub fn get_language_plugin(path: &Path) -> Result { + let plugin = match get_language_plugin_type(path)? { + PluginType::NoTests => Plugin::NoTests(NoTestsPlugin::new()), + PluginType::CSharp => Plugin::CSharp(CSharpPlugin::new()), + PluginType::Make => Plugin::Make(MakePlugin::new()), + PluginType::Python3 => Plugin::Python3(Python3Plugin::new()), + PluginType::R => Plugin::R(RPlugin::new()), + PluginType::Maven => Plugin::Maven(MavenPlugin::new()?), + PluginType::Ant => Plugin::Ant(AntPlugin::new()?), + }; + Ok(plugin) } diff --git a/tmc-langs/src/lib.rs b/tmc-langs/src/lib.rs index b591cede953..3e76fd739fa 100644 --- a/tmc-langs/src/lib.rs +++ b/tmc-langs/src/lib.rs @@ -49,7 +49,7 @@ use std::{ }; use std::{collections::HashMap, ffi::OsStr}; use tmc_langs_framework::{NothingIsStudentFilePolicy, StudentFilePolicy, TmcError, TmcProjectYml}; -use tmc_langs_plugins::{get_language_plugin, tmc_zip, AntPlugin, Plugin}; +use tmc_langs_plugins::{get_language_plugin, tmc_zip, AntPlugin, PluginType}; use tmc_langs_util::progress_reporter; use toml::{map::Map as TomlMap, Value as TomlValue}; use url::Url; @@ -754,7 +754,7 @@ pub fn prepare_stub(exercise_path: &Path, dest_path: &Path) -> Result<(), LangsE submission_processing::prepare_stub(&exercise_path, dest_path)?; // The Ant plugin needs some additional files to be copied over. - if let Plugin::Ant(_) = tmc_langs_plugins::get_language_plugin(exercise_path)? { + if let PluginType::Ant = tmc_langs_plugins::get_language_plugin_type(exercise_path)? { AntPlugin::copy_tmc_junit_runner(dest_path).map_err(|e| TmcError::Plugin(Box::new(e)))?; } Ok(())