'use strict' ; /**
 * This file is loaded by both the extension and debug adapter, so it cannot import 'vscode'
 */ import fs = require ( 'fs' ) ; import os = require ( 'os' ) ; import path = require ( 'path' ) ; let binPathCache = { } ; export const envPath = process . env [ 'PATH' ] || ( process . platform === 'win32' ? process . env [ 'Path' ] : null ) ; export function getBinPathFromEnvVar ( toolName , envVarValue , appendBinToPath ) { toolName = correctBinname ( toolName ) ; if ( envVarValue ) { const paths = envVarValue . split ( path . delimiter ) ; for ( const p of paths ) { const binpath = path . join ( p , appendBinToPath ? 'bin' : '' , toolName ) ; if ( executableFileExists ( binpath ) ) { return binpath ; } } } return null ; } export function getBinPathWithPreferredGopathGoroot ( toolName , preferredGopaths , preferredGoroot ? , alternateTool ? , useCache = true ) { if ( alternateTool && path . isAbsolute ( alternateTool ) && executableFileExists ( alternateTool ) ) { binPathCache [ toolName ] = alternateTool ; return alternateTool ; } if ( useCache && binPathCache [ toolName ] ) { return binPathCache [ toolName ] ; } const binname = alternateTool && ! path . isAbsolute ( alternateTool ) ? alternateTool : toolName ; const pathFromGoBin = getBinPathFromEnvVar ( binname , process . env [ 'GOBIN' ] , false ) ; if ( pathFromGoBin ) { binPathCache [ toolName ] = pathFromGoBin ; return pathFromGoBin ; } for ( const preferred of preferredGopaths ) { if ( typeof preferred === 'string' ) { const pathFrompreferredGoPath = getBinPathFromEnvVar ( binname , preferred , true ) ; if ( pathFrompreferredGoPath ) { binPathCache [ toolName ] = pathFrompreferredGoPath ; return pathFrompreferredGoPath ; } } } const pathFromGoRoot = getBinPathFromEnvVar ( binname , preferredGoroot || getCurrentGoRoot ( ) , true ) ; if ( pathFromGoRoot ) { binPathCache [ toolName ] = pathFromGoRoot ; return pathFromGoRoot ; } const pathFromPath = getBinPathFromEnvVar ( binname , envPath , false ) ; if ( pathFromPath ) { binPathCache [ toolName ] = pathFromPath ; return pathFromPath ; } if ( toolName === 'go' ) { const defaultPathForGo = process . platform === 'win32' ? 'C:\\Go\\bin\\go.exe' : '/usr/local/go/bin/go' ; if ( executableFileExists ( defaultPathForGo ) ) { binPathCache [ toolName ] = defaultPathForGo ; return defaultPathForGo ; } return ; } return toolName ; } /**
 * Returns the goroot path if it exists, otherwise returns an empty string
 */ let currentGoRoot = '' ; export function getCurrentGoRoot ( ) { return currentGoRoot || process . env [ 'GOROOT' ] || '' ; } export function setCurrentGoRoot ( goroot ) { currentGoRoot = goroot ; } function correctBinname ( toolName ) { if ( process . platform === 'win32' ) { return toolName + '.exe' ; } return toolName ; } function executableFileExists ( filePath ) { let exists = true ; try { exists = fs . statSync ( filePath ) . isFile ( ) ; if ( exists ) { fs . accessSync ( filePath , fs . constants . F_OK | fs . constants . X_OK ) ; } } catch ( e ) { exists = false ; } return exists ; } export function fileExists ( filePath ) { try { return fs . statSync ( filePath ) . isFile ( ) ; } catch ( e ) { return false ; } } export function clearCacheForTools ( ) { binPathCache = { } ; } /**
 * Exapnds ~ to homedir in non-Windows platform
 */ export function resolveHomeDir ( inputPath ) { if ( ! inputPath || ! inputPath . trim ( ) ) { return inputPath ; } return inputPath . startsWith ( '~' ) ? path . join ( os . homedir ( ) , inputPath . substr ( 1 ) ) : inputPath ; } export function stripBOM ( s ) { if ( s && s [ 0 ] === '\uFEFF' ) { s = s . substr ( 1 ) ; } return s ; } export function parseEnvFile ( envFilePath ) { const env = { } ; if ( ! envFilePath ) { return env ; } try { const buffer = stripBOM ( fs . readFileSync ( envFilePath , 'utf8' ) ) ; buffer . split ( '\n' ) . forEach ( ( line ) => { const r = line . match ( /^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/ ) ; if ( r !== null ) { let value = r [ 2 ] || '' ; if ( value . length > 0 && value . charAt ( 0 ) === '"' && value . charAt ( value . length - 1 ) === '"' ) { value = value . replace ( /\\n/gm , '\n' ) ; } env [ r [ 1 ] ] = value . replace ( /(^['"]|['"]$)/g , '' ) ; } } ) ; return env ; } catch ( e ) { throw new Error ( `Cannot load environment variables from file ${ envFilePath }` ) ; } } export function getInferredGopath ( folderPath ) { if ( ! folderPath ) { return ; } const dirs = folderPath . toLowerCase ( ) . split ( path . sep ) ; const srcIdx = dirs . lastIndexOf ( 'src' ) ; if ( srcIdx > 0 ) { return folderPath . substr ( 0 , dirs . slice ( 0 , srcIdx ) . join ( path . sep ) . length ) ; } } param gopath param currentFileDirPath export function getCurrentGoWorkspaceFromGOPATH ( gopath , currentFileDirPath ) { if ( ! gopath ) { return ; } const workspaces = gopath . split ( path . delimiter ) ; let currentWorkspace = '' ; currentFileDirPath = fixDriveCasingInWindows ( currentFileDirPath ) ; for ( const workspace of workspaces ) { const possibleCurrentWorkspace = path . join ( workspace , 'src' ) ; if ( currentFileDirPath . startsWith ( possibleCurrentWorkspace ) || ( process . platform === 'win32' && currentFileDirPath . toLowerCase ( ) . startsWith ( possibleCurrentWorkspace . toLowerCase ( ) ) ) ) { if ( possibleCurrentWorkspace . length > currentWorkspace . length ) { currentWorkspace = currentFileDirPath . substr ( 0 , possibleCurrentWorkspace . length ) ; } } } return currentWorkspace ; } export function fixDriveCasingInWindows ( pathToFix ) { return process . platform === 'win32' && pathToFix ? pathToFix . substr ( 0 , 1 ) . toUpperCase ( ) + pathToFix . substr ( 1 ) : pathToFix ; } param toolPath export function getToolFromToolPath ( toolPath ) { if ( ! toolPath ) { return ; } let tool = path . basename ( toolPath ) ; if ( process . platform === 'win32' && tool . endsWith ( '.exe' ) ) { tool = tool . substr ( 0 , tool . length - 4 ) ; } return tool ; }