All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.jbang.source.generators.BaseCmdGenerator Maven / Gradle / Ivy

There is a newer version: 0.121.0
Show newest version
package dev.jbang.source.generators;

import java.io.IOException;
import java.util.*;

import dev.jbang.source.*;
import dev.jbang.util.CommandBuffer;
import dev.jbang.util.Util;

public abstract class BaseCmdGenerator implements CmdGenerator {
	protected final BuildContext ctx;

	protected List arguments = Collections.emptyList();
	protected Map debugString;
	protected String flightRecorderString;

	protected Util.Shell shell = Util.getShell();

	// 8192 character command line length limit imposed by CMD.EXE
	protected static final int COMMAND_LINE_LENGTH_LIMIT = 8000;

	@SuppressWarnings("unchecked")
	public T arguments(List arguments) {
		this.arguments = arguments != null ? arguments : Collections.emptyList();
		return (T) this;
	}

	@SuppressWarnings("unchecked")
	public T shell(Util.Shell shell) {
		this.shell = shell;
		return (T) this;
	}

	@SuppressWarnings("unchecked")
	public T debugString(Map debugString) {
		this.debugString = debugString != null && !debugString.isEmpty() ? debugString : null;
		return (T) this;
	}

	@SuppressWarnings("unchecked")
	public T flightRecorderString(String flightRecorderString) {
		this.flightRecorderString = flightRecorderString != null && !flightRecorderString.isEmpty()
				? flightRecorderString
				: null;
		return (T) this;
	}

	public BaseCmdGenerator(BuildContext ctx) {
		this.ctx = ctx;
	}

	@Override
	public String generate() throws IOException {
		List fullArgs = generateCommandLineList();
		return generateCommandLineString(fullArgs);
	}

	protected abstract List generateCommandLineList() throws IOException;

	protected String generateCommandLineString(List fullArgs) throws IOException {
		CommandBuffer cb = CommandBuffer.of(fullArgs);
		return cb.asCommandLine(shell);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy