Skip to content

Commit ab09772

Browse files
committed
Added compression and naive to compression APIs
1 parent 7f14436 commit ab09772

File tree

12 files changed

+143
-82
lines changed

12 files changed

+143
-82
lines changed

bindings/tmc-langs-node/src/bin/generate-bindings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
// listLocalCourseExercises
1414
tmc_langs::LocalExercise,
1515
// prepareSubmission
16-
tmc_langs::OutputFormat,
16+
tmc_langs::Compression,
1717
// refreshCourse
1818
tmc_langs::RefreshData,
1919
tmc_langs::RefreshExercise,

bindings/tmc-langs-node/src/lib.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::{
1313
};
1414
use thiserror::Error;
1515
use tmc_langs::{
16-
file_util, ClientError, Credentials, DownloadOrUpdateCourseExercisesResult, FeedbackAnswer,
17-
LangsError, Language, NewSubmission, OutputFormat, SubmissionFinished, TmcClient, TmcConfig,
16+
file_util, ClientError, Compression, Credentials, DownloadOrUpdateCourseExercisesResult,
17+
FeedbackAnswer, LangsError, Language, NewSubmission, SubmissionFinished, TmcClient, TmcConfig,
1818
};
1919

2020
#[derive(Debug, Error)]
@@ -96,10 +96,15 @@ fn clean(mut cx: FunctionContext) -> JsResult<JsValue> {
9696
}
9797

9898
fn compress_project(mut cx: FunctionContext) -> JsResult<JsValue> {
99-
parse_args!(cx, exercise_path: PathBuf, output_path: PathBuf);
99+
parse_args!(
100+
cx,
101+
exercise_path: PathBuf,
102+
output_path: PathBuf,
103+
compression: Compression
104+
);
100105
lock!(cx, exercise_path);
101106

102-
let res = tmc_langs::compress_project_to(&exercise_path, &output_path);
107+
let res = tmc_langs::compress_project_to(&exercise_path, &output_path, compression);
103108
convert_res(&mut cx, res)
104109
}
105110

@@ -188,10 +193,10 @@ fn prepare_submission(mut cx: FunctionContext) -> JsResult<JsValue> {
188193
.expect("invalid key-value pair");
189194
}
190195
}
191-
let output_format = match output_format.as_str() {
192-
"Tar" => OutputFormat::Tar,
193-
"Zip" => OutputFormat::Zip,
194-
"TarZstd" => OutputFormat::TarZstd,
196+
let compression = match output_format.as_str() {
197+
"Tar" => Compression::Tar,
198+
"Zip" => Compression::Zip,
199+
"TarZstd" => Compression::TarZstd,
195200
_ => panic!("unrecognized output format"),
196201
};
197202

@@ -202,7 +207,7 @@ fn prepare_submission(mut cx: FunctionContext) -> JsResult<JsValue> {
202207
tmc_params,
203208
&clone_path,
204209
stub_zip_path.as_deref(),
205-
output_format,
210+
compression,
206211
);
207212
convert_res(&mut cx, res)
208213
}

bindings/tmc-langs-node/ts/functions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function prepareSolution(
3131
): void;
3232
export function prepareStub(exercisePath: string, outputPath: string): void;
3333
export function prepareSubmission(
34-
outputFormat: types.OutputFormat,
34+
outputFormat: types.Compression,
3535
clonePath: string,
3636
outputPath: string,
3737
stubZipPath: string | null,

bindings/tmc-langs-node/ts/generated.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ExercisePackagingConfiguration { student_file_paths: Array<stri
88

99
export interface LocalExercise { exercise_slug: string, exercise_path: string, }
1010

11-
export type OutputFormat = "Tar" | "Zip" | "TarZstd";
11+
export type Compression = "Tar" | "Zip" | "TarZstd";
1212

1313
export interface RefreshData { new_cache_path: string, course_options: object, exercises: Array<RefreshExercise>, }
1414

bindings/tmc-langs-node/ts/tmc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Tmc {
7373
}
7474

7575
prepareSubmission(
76-
outputFormat: types.OutputFormat,
76+
outputFormat: types.Compression,
7777
clonePath: string,
7878
outputPath: string,
7979
stubZipPath: string | null,

tmc-langs-cli/src/app.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use clap::Parser;
55
use schemars::JsonSchema;
66
use std::{path::PathBuf, str::FromStr};
77
use tmc_langs::{
8-
CombinedCourseData, CourseData, CourseDetails, CourseExercise,
8+
CombinedCourseData, Compression, CourseData, CourseDetails, CourseExercise,
99
DownloadOrUpdateCourseExercisesResult, ExerciseDesc, ExerciseDetails,
10-
ExercisePackagingConfiguration, Language, LocalExercise, NewSubmission, Organization,
11-
OutputFormat, Review, RunResult, StyleValidationResult, Submission, SubmissionFeedbackResponse,
12-
SubmissionFinished, UpdateResult, UpdatedExercise,
10+
ExercisePackagingConfiguration, Language, LocalExercise, NewSubmission, Organization, Review,
11+
RunResult, StyleValidationResult, Submission, SubmissionFeedbackResponse, SubmissionFinished,
12+
UpdateResult, UpdatedExercise,
1313
};
1414
// use tmc_langs_util::task_executor::RefreshData;
1515

@@ -59,29 +59,41 @@ pub enum Command {
5959
exercise_path: PathBuf,
6060
},
6161

62-
/// Compresses the target exercise into a ZIP. Only includes student files using the student file policy of the exercise's plugin
62+
/// Compresses the target exercise. Only includes student files using the student file policy of the exercise's plugin
6363
#[clap(long_about = SCHEMA_NULL)]
6464
CompressProject {
6565
/// Path to the directory where the exercise resides.
6666
#[clap(long)]
6767
exercise_path: PathBuf,
68-
/// Path to the output ZIP archive. Overwritten if it already exists.
68+
/// Path to the output archive. Overwritten if it already exists.
6969
#[clap(long)]
7070
output_path: PathBuf,
71+
/// Compression algorithm to use.
72+
#[clap(long, default_value_t = Compression::Zip)]
73+
compression: Compression,
74+
/// If set, simply compresses the target directory with all of its files.
75+
#[clap(long)]
76+
naive: bool,
7177
},
7278

7379
#[clap(subcommand)]
7480
Core(Core),
7581

76-
/// Extracts an exercise from a ZIP archive. If the output-path is a project root, the plugin's student file policy will be used to avoid overwriting student files
82+
/// Extracts an exercise from an archive. If the output-path is a project root, the plugin's student file policy will be used to avoid overwriting student files
7783
#[clap(long_about = SCHEMA_NULL)]
7884
ExtractProject {
79-
/// Path to the ZIP archive.
85+
/// Path to the archive.
8086
#[clap(long)]
8187
archive_path: PathBuf,
8288
/// Path to the directory where the archive will be extracted.
8389
#[clap(long)]
8490
output_path: PathBuf,
91+
/// Compression algorithm used for the archive.
92+
#[clap(long, default_value_t = Compression::Zip)]
93+
compression: Compression,
94+
/// If set, simply extracts the target directory with all of its files.
95+
#[clap(long)]
96+
naive: bool,
8597
},
8698

8799
/// Parses @Points notations from an exercise's exercise files and returns the point names found
@@ -144,29 +156,36 @@ pub enum Command {
144156
output_path: PathBuf,
145157
},
146158

147-
/// Takes a submission ZIP archive and turns it into an archive with reset test files, and tmc-params, ready for further processing
159+
/// Takes a submission archive and turns it into an archive with reset test files, and tmc-params, ready for further processing
148160
#[clap(long_about = SCHEMA_NULL)]
149161
PrepareSubmission {
150162
/// The output format of the submission archive. Defaults to tar.
151-
#[clap(long, default_value = "tar")]
152-
output_format: OutputFormatWrapper,
163+
#[clap(long, default_value_t = Compression::Tar)]
164+
output_format: Compression,
153165
/// Path to exercise's clone path, where the unmodified test files will be copied from.
154166
#[clap(long)]
155167
clone_path: PathBuf,
156168
/// Path to the resulting archive. Overwritten if it already exists.
157169
#[clap(long)]
158170
output_path: PathBuf,
159-
/// If given, the tests will be copied from this stub ZIP instead, effectively ignoring hidden tests.
160-
#[clap(long)]
161-
stub_zip_path: Option<PathBuf>,
162-
/// Path to the submission ZIP archive.
171+
/// If given, the tests will be copied from this stub instead, effectively ignoring hidden tests.
172+
// alias for backwards compatibility
173+
#[clap(long, alias = "stub_zip_path")]
174+
stub_archive_path: Option<PathBuf>,
175+
/// Compression algorithm used for the stub archive.
176+
#[clap(long, default_value_t = Compression::Zip)]
177+
stub_compression: Compression,
178+
/// Path to the submission archive.
163179
#[clap(long)]
164180
submission_path: PathBuf,
181+
/// Compression algorithm used for the submission.
182+
#[clap(long, default_value_t = Compression::Zip)]
183+
submission_compression: Compression,
165184
/// A key-value pair in the form key=value to be written into .tmcparams. If multiple pairs with the same key are given, the values are collected into an array.
166185
#[clap(long)]
167186
tmc_param: Vec<String>,
168-
#[clap(long)]
169187
/// If given, the contents in the resulting archive will be nested inside a directory with this name.
188+
#[clap(long)]
170189
top_level_dir_name: Option<String>,
171190
},
172191

@@ -549,22 +568,6 @@ impl FromStr for Locale {
549568
}
550569
}
551570

552-
pub struct OutputFormatWrapper(pub OutputFormat);
553-
554-
impl FromStr for OutputFormatWrapper {
555-
type Err = anyhow::Error;
556-
557-
fn from_str(s: &str) -> Result<Self, Self::Err> {
558-
let format = match s {
559-
"tar" => OutputFormat::Tar,
560-
"zip" => OutputFormat::Zip,
561-
"zstd" => OutputFormat::TarZstd,
562-
_ => anyhow::bail!("invalid format"),
563-
};
564-
Ok(OutputFormatWrapper(format))
565-
}
566-
}
567-
568571
// == utilities for printing the JSON schema of the objects printed to stdout by the CLI ==
569572
const SCHEMA_NULL: &str = "Result data JSON format: null";
570573
const SCHEMA_TOKEN: &str = r#"Result data JSON format:

tmc-langs-cli/src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use self::{
1212
};
1313
use crate::app::{Locale, Opt};
1414
use anyhow::{Context, Result};
15-
use app::{Command, Core, OutputFormatWrapper, Settings};
15+
use app::{Command, Core, Settings};
1616
use clap::{ErrorKind, IntoApp, Parser};
1717
use serde::Serialize;
1818
use serde_json::Value;
@@ -217,9 +217,11 @@ fn run_app(matches: Opt) -> Result<()> {
217217
Command::CompressProject {
218218
exercise_path,
219219
output_path,
220+
compression,
221+
naive,
220222
} => {
221223
file_util::lock!(exercise_path);
222-
tmc_langs::compress_project_to(&exercise_path, &output_path)?;
224+
tmc_langs::compress_project_to(&exercise_path, &output_path, compression)?;
223225
OutputKind::finished(format!(
224226
"compressed project from {} to {}",
225227
exercise_path.display(),
@@ -236,6 +238,8 @@ fn run_app(matches: Opt) -> Result<()> {
236238
Command::ExtractProject {
237239
archive_path,
238240
output_path,
241+
compression,
242+
naive,
239243
} => {
240244
let mut archive = file_util::open_file_lock(&archive_path)?;
241245
let mut guard = archive.write()?;
@@ -356,10 +360,12 @@ fn run_app(matches: Opt) -> Result<()> {
356360

357361
Command::PrepareSubmission {
358362
clone_path,
359-
output_format: OutputFormatWrapper(output_format),
363+
output_format,
360364
output_path,
361-
stub_zip_path,
365+
stub_archive_path,
366+
stub_compression,
362367
submission_path,
368+
submission_compression,
363369
tmc_param,
364370
top_level_dir_name,
365371
} => {
@@ -400,7 +406,7 @@ fn run_app(matches: Opt) -> Result<()> {
400406
top_level_dir_name,
401407
tmc_params,
402408
&clone_path,
403-
stub_zip_path.as_deref(),
409+
stub_archive_path.as_deref(),
404410
output_format,
405411
)?;
406412
OutputKind::finished(format!(

tmc-langs-framework/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,42 @@ pub use self::{
2323
tmc_project_yml::{PythonVer, TmcProjectYml},
2424
};
2525
pub use nom;
26+
use serde::Deserialize;
27+
use std::{fmt::Display, str::FromStr};
28+
use ts_rs::TS;
29+
30+
/// Supported compression methods.
31+
#[derive(Debug, Clone, Copy, Deserialize)]
32+
#[cfg_attr(feature = "ts", derive(TS))]
33+
pub enum Compression {
34+
/// .tar
35+
Tar,
36+
/// .zip
37+
Zip,
38+
/// .tar.ztd
39+
TarZstd,
40+
}
41+
42+
impl Display for Compression {
43+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44+
match self {
45+
Self::Tar => write!(f, "tar"),
46+
Self::Zip => write!(f, "zip"),
47+
Self::TarZstd => write!(f, "zstd"),
48+
}
49+
}
50+
}
51+
52+
impl FromStr for Compression {
53+
type Err = &'static str;
54+
55+
fn from_str(s: &str) -> Result<Self, Self::Err> {
56+
let format = match s {
57+
"tar" => Compression::Tar,
58+
"zip" => Compression::Zip,
59+
"zstd" => Compression::TarZstd,
60+
_ => return Err("invalid format"),
61+
};
62+
Ok(format)
63+
}
64+
}

tmc-langs-plugins/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::{
99
path::{Path, PathBuf},
1010
};
1111
use tmc_langs_csharp::CSharpPlugin;
12+
use tmc_langs_framework::{Compression, LanguagePlugin, TmcError, TmcProjectYml};
1213
pub use tmc_langs_framework::{
1314
ExerciseDesc, ExercisePackagingConfiguration, Language, NothingIsStudentFilePolicy, RunResult,
1415
StudentFilePolicy, StyleValidationResult, StyleValidationStrategy,
1516
};
16-
use tmc_langs_framework::{LanguagePlugin, TmcError, TmcProjectYml};
1717
pub use tmc_langs_java::{AntPlugin, MavenPlugin};
1818
pub use tmc_langs_make::MakePlugin;
1919
pub use tmc_langs_notests::NoTestsPlugin;
@@ -50,6 +50,14 @@ pub fn extract_project_overwrite(
5050
Ok(())
5151
}
5252

53+
pub fn compress_project(path: &Path, compression: Compression) -> Result<Vec<u8>, PluginError> {
54+
match compression {
55+
Compression::Zip => compress_project_to_zip(path),
56+
Compression::Tar => todo!(),
57+
Compression::TarZstd => todo!(),
58+
}
59+
}
60+
5361
/// See `LanguagePlugin::compress_project`.
5462
// TODO: clean up
5563
pub fn compress_project_to_zip(path: &Path) -> Result<Vec<u8>, PluginError> {

tmc-langs/src/data.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,6 @@ impl Display for ShellString {
150150
}
151151
}
152152

153-
/// Output formats for an archive.
154-
#[derive(Debug)]
155-
#[cfg_attr(feature = "ts", derive(TS))]
156-
pub enum OutputFormat {
157-
Tar,
158-
Zip,
159-
TarZstd,
160-
}
161-
162153
#[derive(Debug)]
163154
pub enum DownloadResult {
164155
Success {

0 commit comments

Comments
 (0)