corge/command/build/
target_path.rs

1use anyhow::Context;
2use std::fs;
3use std::path::PathBuf;
4
5pub struct TargetCachePath {
6    pub project: PathBuf,
7    pub dependency: PathBuf,
8}
9
10pub struct TargetToolchainPath {
11    pub cache: TargetCachePath,
12    pub output: PathBuf,
13}
14
15pub struct TargetBuildModePath {
16    pub toolchain: TargetToolchainPath,
17}
18
19pub struct TargetPath {
20    pub build_mode: TargetBuildModePath,
21}
22
23impl TargetPath {
24    pub fn create(project_path: &PathBuf, build_mode: &str, toolchain_name: &str) -> anyhow::Result<Self> {
25        let toolchain_path = project_path.join("target").join(build_mode).join(toolchain_name);
26        let cache_path = toolchain_path.join("cache");
27
28        let this = Self {
29            build_mode: TargetBuildModePath {
30                toolchain: TargetToolchainPath {
31                    cache: TargetCachePath {
32                        project: cache_path.join("project"),
33                        dependency: cache_path.join("dependency"),
34                    },
35                    output: toolchain_path.join("output"),
36                }
37            }
38        };
39
40        fs::create_dir_all(&this.build_mode.toolchain.cache.project)
41            .context("Failed to create target project cache directory")?;
42        fs::create_dir_all(&this.build_mode.toolchain.cache.dependency)
43            .context("Failed to create target dependency cache directory")?;
44        fs::create_dir_all(&this.build_mode.toolchain.output)
45            .context("Failed to create target output directory")?;
46
47        Ok(this)
48    }
49}