import * as os from 'os' ; import * as vscode from 'vscode' ; import * as assert from 'assert' ; import * as cp from 'child_process' ; import { LoggingService } from '../services/logging-service' ; import { isString , isArrayOfString } from './helper' ; export const EXTENSION_ID = 'fortran' ; export const LANG_SERVER_TOOL_ID = 'fortran-language-server' ; export const FORMATTERS = [ 'Disabled' , 'findent' , 'fprettify' ] ; export const envDelimiter = process . platform === 'win32' ? ';' : ':' ; warning param folder returns export function FortranDocumentSelector ( folder ? ) { if ( folder ) { return [ { scheme : 'file' , language : 'FortranFreeForm' , pattern : `${ folder . uri . fsPath }/**/*` } , { scheme : 'file' , language : 'FortranFixedForm' , pattern : `${ folder . uri . fsPath }/**/*` } , ] ; } else { return [ { scheme : 'file' , language : 'FortranFreeForm' } , { scheme : 'file' , language : 'FortranFixedForm' } , ] ; } } param tool param msg param toolType param opts param logger param action export function promptForMissingTool ( tool , msg , toolType , opts , logger ? , action ? ) { const items = [ 'Install' ] ; return vscode . window . showInformationMessage ( msg , ... opts ) . then ( selected => { if ( selected === 'Install' ) { switch ( toolType ) { case 'Python' : installPythonTool ( tool , logger ) ; break ; case 'VSExt' : logger . logInfo ( `Installing VS Marketplace Extension with id: ${ tool }` ) ; vscode . commands . executeCommand ( 'extension.open' , tool ) ; vscode . commands . executeCommand ( 'workbench.extensions.installExtension' , tool ) ; logger . logInfo ( `Extension ${ tool } successfully installed` ) ; break ; default : logger . logError ( `Failed to install tool: ${ tool }` ) ; vscode . window . showErrorMessage ( `Failed to install tool: ${ tool }` ) ; break ; } } else if ( selected === "Don't Show Again" ) { action ( ) ; } } ) ; } param pyPackage param logger export function installPythonTool ( pyPackage , logger ? ) { const installProcess = cp . spawn ( 'pip' , 'install --user --upgrade ' . concat ( pyPackage ) . split ( ' ' ) ) ; installProcess . stdout . on ( 'data' , data => { logger . logInfo ( `pip install: ${ data }` ) ; } ) ; installProcess . on ( 'exit' , ( code , signal ) => { if ( code !== 0 ) { logger . logError ( `Python package ${ pyPackage } failed to install with code: ${ code }, signal: ${ signal }` ) ; } else { logger . logInfo ( `Successfully installed ${ pyPackage }.` ) ; } } ) ; installProcess . on ( 'error' , err => { logger . logError ( `${ err }` ) ; } ) ; } param input param additionalEnvironment returns export function resolveVariables ( input , additionalEnvironment ? ) { if ( ! input ) { return '' ; } let regexp = ( ) => /\$\{((env|config|workspaceFolder|file|fileDirname|fileBasenameNoExtension)(\.|:))?(.*?)\}/g ; let ret = input ; const cycleCache = new Set ( ) ; while ( ! cycleCache . has ( ret ) ) { cycleCache . add ( ret ) ; ret = ret . replace ( regexp ( ) , ( match , ignored1 , varType , ignored2 , name ) => { if ( ! varType ) { varType = 'workspaceFolder' ; } let newValue ; switch ( varType ) { case 'env' : { if ( additionalEnvironment ) { const v = additionalEnvironment [ name ] ; if ( isString ( v ) ) { newValue = v ; } else if ( input === match && isArrayOfString ( v ) ) { newValue = v . join ( envDelimiter ) ; } } if ( newValue === undefined ) { newValue = process . env [ name ] ; } break ; } case 'config' : { const config = vscode . workspace . getConfiguration ( ) ; if ( config ) { newValue = config . get < string > ( name ) ; } break ; } case 'workspaceFolder' : { if ( name && vscode . workspace && vscode . workspace . workspaceFolders ) { let folder = vscode . workspace . workspaceFolders . find ( folder => folder . name . toLocaleLowerCase ( ) === name . toLocaleLowerCase ( ) ) ; if ( folder ) { newValue = folder . uri . fsPath ; } else { folder = vscode . workspace . workspaceFolders [ 0 ] ; newValue = folder . uri . fsPath ; } } break ; } default : { assert . fail ( 'unknown varType matched' ) ; } } return newValue !== undefined ? newValue : match ; } ) ; } regexp = ( ) => /^~/g ; ret = ret . replace ( regexp ( ) , ( match , name ) => os . homedir ( ) ) ; return ret ; }