Skip to content

Commit

Permalink
feat(execute): save webout, log, print and code after completion
Browse files Browse the repository at this point in the history
  • Loading branch information
YuryShkoda committed May 4, 2023
1 parent 96cb0a3 commit 0c48832
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
56 changes: 41 additions & 15 deletions src/commands/execute-code/ExecuteCodeCommand.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { window, ExtensionContext, commands } from 'vscode'
import { getEditorContent } from '../../utils/editor'
import { selectTarget } from '../../utils/target'
import { createAndOpenLogFile, handleErrorResponse } from '../../utils/utils'
import {
createAndOpenLogFile,
handleErrorResponse,
getTimestamp
} from '../../utils/utils'
import { executeCode } from '../../utils/executeCode'
import { updateSasjsConstants } from '../../utils/setConstants'
import { TargetCommand } from '../../types/commands/targetCommand'
import { ScriptExecutionResult } from '@sasjs/adapter'
import { createFile, createFolder } from '../../utils/file'
import * as path from 'path'

interface ExecutionArtifacts extends ScriptExecutionResult {
code?: string
}

export class ExecuteCodeCommand extends TargetCommand {
constructor(private context: ExtensionContext) {
Expand All @@ -29,17 +38,21 @@ export class ExecuteCodeCommand extends TargetCommand {
}

const execFilePath = window.activeTextEditor?.document.fileName

const sasCodeInjection = `options set=SAS_EXECFILEPATH "${execFilePath}";`

const currentFileContent = `${sasCodeInjection}\n${getEditorContent()}`
const editorContent = getEditorContent()
const currentFileContent = `${sasCodeInjection}\n${editorContent}`

commands.executeCommand('setContext', 'isSasjsCodeExecuting', true)

await executeCode(target, currentFileContent || '')
.then(async ({ log }) => {
.then(async (res) => {
process.outputChannel.appendLine('SASjs: Code executed successfully!')
await handleSuccessResponse(log)

if (typeof res.log === 'object') {
res.log = JSON.stringify(res.log, null, 2)
}

await this.saveExecutionArtifacts({ ...res, code: editorContent })
})
.catch(async (err) => {
await handleErrorResponse(err, 'Error executing code')
Expand All @@ -48,14 +61,27 @@ export class ExecuteCodeCommand extends TargetCommand {
commands.executeCommand('setContext', 'isSasjsCodeExecuting', false)
})
}
}

const handleSuccessResponse = async (log: any) => {
if (typeof log === 'object') {
return await createAndOpenLogFile(JSON.stringify(log, null, 2))
}
private saveExecutionArtifacts = async (result: ExecutionArtifacts) => {
const { buildDestinationResultsFolder: resultsFolder } =
process.sasjsConstants
const timestamp = getTimestamp()
const folderPath = path.join(resultsFolder, timestamp)

await createFolder(folderPath)

const { log, webout, printOutput, code } = result

if (webout) {
await createFile(path.join(folderPath, 'webout.txt'), webout)
}
if (printOutput) {
await createFile(path.join(folderPath, 'print.lst'), printOutput)
}
if (code) {
await createFile(path.join(folderPath, 'code.sas'), code)
}

if (typeof log === 'string') {
return await createAndOpenLogFile(log)
await createAndOpenLogFile(log, path.join(folderPath, 'log.log'))
}
}
5 changes: 3 additions & 2 deletions src/utils/executeCode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Target, ServerType, decodeFromBase64 } from '@sasjs/utils'
import { getAuthConfig, getAuthConfigSas9 } from './config'
import { getSASjs } from './getSASjs'
import { ScriptExecutionResult } from '@sasjs/adapter'

export const executeCode = async (target: Target, code: string) => {
if (target.serverType === ServerType.SasViya) {
Expand Down Expand Up @@ -55,11 +56,11 @@ const executeOnSasJS = async (target: Target, code: string) => {
const sasjs = getSASjs(target)
const authConfig = await getAuthConfig(target)

const executionResult = await sasjs.executeScript({
const executionResult: ScriptExecutionResult = await sasjs.executeScript({
linesOfCode: code.split('\n'),
runTime: 'sas',
authConfig
})

return { log: executionResult }
return executionResult
}
4 changes: 4 additions & 0 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export async function readFile(filePath: string) {
.readFile(Uri.file(filePath))
.then((content) => Buffer.from(content).toString('utf8'))
}

export async function createFolder(filePath: string) {
return await workspace.fs.createDirectory(Uri.file(filePath))
}
5 changes: 2 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { window, workspace, ViewColumn } from 'vscode'
import {
Target,
timestampToYYYYMMDDHHMMSS,
fileExists,
folderExists,
copy
} from '@sasjs/utils'
Expand Down Expand Up @@ -103,12 +102,12 @@ export const handleErrorResponse = async (e: any, message: string) => {
}
}

export const createAndOpenLogFile = async (log: string) => {
export const createAndOpenLogFile = async (log: string, filePath?: string) => {
const { buildDestinationResultsFolder: resultsFolder } =
process.sasjsConstants

const timestamp = getTimestamp()
const resultsPath = path.join(resultsFolder, `${timestamp}.log`)
const resultsPath = filePath || path.join(resultsFolder, `${timestamp}.log`)

process.outputChannel.appendLine(
`SASjs: Attempting to create log file at ${resultsPath}.`
Expand Down

0 comments on commit 0c48832

Please sign in to comment.