rustbasic-webp 0.0.2

High-performance WebP image conversion and optimization package for the RustBasic Framework.
Documentation
use std::fs;
use std::path::PathBuf;
use std::env;

fn main() {
    // Hanya jalankan jika kita tidak sedang dalam proses rilis atau docs
    if env::var("DOCS_RS").is_ok() {
        return;
    }

    // Ambil direktori kerja saat ini
    let project_root = match env::var("PWD") {
        Ok(pwd) => PathBuf::from(pwd),
        Err(_) => match env::current_dir() {
            Ok(dir) => dir,
            Err(_) => return,
        },
    };

    // Pastikan ini adalah proyek RustBasic (minimal ada Cargo.toml)
    if !project_root.join("Cargo.toml").exists() {
        return;
    }

    // JANGAN lakukan scaffolding jika kita sedang men-debug/mengecek paket rustbasic-webp itu sendiri
    if project_root.join("src/lib.rs").exists() && !project_root.join("src/app").exists() {
        return;
    }

    println!("cargo:warning=🖼️ rustbasic-webp: Menyiapkan scaffolding otomatis...");

    // 1. Buat folder helpers jika belum ada
    let helpers_dir = project_root.join("src/app/helpers");
    fs::create_dir_all(&helpers_dir).ok();

    // 2. Tulis berkas webp.rs helper
    let webp_helper_path = helpers_dir.join("webp.rs");
    let helper_template = r#"/* ---------------------------------------------------------
 * 📑 LABEL: WEBP UTILITY HELPER (app/helpers/webp.rs)
 * Otomatis dikonfigurasi dan dipelihara oleh rustbasic-webp.
 * --------------------------------------------------------- */

use std::path::Path;
pub use rustbasic_webp::ImageInfo;

/// Konversi gambar mentah (JPEG/PNG/dll) menjadi format WebP berkinerja tinggi.
pub fn to_webp(image_data: &[u8], quality: f32) -> Result<Vec<u8>, String> {
    rustbasic_webp::convert_to_webp(image_data, quality)
}

/// Konversi gambar mentah menjadi WebP dengan mengubah dimensi maksimal (resize).
pub fn to_webp_resized(image_data: &[u8], max_width: u32, max_height: u32, quality: f32) -> Result<Vec<u8>, String> {
    rustbasic_webp::convert_and_resize(image_data, max_width, max_height, quality)
}

/// Konversi gambar mentah menjadi WebP dengan mengubah lebar maksimal (resize width) menjaga aspek rasio.
pub fn to_webp_resized_width(image_data: &[u8], max_width: u32, quality: f32) -> Result<Vec<u8>, String> {
    rustbasic_webp::convert_and_resize_width(image_data, max_width, quality)
}

/// Konversi gambar mentah menjadi WebP dengan mengubah tinggi maksimal (resize height) menjaga aspek rasio.
pub fn to_webp_resized_height(image_data: &[u8], max_height: u32, quality: f32) -> Result<Vec<u8>, String> {
    rustbasic_webp::convert_and_resize_height(image_data, max_height, quality)
}

/// Mendapatkan informasi metadata (lebar, tinggi, format, byte) dari data gambar biner.
pub fn get_image_info(image_data: &[u8]) -> Result<ImageInfo, String> {
    rustbasic_webp::get_image_info(image_data)
}

/// Konversi gambar dari file lokal dan simpan langsung ke file WebP baru.
pub fn convert_file_to_webp<P: AsRef<Path>>(input_path: P, output_path: P, quality: f32) -> Result<(), String> {
    rustbasic_webp::convert_file_to_webp(input_path, output_path, quality)
}
"#;

    fs::write(&webp_helper_path, helper_template).ok();


    // 3. Tulis/Update src/app/helpers/mod.rs
    let helpers_mod_path = helpers_dir.join("mod.rs");
    let mut helpers_mod_content = fs::read_to_string(&helpers_mod_path).unwrap_or_default();
    if !helpers_mod_content.contains("pub mod webp;") {
        if helpers_mod_content.is_empty() {
            helpers_mod_content.push_str("pub mod webp;\n");
        } else {
            helpers_mod_content.push_str("\npub mod webp;\n");
        }
        fs::write(&helpers_mod_path, helpers_mod_content).ok();
    }

    // 4. Daftarkan 'helpers' di src/app/mod.rs
    let app_mod_path = project_root.join("src/app/mod.rs");
    if app_mod_path.exists() {
        let mut app_mod_content = fs::read_to_string(&app_mod_path).unwrap_or_default();
        if !app_mod_content.contains("pub mod helpers;") {
            if let Some(pos) = app_mod_content.find("pub mod http;") {
                app_mod_content.insert_str(pos, "pub mod helpers;\n");
            } else {
                app_mod_content.push_str("\npub mod helpers;\n");
            }
            fs::write(app_mod_path, app_mod_content).ok();
        }
    }
}