import { execSync } from 'child_process' ; import { ComponentDetails } from 'jfrog-client-js' ; import * as path from 'path' ; import Set from 'typescript-collections/dist/lib/Set' ; import * as vscode from 'vscode' ; import { FocusType } from '../focus/abstractFocus' ; import { GoTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesRoot/goTree' ; import { DependenciesTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesTreeNode' ; import { TreesManager } from '../treeDataProviders/treesManager' ; export class GoUtils { public static readonly DOCUMENT_SELECTOR : vscode . DocumentSelector = { scheme : 'file' , pattern : '**/go.mod' } ; public static readonly PKG_TYPE : string = 'go' ; param document public static getDependenciesPos ( document ) { let res = [ ] ; let goModContent = document . getText ( ) ; let dependenciesMatch = goModContent . match ( 'require' ) ; if ( ! dependenciesMatch ) { return res ; } res . push ( document . positionAt ( < number > dependenciesMatch . index ) ) ; res . push ( new vscode . Position ( res [ 0 ] . line , res [ 0 ] . character + dependenciesMatch [ 0 ] . length ) ) ; return res ; } param document param dependenciesTreeNode public static getDependencyPos ( document , dependenciesTreeNode , focusType ) { let res = [ ] ; let goModContent = document . getText ( ) ; let dependencyMatch = goModContent . match ( '(' + dependenciesTreeNode . generalInfo . artifactId + 's* )vs*.*' ) ; if ( ! dependencyMatch ) { return res ; } switch ( focusType ) { case FocusType . Dependency : res . push ( document . positionAt ( < number > dependencyMatch . index ) ) ; break ; case FocusType . DependencyVersion : res . push ( document . positionAt ( < number > dependencyMatch . index + dependencyMatch [ 1 ] . length ) ) ; break ; } res . push ( new vscode . Position ( res [ 0 ] . line , res [ 0 ] . character + dependencyMatch [ 0 ] . length ) ) ; return res ; } param goMods param componentsToScan param treesManager param parent param quickScan public static async createDependenciesTrees ( goMods , componentsToScan , treesManager , parent , quickScan ) { if ( ! goMods ) { treesManager . logManager . logMessage ( 'No go.mod files found in workspaces.' , 'DEBUG' ) ; return ; } treesManager . logManager . logMessage ( 'go.mod files to scan: [' + goMods . toString ( ) + ']' , 'DEBUG' ) ; if ( ! GoUtils . verifyGoInstalled ( ) ) { vscode . window . showErrorMessage ( 'Could not scan go project dependencies, because go CLI is not in the PATH.' ) ; return ; } for ( let goMod of goMods ) { treesManager . logManager . logMessage ( 'Analyzing go.mod files' , 'INFO' ) ; let dependenciesTreeNode = new GoTreeNode ( path . dirname ( goMod . fsPath ) , componentsToScan , treesManager , parent ) ; dependenciesTreeNode . refreshDependencies ( quickScan ) ; } } public static verifyGoInstalled ( ) { try { execSync ( 'go version' ) ; } catch ( error ) { return false ; } return true ; } }