How to use the dockerfile-ast.DockerfileParser.parse function in dockerfile-ast

To help you get started, we’ve selected a few dockerfile-ast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rcjsuen / dockerfile-language-server-nodejs / src / dockerFormatter.ts View on Github external
public formatOnType(document: TextDocument, position: Position, ch: string, options: FormattingOptions): TextEdit[] {
		const dockerfile = DockerfileParser.parse(document.getText());
		// check that the inserted character is the escape character
		if (dockerfile.getEscapeCharacter() === ch) {
			for (let comment of dockerfile.getComments()) {
				// ignore if we're in a comment
				if (comment.getRange().start.line === position.line) {
					return [];
				}
			}

			const directive = dockerfile.getDirective();
			// ignore if we're in the parser directive
			if (directive && position.line === 0) {
				return [];
			}

			const content = document.getText();
github rcjsuen / dockerfile-language-server-nodejs / src / dockerAssist.ts View on Github external
public computeProposals(document: TextDocument, position: Position): CompletionItem[] | PromiseLike {
		let buffer = document.getText();
		let offset = document.offsetAt(position);
		let dockerfile = DockerfileParser.parse(buffer);
		let escapeCharacter = dockerfile.getEscapeCharacter();

		let directive = dockerfile.getDirective();
		if (directive !== null && position.line === 0) {
			let range = directive.getNameRange();
			if (position.character <= range.start.character) {
				// in whitespace before the directive's name
				return [this.createEscape(0, offset, Directive.escape)];
			} else if (position.character <= range.end.character) {
				// in the name
				return [this.createEscape(position.character - range.start.character, offset, Directive.escape)];
			}
			return [];
		}

		// directive only possible on the first line
github rcjsuen / dockerfile-language-server-nodejs / src / dockerHover.ts View on Github external
onHover(document: TextDocument, textDocumentPosition: TextDocumentPositionParams): Hover | null {
		let dockerfile = DockerfileParser.parse(document.getText());
		let directive = dockerfile.getDirective();
		let image = dockerfile.getContainingImage(textDocumentPosition.position);

		if (textDocumentPosition.position.line === 0 && directive !== null && directive.getDirective() === Directive.escape) {
			let range = directive.getNameRange();
			if (Util.isInsideRange(textDocumentPosition.position, range)) {
				return this.markdown.getMarkdown(Directive.escape);
			}
		}

		for (let instruction of image.getInstructions()) {
			for (let variable of instruction.getVariables()) {
				// are we hovering over a variable
				if (Util.isInsideRange(textDocumentPosition.position, variable.getNameRange())) {
					let resolved = image.resolveVariable(variable.getName(), variable.getNameRange().start.line);
					if (resolved || resolved === "") {
github rcjsuen / dockerfile-language-server-nodejs / src / dockerValidator.ts View on Github external
validate(document: TextDocument): Diagnostic[] {
		this.document = document;
		let problems: Diagnostic[] = [];
		let dockerfile = DockerfileParser.parse(document.getText());
		this.parseDirective(dockerfile, problems);
		let instructions = dockerfile.getInstructions();
		if (instructions.length === 0 || dockerfile.getARGs().length === instructions.length) {
			// no instructions in this file, or only ARGs
			problems.push(Validator.createNoSourceImage(document.positionAt(0), document.positionAt(0)));
		}

		let cmds = dockerfile.getCMDs();
		if (cmds.length > 1) {
			// more than one CMD found, warn the user
			for (let cmd of cmds) {
				let diagnostic = this.createMultipleInstructions(cmd.getInstructionRange(), this.settings.instructionCmdMultiple, "CMD");
				if (diagnostic) {
					problems.push(diagnostic);
				}
			}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerHighlight.ts View on Github external
public computeHighlightRanges(document: TextDocument, position: Position): DocumentHighlight[] {
		let dockerfile = DockerfileParser.parse(document.getText());
		let provider = new DockerDefinition();
		let location = provider.computeDefinition(document, position);
		let image = location === null ? dockerfile.getContainingImage(position) : dockerfile.getContainingImage(location.range.start);
		const highlights: DocumentHighlight[] = [];
		if (location === null) {
			for (let instruction of dockerfile.getCOPYs()) {
				let flag = instruction.getFromFlag();
				if (flag) {
					let range = flag.getValueRange();
					if (range && range.start.line === position.line && range.start.character <= position.character && position.character <= range.end.character) {
						let stage = flag.getValue();
	
						for (let other of dockerfile.getCOPYs()) {
							let otherFlag = other.getFromFlag();
							if (otherFlag && otherFlag.getValue() === stage) {
								highlights.push(DocumentHighlight.create(otherFlag.getValueRange(), DocumentHighlightKind.Read));
github alibaba / funcraft / lib / build / parser.js View on Github external
async function funfileToDockerfile(funfilePath) {

  const content = await fs.readFile(funfilePath, 'utf8');

  const funfile = DockerfileParser.parse(content);

  const dockerfile = [];

  for (let instruction of funfile.getInstructions()) {

    const ins = instruction.getInstruction();

    if (_.includes(RESERVED_DOCKER_CMD, ins)) {
      throw new Error(`Currently, Funfile does not support the semantics of '${ins}'. 
If you have a requirement, you can submit the issue at https://github.com/alibaba/funcraft/issues.`);
    }

    if (ins.toUpperCase() === 'RUNTIME') {
      const runtimeArgs = instruction.getArguments();

      if (runtimeArgs.length !== 1) {
github rcjsuen / dockerfile-language-server-nodejs / src / dockerFormatter.ts View on Github external
private format(document: TextDocument, lines: number[], options?: FormattingOptions): TextEdit[] {
		let content = document.getText();
		let dockerfile = DockerfileParser.parse(content);
		const indentedLines: boolean[] = [];
		for (let i = 0; i < document.lineCount; i++) {
			indentedLines[i] = false;
		}
		for (let instruction of dockerfile.getInstructions()) {
			let range = instruction.getRange();
			indentedLines[range.start.line] = false;
			for (let i = range.start.line + 1; i <= range.end.line; i++) {
				indentedLines[i] = true;
			}
		}
		return this.formatLines(document, content, lines, indentedLines, options);
	}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerSymbols.ts View on Github external
public parseSymbolInformation(document: TextDocument, textDocumentURI: string): SymbolInformation[] {
		let dockerfile = DockerfileParser.parse(document.getText());
		let directive = dockerfile.getDirective();
		let symbols: SymbolInformation[] = [];
		if (directive !== null) {
			symbols.push(this.createSymbolInformation(directive.getName(), textDocumentURI, directive.getNameRange(), SymbolKind.Property));
		}
		for (let instruction of dockerfile.getInstructions()) {
			symbols.push(this.createSymbolInformation(instruction.getInstruction(), textDocumentURI, instruction.getInstructionRange(), SymbolKind.Function));
		}
		return symbols;
	}
github rcjsuen / dockerfile-language-server-nodejs / src / dockerSignatures.ts View on Github external
public computeSignatures(document: TextDocument, position: Position): SignatureHelp {
		let dockerfile = DockerfileParser.parse(document.getText());
		if (position.line === 0) {
			let directive = dockerfile.getDirective();
			if (directive !== null && directive.getDirective() === Directive.escape) {
				return {
					signatures: [
						{
							label: "escape=`\\`",
							documentation: this.documentation.getDocumentation("signatureEscape"),
							parameters: [
								{
									label: "\\",
									documentation: this.documentation.getDocumentation("signatureEscape_Param")
								}
							]
						}
					],
github rcjsuen / dockerfile-language-server-nodejs / src / dockerDefinition.ts View on Github external
public computeDefinition(document: TextDocument, position: Position): Location | null {
		let dockerfile = DockerfileParser.parse(document.getText());
		let definition = this.computeBuildStageDefinition(document.uri, dockerfile, position);
		if (definition !== null) {
			return definition;
		}
		definition = this.computeVariableDefinition(document.uri, dockerfile, position);
		if (definition !== null) {
			return definition;
		}

		return null;
	}
}

dockerfile-ast

Parse a Dockerfile into an array of instructions and comments.

MIT
Latest version published 1 year ago

Package Health Score

51 / 100
Full package analysis