Skip to content

Commit 370a6bc

Browse files
bors[bot]qryxip
andauthored
Merge #63
63: Add a prefix to each package names to adapt to rust-lang/cargo#7959 r=qryxip a=qryxip Fixes #62. bors r+ Co-authored-by: Ryo Yamashita <[email protected]>
2 parents 289087e + e45143d commit 370a6bc

10 files changed

+203
-63
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Added
6+
7+
- [`new`] Added `contest` variable to `new.path`.
8+
9+
### Changed
10+
11+
- [`new`] Package names will be `"contest{}"` to adapt to [rust-lang/cargo#7959](https://github.com/rust-lang/cargo/pull/7959).
12+
13+
Modify `new.path` as following.
14+
15+
```diff
16+
-path = "./{{ package_name }}"
17+
+path = "./{{ contest }}"
18+
```
19+
20+
### Fixed
21+
22+
- [`new`] `new.path` will work correctly.
23+
- [`new`] `new` command for yukicoder will work properly.
24+
325
## [0.5.1] - 2020-09-04Z
426

527
### Added

resources/compete.toml.liquid

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,23 @@ test-suite = {% raw %}"{{ manifest_dir }}/testcases/{{ problem | kebabcase }}.ym
2020
#open = '["emacsclient", "-n"] + (.paths | map([.src, .test_suite]) | flatten)'
2121

2222
[new]
23+
# Platform
24+
#
25+
# - atcoder
26+
# - codeforces
27+
# - yukicoder
2328
platform = "{{ new_platform }}"
24-
path = {% raw %}"./{{ package_name }}"{% endraw %}
29+
# Path (Liquid template)
30+
#
31+
# Variables:
32+
#
33+
# - `contest`: Contest ID. **May be nil**
34+
# - `package_name`: Package name
35+
{% if new_platform == "yukicoder" -%}
36+
path = {% raw %}"./{{ contest | default: 'problems' }}"{% endraw %}
37+
{%- else -%}
38+
path = {% raw %}"./{{ contest }}"{% endraw %}
39+
{%- endif %}
2540

2641
[new.template]
2742
{% if new_template_lockfile -%}

src/commands/new.rs

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
},
66
shell::{ColorChoice, Shell},
77
};
8-
use anyhow::{anyhow, Context as _};
8+
use anyhow::{anyhow, bail, Context as _};
99
use heck::KebabCase as _;
1010
use itertools::Itertools as _;
1111
use liquid::object;
@@ -88,12 +88,14 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
8888
shell,
8989
)?;
9090

91-
let package_name = outcome
91+
let contest = outcome
9292
.contest
9393
.as_ref()
9494
.map(|RetrieveTestCasesOutcomeContest { id, .. }| id)
9595
.unwrap_or(&contest);
9696

97+
let group = Group::Atcoder(contest);
98+
9799
let problems = outcome
98100
.problems
99101
.iter()
@@ -105,9 +107,8 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
105107
let (manifest_dir, src_paths) = create_new_package(
106108
&cargo_compete_config_path,
107109
&cargo_compete_config,
108-
package_name,
110+
group,
109111
&problems,
110-
false,
111112
shell,
112113
)?;
113114

@@ -145,12 +146,14 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
145146
shell,
146147
)?;
147148

148-
let package_name = outcome
149+
let contest = outcome
149150
.contest
150151
.as_ref()
151152
.map(|RetrieveTestCasesOutcomeContest { id, .. }| id)
152153
.unwrap_or(&contest);
153154

155+
let group = Group::Codeforces(contest);
156+
154157
let problems = outcome
155158
.problems
156159
.iter()
@@ -162,9 +165,8 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
162165
let (manifest_dir, src_paths) = create_new_package(
163166
&cargo_compete_config_path,
164167
&cargo_compete_config,
165-
package_name,
168+
group,
166169
&problems,
167-
false,
168170
shell,
169171
)?;
170172

@@ -198,13 +200,16 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
198200
let outcome =
199201
crate::web::retrieve_testcases::dl_from_yukicoder(contest, problems, full, shell)?;
200202

201-
let package_name = outcome
203+
let contest = outcome
202204
.contest
203205
.as_ref()
204206
.map(|RetrieveTestCasesOutcomeContest { id, .. }| &**id)
205207
.or(contest);
206-
let is_no = package_name.is_none();
207-
let package_name = package_name.unwrap_or("problems");
208+
209+
let group = match contest {
210+
None => Group::YukicoderProblems,
211+
Some(contest) => Group::YukicoderContest(contest),
212+
};
208213

209214
let problems = outcome
210215
.problems
@@ -217,9 +222,8 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
217222
let (manifest_dir, src_paths) = create_new_package(
218223
&cargo_compete_config_path,
219224
&cargo_compete_config,
220-
package_name,
225+
group,
221226
&problems,
222-
is_no,
223227
shell,
224228
)?;
225229

@@ -254,31 +258,47 @@ fn urls(outcome: &RetrieveTestCasesOutcome) -> Vec<Url> {
254258
outcome.problems.iter().map(|p| p.url.clone()).collect()
255259
}
256260

261+
#[derive(Copy, Clone, Debug)]
262+
enum Group<'a> {
263+
Atcoder(&'a str),
264+
Codeforces(&'a str),
265+
YukicoderProblems,
266+
YukicoderContest(&'a str),
267+
}
268+
269+
impl<'a> Group<'a> {
270+
fn contest(self) -> Option<&'a str> {
271+
match self {
272+
Self::Atcoder(contest)
273+
| Self::Codeforces(contest)
274+
| Self::YukicoderContest(contest) => Some(contest),
275+
Self::YukicoderProblems => None,
276+
}
277+
}
278+
279+
fn package_name(self) -> String {
280+
match self {
281+
Self::Atcoder(contest) => contest.to_owned(),
282+
Self::Codeforces(contest) | Self::YukicoderContest(contest) => {
283+
format!("contest{}", contest)
284+
}
285+
Self::YukicoderProblems => "problems".to_owned(),
286+
}
287+
}
288+
}
289+
257290
fn create_new_package(
258291
cargo_compete_config_path: &Path,
259292
cargo_compete_config: &CargoCompeteConfig,
260-
package_name: &str,
293+
group: Group<'_>,
261294
problems: &BTreeMap<&str, &Url>,
262-
problems_are_yukicoder_no: bool,
263295
shell: &mut Shell,
264296
) -> anyhow::Result<(PathBuf, Vec<PathBuf>)> {
265-
crate::process::process(crate::process::cargo_exe()?)
266-
.args(&[
267-
"new",
268-
"-q",
269-
"--vcs",
270-
"none",
271-
"--name",
272-
package_name,
273-
package_name,
274-
])
275-
.cwd(cargo_compete_config_path.with_file_name(""))
276-
.exec()?;
277-
278297
let cargo_compete_config_dir = cargo_compete_config_path.with_file_name("");
279298

280299
let manifest_dir = cargo_compete_config.new.path.render(&object!({
281-
"package_name": package_name,
300+
"contest": group.contest(),
301+
"package_name": group.package_name(),
282302
}))?;
283303
let manifest_dir = Path::new(&manifest_dir);
284304
let manifest_dir = cargo_compete_config_path
@@ -287,21 +307,39 @@ fn create_new_package(
287307

288308
let manifest_path = manifest_dir.join("Cargo.toml");
289309

310+
if manifest_dir.exists() {
311+
bail!(
312+
"could not create a new package. `{}` already exists",
313+
manifest_dir.display(),
314+
);
315+
}
316+
317+
crate::process::process(crate::process::cargo_exe()?)
318+
.arg("new")
319+
.arg("-q")
320+
.arg("--vcs")
321+
.arg("none")
322+
.arg("--name")
323+
.arg(group.package_name())
324+
.arg(&manifest_dir)
325+
.cwd(cargo_compete_config_path.with_file_name(""))
326+
.exec()?;
327+
290328
let mut package_metadata_cargo_compete_bin = problems
291329
.keys()
292330
.map(|problem_index| {
293331
format!(
294332
r#"{} = {{ name = "", problem = {{ {} }} }}
295333
"#,
296334
escape_key(&problem_index.to_kebab_case()),
297-
match (cargo_compete_config.new.platform, problems_are_yukicoder_no) {
298-
(PlatformKind::Atcoder, _) | (PlatformKind::Codeforces, _) => {
335+
match group {
336+
Group::Atcoder(_) | Group::Codeforces(_) => {
299337
r#"platform = "", contest = "", index = "", url = """#
300338
}
301-
(PlatformKind::Yukicoder, true) => {
302-
r#"platform = "", kind = "no", no = "", url = """#
339+
Group::YukicoderProblems => {
340+
r#"platform = "", kind = "problem", no = 0, url = """#
303341
}
304-
(PlatformKind::Yukicoder, false) => {
342+
Group::YukicoderContest(_) => {
305343
r#"platform = "", kind = "contest", contest = "", index = "", url = """#
306344
}
307345
}
@@ -314,34 +352,35 @@ fn create_new_package(
314352
package_metadata_cargo_compete_bin[&problem_index.to_kebab_case()]["name"] =
315353
toml_edit::value(format!(
316354
"{}-{}",
317-
package_name,
355+
group.package_name(),
318356
problem_index.to_kebab_case(),
319357
));
320358

321359
let tbl =
322360
&mut package_metadata_cargo_compete_bin[&problem_index.to_kebab_case()]["problem"];
323361

324-
match cargo_compete_config.new.platform {
325-
PlatformKind::Atcoder => {
362+
match group {
363+
Group::Atcoder(contest) => {
326364
tbl["platform"] = toml_edit::value("atcoder");
327-
tbl["contest"] = toml_edit::value(package_name);
365+
tbl["contest"] = toml_edit::value(contest);
328366
tbl["index"] = toml_edit::value(&**problem_index);
329367
tbl["url"] = toml_edit::value(problem_url.as_str());
330368
}
331-
PlatformKind::Codeforces => {
369+
Group::Codeforces(contest) => {
332370
tbl["platform"] = toml_edit::value("codeforces");
333-
tbl["contest"] = toml_edit::value(package_name);
371+
tbl["contest"] = toml_edit::value(contest);
334372
tbl["index"] = toml_edit::value(&**problem_index);
335373
tbl["url"] = toml_edit::value(problem_url.as_str());
336374
}
337-
PlatformKind::Yukicoder => {
375+
Group::YukicoderProblems => {
338376
tbl["platform"] = toml_edit::value("yukicoder");
339-
if problems_are_yukicoder_no {
340-
tbl["no"] = toml_edit::value(&**problem_index);
341-
} else {
342-
tbl["contest"] = toml_edit::value(package_name);
343-
tbl["index"] = toml_edit::value(&**problem_index);
344-
}
377+
tbl["no"] = toml_edit::value(problem_index.parse::<i64>()?);
378+
tbl["url"] = toml_edit::value(problem_url.as_str());
379+
}
380+
Group::YukicoderContest(contest) => {
381+
tbl["platform"] = toml_edit::value("yukicoder");
382+
tbl["contest"] = toml_edit::value(contest);
383+
tbl["index"] = toml_edit::value(&**problem_index);
345384
tbl["url"] = toml_edit::value(problem_url.as_str());
346385
}
347386
}
@@ -353,7 +392,7 @@ fn create_new_package(
353392
let mut tbl = toml_edit::Table::new();
354393
tbl["name"] = toml_edit::value(format!(
355394
"{}-{}",
356-
package_name,
395+
group.package_name(),
357396
problem_index.to_kebab_case(),
358397
));
359398
tbl["path"] = toml_edit::value(format!("src/bin/{}.rs", problem_index.to_kebab_case()));
@@ -461,7 +500,11 @@ fn create_new_package(
461500

462501
shell.status(
463502
"Created",
464-
format!("`{}` package at {}", package_name, manifest_dir.display()),
503+
format!(
504+
"`{}` package at {}",
505+
group.package_name(),
506+
manifest_dir.display(),
507+
),
465508
)?;
466509

467510
Ok((manifest_dir, src_paths))

0 commit comments

Comments
 (0)