Skip to content

Setup #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
setup xstate with interpreter
  • Loading branch information
ShMcK committed Jun 2, 2019
commit e88651e2f5ae5cd742dd05e7f84d8a632da8abf6
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"source.fixAll": true,
},
"tslint.enable": true,
"tslint.jsEnable": true,
"[javascript]": {
"editor.formatOnSave": true
},
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
],
"publisher": "Shawn McKay <[email protected]>",
"activationEvents": [
"onCommand:coderoad.tutorial_load"
"onCommand:coderoad.start"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "coderoad.tutorial_load",
"title": "Load Tutorial",
"command": "coderoad.start",
"title": "Start",
"category": "CodeRoad"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"machine": "node ./out/state/index.js",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
Expand Down
9 changes: 5 additions & 4 deletions src/editor/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import * as vscode from 'vscode'

// import runTest from './runTest'
import tutorialLoad from './tutorialLoad'
import start from './start'
// import loadSolution from './loadSolution'
// import quit from './quit'

const COMMANDS = {
// TUTORIAL_SETUP: 'coderoad.tutorial_setup',
TUTORIAL_LOAD: 'coderoad.tutorial_load',
START: 'coderoad.start',
// RUN_TEST: 'coderoad.test_run',
// LOAD_SOLUTION: 'coderoad.solution_load',
// QUIT: 'coderoad.quit',
}

export default (context: vscode.ExtensionContext): void => {
const commands = {
[COMMANDS.TUTORIAL_LOAD](): void {
tutorialLoad(context)
[COMMANDS.START](): void {
console.log('TUTORIAL_START')
start(context)
},
// [COMMANDS.RUN_TEST]: runTest,
// [COMMANDS.LOAD_SOLUTION]: loadSolution,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import * as vscode from 'vscode'
import * as CR from 'typings'

import api from '../../services/api'
import tutorialSetup from '../../services/tutorialSetup'
import { loadProgressPosition } from '../../services/position'
import * as storage from '../../services/storage'
import rootSetup from '../../services/rootSetup'
import { isEmptyWorkspace, openReadme } from '../workspace'
import * as git from '../../services/git'
import { setWorkspaceRoot } from '../../services/node'
import { setStorage } from '../../editor/storage'
import createStateMachine from '../../state'

/*
new
if current workspace is empty, use it
if not, open a new folder then start
*/

async function continueTutorial() {
// TODO: verify that tutorial is loaded in workspace
// TODO: verify progress
// TODO: verify setup
await loadProgressPosition()
await openReadme()
}
// async function continueTutorial() {
// // TODO: verify that tutorial is loaded in workspace
// // TODO: verify progress
// // TODO: verify setup
// await loadProgressPosition()
// await openReadme()
// }

async function newTutorial(tutorial: CR.Tutorial) {
// if workspace isn't empty, clear it out if given permission
Expand All @@ -40,23 +38,15 @@ async function newTutorial(tutorial: CR.Tutorial) {
}


async function validateCanContinue(): Promise<boolean> {
// validate tutorial & progress found in local storage
// validate git is setup with a remote
const [tutorial, progress, hasGit, hasGitRemote] = await Promise.all([
storage.getTutorial(),
storage.getProgress(),
git.gitVersion(),
git.gitCheckRemoteExists(),
])
return !!(tutorial && progress && hasGit && hasGitRemote)
}

export default async function tutorialLoad(context: vscode.ExtensionContext): Promise<void> {
console.log(`tutorialLoad ${JSON.stringify(context)}`)
export default async function start(context: vscode.ExtensionContext): Promise<void> {
console.log('start', context)

// setup connection to workspace
await rootSetup(context)
await setWorkspaceRoot()
// set workspace context path
await setStorage(context.workspaceState)
// initiate the state machine
createStateMachine()
return;

// const modes = ['New']
Expand Down
10 changes: 0 additions & 10 deletions src/editor/index.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/editor/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode'

import createCommands from './commands'
import createViews from './views'
import createStateMachine from '../state'

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
console.log('ACTIVATE!')

// commands
createCommands(context)

// tasks
// add tasks here

// views
createViews(context)


}

// this method is called when your extension is deactivated
export function deactivate(context: vscode.ExtensionContext): void {
// cleanup subscriptions/tasks
console.log('deactivate context', context)
for (const disposable of context.subscriptions) {
disposable.dispose()
}
}
File renamed without changes.
File renamed without changes.
30 changes: 1 addition & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,2 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode'
export { activate, deactivate } from './editor/init'

import createCommands from './editor/commands'
import createViews from './views'

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
console.log('ACTIVATE!')

// commands
createCommands(context)

// tasks
// add tasks here

// views
createViews(context)
}

// this method is called when your extension is deactivated
export function deactivate(context: vscode.ExtensionContext): void {
// cleanup subscriptions/tasks
console.log('deactivate context', context)
for (const disposable of context.subscriptions) {
disposable.dispose()
}
}
8 changes: 0 additions & 8 deletions src/services/rootSetup.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let initialProgress: CR.Progress = {

export default {
start: async () => {
console.log('ACTION: start')
// verify that the user has a tutorial & progress
// verify git is setup with a coderoad remote
const [tutorial, progress, hasGit, hasGitRemote] = await Promise.all([
Expand All @@ -31,16 +32,19 @@ export default {
tutorialLoad: assign({
// load initial data, progress & position
data(): CR.TutorialData {
console.log('ACTION: tutorialLoad.data')
if (!initialTutorial) {
throw new Error('No Tutorial loaded')
}
return initialTutorial.data

},
progress(): CR.Progress {
console.log('ACTION: tutorialLoad.progress')
return initialProgress
},
position() {
console.log('ACTION: tutorialLoad.position')
if (!initialTutorial) {
throw new Error('No Tutorial loaded')
}
Expand Down
1 change: 1 addition & 0 deletions src/state/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as CR from 'typings'
export default {
// skip to the stage if the level has already been started
hasNoNextLevelProgress: (context: CR.MachineContext): boolean => {
console.log('GUARD: hasNoNextLevelProgress')
return false
},
}
28 changes: 28 additions & 0 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { interpret } from 'xstate'
import machine from './machine'


const createStateMachine = () => {
const machineOptions = {
logger: console.log,
devTools: true,
deferEvents: true,
execute: true
}
// machine interpreter
// https://xstate.js.org/docs/guides/interpretation.html
const service = interpret(machine, machineOptions)
// logging
.onTransition(state => {
console.log('state', state)
if (state.changed) {
console.log('transition')
console.log(state.value)
}
})
// initialize
service.start()
return service
}

export default createStateMachine
7 changes: 4 additions & 3 deletions src/state/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import actions from './actions'
import guards from './guards'
import initialContext from './context'

// TODO: replace with API

export const tutorialMachine = Machine<
export const machine = Machine<
CR.MachineContext,
CR.MachineStateSchema,
CR.MachineEvent
Expand All @@ -18,6 +16,7 @@ export const tutorialMachine = Machine<
initial: 'Start',
states: {
Start: {
initial: 'Initial',
states: {
Initial: {
onEntry: 'start',
Expand Down Expand Up @@ -157,3 +156,5 @@ export const tutorialMachine = Machine<
activities: {},
},
)

export default machine