|
20 | 20 | import java.util.HashMap;
|
21 | 21 | import java.util.LinkedHashMap;
|
22 | 22 | import java.util.List;
|
| 23 | +import java.util.Map; |
23 | 24 | import java.util.Optional;
|
24 | 25 | import java.util.ServiceLoader;
|
25 | 26 | import java.util.Set;
|
@@ -63,6 +64,18 @@ private ConfigUtils() {}
|
63 | 64 | private static final Path SHIMS_DIR = CONFIG_DIR.resolve("mcp-servers");
|
64 | 65 | private static final Path CONFIG_PATH = CONFIG_DIR.resolve("config.json");
|
65 | 66 |
|
| 67 | + private static final Map<String, String> SHELL_TO_CONFIG = Map.of( |
| 68 | + "zsh", |
| 69 | + ".zshrc", |
| 70 | + "bash", |
| 71 | + ".bashrc", |
| 72 | + "sh", |
| 73 | + ".profile", |
| 74 | + "dash", |
| 75 | + ".profile", |
| 76 | + "ksh", |
| 77 | + ".profile"); |
| 78 | + |
66 | 79 | private static final DefaultConfigProvider DEFAULT_CONFIG_PROVIDER;
|
67 | 80 |
|
68 | 81 | static {
|
@@ -410,46 +423,54 @@ public static void ensureMcpServersDirInPath() {
|
410 | 423 | private static boolean tryAddToShellConfigs(String shimsDirStr) {
|
411 | 424 | var pathExport = "export PATH=\"" + shimsDirStr + ":$PATH\"";
|
412 | 425 | var comment = "# Added by smithy-mcp";
|
413 |
| - var configFiles = List.of(".zshrc", ".bashrc", ".profile", ".bash_profile"); |
414 |
| - |
415 |
| - String addedTo = null; |
416 |
| - |
417 |
| - for (var configFile : configFiles) { |
418 |
| - var configPath = resolveFromHomeDir(configFile); |
419 |
| - |
420 |
| - if (Files.exists(configPath) && Files.isWritable(configPath)) { |
421 |
| - try { |
422 |
| - // Check if already present |
423 |
| - var lines = new ArrayList<>(Files.readAllLines(configPath, StandardCharsets.UTF_8)); |
424 |
| - boolean alreadyPresent = lines.stream() |
425 |
| - .anyMatch(line -> line.contains(shimsDirStr) |
426 |
| - && line.contains("PATH") |
427 |
| - && !(line.trim().startsWith("#"))); |
428 |
| - |
429 |
| - if (!alreadyPresent) { |
430 |
| - // Add the export statement |
431 |
| - lines.add(""); |
432 |
| - lines.add(comment); |
433 |
| - lines.add(pathExport); |
434 |
| - |
435 |
| - Files.write(configPath, |
436 |
| - lines, |
437 |
| - StandardCharsets.UTF_8, |
438 |
| - StandardOpenOption.WRITE, |
439 |
| - StandardOpenOption.TRUNCATE_EXISTING); |
440 |
| - |
441 |
| - System.out.println("Added mcp-servers directory to PATH in " + configFile); |
442 |
| - addedTo = configFile; |
443 |
| - } |
444 |
| - } catch (IOException e) { |
445 |
| - // Continue to next config file if this one fails |
446 |
| - } |
447 |
| - } |
| 426 | + |
| 427 | + // Get the current shell from SHELL environment variable |
| 428 | + String shellPath = System.getenv("SHELL"); |
| 429 | + String configFile = null; |
| 430 | + |
| 431 | + if (shellPath != null) { |
| 432 | + // Extract shell name from path (e.g., "/bin/zsh" -> "zsh") |
| 433 | + String shellName = shellPath.substring(shellPath.lastIndexOf('/') + 1); |
| 434 | + configFile = SHELL_TO_CONFIG.get(shellName); |
448 | 435 | }
|
449 | 436 |
|
450 |
| - if (addedTo != null) { |
451 |
| - System.out.println("Please restart your shell or run 'source ~/" + configFiles + "' to reload your PATH"); |
452 |
| - return true; |
| 437 | + // If we don't have a mapping for the shell or SHELL is not set, fall back to original behavior |
| 438 | + if (configFile == null) { |
| 439 | + return false; |
| 440 | + } |
| 441 | + |
| 442 | + var configPath = resolveFromHomeDir(configFile); |
| 443 | + |
| 444 | + if (Files.exists(configPath) && Files.isWritable(configPath)) { |
| 445 | + try { |
| 446 | + // Check if already present |
| 447 | + var lines = new ArrayList<>(Files.readAllLines(configPath, StandardCharsets.UTF_8)); |
| 448 | + boolean alreadyPresent = lines.stream() |
| 449 | + .anyMatch(line -> line.contains(shimsDirStr) |
| 450 | + && line.contains("PATH") |
| 451 | + && !(line.trim().startsWith("#"))); |
| 452 | + |
| 453 | + if (!alreadyPresent) { |
| 454 | + // Add the export statement |
| 455 | + lines.add(""); |
| 456 | + lines.add(comment); |
| 457 | + lines.add(pathExport); |
| 458 | + |
| 459 | + Files.write(configPath, |
| 460 | + lines, |
| 461 | + StandardCharsets.UTF_8, |
| 462 | + StandardOpenOption.WRITE, |
| 463 | + StandardOpenOption.TRUNCATE_EXISTING); |
| 464 | + |
| 465 | + System.out.println("Added mcp-servers directory to PATH in " + configFile); |
| 466 | + System.out.println( |
| 467 | + "Please restart your shell or run 'source ~/" + configFile + "' to reload your PATH"); |
| 468 | + return true; |
| 469 | + } |
| 470 | + return true; // Already present, so consider it successful |
| 471 | + } catch (IOException e) { |
| 472 | + // Fall through to return false |
| 473 | + } |
453 | 474 | }
|
454 | 475 | return false;
|
455 | 476 | }
|
|
0 commit comments