|
1 | | -use std::{collections::HashMap, env::current_dir, fmt::Display, fs, str::FromStr}; |
| 1 | +use std::{fmt::Display, str::FromStr}; |
2 | 2 |
|
3 | | -use anyhow::{anyhow, Result}; |
4 | | -use async_zip::base::write::ZipFileWriter; |
| 3 | +use anyhow::Result; |
5 | 4 | use clap::{Parser, Subcommand}; |
6 | | -use tokio::fs::File; |
| 5 | +use commands::init; |
| 6 | +use manifest::Loader; |
| 7 | +use modrinth::Client; |
7 | 8 |
|
| 9 | +mod commands; |
8 | 10 | mod manifest; |
9 | 11 | mod modrinth; |
10 | 12 | mod mrpack; |
11 | 13 |
|
12 | | -use manifest::{Loader, Manifest}; |
13 | | -use modrinth::{Client, VersionType}; |
14 | | -use toml_edit::{DocumentMut, InlineTable}; |
15 | | - |
16 | 14 | /// Podzol - A modpack package manager |
17 | 15 | #[derive(Parser)] |
18 | 16 | #[command(version, about, long_about = None)] |
@@ -101,100 +99,13 @@ async fn main() -> Result<()> { |
101 | 99 | projects, |
102 | 100 | project_type, |
103 | 101 | } => { |
104 | | - let manifest_src = fs::read_to_string("podzol.toml")?; |
105 | | - let mut document: DocumentMut = manifest_src.parse()?; |
106 | | - let manifest: Manifest = toml_edit::de::from_document(document.clone())?; |
107 | | - |
108 | | - for name in projects { |
109 | | - let project = client.get_project(&name).await?; |
110 | | - |
111 | | - let versions = client |
112 | | - .get_project_versions( |
113 | | - &name, |
114 | | - &manifest.enviroment.minecraft, |
115 | | - &manifest.enviroment.loaders, |
116 | | - ) |
117 | | - .await?; |
118 | | - |
119 | | - // FIXME: use a proper strategy to choose |
120 | | - let version = &versions[0]; |
121 | | - let version_number = &version.version_number; |
122 | | - |
123 | | - let mut mod_table = InlineTable::new(); |
124 | | - mod_table.insert("version", version_number.into()); |
125 | | - mod_table.insert("side", project.get_side().to_string().into()); |
126 | | - document[project_type.as_table()][&name] = mod_table.into(); |
127 | | - |
128 | | - println!( |
129 | | - "Added {name} {version_number} to {}", |
130 | | - project_type.as_table() |
131 | | - ); |
132 | | - } |
133 | | - |
134 | | - fs::write("podzol.toml", document.to_string())?; |
| 102 | + commands::add(&client, projects, project_type).await?; |
135 | 103 | } |
136 | 104 | Commands::Export => { |
137 | | - let manifest: Manifest = toml_edit::de::from_slice(&fs::read("podzol.toml")?)?; |
138 | | - |
139 | | - let mut writer = ZipFileWriter::with_tokio( |
140 | | - File::create(format!( |
141 | | - "{}-{}.mrpack", |
142 | | - manifest.pack.name, manifest.pack.version |
143 | | - )) |
144 | | - .await?, |
145 | | - ); |
146 | | - |
147 | | - manifest.build_mrpack(&client, &mut writer).await?; |
148 | | - |
149 | | - writer.close().await?; |
| 105 | + commands::export(&client).await?; |
150 | 106 | } |
151 | 107 | Commands::Init { version, name, .. } => { |
152 | | - let current_dir = current_dir().expect("Failed to get current directory"); |
153 | | - |
154 | | - let name = if let Some(name) = name { |
155 | | - name |
156 | | - } else { |
157 | | - // TODO: some degree of error handling I guess |
158 | | - current_dir |
159 | | - .file_name() |
160 | | - .and_then(|name| name.to_str()) |
161 | | - .unwrap_or("pack") |
162 | | - .to_string() |
163 | | - }; |
164 | | - |
165 | | - let minecraft_version = if let Some(version) = version { |
166 | | - version |
167 | | - } else { |
168 | | - let versions = client.get_game_versions().await?; |
169 | | - |
170 | | - let latest_version = versions |
171 | | - .into_iter() |
172 | | - .filter(|version| matches!(version.version_type, VersionType::Release)) |
173 | | - .max_by_key(|version| version.date) |
174 | | - .ok_or(anyhow!("No valid Minecraft versions found"))?; |
175 | | - |
176 | | - latest_version.version |
177 | | - }; |
178 | | - |
179 | | - let manifest = Manifest { |
180 | | - pack: manifest::Pack { |
181 | | - name, |
182 | | - version: "0.1.0".to_string(), |
183 | | - description: None, |
184 | | - }, |
185 | | - enviroment: manifest::Enviroment { |
186 | | - minecraft: minecraft_version, |
187 | | - loaders: HashMap::new(), |
188 | | - }, |
189 | | - files: HashMap::new(), |
190 | | - mods: HashMap::new(), |
191 | | - resource_packs: HashMap::new(), |
192 | | - shaders: HashMap::new(), |
193 | | - }; |
194 | | - |
195 | | - fs::write("podzol.toml", &toml_edit::ser::to_string_pretty(&manifest)?)?; |
196 | | - |
197 | | - git2::Repository::init(current_dir)?; |
| 108 | + init(&client, version, name).await?; |
198 | 109 | } |
199 | 110 | Commands::Remove => todo!(), |
200 | 111 | } |
|
0 commit comments