|
| 1 | +package com.github.eirslett.maven.plugins.frontend.mojo; |
| 2 | + |
| 3 | +import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory; |
| 4 | +import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig; |
| 5 | +import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException; |
| 6 | +import org.apache.maven.execution.MavenSession; |
| 7 | +import org.apache.maven.plugins.annotations.Component; |
| 8 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 9 | +import org.apache.maven.plugins.annotations.Mojo; |
| 10 | +import org.apache.maven.plugins.annotations.Parameter; |
| 11 | +import org.apache.maven.settings.crypto.SettingsDecrypter; |
| 12 | +import org.sonatype.plexus.build.incremental.BuildContext; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.util.Collections; |
| 16 | + |
| 17 | +@Mojo(name = "bun", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true) |
| 18 | +public final class BunMojo extends AbstractFrontendMojo { |
| 19 | + |
| 20 | + private static final String NPM_REGISTRY_URL = "npmRegistryURL"; |
| 21 | + |
| 22 | + /** |
| 23 | + * bun arguments. Default is "install". |
| 24 | + */ |
| 25 | + @Parameter(defaultValue = "", property = "frontend.bun.arguments", required = false) |
| 26 | + private String arguments; |
| 27 | + |
| 28 | + @Parameter(property = "frontend.bun.bunInheritsProxyConfigFromMaven", required = false, |
| 29 | + defaultValue = "true") |
| 30 | + private boolean bunInheritsProxyConfigFromMaven; |
| 31 | + |
| 32 | + /** |
| 33 | + * Registry override, passed as the registry option during npm install if set. |
| 34 | + */ |
| 35 | + @Parameter(property = NPM_REGISTRY_URL, required = false, defaultValue = "") |
| 36 | + private String npmRegistryURL; |
| 37 | + |
| 38 | + @Parameter(property = "session", defaultValue = "${session}", readonly = true) |
| 39 | + private MavenSession session; |
| 40 | + |
| 41 | + @Component |
| 42 | + private BuildContext buildContext; |
| 43 | + |
| 44 | + @Component(role = SettingsDecrypter.class) |
| 45 | + private SettingsDecrypter decrypter; |
| 46 | + |
| 47 | + /** |
| 48 | + * Skips execution of this mojo. |
| 49 | + */ |
| 50 | + @Parameter(property = "skip.bun", defaultValue = "${skip.bun}") |
| 51 | + private boolean skip; |
| 52 | + |
| 53 | + @Override |
| 54 | + protected boolean skipExecution() { |
| 55 | + return this.skip; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException { |
| 60 | + File packageJson = new File(this.workingDirectory, "package.json"); |
| 61 | + if (this.buildContext == null || this.buildContext.hasDelta(packageJson) |
| 62 | + || !this.buildContext.isIncremental()) { |
| 63 | + ProxyConfig proxyConfig = getProxyConfig(); |
| 64 | + factory.getBunRunner(proxyConfig, getRegistryUrl()).execute(this.arguments, |
| 65 | + this.environmentVariables); |
| 66 | + } else { |
| 67 | + getLog().info("Skipping bun install as package.json unchanged"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private ProxyConfig getProxyConfig() { |
| 72 | + if (this.bunInheritsProxyConfigFromMaven) { |
| 73 | + return MojoUtils.getProxyConfig(this.session, this.decrypter); |
| 74 | + } else { |
| 75 | + getLog().info("bun not inheriting proxy config from Maven"); |
| 76 | + return new ProxyConfig(Collections.<ProxyConfig.Proxy>emptyList()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private String getRegistryUrl() { |
| 81 | + // check to see if overridden via `-D`, otherwise fallback to pom value |
| 82 | + return System.getProperty(NPM_REGISTRY_URL, this.npmRegistryURL); |
| 83 | + } |
| 84 | +} |
0 commit comments