From 859350f0eed9c68f4badeeaf5c1ea3eca61122cd Mon Sep 17 00:00:00 2001 From: ysandler Date: Tue, 15 Sep 2020 00:16:57 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=A9=E2=80=8D=F0=9F=92=BB=20refact:=20p?= =?UTF-8?q?ulled=20comands=20out=20of=20app=20entry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/userTestOne.js | 0 src/Entities/BrightScreen.ts | 3 + src/{UseCases => Entities}/CourseProvider.ts | 0 src/{UseCases => Entities}/LessonsProvider.ts | 0 .../Commands/CreateStarterFileForLesson.ts | 29 ++++ src/UseCases/Commands/DownloadCourseComand.ts | 20 +++ src/UseCases/Commands/RunTestComand.ts | 87 +++++++++++ .../Commands/SearchForCourseComand.ts | 8 ++ .../Commands/StartupBrightScreenComand.ts | 9 ++ src/UseCases/setupBrightScreen.ts | 2 +- src/UseCases/setupComands.ts | 21 +++ src/UseCases/setupCoursesTree.ts | 2 +- src/extension.ts | 136 +----------------- 13 files changed, 181 insertions(+), 136 deletions(-) delete mode 100644 demo/userTestOne.js rename src/{UseCases => Entities}/CourseProvider.ts (100%) rename src/{UseCases => Entities}/LessonsProvider.ts (100%) create mode 100644 src/UseCases/Commands/CreateStarterFileForLesson.ts create mode 100644 src/UseCases/Commands/DownloadCourseComand.ts create mode 100644 src/UseCases/Commands/RunTestComand.ts create mode 100644 src/UseCases/Commands/SearchForCourseComand.ts create mode 100644 src/UseCases/Commands/StartupBrightScreenComand.ts create mode 100644 src/UseCases/setupComands.ts diff --git a/demo/userTestOne.js b/demo/userTestOne.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/Entities/BrightScreen.ts b/src/Entities/BrightScreen.ts index 8e790a7..9d90309 100644 --- a/src/Entities/BrightScreen.ts +++ b/src/Entities/BrightScreen.ts @@ -1,3 +1,4 @@ +import * as vscode from 'vscode' import BrightScreenInterface from '../Interfaces/BrightScreenInterface' import LessonInterface from '../Interfaces/LessonInterface' @@ -7,6 +8,7 @@ class BrightScreen { public courseName: string public documentationUrl: string public lessons: LessonInterface[] + public outputChannel: vscode.OutputChannel constructor (props: BrightScreenInterface) { @@ -16,6 +18,7 @@ class BrightScreen { this.courseName = props.courseName || '' this.documentationUrl = props.documentationUrl || '' this.lessons = props.lessons || [] + this.outputChannel = vscode.window.createOutputChannel('brightScreen') return BrightScreen.instance } diff --git a/src/UseCases/CourseProvider.ts b/src/Entities/CourseProvider.ts similarity index 100% rename from src/UseCases/CourseProvider.ts rename to src/Entities/CourseProvider.ts diff --git a/src/UseCases/LessonsProvider.ts b/src/Entities/LessonsProvider.ts similarity index 100% rename from src/UseCases/LessonsProvider.ts rename to src/Entities/LessonsProvider.ts diff --git a/src/UseCases/Commands/CreateStarterFileForLesson.ts b/src/UseCases/Commands/CreateStarterFileForLesson.ts new file mode 100644 index 0000000..dcbbfa2 --- /dev/null +++ b/src/UseCases/Commands/CreateStarterFileForLesson.ts @@ -0,0 +1,29 @@ +import * as vscode from 'vscode' +import * as fs from 'fs' + +import BrightScreen from '../../Entities/BrightScreen' + +const CreateStarterFileForLesson: vscode.Disposable = vscode.commands.registerCommand('brightscreen.createStarterFileForLesson', treeItemContext => { + const brightScreen = BrightScreen.getInstance() + const workspaceFolder = brightScreen.workspaceFolder + + let starterFileAsString :string + try{ + starterFileAsString = fs.readFileSync(`${workspaceFolder}/.brightScreen/${treeItemContext.starterFileLocation}`, 'utf-8') + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Issue reading starter file provided by course.') + return + } + try { + vscode.window.activeTextEditor?.edit(builder => { + builder.insert(new vscode.Position(0, 0), starterFileAsString) + }) + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Coule Not create starter file for the lesson.') + return + } +}) + +export default CreateStarterFileForLesson diff --git a/src/UseCases/Commands/DownloadCourseComand.ts b/src/UseCases/Commands/DownloadCourseComand.ts new file mode 100644 index 0000000..96b7876 --- /dev/null +++ b/src/UseCases/Commands/DownloadCourseComand.ts @@ -0,0 +1,20 @@ +import * as vscode from 'vscode' +import BrightScreen from '../../Entities/BrightScreen' +import installCourse from '../installCourse' + +const DownloadCourseComand: vscode.Disposable = vscode.commands.registerCommand('brightscreen.downloadCourse', async (treeItemContext) => { + const brightScreen = BrightScreen.getInstance() + if (brightScreen) { + vscode.window.showErrorMessage('brightScreen Course is already installed') + return + } + + const repo = treeItemContext.repo + const workspaceFolder: string = vscode.workspace.rootPath || '' + const outputChannel = vscode.window.createOutputChannel('brightScreen') + + installCourse(workspaceFolder, repo, outputChannel) +}) + +export default DownloadCourseComand + diff --git a/src/UseCases/Commands/RunTestComand.ts b/src/UseCases/Commands/RunTestComand.ts new file mode 100644 index 0000000..793e0f5 --- /dev/null +++ b/src/UseCases/Commands/RunTestComand.ts @@ -0,0 +1,87 @@ +import * as vscode from 'vscode' +import * as fs from 'fs' +import { exec } from 'child_process' + +import BrightScreen from '../../Entities/BrightScreen' + +const RunTestCommand = vscode.commands.registerCommand('brightscreen.runTests', (treeItemContext) => { + const brightscreen = BrightScreen.getInstance() + const workspaceFolder = brightscreen.workspaceFolder + const outputChannel = brightscreen.outputChannel + + let lessonCodeAsString: string + try { + lessonCodeAsString = fs.readFileSync(`${workspaceFolder}/.brightScreen/${treeItemContext.location}`, 'utf-8') + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Could not find file') + return + } + + let userCodeAsString: string | undefined + try { + userCodeAsString = vscode.window.activeTextEditor?.document.getText() + if (typeof userCodeAsString !== 'string') { + vscode.window.showErrorMessage('Could not get active text editor') + return + } + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Could not get active text editor') + return + } + + let mergedCodeAsString: string + try { + mergedCodeAsString = lessonCodeAsString.replace(treeItemContext.replacementSubstring, userCodeAsString) + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Could not create test') + return + } + + const runningTestPath = `${workspaceFolder}/.brightScreen/runningTest.${treeItemContext.fileExtention}` + try { + fs.writeFileSync(runningTestPath, mergedCodeAsString) + } catch (err) { + console.log(err) + vscode.window.showErrorMessage('Could not create ./runningTest file') + } + + exec(`${treeItemContext.executionPrefix} ${runningTestPath}`, (error, stdout, stderr) => { + if (stderr) { + console.log('stderr: ', stderr) + return + } + + if (error !== null) { + console.log('exec error: ', error) + } + + let output: { didPass: boolean, message: string } + try { + output = JSON.parse(stdout) + } catch (err) { + console.log(err) + outputChannel.appendLine('Problem reading code from active text editor. Make sure to focus on the code you wish to test.') + vscode.window.showErrorMessage(`Issue receiving test results from ${treeItemContext.label}`, 'Read More').then( selection => { + if (selection === 'Read More') outputChannel.show() + }) + return + } + + outputChannel.appendLine(`${treeItemContext.label}: ${output.message}`) + + if (output.didPass) { + vscode.window.showInformationMessage(`${treeItemContext.label} Passed!`, 'Read More').then( selection => { + if (selection === 'Read More') outputChannel.show() + }) + } else { + vscode.window.showErrorMessage(`${treeItemContext.label} Failed!`).then( selection => { + if (selection === 'Read More') outputChannel.show() + }) + } + }) +}) + +export default RunTestCommand diff --git a/src/UseCases/Commands/SearchForCourseComand.ts b/src/UseCases/Commands/SearchForCourseComand.ts new file mode 100644 index 0000000..db50d75 --- /dev/null +++ b/src/UseCases/Commands/SearchForCourseComand.ts @@ -0,0 +1,8 @@ +import * as vscode from 'vscode' +import setupCoursesTree from '../setupCoursesTree' + +const SearchForCoursesComand: vscode.Disposable = vscode.commands.registerCommand('brightscreen.searchForCourses', async () => { + setupCoursesTree() +}) + +export default SearchForCoursesComand diff --git a/src/UseCases/Commands/StartupBrightScreenComand.ts b/src/UseCases/Commands/StartupBrightScreenComand.ts new file mode 100644 index 0000000..c512dae --- /dev/null +++ b/src/UseCases/Commands/StartupBrightScreenComand.ts @@ -0,0 +1,9 @@ +import * as vscode from 'vscode' +import setupBrightScreen from '../../UseCases/setupBrightScreen' + +const StartupBrightScreenComand: vscode.Disposable = vscode.commands.registerCommand('brightscreen.startBrightScreen', () => { + setupBrightScreen() +}) + +export default StartupBrightScreenComand + diff --git a/src/UseCases/setupBrightScreen.ts b/src/UseCases/setupBrightScreen.ts index 156ae79..031d3c6 100644 --- a/src/UseCases/setupBrightScreen.ts +++ b/src/UseCases/setupBrightScreen.ts @@ -3,7 +3,7 @@ import * as fs from 'fs' import * as path from 'path' import BrightScreen from '../Entities/BrightScreen' import LessonInterface from '../Interfaces/LessonInterface' -import LessonsProvider from './LessonsProvider' +import LessonsProvider from '../Entities/LessonsProvider' function setupBrightScreen (): void { const workspaceFolder: string = vscode.workspace.rootPath || '' diff --git a/src/UseCases/setupComands.ts b/src/UseCases/setupComands.ts new file mode 100644 index 0000000..07f87a9 --- /dev/null +++ b/src/UseCases/setupComands.ts @@ -0,0 +1,21 @@ +import * as vscode from 'vscode' +import StartupBrightScreenComand from './Commands/StartupBrightScreenComand' +import DownloadCourseComand from './Commands/DownloadCourseComand' +import SearchForCoursesComand from './Commands/SearchForCourseComand' +import CreateStarterFileForLesson from './Commands/CreateStarterFileForLesson' +import RunTestCommand from './Commands/RunTestComand' + + +function setupComands (context: vscode.ExtensionContext) { + const comands = [ + StartupBrightScreenComand, + DownloadCourseComand, + SearchForCoursesComand, + CreateStarterFileForLesson, + RunTestCommand + ] + + context.subscriptions.push(...comands) +} + +export default setupComands \ No newline at end of file diff --git a/src/UseCases/setupCoursesTree.ts b/src/UseCases/setupCoursesTree.ts index 1f99116..705efad 100644 --- a/src/UseCases/setupCoursesTree.ts +++ b/src/UseCases/setupCoursesTree.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode' import axios from 'axios' -import CourseProvider from './CourseProvider' +import CourseProvider from '../Entities/CourseProvider' function setupCoursesTree (): void { let courses: any diff --git a/src/extension.ts b/src/extension.ts index f2f11da..b18e101 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,144 +1,12 @@ import * as vscode from 'vscode' -import * as fs from 'fs' -import BrightScreen from './Entities/BrightScreen' -import setupBrightScreen from './UseCases/setupBrightScreen' +import setupComands from './UseCases/setupComands' import setupCoursesTree from './UseCases/setupCoursesTree' -import LessonInterface from './Interfaces/LessonInterface' -import { exec } from 'child_process' -import installCourse from './UseCases/installCourse' export function activate(context: vscode.ExtensionContext) { console.log('brightscreen is now active') - let brightScreen: BrightScreen - let workspaceFolder: string = vscode.workspace.rootPath || '' - let lessons: LessonInterface[] - const outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel('brightScreen') - setupCoursesTree() - - const startupBrightScreenCommand = vscode.commands.registerCommand('brightscreen.startBrightScreen', () => { - setupBrightScreen() - brightScreen = BrightScreen.getInstance() - workspaceFolder = brightScreen.workspaceFolder - lessons = brightScreen.lessons - }) - - const searchForCoursesComand = vscode.commands.registerCommand('brightscreen.searchForCourses', async () => { - // console.log(await vscode.window.showInputBox()) - setupCoursesTree() - }) - - const downloadCourseComand = vscode.commands.registerCommand('brightscreen.downloadCourse', async (treeItemContext) => { - console.log(treeItemContext) - const repo = treeItemContext.repo - // console.log(await vscode.window.showInputBox()) - if (brightScreen) { - vscode.window.showErrorMessage('brightScreen Course is already installed') - return - } - installCourse(workspaceFolder, repo, outputChannel) - // setupBrightScreen() - }) - - const createStarterFileForLesson = vscode.commands.registerCommand('brightscreen.createStarterFileForLesson', treeItemContext => { - let starterFileAsString :string - try{ - starterFileAsString = fs.readFileSync(`${workspaceFolder}/.brightScreen/${treeItemContext.starterFileLocation}`, 'utf-8') - } catch (err) { - console.log(err) - vscode.window.showErrorMessage('Issue reading starter file provided by course.') - return - } - try { - vscode.window.activeTextEditor?.edit(builder => { - builder.insert(new vscode.Position(0, 0), starterFileAsString) - // builder.replace(vscode.window?.activeTextEditor.selection, starterFileAsString); - }) - } catch (err) { - console.log(err) - vscode.window.showErrorMessage('Coule Not create starter file for the lesson.') - return - } - }) - - const runTestCommand = vscode.commands.registerCommand('brightscreen.runTests', (treeItemContext) => { - let lessonCodeAsString: string - try { - lessonCodeAsString = fs.readFileSync(`${workspaceFolder}/.brightScreen/${treeItemContext.location}`, 'utf-8') - } catch (err) { - console.log(err) - vscode.window.showErrorMessage('Could not find file') - return - } - - let userCodeAsString: string | undefined - try { - userCodeAsString = vscode.window.activeTextEditor?.document.getText() - if (typeof userCodeAsString !== 'string') { - vscode.window.showErrorMessage('Could not get active text editor') - return - } - } catch (err) { - console.log(err) - vscode.window.showErrorMessage('Could not get active text editor') - return - } - - let mergedCodeAsString: string - try { - mergedCodeAsString = lessonCodeAsString.replace(treeItemContext.replacementSubstring, userCodeAsString) - } catch (err) { - console.log(err) - vscode.window.showErrorMessage('Could not create test') - return - } - - const runningTestPath = `${workspaceFolder}/.brightScreen/runningTest.${treeItemContext.fileExtention}` - try { - fs.writeFileSync(runningTestPath, mergedCodeAsString) - } catch (err) { - console.log(err) - vscode.window.showErrorMessage('Could not create ./runningTest file') - } - - exec(`${treeItemContext.executionPrefix} ${runningTestPath}`, (error, stdout, stderr) => { - if (stderr) { - console.log('stderr: ', stderr) - return - } - - if (error !== null) { - console.log('exec error: ', error) - } - - let output: { didPass: boolean, message: string } - try { - output = JSON.parse(stdout) - } catch (err) { - console.log(err) - outputChannel.appendLine('Problem reading code from active text editor. Make sure to focus on the code you wish to test.') - vscode.window.showErrorMessage(`Issue receiving test results from ${treeItemContext.label}`, 'Read More').then( selection => { - if (selection === 'Read More') outputChannel.show() - }) - return - } - - outputChannel.appendLine(`${treeItemContext.label}: ${output.message}`) - - if (output.didPass) { - vscode.window.showInformationMessage(`${treeItemContext.label} Passed!`, 'Read More').then( selection => { - if (selection === 'Read More') outputChannel.show() - }) - } else { - vscode.window.showErrorMessage(`${treeItemContext.label} Failed!`).then( selection => { - if (selection === 'Read More') outputChannel.show() - }) - } - }) - }) - - context.subscriptions.push(...[runTestCommand, startupBrightScreenCommand, searchForCoursesComand, downloadCourseComand]) + setupComands(context) } export function deactivate() {}