|
| 1 | +package dev.jbang.cli; |
| 2 | + |
| 3 | +import java.io.IOError; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.Paths; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Optional; |
| 12 | + |
| 13 | +import org.eclipse.aether.artifact.Artifact; |
| 14 | +import org.jline.terminal.Terminal; |
| 15 | +import org.jline.terminal.TerminalBuilder; |
| 16 | + |
| 17 | +import dev.jbang.dependencies.DependencyUtil; |
| 18 | +import dev.jbang.search.ArtifactSearchWidget; |
| 19 | +import dev.jbang.source.update.FileUpdateStrategy; |
| 20 | +import dev.jbang.source.update.FileUpdaters; |
| 21 | + |
| 22 | +import picocli.CommandLine; |
| 23 | +import picocli.CommandLine.Command; |
| 24 | + |
| 25 | +@Command(name = "deps", description = "Manage dependencies in jbang files.", subcommands = { DepsAdd.class, |
| 26 | + DepsSearch.class }) |
| 27 | +public class Deps extends BaseCommand { |
| 28 | + |
| 29 | + @Override |
| 30 | + public Integer doCall() throws IOException { |
| 31 | + // This is a parent command, subcommands handle the actual work |
| 32 | + return EXIT_OK; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +@Command(name = "search", header = "Search for artifacts in local and central Maven repositories.", description = { |
| 37 | + "", |
| 38 | + " ${COMMAND-FULL-NAME}", |
| 39 | + " (to search for artifacts interactively)", |
| 40 | + " or ${COMMAND-FULL-NAME} jash", |
| 41 | + " (to search for 'jash' interactively)", |
| 42 | + " or ${COMMAND-FULL-NAME} myapp.java", |
| 43 | + " (to search for dependencies and add them to myapp.java)", |
| 44 | + " or ${COMMAND-FULL-NAME} jash myapp.java", |
| 45 | + " (to search initially for 'jash' and add dependency to myapp.java)", |
| 46 | + "", |
| 47 | + " note: JBang will detect if the arguments are a file or query, but if you want to be explicit you can use the --query and --target options.", |
| 48 | + "", |
| 49 | + "" }) |
| 50 | +class DepsSearch extends BaseCommand { |
| 51 | + |
| 52 | + @CommandLine.Option(names = "--max", description = "Maximum number of results to return.", defaultValue = "100") |
| 53 | + int max; |
| 54 | + |
| 55 | + @CommandLine.Option(names = { "--query", |
| 56 | + "-q" }, description = "Artifact pattern to search for.", arity = "0..1") |
| 57 | + Optional<String> query; |
| 58 | + |
| 59 | + @CommandLine.Option(names = { |
| 60 | + "--target" }, description = "Target where to add the dependency, i.e. app.java or build.jbang", arity = "0..1") |
| 61 | + Optional<Path> target; |
| 62 | + |
| 63 | + @CommandLine.Parameters(arity = "0..2", paramLabel = "[query] [target]", description = "Artifact pattern to search for and target where to add the dependency, i.e. app.java or build.jbang. Query and target can both be optional") |
| 64 | + List<String> args = new ArrayList<>(); |
| 65 | + |
| 66 | + private boolean looksLikeATarget(String a0) { |
| 67 | + return Files.exists(Paths.get(a0)); |
| 68 | + } |
| 69 | + |
| 70 | + private void updateTarget(String a0) throws IOException { |
| 71 | + if (target.isPresent()) { |
| 72 | + throw new IllegalArgumentException("Cannot provide both target as as parameter and as option"); |
| 73 | + } else { |
| 74 | + target = Optional.of(Paths.get(a0)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void updateQuery(String a0) { |
| 79 | + if (query.isPresent()) { |
| 80 | + throw new IllegalArgumentException("Cannot provide both query as as parameter and as option"); |
| 81 | + } else { |
| 82 | + query = Optional.of(a0); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Integer doCall() throws IOException { |
| 88 | + |
| 89 | + if (args.size() == 1) { // either query or target |
| 90 | + String a0 = args.get(0); |
| 91 | + if (looksLikeATarget(a0)) { |
| 92 | + updateTarget(a0); |
| 93 | + } else { |
| 94 | + updateQuery(a0); |
| 95 | + } |
| 96 | + } else if (args.size() == 2) { // both query and target |
| 97 | + updateQuery(args.get(0)); |
| 98 | + updateTarget(args.get(1)); |
| 99 | + } |
| 100 | + |
| 101 | + if (target.isPresent() && !Files.exists(target.get())) { |
| 102 | + throw new ExitException(EXIT_INVALID_INPUT, "Target file does not exist: " + target.get()); |
| 103 | + } |
| 104 | + |
| 105 | + try (Terminal terminal = TerminalBuilder.builder().system(true).build()) { |
| 106 | + try { |
| 107 | + Artifact artifact = new ArtifactSearchWidget(terminal).search(query.orElse("")); |
| 108 | + if (target.isPresent()) { |
| 109 | + DepsAdd.updateFile(target.get(), Collections.singletonList(artifactGav(artifact))); |
| 110 | + info("Added " + artifactGav(artifact) + " to " + target.get()); |
| 111 | + } else { |
| 112 | + System.out.printf("%s:%s:%s%n", artifact.getGroupId(), artifact.getArtifactId(), |
| 113 | + artifact.getVersion()); |
| 114 | + } |
| 115 | + return EXIT_OK; |
| 116 | + } catch (IOError e) { |
| 117 | + return EXIT_INVALID_INPUT; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static String artifactGav(Artifact artifact) { |
| 123 | + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion(); |
| 124 | + } |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +@Command(name = "add", description = "Add dependencies to a jbang file.") |
| 129 | +class DepsAdd extends BaseCommand { |
| 130 | + |
| 131 | + @CommandLine.Parameters(description = "Dependencies to add (groupId:artifactId:version) and target file (.java or build.jbang)") |
| 132 | + List<String> parameters = new ArrayList<>(); |
| 133 | + |
| 134 | + @Override |
| 135 | + public Integer doCall() throws IOException { |
| 136 | + if (parameters.size() < 2) { |
| 137 | + throw new ExitException(EXIT_INVALID_INPUT, "At least one dependency and target file are required"); |
| 138 | + } |
| 139 | + |
| 140 | + // Last parameter is the target file |
| 141 | + Path targetFile = Paths.get(parameters.get(parameters.size() - 1)); |
| 142 | + List<String> dependencies = parameters.subList(0, parameters.size() - 1); |
| 143 | + |
| 144 | + if (!Files.exists(targetFile)) { |
| 145 | + throw new ExitException(EXIT_INVALID_INPUT, "Target file does not exist: " + targetFile); |
| 146 | + } |
| 147 | + |
| 148 | + updateFile(targetFile, dependencies); |
| 149 | + |
| 150 | + info("Added dependencies to " + targetFile); |
| 151 | + return EXIT_OK; |
| 152 | + } |
| 153 | + |
| 154 | + static public void updateFile(Path file, List<String> dependencies) throws IOException { |
| 155 | + // Validate dependencies |
| 156 | + for (String dep : dependencies) { |
| 157 | + if (!DependencyUtil.looksLikeAGav(dep)) { |
| 158 | + throw new ExitException(EXIT_INVALID_INPUT, "Invalid dependency format: " + dep); |
| 159 | + } |
| 160 | + } |
| 161 | + if (!Files.exists(file)) { |
| 162 | + throw new ExitException(EXIT_INVALID_INPUT, "File does not exist: " + file); |
| 163 | + } |
| 164 | + |
| 165 | + FileUpdateStrategy strategy = FileUpdaters.forFile(file); |
| 166 | + strategy.updateFile(file, dependencies); |
| 167 | + } |
| 168 | +} |
0 commit comments