👩‍💻 refact: pulled comands out of app entry

This commit is contained in:
ysandler 2020-09-15 00:16:57 -05:00 committed by Joshua Shoemaker
parent bd2ca64f9c
commit 859350f0ee
13 changed files with 181 additions and 136 deletions

View File

View File

@ -1,3 +1,4 @@
import * as vscode from 'vscode'
import BrightScreenInterface from '../Interfaces/BrightScreenInterface' import BrightScreenInterface from '../Interfaces/BrightScreenInterface'
import LessonInterface from '../Interfaces/LessonInterface' import LessonInterface from '../Interfaces/LessonInterface'
@ -7,6 +8,7 @@ class BrightScreen {
public courseName: string public courseName: string
public documentationUrl: string public documentationUrl: string
public lessons: LessonInterface[] public lessons: LessonInterface[]
public outputChannel: vscode.OutputChannel
constructor (props: BrightScreenInterface) { constructor (props: BrightScreenInterface) {
@ -16,6 +18,7 @@ class BrightScreen {
this.courseName = props.courseName || '' this.courseName = props.courseName || ''
this.documentationUrl = props.documentationUrl || '' this.documentationUrl = props.documentationUrl || ''
this.lessons = props.lessons || [] this.lessons = props.lessons || []
this.outputChannel = vscode.window.createOutputChannel('brightScreen')
return BrightScreen.instance return BrightScreen.instance
} }

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -3,7 +3,7 @@ import * as fs from 'fs'
import * as path from 'path' import * as path from 'path'
import BrightScreen from '../Entities/BrightScreen' import BrightScreen from '../Entities/BrightScreen'
import LessonInterface from '../Interfaces/LessonInterface' import LessonInterface from '../Interfaces/LessonInterface'
import LessonsProvider from './LessonsProvider' import LessonsProvider from '../Entities/LessonsProvider'
function setupBrightScreen (): void { function setupBrightScreen (): void {
const workspaceFolder: string = vscode.workspace.rootPath || '' const workspaceFolder: string = vscode.workspace.rootPath || ''

View File

@ -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

View File

@ -1,6 +1,6 @@
import * as vscode from 'vscode' import * as vscode from 'vscode'
import axios from 'axios' import axios from 'axios'
import CourseProvider from './CourseProvider' import CourseProvider from '../Entities/CourseProvider'
function setupCoursesTree (): void { function setupCoursesTree (): void {
let courses: any let courses: any

View File

@ -1,144 +1,12 @@
import * as vscode from 'vscode' import * as vscode from 'vscode'
import * as fs from 'fs' import setupComands from './UseCases/setupComands'
import BrightScreen from './Entities/BrightScreen'
import setupBrightScreen from './UseCases/setupBrightScreen'
import setupCoursesTree from './UseCases/setupCoursesTree' 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) { export function activate(context: vscode.ExtensionContext) {
console.log('brightscreen is now active') 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() setupCoursesTree()
setupComands(context)
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])
} }
export function deactivate() {} export function deactivate() {}