|
| 1 | +use crate::{CliSettings, Result, Workspace}; |
| 2 | +use anyhow::{anyhow, Context}; |
| 3 | +use std::{ |
| 4 | + path::{Path, PathBuf}, |
| 5 | + process::Stdio, |
| 6 | +}; |
| 7 | +use tokio::process::Command; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub(crate) struct TailwindCli { |
| 11 | + version: String, |
| 12 | +} |
| 13 | + |
| 14 | +impl TailwindCli { |
| 15 | + const V3_TAG: &'static str = "v3.4.15"; |
| 16 | + const V4_TAG: &'static str = "v4.1.5"; |
| 17 | + |
| 18 | + pub(crate) fn new(version: String) -> Self { |
| 19 | + Self { version } |
| 20 | + } |
| 21 | + |
| 22 | + pub(crate) async fn serve( |
| 23 | + manifest_dir: PathBuf, |
| 24 | + input_path: Option<PathBuf>, |
| 25 | + output_path: Option<PathBuf>, |
| 26 | + ) -> Result<tokio::task::JoinHandle<Result<()>>> { |
| 27 | + Ok(tokio::spawn(async move { |
| 28 | + let Some(tailwind) = Self::autodetect(&manifest_dir) else { |
| 29 | + return Ok(()); |
| 30 | + }; |
| 31 | + |
| 32 | + if !tailwind.get_binary_path()?.exists() { |
| 33 | + tracing::info!("Installing tailwindcss@{}", tailwind.version); |
| 34 | + tailwind.install_github().await?; |
| 35 | + } |
| 36 | + |
| 37 | + let proc = tailwind.watch(&manifest_dir, input_path, output_path)?; |
| 38 | + proc.wait_with_output().await?; |
| 39 | + |
| 40 | + Ok(()) |
| 41 | + })) |
| 42 | + } |
| 43 | + |
| 44 | + /// Use the correct tailwind version based on the manifest directory. |
| 45 | + /// - If `tailwind.config.js` or `tailwind.config.ts` exists, use v3. |
| 46 | + /// - If `tailwind.css` exists, use v4. |
| 47 | + pub(crate) fn autodetect(manifest_dir: &Path) -> Option<Self> { |
| 48 | + if manifest_dir.join("tailwind.config.js").exists() { |
| 49 | + return Some(Self::v3()); |
| 50 | + } |
| 51 | + |
| 52 | + if manifest_dir.join("tailwind.config.ts").exists() { |
| 53 | + return Some(Self::v3()); |
| 54 | + } |
| 55 | + |
| 56 | + if manifest_dir.join("tailwind.css").exists() { |
| 57 | + return Some(Self::v4()); |
| 58 | + } |
| 59 | + |
| 60 | + None |
| 61 | + } |
| 62 | + |
| 63 | + pub(crate) fn v4() -> Self { |
| 64 | + Self::new(Self::V4_TAG.to_string()) |
| 65 | + } |
| 66 | + |
| 67 | + pub(crate) fn v3() -> Self { |
| 68 | + Self::new(Self::V3_TAG.to_string()) |
| 69 | + } |
| 70 | + |
| 71 | + pub(crate) fn watch( |
| 72 | + &self, |
| 73 | + manifest_dir: &Path, |
| 74 | + input_path: Option<PathBuf>, |
| 75 | + output_path: Option<PathBuf>, |
| 76 | + ) -> Result<tokio::process::Child> { |
| 77 | + let binary_path = self.get_binary_path()?; |
| 78 | + |
| 79 | + let input_path = input_path.unwrap_or_else(|| manifest_dir.join("tailwind.css")); |
| 80 | + let output_path = |
| 81 | + output_path.unwrap_or_else(|| manifest_dir.join("assets").join("tailwind.css")); |
| 82 | + |
| 83 | + if !output_path.exists() { |
| 84 | + std::fs::create_dir_all(output_path.parent().unwrap()) |
| 85 | + .context("failed to create tailwindcss output directory")?; |
| 86 | + } |
| 87 | + |
| 88 | + let mut cmd = Command::new(binary_path); |
| 89 | + let proc = cmd |
| 90 | + .arg("--input") |
| 91 | + .arg(input_path) |
| 92 | + .arg("--output") |
| 93 | + .arg(output_path) |
| 94 | + .arg("--watch") |
| 95 | + .stdout(Stdio::piped()) |
| 96 | + .stderr(Stdio::piped()) |
| 97 | + .spawn()?; |
| 98 | + |
| 99 | + Ok(proc) |
| 100 | + } |
| 101 | + |
| 102 | + fn get_binary_path(&self) -> anyhow::Result<PathBuf> { |
| 103 | + if CliSettings::prefer_no_downloads() { |
| 104 | + which::which("tailwindcss").map_err(|_| anyhow!("Missing tailwindcss@{}", self.version)) |
| 105 | + } else { |
| 106 | + let installed_name = self.installed_bin_name(); |
| 107 | + let install_dir = self.install_dir()?; |
| 108 | + Ok(install_dir.join(installed_name)) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fn installed_bin_name(&self) -> String { |
| 113 | + let mut name = format!("tailwindcss-{}", self.version); |
| 114 | + if cfg!(windows) { |
| 115 | + name = format!("{name}.exe"); |
| 116 | + } |
| 117 | + name |
| 118 | + } |
| 119 | + |
| 120 | + async fn install_github(&self) -> anyhow::Result<()> { |
| 121 | + tracing::debug!( |
| 122 | + "Attempting to install tailwindcss@{} from GitHub", |
| 123 | + self.version |
| 124 | + ); |
| 125 | + |
| 126 | + let url = self.git_install_url().ok_or_else(|| { |
| 127 | + anyhow!( |
| 128 | + "no available GitHub binary for tailwindcss@{}", |
| 129 | + self.version |
| 130 | + ) |
| 131 | + })?; |
| 132 | + |
| 133 | + // Get the final binary location. |
| 134 | + let binary_path = self.get_binary_path()?; |
| 135 | + |
| 136 | + // Download then extract tailwindcss. |
| 137 | + let bytes = reqwest::get(url).await?.bytes().await?; |
| 138 | + |
| 139 | + std::fs::create_dir_all(binary_path.parent().unwrap()) |
| 140 | + .context("failed to create tailwindcss directory")?; |
| 141 | + |
| 142 | + std::fs::write(&binary_path, &bytes).context("failed to write tailwindcss binary")?; |
| 143 | + |
| 144 | + // Make the binary executable. |
| 145 | + #[cfg(unix)] |
| 146 | + { |
| 147 | + use std::os::unix::fs::PermissionsExt; |
| 148 | + let mut perms = binary_path.metadata()?.permissions(); |
| 149 | + perms.set_mode(0o755); |
| 150 | + std::fs::set_permissions(&binary_path, perms)?; |
| 151 | + } |
| 152 | + |
| 153 | + Ok(()) |
| 154 | + } |
| 155 | + |
| 156 | + fn downloaded_bin_name(&self) -> Option<String> { |
| 157 | + let platform = match target_lexicon::HOST.operating_system { |
| 158 | + target_lexicon::OperatingSystem::Linux => "linux", |
| 159 | + target_lexicon::OperatingSystem::Darwin(_) => "macos", |
| 160 | + target_lexicon::OperatingSystem::Windows => "windows", |
| 161 | + _ => return None, |
| 162 | + }; |
| 163 | + |
| 164 | + let arch = match target_lexicon::HOST.architecture { |
| 165 | + target_lexicon::Architecture::X86_64 if platform == "windows" => "x64.exe", |
| 166 | + target_lexicon::Architecture::X86_64 => "x64", |
| 167 | + target_lexicon::Architecture::Aarch64(_) => "arm64", |
| 168 | + _ => return None, |
| 169 | + }; |
| 170 | + |
| 171 | + Some(format!("tailwindcss-{}-{}", platform, arch)) |
| 172 | + } |
| 173 | + |
| 174 | + fn install_dir(&self) -> Result<PathBuf> { |
| 175 | + let bindgen_dir = Workspace::dioxus_home_dir().join("tailwind/"); |
| 176 | + Ok(bindgen_dir) |
| 177 | + } |
| 178 | + |
| 179 | + fn git_install_url(&self) -> Option<String> { |
| 180 | + // eg: |
| 181 | + // |
| 182 | + // https://github.com/tailwindlabs/tailwindcss/releases/download/v4.1.5/tailwindcss-linux-arm64 |
| 183 | + // |
| 184 | + // tailwindcss-linux-arm64 |
| 185 | + // tailwindcss-linux-x64 |
| 186 | + // tailwindcss-macos-arm64 |
| 187 | + // tailwindcss-macos-x64 |
| 188 | + // tailwindcss-windows-x64.exe |
| 189 | + // tailwindcss-linux-arm64-musl |
| 190 | + // tailwindcss-linux-x64-musl |
| 191 | + Some(format!( |
| 192 | + "https://github.com/tailwindlabs/tailwindcss/releases/download/{}/{}", |
| 193 | + self.version, |
| 194 | + self.downloaded_bin_name()? |
| 195 | + )) |
| 196 | + } |
| 197 | +} |
0 commit comments