import * as path from "path" ; import * as vscode from "vscode" ; import { getCurrentBuildTool } from './imageToolRegistry' ; import { kubeChannel } from "../kubeChannel" ; import { shell } from "../shell" ; param shellOpts param imagePrefix return export async function buildAndPushImage ( shellOpts , imagePrefix ? ) { const cwd = shellOpts . cwd || vscode . workspace . rootPath ; const image = await getDefaultImageName ( cwd , imagePrefix ) ; await buildImage ( image , shellOpts ) ; if ( imagePrefix ) { await pushImage ( image , shellOpts ) ; } return image ; } /**
 * Returns a promise that resolves to a command line for building the given image.
 * Rejects if couldn't get a command line.
 */ export async function getBuildCommand ( image ) { const tool = await getCurrentBuildTool ( ) ; return tool . getBuildCommand ( image ) ; } /**
 * Returns a promise that resolves to a command line for pushing the given image.
 * Rejects if couldn't get a command line.
 */ export async function getPushCommand ( image ) { const tool = await getCurrentBuildTool ( ) ; return tool . getPushCommand ( image ) ; } function sanitiseTag ( name ) { return name . toLowerCase ( ) . replace ( /[^a-z0-9._-]/g , '-' ) ; } async function getDefaultImageName ( cwd , imagePrefix ? ) { const name = sanitiseTag ( path . basename ( cwd ) ) ; const version = await findVersion ( cwd ) ; let image = `${ name }:${ version }` ; if ( imagePrefix ) { image = `${ imagePrefix }/${ image }` ; } return image ; } async function findVersion ( cwd ) { const shellOpts = Object . assign ( { } , shell . execOpts ( ) , { cwd } ) ; const shellResult = await shell . execCore ( 'git describe --always --dirty' , shellOpts ) ; return shellResult . code === 0 ? shellResult . stdout . trim ( ) : "latest" ; } async function buildImage ( image , shellOpts ) { const buildCommand = await getBuildCommand ( image ) ; const result = await shell . execCore ( buildCommand , shellOpts ) ; if ( result . code !== 0 ) { throw new Error ( `Image build failed: ${ result . stderr }` ) ; } kubeChannel . showOutput ( image + ' built.' ) ; } async function pushImage ( image , shellOpts ) { const pushCommand = await getPushCommand ( image ) ; const result = await shell . execCore ( pushCommand , shellOpts ) ; if ( result . code !== 0 ) { throw new Error ( `Image push failed: ${ result . stderr }` ) ; } kubeChannel . showOutput ( image + ' pushed.' ) ; }