Skip to content

Commit

Permalink
refactor: auto setContext (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinhui authored Aug 30, 2020
1 parent fec3411 commit 7c8d5cc
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 96 deletions.
66 changes: 49 additions & 17 deletions extensions/iceworks-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{
"id": "welcome",
"name": "Welcome",
"when": "iceworks:isNotTargetProject"
"when": "iceworks:projectIsNotTarget"
},
{
"id": "quickEntries",
Expand All @@ -46,22 +46,22 @@
{
"id": "npmScripts",
"name": "%iceworksApp.view.npmScripts.name%",
"when": "!iceworks:isNotTargetProject"
"when": "!iceworks:projectIsNotTarget"
},
{
"id": "pages",
"name": "%iceworksApp.view.pages.name%",
"when": "!iceworks:isNotTargetProject && !iceworks:isPegasusProject"
"when": "!iceworks:projectIsNotTarget && !iceworks:projectIsPegasus"
},
{
"id": "components",
"name": "%iceworksApp.view.components.name%",
"when": "!iceworks:isNotTargetProject && !iceworks:isPegasusProject"
"when": "!iceworks:projectIsNotTarget && !iceworks:projectIsPegasus"
},
{
"id": "nodeDependencies",
"name": "%iceworksApp.view.nodeDependencies.name%",
"when": "!iceworks:isNotTargetProject"
"when": "!iceworks:projectIsNotTarget"
}
]
},
Expand Down Expand Up @@ -148,14 +148,6 @@
"dark": "assets/dark/runDev.svg"
}
},
{
"command": "iceworksApp.npmScripts.stopDev",
"title": "%iceworksApp.command.npmScripts.stopDev.title%",
"icon": {
"light": "assets/light/stop.svg",
"dark": "assets/dark/stop.svg"
}
},
{
"command": "iceworksApp.editorMenu.runBuild",
"title": "%iceworksApp.command.npmScripts.runBuild.title%",
Expand Down Expand Up @@ -272,16 +264,56 @@
"menus": {
"commandPalette": [
{
"command": "iceworksApp.nodeDependencies.reinstall"
"command": "iceworksApp.npmScripts.refresh",
"when": "false"
},
{
"command": "iceworksApp.npmScripts.run",
"when": "false"
},
{
"command": "iceworksApp.nodeDependencies.dependencies.add"
"command": "iceworksApp.npmScripts.stop",
"when": "false"
},
{
"command": "iceworksApp.nodeDependencies.devDependencies.add"
"command": "iceworksApp.pages.add",
"when": "false"
},
{
"command": "iceworksApp.nodeDependencies.addDepsAndDevDeps"
"command": "iceworksApp.pages.refresh",
"when": "false"
},
{
"command": "iceworksApp.pages.openFile",
"when": "false"
},
{
"command": "iceworksApp.pages.delete",
"when": "false"
},
{
"command": "iceworksApp.components.add",
"when": "false"
},
{
"command": "iceworksApp.components.refresh",
"when": "false"
},
{
"command": "iceworksApp.components.delete",
"when": "false"
},
{
"command": "iceworksApp.components.openFile",
"when": "false"
},
{
"command": "iceworksApp.nodeDependencies.refresh",
"when": "false"
},
{
"command": "iceworksApp.nodeDependencies.upgrade",
"when": "false"
}
],
"editor/title": [
Expand Down
22 changes: 0 additions & 22 deletions extensions/iceworks-app/src/autoSetViewContext.ts

This file was deleted.

9 changes: 9 additions & 0 deletions extensions/iceworks-app/src/autoStart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as vscode from 'vscode';
import { checkIsNotTarget } from '@iceworks/project-service';

export default async function () {
const isNotTargetProject = await checkIsNotTarget();
if (isNotTargetProject) {
vscode.commands.executeCommand('iceworks-project-creator.start');
}
}
2 changes: 1 addition & 1 deletion extensions/iceworks-app/src/createEditorMenuAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function createEditorMenuAction() {
}

// Prepare VS Code debug config
setDebugConfig();
await setDebugConfig();

// Run Debug
let workspaceFolder;
Expand Down
6 changes: 3 additions & 3 deletions extensions/iceworks-app/src/debugConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import * as path from 'path';
import { parse } from 'comment-json';
import { checkIsPegasusProject } from '@iceworks/project-service';
import { getLaunchConfig, getTasksConfig } from './getDefaultConfigs';

// Iceworks debug config
Expand All @@ -26,7 +27,7 @@ function writeConfigFile(filePath: string, config: IDebugConfig) {
}

// Prepare VS Code debug config
export function setDebugConfig() {
export async function setDebugConfig() {
const { rootPath = __dirname } = vscode.workspace;

// Make .vscode directory
Expand All @@ -36,14 +37,13 @@ export function setDebugConfig() {
}

// Set pegasus service url
let isPegasusProject = false;
const isPegasusProject = await checkIsPegasusProject();
let specialLaunchUrl = '';
try {
const abcConfigFile = path.join(rootPath, 'abc.json');
if (fs.existsSync(abcConfigFile)) {
const abcConfig = fs.readJSONSync(abcConfigFile);
if (abcConfig.type === 'pegasus' && abcConfig.group && abcConfig.name) {
isPegasusProject = true;
specialLaunchUrl = `${BASE_URL}/${abcConfig.group}/${abcConfig.name}`;
}
}
Expand Down
18 changes: 11 additions & 7 deletions extensions/iceworks-app/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as vscode from 'vscode';
import { window, ViewColumn } from 'vscode';
import { connectService, getHtmlForWebview } from '@iceworks/vscode-webview/lib/vscode';
import { getProjectType, checkIsPegasusProject } from '@iceworks/project-service';
import {
getProjectType,
checkIsPegasusProject,
autoSetContext as autoSetContextByProject,
} from '@iceworks/project-service';
import { Recorder, recordDAU } from '@iceworks/recorder';
import { initExtension, checkIsAliInternal, registerCommand } from '@iceworks/common-service';
import { createNpmScriptsTreeView } from './views/npmScriptsView';
Expand All @@ -14,7 +18,7 @@ import { showExtensionsQuickPickCommandId } from './constants';
import showEntriesQuickPick from './quickPicks/showEntriesQuickPick';
import createEditorMenuAction from './createEditorMenuAction';
import createExtensionsStatusBar from './statusBar/createExtensionsStatusBar';
import autoSetViewContext from './autoSetViewContext';
import autoStart from './autoStart';
import i18n from './i18n';

// eslint-disable-next-line
Expand All @@ -24,11 +28,12 @@ const recorder = new Recorder(name, version);
export async function activate(context: vscode.ExtensionContext) {
const { subscriptions, extensionPath } = context;

// auto set configuration & context
initExtension(context);
autoSetContextByProject();

const projectType = await getProjectType();
const isPegasusProject = await checkIsPegasusProject();
vscode.commands.executeCommand('setContext', 'iceworks:isPegasusProject', isPegasusProject);
// auto set configuration
initExtension(context);

// init statusBarItem
const extensionsStatusBar = createExtensionsStatusBar();
Expand Down Expand Up @@ -109,14 +114,13 @@ export async function activate(context: vscode.ExtensionContext) {
if (visible && !didSetViewContext) {
didSetViewContext = true;
recordDAU();
autoSetViewContext();
autoStart();
}
});
});

// init editor title menu
if (projectType !== 'unknown') {
vscode.commands.executeCommand('setContext', 'iceworks:isAliInternal', await checkIsAliInternal());
vscode.commands.executeCommand('setContext', 'iceworks:showScriptIconInEditorTitleMenu', true);
await createEditorMenuAction();
}
Expand Down
79 changes: 59 additions & 20 deletions extensions/iceworks-app/src/getQuickEntryOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getProjectFramework } from '@iceworks/project-service';
import { getProjectFramework, checkIsPegasusProject, checkIsNotTarget } from '@iceworks/project-service';
import { checkIsAliInternal } from '@iceworks/common-service';
import i18n from './i18n';

const entries = [
Expand All @@ -8,14 +9,37 @@ const entries = [
command: 'iceworks-project-creator.start',
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.pageBuilder.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.pageBuilder.detail'),
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.generatePage.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.generatePage.detail'),
command: 'iceworks-ui-builder.generate-page',
async condition() {
return !(await checkIsNotTarget()) && !(await checkIsPegasusProject());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.createPage.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.createPage.detail'),
command: 'iceworks-ui-builder.create-page',
async condition() {
return !(await checkIsNotTarget()) && !(await checkIsPegasusProject());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.generateComponent.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.generateComponent.detail'),
command: 'iceworks-ui-builder.generate-component',
async condition() {
const projectFramework = await getProjectFramework();
return projectFramework === 'icejs';
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.materialImport.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.materialImport.detail'),
command: 'iceworks-material-helper.start',
async condition() {
return !(await checkIsNotTarget());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.showMaterialDocs.label'),
Expand All @@ -26,21 +50,41 @@ const entries = [
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.runDebug.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.runDebug.detail'),
command: 'iceworksApp.editorMenu.runDebug',
async condition() {
return !(await checkIsNotTarget());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.runBuild.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.runBuild.detail'),
command: 'iceworksApp.editorMenu.runBuild',
async condition() {
return !(await checkIsNotTarget());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.DefPublish.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.DefPublish.detail'),
command: 'iceworksApp.editorMenu.DefPublish',
async condition() {
return (await checkIsAliInternal()) && !(await checkIsNotTarget());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.reinstall.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.reinstall.detail'),
command: 'iceworksApp.nodeDependencies.reinstall',
async condition() {
return !(await checkIsNotTarget());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.addDepsAndDevDeps.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.addDepsAndDevDeps.detail'),
command: 'iceworksApp.nodeDependencies.addDepsAndDevDeps',
async condition() {
return !(await checkIsNotTarget());
},
},
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.openSettings.label'),
Expand All @@ -49,21 +93,16 @@ const entries = [
},
];

const optionalEntries = [
{
label: i18n.format('extension.iceworksApp.showEntriesQuickPick.generateComponent.label'),
detail: i18n.format('extension.iceworksApp.showEntriesQuickPick.generateComponent.detail'),
command: 'iceworks-ui-builder.generate-component',
},
];
export default async function () {
const conditionResults = await Promise.all(
entries.map(async function ({ condition }) {
if (condition) {
return await condition();
} else {
return true;
}
})
);

export default async () => {
const projectFramework = await getProjectFramework();
if (projectFramework === 'icejs') {
const newEntries = [...entries];
newEntries.splice(1, 0, ...optionalEntries);
return newEntries;
} else {
return entries;
}
};
return entries.filter((v, index) => conditionResults[index]);
}
14 changes: 7 additions & 7 deletions extensions/iceworks-app/src/locales/en-US.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"extension.iceworksApp.showEntriesQuickPick.projectCreater.label": "Create Application",
"extension.iceworksApp.showEntriesQuickPick.projectCreater.detail": "Create a Universal Application(React/Rax/Vue, etc) quickly",
"extension.iceworksApp.showEntriesQuickPick.pageBuilder.label": "Build Page",
"extension.iceworksApp.showEntriesQuickPick.pageBuilder.detail": "Build Page by assemblying blocks",
"extension.iceworksApp.showEntriesQuickPick.generatePage.label": "Build Page by assembling",
"extension.iceworksApp.showEntriesQuickPick.generatePage.detail": "Generating pages by block assembly",
"extension.iceworksApp.showEntriesQuickPick.createPage.label": "Build page by configure",
"extension.iceworksApp.showEntriesQuickPick.createPage.detail": "Generate pages by using template visualization configuration",
"extension.iceworksApp.showEntriesQuickPick.materialImport.label": "Import Component",
"extension.iceworksApp.showEntriesQuickPick.materialImport.detail": "Add Component to the Application in a visual way",
"extension.iceworksApp.showEntriesQuickPick.generateComponent.label": "Generate Component",
Expand All @@ -17,6 +19,8 @@
"extension.iceworksApp.showEntriesQuickPick.runDebug.detail": "Debug and Preview the application through VS Code",
"extension.iceworksApp.showEntriesQuickPick.runBuild.label": "Run Build",
"extension.iceworksApp.showEntriesQuickPick.runBuild.detail": "Build the application of online resources",
"extension.iceworksApp.showEntriesQuickPick.DefPublish.label": "DEF Publish",
"extension.iceworksApp.showEntriesQuickPick.DefPublish.detail": "Publish Application to DEF",
"extension.iceworksApp.showEntriesQuickPick.reinstall.label": "Reinstall Dependencies",
"extension.iceworksApp.showEntriesQuickPick.reinstall.detail": "Reinstall the dependency packages for the application",
"extension.iceworksApp.showEntriesQuickPick.addDepsAndDevDeps.label": "Add Dependency",
Expand All @@ -30,9 +34,5 @@
"extension.iceworksApp.showDepsQuickPick.quickPickItem.detail": "Install <%= label %>",
"extension.iceworksApp.openEntryFile.ErrorMessage": "Cannot find entry",
"extension.iceworksApp.extension.title": "Iceworks Settings",
"extension.iceworksApp.extension.emptyWorkplace": "Current workspace is empty, please open or create an application.",
"extension.iceworksApp.views.pageViews.createPage": "Create Page",
"extension.iceworksApp.views.pageViews.createPageDetail": "Create page through page templates",
"extension.iceworksApp.views.pageViews.generatePage": "Generate Page",
"extension.iceworksApp.views.pageViews.generatePageDetail": "Generate page through assmebling component materials"
"extension.iceworksApp.extension.emptyWorkplace": "Current workspace is empty, please open or create an application."
}
Loading

0 comments on commit 7c8d5cc

Please sign in to comment.