valheim-mod-manager 0.3.0

Mod manager for Valheim game
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use crate::cli::ProfileCommand;
use crate::error::AppResult;
use crate::target::GAME;
use std::path::Path;
use thunderstore_engine::profile::{layout, modlist};

/// Runs a profile-management operation against `base`.
///
/// Profile directories are the r2modman compatibility unit, and the persisted
/// *selection*, the choice of which profile subsequent commands operate on, is
/// now maintained by the engine's `layout` functions too: `layout::rename` and
/// `layout::delete` never leave it pointing at a directory that no longer
/// exists. What is left here is wording, dispatch, and the odd bit of pre-flight
/// reporting (whether a `println!` is warranted) that the engine has no reason
/// to know about.
/// Refuses a name that does not already exist, in the same words `vmm profile
/// use` and `--profile` use for it.
///
/// The engine's `delete`, `rename`, and `duplicate` do not check existence
/// first: they let `remove_dir_all`, `fs::rename`, and `read_dir` fail, which
/// surfaces an errno rather than advice, and words the one mistake three more
/// ways. [`layout::require_profile`] is the same predicate `use_profile` and
/// `InstallTarget::resolve` gate on, so going through it is what keeps all of
/// them saying the same thing.
fn require_profile(base: &Path, name: &str) -> AppResult<()> {
  layout::require_profile(base, GAME, name)
    .map_err(|bad| crate::target::profile_error(bad.into()))?;

  Ok(())
}

pub fn run(base: &Path, command: &ProfileCommand) -> AppResult<()> {
  match command {
    ProfileCommand::List => {
      let selected = layout::read_selection(base, GAME);
      let names = layout::list(base, GAME)?;

      if names.is_empty() {
        println!(
          "vmm: {}",
          crate::error::advice_message(
            "there are no profiles yet.",
            "Nothing was changed.",
            &["vmm profile create <name>"],
          )
        );

        return Ok(());
      }

      for name in names {
        let marker = if selected.as_deref() == Some(name.as_str()) {
          "*"
        } else {
          " "
        };

        // A profile whose `mods.yml` fails to parse is reported in place rather
        // than aborting: `list` is what a user reaches for precisely when
        // something already looks wrong, so one bad profile must not hide the
        // rest.
        match modlist::read(&layout::profile_dir(base, GAME, &name)) {
          Ok(mods) => println!("{marker} {name} ({} mods)", mods.len()),
          Err(error) => println!("{marker} {name} (mods.yml unreadable: {error})"),
        }
      }
    }
    ProfileCommand::Create { name } => {
      let dir = layout::create(base, GAME, name)?;

      println!("created {}", dir.display());
    }
    ProfileCommand::Delete { name } => {
      // Read before deleting: `layout::delete` clears the selection itself
      // when it pointed at the deleted profile, so this is only to decide
      // whether the second line below is warranted, not to maintain
      // anything.
      let was_selected = layout::read_selection(base, GAME).as_deref() == Some(name.as_str());

      require_profile(base, name)?;
      layout::delete(base, GAME, name)?;

      println!("deleted {name}");

      if was_selected {
        println!("cleared the selection; commands now use the game directory");
      }
    }
    ProfileCommand::Rename { from, to } => {
      // `layout::rename` carries the selection along itself when it pointed
      // at `from`, so nothing further is needed here.
      require_profile(base, from)?;
      layout::rename(base, GAME, from, to)?;

      println!("renamed {from} to {to}");
    }
    ProfileCommand::Duplicate { from, to } => {
      require_profile(base, from)?;

      let dir = layout::duplicate(base, GAME, from, to)?;

      println!("duplicated {from} to {}", dir.display());
    }
    ProfileCommand::Use { name } => {
      // The check and the write both stay in `use_profile`; vmm only re-words
      // the refusal it carries back.
      layout::use_profile(base, GAME, name).map_err(crate::target::profile_error)?;

      println!("using profile {name}");
    }
    ProfileCommand::Clear => {
      layout::clear_selection(base, GAME)?;

      println!("cleared the selection; commands now use the game directory");
    }
  }

  Ok(())
}

#[cfg(test)]
mod tests {
  use super::*;
  use tempfile::tempdir;
  use thunderstore_engine::profile::layout;

  #[test]
  fn naming_a_missing_profile_advises_the_same_way_however_it_was_named() {
    let dir = tempdir().unwrap();
    let game_dir = tempdir().unwrap();
    let base = dir.path();

    // `vmm profile use nope` and `vmm --profile nope <anything>` are the same
    // mistake, so they must not answer it to two different standards: one with
    // advice and one with the engine's bare sentence.
    let selecting = run(
      base,
      &ProfileCommand::Use {
        name: "nope".to_string(),
      },
    )
    .unwrap_err();
    let resolving = crate::target::resolve(
      base.to_path_buf(),
      game_dir.path().to_path_buf(),
      Some("nope"),
    )
    .unwrap_err();

    assert_eq!(selecting.to_string(), resolving.to_string());
    assert_eq!(
      resolving.to_string(),
      crate::target::missing_profile_error("nope").to_string()
    );
  }

  #[test]
  fn every_command_taking_an_existing_profile_advises_the_same_way_about_a_missing_one() {
    let dir = tempdir().unwrap();
    let base = dir.path();
    let expected = crate::target::missing_profile_error("nope").to_string();

    // Naming a profile that isn't there is one mistake, so it must not be
    // answered to two standards: advice from `use`, and a raw `remove_dir_all`
    // or `fs::rename` errno from everything else.
    let commands: [(&str, ProfileCommand); 4] = [
      (
        "use",
        ProfileCommand::Use {
          name: "nope".to_string(),
        },
      ),
      (
        "delete",
        ProfileCommand::Delete {
          name: "nope".to_string(),
        },
      ),
      (
        "rename",
        ProfileCommand::Rename {
          from: "nope".to_string(),
          to: "other".to_string(),
        },
      ),
      (
        "duplicate",
        ProfileCommand::Duplicate {
          from: "nope".to_string(),
          to: "other".to_string(),
        },
      ),
    ];

    for (label, command) in &commands {
      let message = run(base, command).unwrap_err().to_string();

      assert_eq!(message, expected, "`profile {label}` worded it differently");
    }

    // Refusing left nothing half-made: no directory for the copy's target name.
    assert!(!layout::profile_dir(base, GAME, "other").exists());
  }

  #[test]
  fn create_duplicate_rename_delete_lifecycle() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    run(
      base,
      &ProfileCommand::Create {
        name: "main".to_string(),
      },
    )
    .unwrap();

    assert!(layout::profile_dir(base, GAME, "main").is_dir());

    // Creating twice is refused rather than adopting the existing directory.
    assert!(
      run(
        base,
        &ProfileCommand::Create {
          name: "main".to_string(),
        }
      )
      .is_err()
    );

    run(
      base,
      &ProfileCommand::Duplicate {
        from: "main".to_string(),
        to: "copy".to_string(),
      },
    )
    .unwrap();

    run(
      base,
      &ProfileCommand::Rename {
        from: "copy".to_string(),
        to: "renamed".to_string(),
      },
    )
    .unwrap();

    assert_eq!(
      layout::list(base, GAME).unwrap(),
      vec!["main".to_string(), "renamed".to_string()]
    );

    run(
      base,
      &ProfileCommand::Delete {
        name: "renamed".to_string(),
      },
    )
    .unwrap();

    assert_eq!(layout::list(base, GAME).unwrap(), vec!["main".to_string()]);
  }

  #[test]
  fn use_requires_an_existing_profile() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    assert!(
      run(
        base,
        &ProfileCommand::Use {
          name: "ghost".to_string(),
        }
      )
      .is_err()
    );
    assert_eq!(layout::read_selection(base, GAME), None);

    run(
      base,
      &ProfileCommand::Create {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Use {
        name: "main".to_string(),
      },
    )
    .unwrap();

    assert_eq!(layout::read_selection(base, GAME), Some("main".to_string()));
  }

  #[test]
  fn renaming_the_selected_profile_follows_the_selection() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    run(
      base,
      &ProfileCommand::Create {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Use {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Rename {
        from: "main".to_string(),
        to: "primary".to_string(),
      },
    )
    .unwrap();

    // The selection must never point at a directory that no longer exists.
    assert_eq!(
      layout::read_selection(base, GAME),
      Some("primary".to_string())
    );
  }

  #[test]
  fn deleting_the_selected_profile_clears_the_selection() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    run(
      base,
      &ProfileCommand::Create {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Create {
        name: "other".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Use {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Delete {
        name: "main".to_string(),
      },
    )
    .unwrap();

    assert_eq!(layout::read_selection(base, GAME), None);

    // Deleting an unselected profile leaves the selection alone.
    run(
      base,
      &ProfileCommand::Use {
        name: "other".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Create {
        name: "third".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Delete {
        name: "third".to_string(),
      },
    )
    .unwrap();

    assert_eq!(
      layout::read_selection(base, GAME),
      Some("other".to_string())
    );
  }

  #[test]
  fn clear_returns_to_game_dir_mode() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    run(
      base,
      &ProfileCommand::Create {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Use {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(base, &ProfileCommand::Clear).unwrap();

    assert_eq!(layout::read_selection(base, GAME), None);
  }

  #[test]
  fn list_succeeds_with_no_profiles() {
    let dir = tempdir().unwrap();

    assert!(run(dir.path(), &ProfileCommand::List).is_ok());
  }

  #[test]
  fn list_survives_a_profile_with_an_unparseable_mods_yml() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    run(
      base,
      &ProfileCommand::Create {
        name: "good".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Create {
        name: "bad".to_string(),
      },
    )
    .unwrap();
    std::fs::write(
      layout::profile_dir(base, GAME, "bad").join("mods.yml"),
      "not: [valid",
    )
    .unwrap();

    // One profile's corrupt `mods.yml` must not hide every other profile.
    assert!(run(base, &ProfileCommand::List).is_ok());
  }

  #[test]
  fn list_marks_the_selected_profile() {
    let dir = tempdir().unwrap();
    let base = dir.path();

    run(
      base,
      &ProfileCommand::Create {
        name: "main".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Create {
        name: "spare".to_string(),
      },
    )
    .unwrap();
    run(
      base,
      &ProfileCommand::Use {
        name: "main".to_string(),
      },
    )
    .unwrap();

    // The `*` column is the only thing in `list`'s output naming which profile
    // every other command will act on, so it has to be driven by the selection
    // rather than by position. Listing with one profile selected and one not
    // exercises both arms of the marker.
    assert_eq!(layout::read_selection(base, GAME).as_deref(), Some("main"));
    assert!(run(base, &ProfileCommand::List).is_ok());
  }
}