|
| 1 | +use std::{path::PathBuf, process::Command}; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use crate::builder::swift::swift_bin; |
| 6 | + |
| 7 | +pub const FORMAT_VERSION: u32 = 1; |
| 8 | + |
| 9 | +pub struct BuildSettings { |
| 10 | + pub debug: bool, |
| 11 | +} |
| 12 | + |
| 13 | +// TODO: Min ios version, etc. |
| 14 | +pub struct ProjectConfig { |
| 15 | + pub product: String, |
| 16 | + pub version_num: String, |
| 17 | + pub version_string: String, |
| 18 | + pub bundle_id: String, |
| 19 | + pub project_path: PathBuf, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Deserialize, Serialize)] |
| 23 | +struct TomlConfig { |
| 24 | + pub format_version: u32, |
| 25 | + pub project: ProjectTomlConfig, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Deserialize, Serialize)] |
| 29 | +struct ProjectTomlConfig { |
| 30 | + pub version_num: String, |
| 31 | + pub version_string: String, |
| 32 | + pub bundle_id: String, |
| 33 | +} |
| 34 | + |
| 35 | +// TODO: Check platforms |
| 36 | +#[derive(Deserialize)] |
| 37 | +struct SwiftPackageDump { |
| 38 | + name: String, |
| 39 | + targets: Vec<SwiftPackageTarget>, |
| 40 | +} |
| 41 | + |
| 42 | +// TODO: Resources |
| 43 | +#[derive(Deserialize)] |
| 44 | +struct SwiftPackageTarget { |
| 45 | + name: String, |
| 46 | +} |
| 47 | + |
| 48 | +impl ProjectConfig { |
| 49 | + pub fn load(project_path: PathBuf, toolchain_path: &str) -> Result<Self, String> { |
| 50 | + let toml_config = TomlConfig::load_or_default(project_path.clone())?; |
| 51 | + let swift = swift_bin(toolchain_path)?; |
| 52 | + let raw_package = Command::new(swift) |
| 53 | + .arg("package") |
| 54 | + .arg("dump-package") |
| 55 | + .current_dir(&project_path) |
| 56 | + .output() |
| 57 | + .map_err(|e| format!("Failed to execute swift command: {}", e))?; |
| 58 | + if !raw_package.status.success() { |
| 59 | + return Err(format!( |
| 60 | + "Failed to dump package: {}", |
| 61 | + String::from_utf8_lossy(&raw_package.stderr) |
| 62 | + )); |
| 63 | + } |
| 64 | + let package: SwiftPackageDump = serde_json::from_slice(&raw_package.stdout) |
| 65 | + .map_err(|e| format!("Failed to parse package dump: {}", e))?; |
| 66 | + |
| 67 | + Ok(ProjectConfig { |
| 68 | + product: package.name, |
| 69 | + version_num: toml_config.project.version_num, |
| 70 | + version_string: toml_config.project.version_string, |
| 71 | + bundle_id: toml_config.project.bundle_id, |
| 72 | + project_path, |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl TomlConfig { |
| 78 | + pub fn default(bundle_id: &str) -> Self { |
| 79 | + TomlConfig { |
| 80 | + format_version: FORMAT_VERSION, |
| 81 | + project: ProjectTomlConfig { |
| 82 | + version_num: "1".to_string(), |
| 83 | + version_string: "1.0.0".to_string(), |
| 84 | + bundle_id: bundle_id.to_string(), |
| 85 | + }, |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + pub fn load_or_default(project_path: PathBuf) -> Result<Self, String> { |
| 90 | + if project_path.exists() { |
| 91 | + Self::load(project_path) |
| 92 | + } else { |
| 93 | + let config = Self::default("com.example.myapp"); |
| 94 | + config.save(project_path)?; |
| 95 | + Ok(config) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn load(project_path: PathBuf) -> Result<Self, String> { |
| 100 | + let content = |
| 101 | + std::fs::read_to_string(project_path.join("ycode.toml")).map_err(|e| e.to_string())?; |
| 102 | + let config: TomlConfig = toml::from_str(&content).map_err(|e| e.to_string())?; |
| 103 | + if config.format_version != FORMAT_VERSION { |
| 104 | + return Err(format!( |
| 105 | + "Unsupported format version: {}, expected: {}", |
| 106 | + config.format_version, FORMAT_VERSION |
| 107 | + )); |
| 108 | + } |
| 109 | + Ok(config) |
| 110 | + } |
| 111 | + |
| 112 | + pub fn save(&self, project_path: PathBuf) -> Result<(), String> { |
| 113 | + let content = toml::to_string(self).map_err(|e| e.to_string())?; |
| 114 | + std::fs::write(project_path.join("ycode.toml"), content).map_err(|e| e.to_string())?; |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | +} |
0 commit comments