dioxus-iconify 0.4.2

CLI tool for importing/vendoring icons from [Iconify](https://icon-sets.iconify.design/) (material, lucid, heroicons,....) or from local SVG files in Dioxus projects
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
mod api;
mod generator;
mod naming;
mod svg;

use anyhow::{Context, Result, anyhow};
use clap::{Parser, Subcommand};
use std::collections::HashSet;
use std::path::{Path, PathBuf};

use api::IconifyClient;
use generator::Generator;
use naming::IconIdentifier;

#[derive(Parser)]
#[command(name = "dioxus-iconify")]
#[command(about = "CLI tool for generating Iconify icons in Dioxus projects", long_about = None)]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Output directory for generated icons (default: src/icons)
    #[arg(short, long, global = true, default_value = "src/icons")]
    output: PathBuf,
}

#[derive(Subcommand)]
enum Commands {
    /// Add one or more icons to your project
    #[command(visible_alias = "a")]
    Add {
        /// Icon identifiers, SVG file paths, or directory paths (e.g., mdi:home, ./logo.svg, ./icons/)
        #[arg(required = true)]
        icons: Vec<String>,

        /// Skip icons that already exist (don't overwrite)
        #[arg(long)]
        skip_existing: bool,
    },

    /// Initialize the icons directory (creates mod.rs)
    #[command(visible_alias = "i")]
    Init,

    /// List all generated icons
    #[command(visible_alias = "l")]
    List,

    /// Update all icons by re-fetching from API
    #[command(visible_alias = "u")]
    Update,
    // Future commands (not yet implemented)
    // /// Remove icons from your project
    // #[command(visible_alias = "r")]
    // Remove {
    //     icons: Vec<String>,
    // },
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    if let Err(err) = run().await {
        eprintln!("Error: {:#}", err);
        std::process::exit(1);
    }
}

async fn run() -> Result<()> {
    let cli = Cli::parse();
    let generator = Generator::new(cli.output.clone());

    match cli.command {
        Commands::Add {
            icons,
            skip_existing,
        } => {
            add_icons(&generator, &icons, skip_existing).await?;
        }
        Commands::Init => {
            init_icons_dir(&generator)?;
        }
        Commands::List => {
            list_icons(&generator)?;
        }
        Commands::Update => {
            update_icons(&generator).await?;
        }
    }

    Ok(())
}

async fn add_icons(generator: &Generator, inputs: &[String], skip_existing: bool) -> Result<()> {
    // Classify inputs into three categories
    let mut api_identifiers = Vec::new();
    let mut svg_files = Vec::new();
    let mut svg_directories = Vec::new();

    for input in inputs {
        let path = Path::new(input);

        if path.exists() {
            if path.is_dir() {
                svg_directories.push(path.to_path_buf());
            } else if path.extension().and_then(|s| s.to_str()) == Some("svg") {
                svg_files.push(path.to_path_buf());
            } else {
                return Err(anyhow!(
                    "Path exists but is not SVG file or directory: {}",
                    input
                ));
            }
        } else {
            // Not a filesystem path, treat as API identifier
            api_identifiers.push(input.clone());
        }
    }

    let client = IconifyClient::new()?;
    let mut icons_to_add = Vec::new();
    let mut collections = HashSet::new();
    let mut api_collections = HashSet::new(); // Track which collections came from API

    // Process API icons (existing logic)
    if !api_identifiers.is_empty() {
        println!(
            "📦 Fetching {} icon(s) from Iconify API...",
            api_identifiers.len()
        );

        for icon_id in &api_identifiers {
            // Parse icon identifier
            let identifier = IconIdentifier::parse(icon_id)
                .context(format!("Invalid icon identifier: {}", icon_id))?;

            // Track collections
            collections.insert(identifier.collection.clone());
            api_collections.insert(identifier.collection.clone());

            // Fetch icon from API
            print!("  Fetching {}... ", icon_id);
            let icon = client
                .fetch_icon(&identifier.collection, &identifier.icon_name)
                .await
                .context(format!("Failed to fetch icon: {}", icon_id))?;

            println!("");

            icons_to_add.push((identifier, icon));
        }
    }

    // Process local SVG files (NEW)
    if !svg_files.is_empty() {
        println!("\n📁 Processing {} local SVG file(s)...", svg_files.len());
        for svg_path in &svg_files {
            match process_single_svg(svg_path) {
                Ok((identifier, icon)) => {
                    println!("  {}", identifier.full_name);
                    collections.insert(identifier.collection.clone());
                    icons_to_add.push((identifier, icon));
                }
                Err(e) => {
                    eprintln!("  ⚠ Skipping {}: {}", svg_path.display(), e);
                }
            }
        }
    }

    // Process SVG directories (NEW)
    if !svg_directories.is_empty() {
        println!(
            "\n📂 Scanning {} director(ies) for SVGs...",
            svg_directories.len()
        );
        for dir_path in &svg_directories {
            let collection = match svg::extract_collection_name(dir_path) {
                Ok(c) => c,
                Err(e) => {
                    eprintln!("  ⚠ Skipping {}: {}", dir_path.display(), e);
                    continue;
                }
            };

            let svg_files = match svg::scan_svg_directory(dir_path) {
                Ok(files) => files,
                Err(e) => {
                    eprintln!("  ⚠ Error scanning {}: {}", dir_path.display(), e);
                    continue;
                }
            };

            if !svg_files.is_empty() {
                println!(
                    "  Found {} SVG(s) in {}",
                    svg_files.len(),
                    dir_path.display()
                );
            }

            for (svg_path, icon_name) in svg_files {
                let full_name = format!("{}:{}", collection, icon_name);

                match IconIdentifier::parse(&full_name) {
                    Ok(identifier) => match svg::parse_svg_file(&svg_path) {
                        Ok(icon) => {
                            collections.insert(collection.clone());
                            icons_to_add.push((identifier, icon));
                        }
                        Err(e) => {
                            eprintln!("  ⚠ Skipping {}: {}", svg_path.display(), e);
                        }
                    },
                    Err(e) => {
                        eprintln!("  ⚠ Invalid icon name {}: {}", full_name, e);
                    }
                }
            }
        }
    }

    if icons_to_add.is_empty() {
        println!("\n⚠ No icons to add");
        return Ok(());
    }

    // Handle skip-existing flag
    if skip_existing {
        let existing = generator.get_all_icon_identifiers()?;
        let existing_set: HashSet<_> = existing.iter().collect();

        let original_count = icons_to_add.len();
        icons_to_add.retain(|(id, _)| {
            let keep = !existing_set.contains(&id.full_name);
            if !keep {
                println!("  Skipping existing icon: {}", id.full_name);
            }
            keep
        });

        let skipped_count = original_count - icons_to_add.len();
        if skipped_count > 0 {
            println!("\n⏭ Skipped {} existing icon(s)", skipped_count);
        }

        if icons_to_add.is_empty() {
            println!("\n⚠ All icons already exist, nothing to add");
            return Ok(());
        }
    }

    // Fetch collection info only for API collections (not local SVGs)
    let mut collection_info = std::collections::HashMap::new();
    if !api_collections.is_empty() {
        println!("\n📚 Fetching collection metadata...");
        for collection in &api_collections {
            print!("  Fetching info for {}... ", collection);
            match client.fetch_collection_info(collection).await {
                Ok(info) => {
                    println!("");
                    collection_info.insert(collection.clone(), info);
                }
                Err(e) => {
                    println!("⚠ (skipped: {})", e);
                    // Continue without collection info - it's optional
                }
            }
        }
    }

    // Generate code
    println!("\n📝 Generating Rust code...");
    generator.add_icons(&icons_to_add, &collection_info)?;

    println!(
        "\n✨ Done! Added {} icon(s) to your project.",
        icons_to_add.len()
    );
    println!("\n💡 Usage:");
    println!("   use icons::Icon;");
    for (identifier, _) in &icons_to_add {
        println!(
            "   use icons::{}::{};",
            identifier.module_name(),
            identifier.to_const_name()
        );
    }
    println!(
        "\n   Icon {{ data: {}::{} }}",
        icons_to_add[0].0.module_name(),
        icons_to_add[0].0.to_const_name()
    );

    Ok(())
}

/// Helper function to process a single SVG file
fn process_single_svg(svg_path: &Path) -> Result<(IconIdentifier, api::IconifyIcon)> {
    let collection = svg::extract_collection_name(
        svg_path
            .parent()
            .ok_or_else(|| anyhow!("No parent directory for: {}", svg_path.display()))?,
    )?;

    let icon_name = svg_path
        .file_stem()
        .and_then(|s| s.to_str())
        .ok_or_else(|| anyhow!("Invalid filename: {}", svg_path.display()))?
        .to_string();

    let full_name = format!("{}:{}", collection, icon_name);
    let identifier = IconIdentifier::parse(&full_name)?;
    let icon = svg::parse_svg_file(svg_path)?;

    Ok((identifier, icon))
}

fn init_icons_dir(generator: &Generator) -> Result<()> {
    println!("🔧 Initializing icons directory...");
    generator.init()?;
    println!("✨ Created icons directory with mod.rs");
    println!("\n💡 Next: Run `dioxus-iconify add <icon>` to add icons");
    println!("   Example: dioxus-iconify add mdi:home");
    Ok(())
}

fn list_icons(generator: &Generator) -> Result<()> {
    let icons_by_collection = generator.list_icons()?;

    if icons_by_collection.is_empty() {
        println!("No icons found.");
        println!("\n💡 Add icons with: dioxus-iconify add <icon>");
        println!("   Example: dioxus-iconify add mdi:home");
        return Ok(());
    }

    let total_icons: usize = icons_by_collection.values().map(|v| v.len()).sum();
    println!(
        "📦 Found {} icon(s) across {} collection(s):\n",
        total_icons,
        icons_by_collection.len()
    );

    for (collection, icons) in &icons_by_collection {
        println!(
            "{}/ ({} icon{})",
            collection,
            icons.len(),
            if icons.len() == 1 { "" } else { "s" }
        );
        for icon in icons {
            println!("  {}", icon);
        }
        println!();
    }

    Ok(())
}

async fn update_icons(generator: &Generator) -> Result<()> {
    println!("🔄 Updating all icons...");

    // Get all existing icon identifiers
    let icon_ids = generator.get_all_icon_identifiers()?;

    if icon_ids.is_empty() {
        println!("No icons to update.");
        println!("\n💡 Add icons first with: dioxus-iconify add <icon>");
        println!("   Example: dioxus-iconify add mdi:home");
        return Ok(());
    }

    println!("📦 Found {} icon(s) to update", icon_ids.len());
    println!("\n🌐 Fetching latest versions from Iconify API...");

    let client = IconifyClient::new()?;
    let mut icons_to_update = Vec::new();
    let mut failed_icons = Vec::new();
    let mut collections = std::collections::HashSet::new();

    for icon_id in &icon_ids {
        // Parse icon identifier
        let identifier = match IconIdentifier::parse(icon_id) {
            Ok(id) => id,
            Err(e) => {
                eprintln!("  ⚠ Skipping invalid icon identifier {}: {}", icon_id, e);
                failed_icons.push(icon_id.clone());
                continue;
            }
        };

        // Track collections
        collections.insert(identifier.collection.clone());

        // Fetch icon from API
        print!("  Fetching {}... ", icon_id);
        match client
            .fetch_icon(&identifier.collection, &identifier.icon_name)
            .await
        {
            Ok(icon) => {
                println!("");
                icons_to_update.push((identifier, icon));
            }
            Err(e) => {
                println!("");
                eprintln!("    Error: {}", e);
                failed_icons.push(icon_id.clone());
            }
        }
    }

    if icons_to_update.is_empty() {
        eprintln!("\n❌ Failed to fetch any icons");
        return Ok(());
    }

    // Fetch collection info for all unique collections
    println!("\n📚 Fetching collection metadata...");
    let mut collection_info = std::collections::HashMap::new();
    for collection in collections {
        print!("  Fetching info for {}... ", collection);
        match client.fetch_collection_info(&collection).await {
            Ok(info) => {
                println!("");
                collection_info.insert(collection, info);
            }
            Err(e) => {
                println!("⚠ (skipped: {})", e);
                // Continue without collection info - it's optional
            }
        }
    }

    // Regenerate code
    println!("\n📝 Regenerating Rust code...");
    generator.add_icons(&icons_to_update, &collection_info)?;

    // Force regenerate mod.rs to ensure Icon component is up to date
    generator.regenerate_mod_rs()?;

    println!(
        "\n✨ Updated {} icon(s) successfully!",
        icons_to_update.len()
    );

    if !failed_icons.is_empty() {
        println!("\n⚠ Failed to update {} icon(s):", failed_icons.len());
        for icon_id in &failed_icons {
            println!("  - {}", icon_id);
        }
    }

    Ok(())
}