/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import * as vscode from 'vscode' ; import * as path from 'path' ; import { getValue , isDefaultNotificationDisabled } from './utils' ; import * as extensionFunctions from './extensionFunctions' ; import * as didactExtension from './extension' ; const url = require ( 'url-parse' ) ; export async function processInputs ( incoming , extensionPath ? ) { const output = [ ] ; if ( incoming ) { extensionFunctions . sendTextToOutputChannel ( `Processing command inputs ${ incoming } ${ extensionPath }` ) ; const parsedUrl = new url ( incoming , true ) ; const query = parsedUrl . query ; let commandId = undefined ; let completionMessage = undefined ; let errorMessage = undefined ; if ( query . commandId ) { commandId = getValue ( query . commandId ) ; } if ( ! commandId ) { throw new Error ( 'No command Id provided' ) ; } else { await didactExtension . getTelemetry ( ) . sendCommandTracking ( commandId ) ; if ( query . projectFilePath ) { const projectFilePath = getValue ( query . projectFilePath ) ; if ( projectFilePath ) { const addUri = handleProjectFilePath ( projectFilePath ) ; if ( addUri ) { output . push ( addUri ) ; } } } else if ( query . srcFilePath && extensionPath ) { const srcFilePath = getValue ( query . srcFilePath ) ; if ( srcFilePath ) { const addUri = handleSrcFilePath ( srcFilePath , extensionPath ) ; if ( addUri ) { output . push ( addUri ) ; } } } else if ( query . extFilePath ) { const extFilePath = getValue ( query . extFilePath ) ; if ( extFilePath ) { const addUri = handleExtFilePath ( extFilePath ) ; if ( addUri ) { output . push ( addUri ) ; } } } if ( query . completion ) { completionMessage = getValue ( query . completion ) ; } if ( query . error ) { errorMessage = getValue ( query . error ) ; } if ( query . text ) { const text = getValue ( query . text ) ; if ( text ) { handleText ( text , output ) ; } } else if ( query . user ) { const user = getValue ( query . user ) ; if ( user ) { await handleUser ( user , output , errorMessage ) ; } } if ( query . number ) { const text = getValue ( query . number ) ; if ( text ) { handleNumber ( output , text ) ; } } if ( query . json ) { const text = getValue ( query . json ) ; if ( text ) { output . push ( JSON . parse ( text ) ) ; } } console . log ( `commandId : ${ commandId }` ) ; console . log ( `output : ${ output . toString ( ) }` ) ; await callCommand ( commandId , output , completionMessage , errorMessage ) ; } } } export function handleProjectFilePath ( projectFilePath ) { if ( vscode . workspace . workspaceFolders === undefined ) { return undefined ; } const workspace = vscode . workspace . workspaceFolders [ 0 ] ; const rootPath = workspace . uri . fsPath ; const fullpath = path . resolve ( rootPath , projectFilePath ) ; return vscode . Uri . file ( fullpath ) ; } function handleSrcFilePath ( srcFilePath , extensionPath ) { if ( extensionPath === undefined ) { return undefined ; } return vscode . Uri . file ( path . resolve ( extensionPath , srcFilePath ) ) ; } export function handleExtFilePath ( extFilePath ) { extensionFunctions . sendTextToOutputChannel ( `Processing extension file path input ${ extFilePath }` ) ; if ( extFilePath ) { const separator = '/' ; const array = extFilePath . split ( separator ) ; if ( array && array . length > 0 ) { const extId = array . shift ( ) ; if ( extId ) { const ext = vscode . extensions . getExtension ( extId ) ; if ( ext ) { const pathToAdd = array . join ( separator ) ; const extensionPath = ext . extensionPath ; extensionFunctions . sendTextToOutputChannel ( `-- combining ${ pathToAdd } ${ extensionPath }` ) ; return vscode . Uri . file ( path . resolve ( extensionPath , pathToAdd ) ) ; } } } } return undefined ; } export function handleText ( text , outputs ) { if ( text ) { let inputs = [ ] ; if ( text . split ( '$$' ) . length > 0 ) { inputs = text . split ( '$$' ) ; for ( const input of inputs ) { outputs . push ( input ) ; } } else { outputs . push ( text ) ; } } } async function handleUser ( text , outputs , errorMessage ) { if ( text ) { let inputs = [ ] ; if ( text . split ( '$$' ) . length > 0 ) { inputs = text . split ( '$$' ) ; } else { inputs . push ( text ) ; } try { await collectUserInput ( inputs ) . then ( async ( args ) => { for ( const arg of args ) { outputs . push ( arg ) ; } } ) ; } catch ( error ) { if ( errorMessage ) { vscode . window . showErrorMessage ( errorMessage ) ; } else { vscode . window . showErrorMessage ( `Didact was unable to collect user input: ${ error }` ) ; } } } } async function getUserInput ( prompt ) { return vscode . window . showInputBox ( { prompt : `Enter a ${ prompt }` , placeHolder : prompt } ) ; } async function collectUserInput ( args ) { const outArgs = [ ] ; for ( const prompt of args ) { const result = await getUserInput ( prompt ) ; if ( result ) { outArgs . push ( result ) ; } else { throw new Error ( 'Input aborted' ) ; } } return outArgs ; } export function handleNumber ( output , text ) { output . push ( + text ) ; } async function callCommand ( commandId , args , completionMessage , errorMessage ) { try { await vscode . commands . executeCommand ( commandId , ... args ) . then ( ( ) => { if ( completionMessage ) { vscode . window . showInformationMessage ( completionMessage ) ; } else { if ( ! isDefaultNotificationDisabled ( ) ) { vscode . window . showInformationMessage ( `Didact just executed ${ commandId } with arguments ${ args }` ) ; } } } ) ; } catch ( error ) { if ( errorMessage ) { vscode . window . showErrorMessage ( errorMessage ) ; } else { vscode . window . showErrorMessage ( `Didact was unable to call command ${ commandId }: ${ error }` ) ; } } }