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
use anyhow::{Context, Result};
use bevy::{
app::{App, Plugin},
ecs::system::ResMut,
log,
};
use config::{
builder::{ConfigBuilder, DefaultState},
File,
};
use dip_core::{config::ConfigPlugin as ConfigPluginRaw, prelude::ConfigStartupStage};
use reqwest::Url;
use serde::{de, Deserialize, Deserializer};
use std::{fs, path::PathBuf};
pub struct BundleConfigPlugin;
impl Plugin for BundleConfigPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(ConfigPluginRaw::<BundleConfig>::with_default_str(
include_str!("config/default.toml"),
))
.add_startup_system_to_stage(ConfigStartupStage::Setup, add_sources);
}
}
fn add_sources(mut builder: ResMut<ConfigBuilder<DefaultState>>) {
let config_file_path = BundleConfig::config_file_path();
*builder = builder
.clone()
.add_source(File::with_name(&config_file_path.display().to_string()));
}
pub struct Config;
impl Config {
pub fn config_dir() -> PathBuf {
let p = dirs::data_dir().unwrap().join("dip");
Self::ensure_dir(&p);
p
}
pub fn ensure_dir(p: &PathBuf) {
if !&p.is_dir() {
fs::create_dir_all(&p).unwrap();
}
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct BundleConfig {
#[serde(default)]
#[serde(deserialize_with = "ConfigParser::url_from_str")]
pub repository: Option<Url>,
#[serde(deserialize_with = "ConfigParser::path_from_str")]
bundle_root: PathBuf,
vm: VMConfig,
#[serde(deserialize_with = "ConfigParser::path_from_str")]
data_dir: PathBuf,
}
impl BundleConfig {
pub fn config_file_path() -> PathBuf {
let p = Config::config_dir().join("bundle");
Config::ensure_dir(&p);
p
}
pub fn bundle_root(&self) -> PathBuf {
Config::ensure_dir(&self.bundle_root);
self.bundle_root.clone()
}
pub fn install_root(&self) -> PathBuf {
let p = self.data_dir.join("installs");
Config::ensure_dir(&p);
p
}
pub fn shim_root(&self) -> PathBuf {
let p = self.data_dir.join("shims");
Config::ensure_dir(&p);
p
}
pub fn set_bundle_root(&mut self, bundle_root: &String) -> anyhow::Result<()> {
self.bundle_root = ConfigParser::to_path(&bundle_root.to_string())?;
Ok(())
}
pub fn runtime(&self) -> &VMRuntime {
&self.vm.runtime
}
}
struct ConfigParser;
impl ConfigParser {
fn url_from_str<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Url>, D::Error> {
let s = Deserialize::deserialize(d);
match s {
Ok(s) => match Url::parse(s) {
Ok(url) => Ok(Some(url)),
Err(e) => {
log::warn!("{e}");
Ok(None)
}
},
Err(e) => {
log::warn!("{e}");
Ok(None)
}
}
}
fn path_from_str<'de, D: Deserializer<'de>>(d: D) -> Result<PathBuf, D::Error> {
let s: String = Deserialize::deserialize(d)?;
match Self::to_path(&s) {
Ok(path) => {
if path.is_dir() {
Ok(path)
} else {
Err(de::Error::custom("Bundle path is not a directory"))
}
}
Err(_e) => Err(de::Error::custom("Failed to parse bundle directory path")),
}
}
fn to_path(value: &String) -> Result<PathBuf> {
let p = value
.replace(
"$HOME",
dirs::home_dir()
.context("Cannot find home directory.")?
.to_str()
.context("Failed to convert path to string.")?,
)
.replace(
"$CONFIG_DIR",
dirs::config_dir()
.context("Cannot find config directory.")?
.to_str()
.context("Failed to convert path to string.")?,
)
.replace(
"$DATA_DIR",
dirs::data_dir()
.context("Cannot find data directory.")?
.to_str()
.context("Failed to convert path to string.")?,
)
.into();
Ok(p)
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct VMConfig {
pub runtime: VMRuntime,
}
#[derive(Deserialize, Debug, Clone)]
pub struct VMRuntime {
pub tailwindcss: VersionSet,
pub nodejs: VersionSet,
}
pub type VersionSet = Vec<String>;