|
@@ -0,0 +1,197 @@
|
|
|
|
+use crate::{CliSettings, Result, Workspace};
|
|
|
|
+use anyhow::{anyhow, Context};
|
|
|
|
+use std::{
|
|
|
|
+ path::{Path, PathBuf},
|
|
|
|
+ process::Stdio,
|
|
|
|
+};
|
|
|
|
+use tokio::process::Command;
|
|
|
|
+
|
|
|
|
+#[derive(Debug)]
|
|
|
|
+pub(crate) struct TailwindCli {
|
|
|
|
+ version: String,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl TailwindCli {
|
|
|
|
+ const V3_TAG: &'static str = "v3.4.15";
|
|
|
|
+ const V4_TAG: &'static str = "v4.1.5";
|
|
|
|
+
|
|
|
|
+ pub(crate) fn new(version: String) -> Self {
|
|
|
|
+ Self { version }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) async fn serve(
|
|
|
|
+ manifest_dir: PathBuf,
|
|
|
|
+ input_path: Option<PathBuf>,
|
|
|
|
+ output_path: Option<PathBuf>,
|
|
|
|
+ ) -> Result<tokio::task::JoinHandle<Result<()>>> {
|
|
|
|
+ Ok(tokio::spawn(async move {
|
|
|
|
+ let Some(tailwind) = Self::autodetect(&manifest_dir) else {
|
|
|
|
+ return Ok(());
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ if !tailwind.get_binary_path()?.exists() {
|
|
|
|
+ tracing::info!("Installing tailwindcss@{}", tailwind.version);
|
|
|
|
+ tailwind.install_github().await?;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let proc = tailwind.watch(&manifest_dir, input_path, output_path)?;
|
|
|
|
+ proc.wait_with_output().await?;
|
|
|
|
+
|
|
|
|
+ Ok(())
|
|
|
|
+ }))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Use the correct tailwind version based on the manifest directory.
|
|
|
|
+ /// - If `tailwind.config.js` or `tailwind.config.ts` exists, use v3.
|
|
|
|
+ /// - If `tailwind.css` exists, use v4.
|
|
|
|
+ pub(crate) fn autodetect(manifest_dir: &Path) -> Option<Self> {
|
|
|
|
+ if manifest_dir.join("tailwind.config.js").exists() {
|
|
|
|
+ return Some(Self::v3());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if manifest_dir.join("tailwind.config.ts").exists() {
|
|
|
|
+ return Some(Self::v3());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if manifest_dir.join("tailwind.css").exists() {
|
|
|
|
+ return Some(Self::v4());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ None
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn v4() -> Self {
|
|
|
|
+ Self::new(Self::V4_TAG.to_string())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn v3() -> Self {
|
|
|
|
+ Self::new(Self::V3_TAG.to_string())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn watch(
|
|
|
|
+ &self,
|
|
|
|
+ manifest_dir: &Path,
|
|
|
|
+ input_path: Option<PathBuf>,
|
|
|
|
+ output_path: Option<PathBuf>,
|
|
|
|
+ ) -> Result<tokio::process::Child> {
|
|
|
|
+ let binary_path = self.get_binary_path()?;
|
|
|
|
+
|
|
|
|
+ let input_path = input_path.unwrap_or_else(|| manifest_dir.join("tailwind.css"));
|
|
|
|
+ let output_path =
|
|
|
|
+ output_path.unwrap_or_else(|| manifest_dir.join("assets").join("tailwind.css"));
|
|
|
|
+
|
|
|
|
+ if !output_path.exists() {
|
|
|
|
+ std::fs::create_dir_all(output_path.parent().unwrap())
|
|
|
|
+ .context("failed to create tailwindcss output directory")?;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let mut cmd = Command::new(binary_path);
|
|
|
|
+ let proc = cmd
|
|
|
|
+ .arg("--input")
|
|
|
|
+ .arg(input_path)
|
|
|
|
+ .arg("--output")
|
|
|
|
+ .arg(output_path)
|
|
|
|
+ .arg("--watch")
|
|
|
|
+ .stdout(Stdio::piped())
|
|
|
|
+ .stderr(Stdio::piped())
|
|
|
|
+ .spawn()?;
|
|
|
|
+
|
|
|
|
+ Ok(proc)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn get_binary_path(&self) -> anyhow::Result<PathBuf> {
|
|
|
|
+ if CliSettings::prefer_no_downloads() {
|
|
|
|
+ which::which("tailwindcss").map_err(|_| anyhow!("Missing tailwindcss@{}", self.version))
|
|
|
|
+ } else {
|
|
|
|
+ let installed_name = self.installed_bin_name();
|
|
|
|
+ let install_dir = self.install_dir()?;
|
|
|
|
+ Ok(install_dir.join(installed_name))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn installed_bin_name(&self) -> String {
|
|
|
|
+ let mut name = format!("tailwindcss-{}", self.version);
|
|
|
|
+ if cfg!(windows) {
|
|
|
|
+ name = format!("{name}.exe");
|
|
|
|
+ }
|
|
|
|
+ name
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async fn install_github(&self) -> anyhow::Result<()> {
|
|
|
|
+ tracing::debug!(
|
|
|
|
+ "Attempting to install tailwindcss@{} from GitHub",
|
|
|
|
+ self.version
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ let url = self.git_install_url().ok_or_else(|| {
|
|
|
|
+ anyhow!(
|
|
|
|
+ "no available GitHub binary for tailwindcss@{}",
|
|
|
|
+ self.version
|
|
|
|
+ )
|
|
|
|
+ })?;
|
|
|
|
+
|
|
|
|
+ // Get the final binary location.
|
|
|
|
+ let binary_path = self.get_binary_path()?;
|
|
|
|
+
|
|
|
|
+ // Download then extract tailwindcss.
|
|
|
|
+ let bytes = reqwest::get(url).await?.bytes().await?;
|
|
|
|
+
|
|
|
|
+ std::fs::create_dir_all(binary_path.parent().unwrap())
|
|
|
|
+ .context("failed to create tailwindcss directory")?;
|
|
|
|
+
|
|
|
|
+ std::fs::write(&binary_path, &bytes).context("failed to write tailwindcss binary")?;
|
|
|
|
+
|
|
|
|
+ // Make the binary executable.
|
|
|
|
+ #[cfg(unix)]
|
|
|
|
+ {
|
|
|
|
+ use std::os::unix::fs::PermissionsExt;
|
|
|
|
+ let mut perms = binary_path.metadata()?.permissions();
|
|
|
|
+ perms.set_mode(0o755);
|
|
|
|
+ std::fs::set_permissions(&binary_path, perms)?;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Ok(())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn downloaded_bin_name(&self) -> Option<String> {
|
|
|
|
+ let platform = match target_lexicon::HOST.operating_system {
|
|
|
|
+ target_lexicon::OperatingSystem::Linux => "linux",
|
|
|
|
+ target_lexicon::OperatingSystem::Darwin(_) => "macos",
|
|
|
|
+ target_lexicon::OperatingSystem::Windows => "windows",
|
|
|
|
+ _ => return None,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ let arch = match target_lexicon::HOST.architecture {
|
|
|
|
+ target_lexicon::Architecture::X86_64 if platform == "windows" => "x64.exe",
|
|
|
|
+ target_lexicon::Architecture::X86_64 => "x64",
|
|
|
|
+ target_lexicon::Architecture::Aarch64(_) => "arm64",
|
|
|
|
+ _ => return None,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ Some(format!("tailwindcss-{}-{}", platform, arch))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn install_dir(&self) -> Result<PathBuf> {
|
|
|
|
+ let bindgen_dir = Workspace::dioxus_home_dir().join("tailwind/");
|
|
|
|
+ Ok(bindgen_dir)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn git_install_url(&self) -> Option<String> {
|
|
|
|
+ // eg:
|
|
|
|
+ //
|
|
|
|
+ // https://github.com/tailwindlabs/tailwindcss/releases/download/v4.1.5/tailwindcss-linux-arm64
|
|
|
|
+ //
|
|
|
|
+ // tailwindcss-linux-arm64
|
|
|
|
+ // tailwindcss-linux-x64
|
|
|
|
+ // tailwindcss-macos-arm64
|
|
|
|
+ // tailwindcss-macos-x64
|
|
|
|
+ // tailwindcss-windows-x64.exe
|
|
|
|
+ // tailwindcss-linux-arm64-musl
|
|
|
|
+ // tailwindcss-linux-x64-musl
|
|
|
|
+ Some(format!(
|
|
|
|
+ "https://github.com/tailwindlabs/tailwindcss/releases/download/{}/{}",
|
|
|
|
+ self.version,
|
|
|
|
+ self.downloaded_bin_name()?
|
|
|
|
+ ))
|
|
|
|
+ }
|
|
|
|
+}
|