Fixed project creation and sdk issues for windows, dang bugs

This commit is contained in:
Eric Ratliff
2026-01-25 14:47:10 -06:00
parent 6b05a33daa
commit b664565321

View File

@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf}; use std::path::Path;
use anyhow::{Result, Context}; use anyhow::{Result, Context};
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use reqwest::blocking::Client; use reqwest::blocking::Client;
@@ -72,35 +72,35 @@ pub fn install(sdk_path: &Path) -> Result<()> {
let mut archive = zip::ZipArchive::new(file)?; let mut archive = zip::ZipArchive::new(file)?;
std::fs::create_dir_all(sdk_path)?; std::fs::create_dir_all(sdk_path)?;
archive.extract(sdk_path)?; archive.extract(sdk_path)
.context("Failed to extract Android SDK")?;
// Cleanup // Cleanup
std::fs::remove_file(&temp_zip)?; std::fs::remove_file(&temp_zip)?;
// The zip extracts to cmdline-tools/ but we need it in cmdline-tools/latest/ // The zip extracts to cmdline-tools/ but we need it in cmdline-tools/latest/
// This is required by the Android SDK structure
let extracted_tools = sdk_path.join("cmdline-tools"); let extracted_tools = sdk_path.join("cmdline-tools");
let target_location = sdk_path.join("cmdline-tools").join("latest"); let target_location = sdk_path.join("cmdline-tools").join("latest");
if extracted_tools.exists() && !target_location.exists() { if extracted_tools.exists() && !target_location.exists() {
println!("Reorganizing cmdline-tools directory structure..."); println!("Reorganizing cmdline-tools directory structure...");
// Create a temporary directory to hold the contents
let temp_dir = sdk_path.join("cmdline-tools-temp"); let temp_dir = sdk_path.join("cmdline-tools-temp");
std::fs::rename(&extracted_tools, &temp_dir)?; std::fs::rename(&extracted_tools, &temp_dir)
.context("Failed to rename cmdline-tools to temp directory")?;
// Create the proper structure std::fs::create_dir_all(&target_location)
std::fs::create_dir_all(&target_location)?; .context("Failed to create cmdline-tools/latest directory")?;
// Move everything from temp into latest/
for entry in std::fs::read_dir(&temp_dir)? { for entry in std::fs::read_dir(&temp_dir)? {
let entry = entry?; let entry = entry?;
let dest = target_location.join(entry.file_name()); let dest = target_location.join(entry.file_name());
std::fs::rename(entry.path(), dest)?; std::fs::rename(entry.path(), dest)
.with_context(|| format!("Failed to move {} to latest/", entry.file_name().to_string_lossy()))?;
} }
// Remove temp directory std::fs::remove_dir_all(&temp_dir)
std::fs::remove_dir_all(&temp_dir)?; .context("Failed to remove temporary directory")?;
} }
// Install required packages // Install required packages
@@ -114,7 +114,6 @@ pub fn install(sdk_path: &Path) -> Result<()> {
fn install_packages(sdk_path: &Path) -> Result<()> { fn install_packages(sdk_path: &Path) -> Result<()> {
println!("Installing Android SDK packages..."); println!("Installing Android SDK packages...");
// Find sdkmanager in the expected location
let sdkmanager_path = sdk_path.join("cmdline-tools").join("latest").join("bin"); let sdkmanager_path = sdk_path.join("cmdline-tools").join("latest").join("bin");
let sdkmanager = if cfg!(target_os = "windows") { let sdkmanager = if cfg!(target_os = "windows") {
@@ -142,36 +141,59 @@ fn run_sdkmanager(sdkmanager: &Path, sdk_root: &Path) -> Result<()> {
println!("Accepting licenses..."); println!("Accepting licenses...");
// Accept licenses // Build command based on OS
let mut license_cmd = Command::new(sdkmanager) let mut cmd = if cfg!(target_os = "windows") {
.arg(format!("--sdk_root={}", sdk_root.display())) let mut c = Command::new("cmd");
c.arg("/c");
c.arg(sdkmanager);
c
} else {
Command::new(sdkmanager)
};
cmd.arg(format!("--sdk_root={}", sdk_root.display()))
.arg("--licenses") .arg("--licenses")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::null()) .stdout(Stdio::piped())
.stderr(Stdio::null()) .stderr(Stdio::piped());
.spawn()
let mut child = cmd.spawn()
.context("Failed to spawn sdkmanager for licenses")?; .context("Failed to spawn sdkmanager for licenses")?;
// Write 'y' responses to accept all licenses // Write 'y' responses to accept all licenses
if let Some(mut stdin) = license_cmd.stdin.take() { if let Some(mut stdin) = child.stdin.take() {
// Write enough 'y\n' to accept all licenses (usually 5-6 licenses) // Create a string with many 'y' responses
for _ in 0..10 { let responses = "y\n".repeat(20);
let _ = stdin.write_all(b"y\n"); stdin.write_all(responses.as_bytes())
} .context("Failed to write license responses")?;
drop(stdin); // Close stdin so sdkmanager knows we're done // Explicitly drop stdin to close the pipe
drop(stdin);
} }
let status = license_cmd.wait() let output = child.wait_with_output()
.context("Failed to wait for license acceptance")?; .context("Failed to wait for license acceptance")?;
if !status.success() { if !output.status.success() {
eprintln!("License stderr: {}", String::from_utf8_lossy(&output.stderr));
eprintln!("License stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("{} License acceptance may have failed, continuing anyway...", "".yellow()); println!("{} License acceptance may have failed, continuing anyway...", "".yellow());
} else {
println!("{} Licenses accepted", "".green());
} }
println!("Installing SDK packages (this may take a few minutes)..."); println!("Installing SDK packages (this may take a few minutes)...");
// Install packages // Build command for package installation
let status = Command::new(sdkmanager) let mut cmd = if cfg!(target_os = "windows") {
let mut c = Command::new("cmd");
c.arg("/c");
c.arg(sdkmanager);
c
} else {
Command::new(sdkmanager)
};
let status = cmd
.arg(format!("--sdk_root={}", sdk_root.display())) .arg(format!("--sdk_root={}", sdk_root.display()))
.arg("platform-tools") .arg("platform-tools")
.arg("platforms;android-34") .arg("platforms;android-34")