Skip to content

Commit

Permalink
refactor settings structure and add new command for updating user lan…
Browse files Browse the repository at this point in the history
…uage
  • Loading branch information
xjsender committed Aug 25, 2019
1 parent fcc2b1e commit e51c9ad
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
haoide-vscode is migrated from haoide, more and more features will be delivered in the next months.

## Installation
You can install ``haoide-vscode`` extension by searching ``haoide-vscode`` in the vscode marketplace.
You can install [haoide-vscode](https://marketplace.visualstudio.com/items?itemName=mouseliu.haoide-vscode) extension by searching ``haoide-vscode`` in the vscode marketplace.

## Usage

Expand Down
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "mouseliu",
"repository": "https://github.com/xjsender/haoide-vscode",
"description": "haoide-vscode is a vscode extension for force.com development",
"version": "0.1.2",
"version": "0.1.3",
"engines": {
"vscode": "^1.35.0"
},
Expand Down Expand Up @@ -84,6 +84,11 @@
"title": "%extension.haoide.updateProject.title%",
"category": "Haoide"
},
{
"command": "extension.haoide.updateUserLanguage",
"title": "%extension.haoide.updateUserLanguage.title%",
"category": "Haoide"
},
{
"command": "extension.haoide.retrieveThisFromServer",
"title": "%extension.haoide.retrieveThisFromServer.title%",
Expand Down Expand Up @@ -173,10 +178,15 @@
{
"command": "extension.haoide.createNewProject",
"when": "haoide.hasOpenProject"
},{
},
{
"command": "extension.haoide.updateProject",
"when": "haoide.hasOpenProject"
},
{
"command": "extension.haoide.updateUserLanguage",
"when": "haoide.hasOpenProject"
},
{
"command": "extension.haoide.retrieveThisFromServer",
"when": "haoide.hasOpenProject"
Expand Down Expand Up @@ -239,6 +249,13 @@
"win": "ctrl+alt+e",
"linux": "ctrl+alt+e"
},
{
"command": "extension.haoide.updateUserLanguage",
"key": "alt+u",
"mac": "alt+u",
"win": "alt+u",
"linux": "alt+u"
},
{
"command": "extension.haoide.retrieveThisFromServer",
"key": "ctrl+alt+r",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"extension.haoide.describeMetadata.title": "Describe Metadata",
"extension.haoide.createNewProject.title": "Create New Project",
"extension.haoide.updateProject.title": "Update Project",
"extension.haoide.updateUserLanguage.title": "Update User Language",
"extension.haoide.retrieveThisFromServer.title": "Retrieve This File From Server",
"extension.haoide.retrieveOpenFilesFromServer.title": "Retrieve Open File From Server",
"extension.haoide.deployThisToServer.title": "Deploy This Files to Server",
Expand Down
13 changes: 6 additions & 7 deletions src/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as vscode from "vscode";
import * as util from "../utils/util";
import OAuth from "../salesforce/lib/auth/oauth";
import { startLogin, startServer } from "../salesforce/lib/auth/server";
import { projectSession } from "../settings";
import { _session } from "../settings";
import * as nls from 'vscode-nls';
import ProgressNotification from "../utils/progress";
import { any } from "bluebird";
Expand Down Expand Up @@ -73,16 +73,16 @@ export async function authorizeNewProject(projectName?: string, loginUrl?: strin
* @returns Promise<any>
*/
export function authorizeDefaultProject() {
let session = projectSession.getSession();
let oauth = new OAuth(session["loginUrl"]);
let session = _session.getSession();
let oauth = new OAuth(session.instanceUrl);

return ProgressNotification.showProgress(
oauth, "refreshToken", {
refresh_token: session["refreshToken"]
refresh_token: session.refreshToken
}
)
.then( body => {
projectSession.setSessionId(body["access_token"]);
_session.setSessionId(body["access_token"]);

// Add project to workspace
let projectName = util.getDefaultProject();
Expand All @@ -102,8 +102,7 @@ export function authorizeDefaultProject() {

// Refresh token expired, start new authorization
authorizeNewProject(
session["projectName"],
session["loginUrl"]
session.projectName, session.loginUrl
);
}
});
Expand Down
42 changes: 35 additions & 7 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as vscode from "vscode";
import * as _ from "lodash";
import * as nls from 'vscode-nls';
import * as util from "../utils/util";
import * as utility from "./utility";
import * as packages from "../utils/package";
Expand All @@ -14,18 +15,45 @@ import MetadataApi from "../salesforce/api/metadata";
import ApexApi from "../salesforce/api/apex";
import RestApi from "../salesforce/api/rest";
import ProgressNotification from "../utils/progress";
import { projectSettings, metadata } from "../settings";
import * as nls from 'vscode-nls';
import { _session, settings, metadata } from "../settings";

const localize = nls.loadMessageBundle();

/**
* Update user language as your chosen
*/
export async function updateUserLanguage() {
// Let user to choose language
let chosenItem: any = await vscode.window.showQuickPick(
_.map(settings.getUserLanguages(), (v, k) => {
return {
label: v,
description: k
};
})
);

let restApi = new RestApi();
ProgressNotification.showProgress(restApi, "patch", {
"serverUrl": "/sobjects/User/" + _session.getUserId(),
"data": {
"LanguageLocaleKey": chosenItem.label
}
})
.then( body => {
vscode.window.showInformationMessage(
`Your lanaguage is updated to ${chosenItem.description}`
);
});
}

export function executeRestTest() {
// Get selection in the active editor
let editor = vscode.window.activeTextEditor;
if (!editor) {
return util.showCommandWarning();
}

let serverUrl = "";
if (editor.selection) {
serverUrl = editor.document.getText(editor.selection);
Expand Down Expand Up @@ -59,7 +87,7 @@ export function executeQuery() {
}

let restApi = new RestApi();
ProgressNotification.showRESTProgress(restApi, "query", {
ProgressNotification.showProgress(restApi, "query", {
soql: soql
})
.then( body => {
Expand All @@ -83,7 +111,7 @@ export async function reloadSobjectCache(sobjects?: string[]) {

// If sobjects is not specified, describe global
if (!sobjects || sobjects.length === 0) {
return ProgressNotification.showRESTProgress(
return ProgressNotification.showProgress(
restApi, "describeGlobal", {}
)
.then( body => {
Expand Down Expand Up @@ -293,7 +321,7 @@ export function refreshThisFromServer() {

// Send get request
let restApi = new RestApi();
ProgressNotification.showRESTProgress(restApi, "query", {
ProgressNotification.showProgress(restApi, "query", {
serverUrl: `/${filep["id"]}`
})
.then( body => {
Expand Down Expand Up @@ -373,7 +401,7 @@ export function updateProject() {
* Create new project based on subscribed metadata objects
*/
export function createNewProject() {
let subscribedMetaObjects = projectSettings.getSubscribedMetaObjects();
let subscribedMetaObjects = settings.getSubscribedMetaObjects();

// If there is no subscribed metaObjects, so subscribe first
if (!subscribedMetaObjects || subscribedMetaObjects.length === 0) {
Expand Down
19 changes: 10 additions & 9 deletions src/commands/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import * as vscode from "vscode";
import * as xmlParser from "fast-xml-parser";
import * as _ from "lodash";
import * as util from "../utils/util";
import * as settingsUtil from "../settings/settingsUtil";
import { projectSettings, projectSession, metadata } from "../settings";
import * as nls from 'vscode-nls';
import * as settingsUtil from "../settings/settingsUtil";
import SessionModel from "../models/session";
import { settings, _session, metadata } from "../settings";

const localize = nls.loadMessageBundle();

Expand All @@ -25,7 +26,7 @@ export function toggleMetadataObjects() {
let metaObjects = _.sortBy(metadataObjects, mo => mo.xmlName);

// Get already subscribed meta objects
let subscribedMetaObjects = projectSettings.getSubscribedMetaObjects();
let subscribedMetaObjects = settings.getSubscribedMetaObjects();

return vscode.window.showQuickPick(
_.map(metaObjects, mo => {
Expand Down Expand Up @@ -130,10 +131,10 @@ export function locateThisInBrowser() {
* @param startUrl? Redirect url after login
*/
export function loginToSFDC(startUrl?: string) {
let session = projectSession.getSession();
let sess: SessionModel = _session.getSession();

let open_url = `${session["instanceUrl"]}/secur/frontdoor.jsp` +
`?sid=${session["sessionId"]}`;
let open_url = `${sess.instanceUrl}/secur/frontdoor.jsp` +
`?sid=${sess.sessionId}`;

if (startUrl) {
open_url += "&retURL=" + startUrl;
Expand All @@ -146,10 +147,10 @@ export function loginToSFDC(startUrl?: string) {
* Command for copying loginUrl to clipboard
*/
export function copyLoginUrl() {
let session = projectSession.getSession();
let sess: SessionModel = _session.getSession();

let loginUrl = `${session["instanceUrl"]}/secur/frontdoor.jsp` +
`?sid=${session["sessionId"]}`;
let loginUrl = `${sess.instanceUrl}/secur/frontdoor.jsp` +
`?sid=${sess.sessionId}`;

// Write loginUrl to clipboard
vscode.env.clipboard.writeText(loginUrl);
Expand Down
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export function activate(context: vscode.ExtensionContext) {
"extension.haoide.updateProject", main.updateProject
));

// Register updateUserLanguage command
context.subscriptions.push(vscode.commands.registerCommand(
"extension.haoide.updateUserLanguage",
main.updateUserLanguage
));

// Register createNewProject command
context.subscriptions.push(vscode.commands.registerCommand(
"extension.haoide.createNewProject", main.createNewProject
Expand Down
10 changes: 10 additions & 0 deletions src/models/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default interface Session {
orgnizationId: string;
userId: string;
sessionId: string;
refreshToken: string;
instanceUrl: string;
loginUrl: string;
projectName: string;
lastUpdatedTime: string;
}
4 changes: 2 additions & 2 deletions src/salesforce/api/apex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as xmlParser from "fast-xml-parser";
import * as auth from "../../commands/auth";
import SOAP from "../lib/soap";
import ProgressNotification from "../../utils/progress";
import { projectSession } from "../../settings";
import { _session } from "../../settings";

export default class ApexApi {
private soap!: SOAP;
Expand All @@ -24,7 +24,7 @@ export default class ApexApi {
}

private initiate(sessionInfo?: any) {
this.session = sessionInfo || projectSession.getSession();
this.session = sessionInfo || _session.getSession();
this.sessionId = this.session["sessionId"];
this.instanceUrl = this.session["instanceUrl"];
this.apiVersion = this.session["apiVersion"] || 46;
Expand Down
6 changes: 3 additions & 3 deletions src/salesforce/api/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as auth from "../../commands/auth";
import * as util from "../../utils/util";
import SOAP from "../lib/soap";
import ProgressNotification from "../../utils/progress";
import { projectSession, projectSettings } from "../../settings";
import { _session, settings } from "../../settings";

export default class MetadataApi {
private soap!: SOAP;
Expand All @@ -25,7 +25,7 @@ export default class MetadataApi {
}

private initiate(session?: any) {
this.session = session || projectSession.getSession();
this.session = session || _session.getSession();
this.sessionId = this.session["sessionId"];
this.instanceUrl = this.session["instanceUrl"];
this.apiVersion = this.session["apiVersion"] || 46;
Expand Down Expand Up @@ -255,7 +255,7 @@ export default class MetadataApi {
"requestType": "Deploy",
"zipfile": zipfile,
"testClasses": testClasses,
"deployOptions": projectSettings.getDeployOptions()
"deployOptions": settings.getDeployOptions()
};

ProgressNotification.showProgress(self, "_invoke_method", options)
Expand Down
7 changes: 4 additions & 3 deletions src/salesforce/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as request from "request-promise";
import * as auth from "../../commands/auth";
import * as querystring from "querystring";
import ProgressNotification from "../../utils/progress";
import { projectSession } from "../../settings";
import { _session } from "../../settings";

export default class RestApi {
private session: any;
Expand All @@ -27,7 +27,7 @@ export default class RestApi {
}

private initiate(session?: any) {
this.session = session || projectSession.getSession();
this.session = session || _session.getSession();
this.sessionId = this.session["sessionId"];
this.instanceUrl = this.session["instanceUrl"];
this.apiVersion = this.session["apiVersion"] || 46;
Expand Down Expand Up @@ -76,7 +76,7 @@ export default class RestApi {
headers: self.headers,
uri: self.buildFullUrl(options.serverUrl),
body: options.data,
json: true
json: options.json || true
};

// Send notification
Expand All @@ -90,6 +90,7 @@ export default class RestApi {
options.progress, `${options.method} is finished`, 100
);

console.log(body);
resolve(body);
})
.catch(err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class SobjectCompletionItemProvider implements vscode.CompletionItemProvi
if (enableDebugMode) {
console.log(pos);
}

// Initiate completion list
let completionItems: CompletionItem[] = [];

Expand Down
4 changes: 2 additions & 2 deletions src/salesforce/lib/auth/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as util from "../../../utils/util";
import * as contextUtil from "../../../utils/context";
import MetadataApi from "../../api/metadata";
import OAuth from "./oauth";
import { projectSession, metadata } from "../../../settings";
import { _session, metadata } from "../../../settings";

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -59,7 +59,7 @@ export function startServer(projectName: any, loginUrl: string) {
"projectName": projectName,
"lastUpdatedTime": moment().format()
};
projectSession.setSession(session);
_session.setSession(session);

// Describe metadata
new MetadataApi(session).describeMetadata({})
Expand Down
8 changes: 4 additions & 4 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ProjectSettings from './settings';
import Settings from './settings';
import ExtensionSettings from './extension';
import ProjectSession from './session';
import Session from './session';
import Metadata from './metadata';

export const extensionSettings = ExtensionSettings.getInstance();
export const projectSettings = ProjectSettings.getInstance();
export const projectSession = ProjectSession.getInstance();
export const settings = Settings.getInstance();
export const _session = Session.getInstance();
export const metadata = Metadata.getInstance();
Loading

0 comments on commit e51c9ad

Please sign in to comment.