Skip to content

Commit

Permalink
Change org to project (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
lask authored Sep 24, 2021
1 parent 5d4a4b2 commit b0a9d37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { clean, gt } from "semver";
import { commands as Commands, env, ExtensionContext, Uri, window as Window } from "vscode";
import { activateTreeView, deactivateTreeView } from "./deviceView";
import { activateLsp, deactivateLsp } from "./lspClient";
import { activateToitStatusBar, createSetOrgCommand } from "./organization";
import { createOutputCommand } from "./output";
import { activateToitStatusBar, createSetProjectCommand } from "./projectCmd";
import { activateSerialView } from "./serialView";
import { createEnsureAuth } from "./toitAuth";
import { createDeployCommand, createRunCommand } from "./toitExec";
Expand Down Expand Up @@ -72,7 +72,7 @@ export async function activate(extContext: ExtensionContext): Promise<void> {
extContext.subscriptions.push(Commands.registerCommand("toit.devRun", createRunCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.devDeploy", createDeployCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.devLogs", createOutputCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.setOrganization", createSetOrgCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.setProject", createSetProjectCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.stopSimulator", createStopSimCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.startSimulator", createStartSimCommand(ctx)));
extContext.subscriptions.push(Commands.registerCommand("toit.revealDevice", async(hwID) => await revealDevice(ctx, hwID)));
Expand Down
14 changes: 7 additions & 7 deletions vscode/src/org.ts → vscode/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

export interface ConsoleOrganization {
export interface ConsoleProject {
name: string;
organization_id: string;
project_id: string;
}
/* eslint-enable @typescript-eslint/naming-convention */

export class Organization {
export class Project {
name: string;
organizationID: string;
projectID: string;

constructor(org: ConsoleOrganization) {
this.name = org.name;
this.organizationID = org.organization_id;
constructor(project: ConsoleProject) {
this.name = project.name;
this.projectID = project.project_id;
}
}
18 changes: 9 additions & 9 deletions vscode/src/organization.ts → vscode/src/projectCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
// found in the LICENSE file.

import { ExtensionContext, StatusBarAlignment, window as Window } from "vscode";
import { Context, ensureAuth, getFirmwareVersion, getOrganization, isAuthenticated, selectOrganization, setOrganization } from "./utils";
import { Context, ensureAuth, getFirmwareVersion, getProject, isAuthenticated, selectProject, setProject } from "./utils";

export async function activateToitStatusBar(ctx: Context, extensionContext: ExtensionContext): Promise<void> {
const toitStatus = Window.createStatusBarItem(StatusBarAlignment.Left, 100);
extensionContext.subscriptions.push(toitStatus);
ctx.setStatusBar(toitStatus);
updateStatus(ctx);
toitStatus.command = "toit.setOrganization";
toitStatus.command = "toit.setProject";
toitStatus.show();
}

export async function updateStatus(ctx: Context): Promise<void> {
if (!await isAuthenticated(ctx)) return;
const toitStatus = ctx.getStatusBar();
if (!toitStatus) return;
const org = await getOrganization(ctx);
const project = await getProject(ctx);
const firmwareVersion = await getFirmwareVersion(ctx);
toitStatus.text = `Toit: ${org} (${firmwareVersion})`;
toitStatus.text = `Toit: ${project} (${firmwareVersion})`;
}

async function executeCommand(ctx: Context) {
if (!await ensureAuth(ctx)) return;

const org = await selectOrganization(ctx);
if (org === undefined) return;
const project = await selectProject(ctx);
if (project === undefined) return;

try {
await setOrganization(ctx, org);
await setProject(ctx, project);
await updateStatus(ctx);
ctx.views.refreshViews();
} catch (e) {
return Window.showErrorMessage(`Unable to change organization: ${e.message}.`);
return Window.showErrorMessage(`Unable to change project: ${e.message}.`);
}
}

export function createSetOrgCommand(ctx: Context): () => void {
export function createSetProjectCommand(ctx: Context): () => void {
return async() => executeCommand(ctx);
}
38 changes: 19 additions & 19 deletions vscode/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { InputBoxOptions, QuickPickItem, StatusBarItem, window as Window, worksp
import { App, ConsoleApp } from "./app";
import { toitExecFilePromise } from "./cli";
import { ConsoleDevice, ConsoleDeviceInfo, Device, DeviceInfo, RelatedDevice } from "./device";
import { ConsoleOrganization, Organization } from "./org";
import { updateStatus } from "./organization";
import { Output } from "./output";
import { ConsoleProject as ConsoleProject, Project as Project } from "./project";
import { updateStatus } from "./projectCmd";
import { ConsoleSerialInfo, SerialInfo, SerialPort, SerialStatus } from "./serialPort";
import { Views } from "./views";

Expand Down Expand Up @@ -138,8 +138,8 @@ interface AuthInfo {
email?: string;
id?: string;
name?: string;
organization_id?: string;
organization_name?: string;
project_id?: string;
project_name?: string;
status: string;
/* eslint-enable @typescript-eslint/naming-convention */
}
Expand Down Expand Up @@ -234,41 +234,41 @@ export async function uninstallApp(ctx: Context, app: App): Promise<void> {
await toitExecFilePromise(ctx, "dev", "-d", app.deviceID, "uninstall", app.jobID );
}

class OrganizationItem extends Organization implements QuickPickItem {
class ProjectItem extends Project implements QuickPickItem {
label: string;

constructor(org: ConsoleOrganization) {
super(org);
constructor(project: ConsoleProject) {
super(project);
this.label = this.name;
}
}

export async function getOrganization(ctx: Context): Promise<string | undefined> {
export async function getProject(ctx: Context): Promise<string | undefined> {
if (!await isAuthenticated(ctx)) return undefined;
const { stdout } = await toitExecFilePromise(ctx, "project", "get" );
// The output of the command if of the form:
// Logged in to Toitware
// 01234567890123
const orgStrOffset = 13;
return stdout.slice(orgStrOffset).trimEnd();
const projectStrOffset = 13;
return stdout.slice(projectStrOffset).trimEnd();
}


async function listOrganizations(ctx: Context): Promise<OrganizationItem[]> {
async function listProjects(ctx: Context): Promise<ProjectItem[]> {
const { stdout } = await toitExecFilePromise(ctx, "project", "list", "-o", "json");
return stdout.split("\n").
filter(str => str !== "").
map(json => JSON.parse(json) as ConsoleOrganization).
map(org => new OrganizationItem(org));
map(json => JSON.parse(json) as ConsoleProject).
map(project => new ProjectItem(project));
}

export async function selectOrganization(ctx: Context): Promise<Organization | undefined> {
const organizations = await listOrganizations(ctx);
return await Window.showQuickPick(organizations, { "placeHolder": "Pick an organization" });
export async function selectProject(ctx: Context): Promise<Project | undefined> {
const projects = await listProjects(ctx);
return await Window.showQuickPick(projects, { "placeHolder": "Pick a project" });
}

export async function setOrganization(ctx: Context, org: Organization): Promise<void> {
await toitExecFilePromise(ctx, "project", "use", org.organizationID);
export async function setProject(ctx: Context, project: Project): Promise<void> {
await toitExecFilePromise(ctx, "project", "use", project.projectID);
}

export function getToitPath(): string {
Expand Down Expand Up @@ -307,7 +307,7 @@ export async function revealDevice(ctx: Context, hwid: string): Promise<void> {

const device = await ctx.views.getDeviceProvider()?.getDevice(deviceInfo.deviceID);
if (!device) {
return; // TODO(Lau): Add warning or error message? Make sure to differentiate between hidden view and wrong org.
return; // TODO(Lau): Add warning or error message? Make sure to differentiate between hidden view and wrong project.
}
await ctx.views.getDeviceView()?.reveal(device, { "focus": true, "select": false, "expand": true });
}

0 comments on commit b0a9d37

Please sign in to comment.