astudios 0.1.3

A CLI tool for managing Android Studio versions
Documentation
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use crate::cli::{Cli, Commands};
use astudios::{
    config::Config,
    downloader::Downloader,
    error::AstudiosError,
    installer::Installer,
    list::AndroidStudioLister,
    model::{AndroidStudio, InstalledAndroidStudio, ReleaseChannel},
    progress::ProgressReporter,
};
use colored::Colorize;
use std::{fs, path::Path, path::PathBuf, process::Command};

/// Handles all CLI commands with proper error handling and user feedback
pub struct CommandHandler;

impl CommandHandler {
    /// Main entry point for handling CLI commands
    pub fn handle(cli: Cli) -> Result<(), AstudiosError> {
        match cli.command {
            Commands::List {
                release,
                beta,
                canary,
                limit,
                all_platforms,
            } => Self::handle_list(release, beta, canary, limit, all_platforms),
            Commands::Download {
                version,
                latest,
                latest_prerelease,
                directory,
            } => Self::handle_download(
                version.as_deref(),
                latest,
                latest_prerelease,
                directory.as_deref(),
            ),
            Commands::Install {
                version,
                latest,
                directory,
                skip_checks,
            } => Self::handle_install(
                version.as_deref(),
                latest,
                directory.as_deref(),
                skip_checks,
            ),
            Commands::Uninstall { version } => Self::handle_uninstall(&version),
            Commands::Use { version } => Self::handle_use(&version),
            Commands::Installed => Self::handle_installed(),
            Commands::Which => Self::handle_which(),
            Commands::Update => Self::handle_update(),
            Commands::Open { path } => Self::handle_open(&path),
        }
    }

    /// Handle the list command to display available Android Studio versions
    fn handle_list(
        release: bool,
        beta: bool,
        canary: bool,
        limit: Option<usize>,
        all_platforms: bool,
    ) -> Result<(), AstudiosError> {
        let lister = AndroidStudioLister::new()?;
        let releases = lister.get_releases()?;

        let mut items = lister.filter_by_channel(releases, release, beta, canary);

        // Filter by current platform unless all_platforms flag is set
        if !all_platforms {
            items = lister.filter_by_current_platform(items);
        }

        let display_items: Vec<_> = if let Some(limit) = limit {
            items.into_iter().take(limit).collect()
        } else {
            items
        };

        // Get installed versions and active version for status display
        let installer = Installer::new()?;
        let installed_studios = installer.list_installed_studios().unwrap_or_default();
        let active_studio = installer.get_active_studio().unwrap_or_default();

        // Display header with platform information
        if all_platforms {
            println!(
                "{}",
                "Available Android Studio versions (all platforms):"
                    .green()
                    .bold()
            );
        } else {
            println!(
                "{} {}:",
                "Available Android Studio versions for".green().bold(),
                AndroidStudioLister::get_current_platform_name()
                    .green()
                    .bold()
            );
        }
        println!();

        if display_items.is_empty() {
            if all_platforms {
                println!(
                    "{} No versions found matching the specified criteria",
                    "⚠️".yellow()
                );
            } else {
                println!(
                    "{} No versions available for {} matching the specified criteria",
                    "⚠️".yellow(),
                    AndroidStudioLister::get_current_platform_name()
                );
                println!();
                println!("Use --all-platforms to see versions for all platforms");
            }
            return Ok(());
        }

        for item in display_items.iter().rev() {
            Self::print_version_info(item, &installed_studios, &active_studio);
        }

        Ok(())
    }

    fn print_version_info(
        item: &AndroidStudio,
        installed_studios: &[InstalledAndroidStudio],
        active_studio: &Option<InstalledAndroidStudio>,
    ) {
        let channel_color = match item.channel_type() {
            ReleaseChannel::Release => "Release".green(),
            ReleaseChannel::Beta => "Beta".yellow(),
            ReleaseChannel::Canary => "Canary".red(),
            ReleaseChannel::ReleaseCandidate => "RC".blue(),
            ReleaseChannel::Patch => "Patch".cyan(),
        };

        // Check if this version is installed
        let is_installed = installed_studios.iter().any(|installed| {
            // Match by version or build number
            installed.version.short_version == item.version
                || installed.version.build_version == item.build
                || installed.version.build_number == item.build
        });

        // Check if this version is currently selected/active
        let is_selected = if let Some(active) = active_studio {
            active.version.short_version == item.version
                || active.version.build_version == item.build
                || active.version.build_number == item.build
        } else {
            false
        };

        // Build status string like xcodes
        let status = match (is_installed, is_selected) {
            (true, true) => " [Installed, Selected]".green(),
            (true, false) => " [Installed]".green(),
            (false, true) => " [Selected]".green(), // This shouldn't happen in practice
            (false, false) => "".normal(),
        };

        println!(
            "{} {} ({}){}",
            ">".dimmed(),
            item.version.bold(),
            channel_color,
            status
        );
        println!("  {} {}", "Name:".dimmed(), item.name);
        println!("  {} {}", "Build:".dimmed(), item.build);
        println!("  {} {}", "Date:".dimmed(), item.date);

        // Show download information for macOS
        if let Some(download) = item.get_platform_download() {
            println!(
                "  {} {} ({})",
                "macOS:".dimmed(),
                "Available".green(),
                download.size
            );
        } else {
            println!("  {} {}", "macOS:".dimmed(), "Not Available".red());
        }

        println!();
    }

    /// Handle the install command to install Android Studio versions
    fn handle_install(
        version: Option<&str>,
        latest: bool,
        directory: Option<&str>,
        skip_checks: bool,
    ) -> Result<(), AstudiosError> {
        let lister = AndroidStudioLister::new()?;

        // Find the target version
        let target_item = if latest {
            lister.get_latest_release()?
        } else if let Some(version_query) = version {
            lister.find_version_by_query(version_query)?
        } else {
            return Err(AstudiosError::General(
                "Please specify a version or use --latest".to_string(),
            ));
        };

        let version_str = &target_item.version;
        let full_name = &target_item.name;
        let install_dir = directory.unwrap_or("/Applications");

        // Display installation header with clear formatting
        println!();
        println!("{}", "".repeat(80).dimmed());
        println!(
            "{} {} {}",
            "🚀".blue(),
            "Installing Android Studio".bold(),
            version_str.cyan()
        );
        println!("   {}", full_name.green());
        println!(
            "   {} {}",
            "Target directory:".dimmed(),
            install_dir.yellow()
        );
        println!("{}", "".repeat(80).dimmed());
        println!();

        let installer = Installer::new()?;
        installer.install_version_with_checks(version_str, full_name, directory, !skip_checks)?;

        // Display success summary
        println!();
        println!("{}", "".repeat(80).dimmed());
        println!(
            "{} {} {}",
            "".green(),
            "Installation Complete".bold().green(),
            "🎉".green()
        );
        println!();
        println!("   {} {}", "Version:".dimmed(), version_str.cyan().bold());
        println!("   {} {}", "Location:".dimmed(), install_dir.yellow());

        // Show different information based on installation directory
        if directory.is_none() || directory == Some("/Applications") {
            println!(
                "   {} {}",
                "Symlink:".dimmed(),
                "/Applications/Android Studio.app".blue()
            );
            println!();
            println!(
                "   {} Launch Android Studio from Applications or run:",
                "💡".blue()
            );
            println!("   {}", "open \"/Applications/Android Studio.app\"".cyan());
        } else {
            println!(
                "   {} {}",
                "App Bundle:".dimmed(),
                format!("{}/Android Studio {}.app", install_dir, version_str).blue()
            );
            println!();
            println!("   {} Launch Android Studio by running:", "💡".blue());
            println!(
                "   {}",
                format!(
                    "open \"{}/Android Studio {}.app\"",
                    install_dir, version_str
                )
                .cyan()
            );
        }

        println!("{}", "".repeat(80).dimmed());

        Ok(())
    }

    /// Handle the download command to download Android Studio versions
    fn handle_download(
        version: Option<&str>,
        latest: bool,
        latest_prerelease: bool,
        directory: Option<&str>,
    ) -> Result<(), AstudiosError> {
        let lister = AndroidStudioLister::new()?;

        // Find the target version
        let target_item = if latest {
            lister.get_latest_release()?
        } else if latest_prerelease {
            lister.get_latest_prerelease()?
        } else if let Some(version_query) = version {
            lister.find_version_by_query(version_query)?
        } else {
            return Err(AstudiosError::General(
                "Please specify a version or use --latest or --latest-prerelease".to_string(),
            ));
        };

        let version_str = &target_item.version;
        let full_name = &target_item.name;

        println!();
        println!(
            "{} Downloading {} ({})...",
            "🚀".blue(),
            full_name.green().bold(),
            version_str
        );
        println!();

        // Determine download directory
        let download_dir = if let Some(dir) = directory {
            PathBuf::from(dir)
        } else {
            // Create version-specific subdirectory in versions directory
            Config::default_download_dir().join(version_str)
        };

        // Ensure download directory exists
        fs::create_dir_all(&download_dir)?;

        // Get appropriate download for current platform
        let download = target_item
            .get_platform_download()
            .ok_or(AstudiosError::Download(
                "No download available for current platform".to_string(),
            ))?;

        // Create filename from URL
        let default_filename = format!("android-studio-{version_str}.dmg");
        let filename = Path::new(&download.link)
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or(&default_filename);

        let download_path = download_dir.join(filename);

        // Skip if file already exists
        if download_path.exists() {
            let metadata = fs::metadata(&download_path)?;
            if metadata.len() > 0 {
                println!(
                    "{} File already exists: {}",
                    "ℹ️".yellow(),
                    download_path.display()
                );
                return Ok(());
            }
        }

        // Use best available downloader
        let downloader = Downloader::detect_best();
        println!(
            "{} Using downloader: {}",
            "📥".blue(),
            downloader.description()
        );

        // Download the file
        downloader.download(&download.link, &download_path, Some(full_name))?;

        println!();
        println!(
            "{} {} downloaded successfully!",
            "".green(),
            full_name.green().bold()
        );
        println!("  Location: {}", download_path.display());

        Ok(())
    }

    /// Handle the uninstall command
    fn handle_uninstall(version: &str) -> Result<(), AstudiosError> {
        let installer = Installer::new()?;

        println!();
        println!("{} Uninstalling Android Studio {}...", "🗑️".red(), version);
        println!();

        installer.uninstall_version(version)?;

        println!();
        println!(
            "{} Successfully uninstalled Android Studio {}",
            "".green(),
            version
        );

        Ok(())
    }

    /// Handle the use command to switch versions
    fn handle_use(version: &str) -> Result<(), AstudiosError> {
        let installer = Installer::new()?;
        installer.switch_to_version(version)?;
        println!("{} Now using Android Studio {}", "".green(), version);
        Ok(())
    }

    /// Handle the installed command to show installed versions
    fn handle_installed() -> Result<(), AstudiosError> {
        let installer = Installer::new()?;
        let installations = installer.list_installed_studios()?;

        if installations.is_empty() {
            println!("{} No Android Studio versions installed", "⚠️".yellow());
            println!();
            println!("Use 'astudios install <version>' to install a version");
        } else {
            println!("{} Installed Android Studio versions:", "📋".green().bold());
            println!();

            let active = installer.get_active_studio()?;
            let active_id = active.as_ref().map(|a| a.identifier());

            for installation in installations {
                let is_active = active_id.as_ref() == Some(&installation.identifier());

                // Enhanced display name with better formatting
                let enhanced_name = installation.enhanced_display_name();
                let detailed_version = installation.extract_detailed_version();

                // Status indicator with better visual distinction
                let status_indicator = if is_active {
                    "✅ [Selected]".green().bold()
                } else {
                    "".normal()
                };

                // Main version line with improved formatting and proper spacing
                if is_active {
                    println!("   {} {}", enhanced_name.cyan().bold(), status_indicator);
                } else {
                    println!("   {}", enhanced_name.cyan().bold());
                }

                // Version and build info with better alignment and spacing
                println!(
                    "        Version: {} | Build: {}",
                    detailed_version.green(),
                    installation.identifier().blue()
                );

                // Path with proper formatting
                let path_str = installation.path.display().to_string();
                println!("        Path: {}", path_str.dimmed());

                println!();
            }
        }

        Ok(())
    }

    /// Handle the which command to show current version
    fn handle_which() -> Result<(), AstudiosError> {
        let installer = Installer::new()?;
        let active = installer.get_active_studio()?;

        match active {
            Some(installation) => {
                println!(
                    "{} Currently using {} ({})",
                    "".green(),
                    installation.display_name().green(),
                    installation.identifier().blue()
                );
                println!(
                    "   Path: {}",
                    installation.path.display().to_string().dimmed()
                );
            }
            None => {
                println!(
                    "{} Android Studio is not installed or symlink is missing",
                    "⚠️".yellow()
                );
                println!();
                println!("Use 'astudios install <version>' to install a version");
            }
        }

        Ok(())
    }

    /// Handle the open command to open a project with the current Android Studio
    fn handle_open(path: &str) -> Result<(), AstudiosError> {
        let installer = Installer::new()?;

        let app_path = match installer.get_active_studio()? {
            Some(studio) => studio.path,
            None => {
                // Fall back to the default symlink path
                let default_path = PathBuf::from("/Applications/Android Studio.app");
                if default_path.exists() {
                    default_path
                } else {
                    println!("{} No Android Studio installation found", "⚠️".yellow());
                    println!();
                    println!("Use 'astudios install <version>' to install a version");
                    return Ok(());
                }
            }
        };

        let project_path = PathBuf::from(path);
        let absolute_path = if project_path.is_absolute() {
            project_path
        } else {
            std::env::current_dir()?.join(project_path)
        };

        if !absolute_path.exists() {
            return Err(AstudiosError::General(format!(
                "Path does not exist: {}",
                absolute_path.display()
            )));
        }

        println!(
            "{} Opening {} with {}...",
            "🚀".blue(),
            absolute_path.display().to_string().cyan(),
            app_path.display().to_string().green()
        );

        let status = Command::new("open")
            .arg("-a")
            .arg(&app_path)
            .arg(&absolute_path)
            .status()
            .map_err(|e| AstudiosError::General(format!("Failed to launch Android Studio: {e}")))?;

        if !status.success() {
            return Err(AstudiosError::General(
                "Failed to open project with Android Studio".to_string(),
            ));
        }

        Ok(())
    }

    /// Handle the update command to refresh version cache
    fn handle_update() -> Result<(), AstudiosError> {
        let reporter = ProgressReporter::new(true);

        // Force refresh by clearing cache
        let lister = AndroidStudioLister::new()?;
        let releases = lister.get_releases()?;

        reporter.finish_with_success("Version list updated");

        println!(
            "{} Found {} available versions",
            "".green(),
            releases.items.len()
        );

        // Show the latest few versions
        let latest_versions: Vec<_> = releases.items.into_iter().take(5).collect();

        if !latest_versions.is_empty() {
            println!();
            println!("{} Latest versions:", "📋".bold());
            for item in latest_versions {
                let channel = match item.channel_type() {
                    ReleaseChannel::Release => "Release".normal(),
                    ReleaseChannel::Beta => "Beta".yellow(),
                    ReleaseChannel::Canary => "Canary".red(),
                    ReleaseChannel::ReleaseCandidate => "RC".blue(),
                    ReleaseChannel::Patch => "Patch".cyan(),
                };
                println!("  {} - {} ({}", item.version.green(), item.build, channel);
            }
        }

        Ok(())
    }
}