Skip to content

Commit d8078bc

Browse files
Introduce devsphere view specific commands
1 parent 4f6456a commit d8078bc

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-1
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@
9999
},
100100
"category": "Atlassian"
101101
},
102+
{
103+
"command": "atlascode.devsphere.review.initialise",
104+
"title": "Initialise Devsphere Review Settings",
105+
"category": "Atlassian"
106+
},
107+
{
108+
"command": "atlascode.devsphere.custom.reset",
109+
"title": "Reset Devsphere Custom Configuration",
110+
"category": "Atlassian"
111+
},
102112
{
103113
"command": "atlascode.jira.searchIssues",
104114
"title": "Search Jira Issue Results",

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export enum Commands {
2929
BitbucketSelectContainer = 'atlascode.bb.selectContainer',
3030
BitbucketPullRequestsOverviewRefresh = 'atlascode.bitbucket.pullRequestsOverview.refresh',
3131
BitbucketPullRequestsOverviewFocus = 'atlascode.views.bb.pullRequestsOverviewTreeView.focus',
32+
InitialiseDevsphereReviewSettings = 'atlascode.devsphere.review.initialise',
33+
ResetDevsphereCustomConfiguration = 'atlascode.devsphere.custom.reset',
3234
BitbucketFetchPullRequests = 'atlascode.bb.fetchPullRequests',
3335
BitbucketRefreshPullRequests = 'atlascode.bb.refreshPullRequests',
3436
BitbucketToggleFileNesting = 'atlascode.bb.toggleFileNesting',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Commands } from '../../commands';
2+
import { executeVSCodeCommand, LifecycleFns, updateConfig } from './devsphereConfigurationManager';
3+
4+
async function reviewSetup(): Promise<void> {
5+
await Promise.all([
6+
executeVSCodeCommand(Commands.BitbucketPullRequestsOverviewFocus),
7+
updateConfig('workbench.editor', 'showTabs', 'single'),
8+
executeVSCodeCommand('workbench.action.closePanel'),
9+
executeVSCodeCommand('workbench.action.closeAuxiliaryBar'),
10+
]);
11+
}
12+
13+
async function reviewTeardown(): Promise<void> {
14+
await Promise.allSettled([
15+
updateConfig('workbench.editor', 'showTabs', 'multiple'),
16+
executeVSCodeCommand('workbench.files.action.focusFilesExplorer'),
17+
]);
18+
}
19+
20+
export const reviewLifecycleFns: LifecycleFns = {
21+
setup: reviewSetup,
22+
shutdown: reviewTeardown,
23+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { commands, Disposable, workspace } from 'vscode';
2+
3+
export async function executeVSCodeCommand(command: string): Promise<void> {
4+
try {
5+
await commands.executeCommand(command);
6+
} catch (error) {
7+
console.error(`Failed to execute VS Code command: ${command}`, error);
8+
}
9+
}
10+
11+
export async function updateConfig(section: string, key: string, value: any): Promise<void> {
12+
const config = workspace.getConfiguration(section);
13+
if (config.get(key) !== value) {
14+
await config.update(key, value, true);
15+
}
16+
}
17+
18+
/**
19+
* Type representing all valid view names in the application.
20+
*
21+
* @important When creating a new view, you must add its name to this union type
22+
* and register its teardown function in the teardownFunctions map.
23+
*/
24+
export enum View {
25+
Review = 'review',
26+
Noop = 'noop',
27+
}
28+
29+
export type LifecycleFns = {
30+
setup: () => Promise<void>;
31+
shutdown: () => Promise<void>;
32+
};
33+
34+
export const getDefaultLifecycleFns = (): LifecycleFns => ({
35+
setup: async () => {},
36+
shutdown: async () => {},
37+
});
38+
39+
export class DevsphereConfigurationManager implements Disposable {
40+
private currentView: View = View.Noop;
41+
42+
constructor(private lifecycleFunctions: Record<View, LifecycleFns>) {}
43+
44+
dispose() {
45+
this.lifecycleFunctions = {
46+
[View.Noop]: getDefaultLifecycleFns(),
47+
[View.Review]: getDefaultLifecycleFns(),
48+
};
49+
}
50+
51+
async setupView(view: View) {
52+
const oldView = this.currentView;
53+
this.currentView = view;
54+
55+
if (oldView !== view) {
56+
try {
57+
await this.lifecycleFunctions[oldView].shutdown();
58+
} catch (error) {
59+
console.error(`Failed to shutdown view: ${oldView}`, error);
60+
}
61+
}
62+
63+
try {
64+
// Setup is called even if oldView is the same because a user may have run the individual
65+
// commands separately and we want a consistent state when the view is re-selected.
66+
await this.lifecycleFunctions[view].setup();
67+
} catch (error) {
68+
console.error(`Failed to setup view: ${view}`, error);
69+
}
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { commands, ExtensionContext } from 'vscode';
2+
3+
import { Commands } from '../../commands';
4+
import { reviewLifecycleFns } from './devsphereConfigReview';
5+
import {
6+
DevsphereConfigurationManager,
7+
getDefaultLifecycleFns,
8+
LifecycleFns,
9+
View,
10+
} from './devsphereConfigurationManager';
11+
12+
// Map view names to their teardown functions - must include all possible ViewName values
13+
const lifecycleFunctions: Record<View, LifecycleFns> = {
14+
[View.Review]: reviewLifecycleFns,
15+
[View.Noop]: getDefaultLifecycleFns(),
16+
};
17+
18+
export function registerDevsphereCommands(context: ExtensionContext): void {
19+
const devsphereConfigManager = new DevsphereConfigurationManager(lifecycleFunctions);
20+
21+
context.subscriptions.push(devsphereConfigManager);
22+
23+
context.subscriptions.push(
24+
commands.registerCommand(Commands.InitialiseDevsphereReviewSettings, () =>
25+
devsphereConfigManager.setupView(View.Review),
26+
),
27+
commands.registerCommand(Commands.ResetDevsphereCustomConfiguration, () =>
28+
devsphereConfigManager.setupView(View.Noop),
29+
),
30+
);
31+
}

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { activate as activateCodebucket } from './codebucket/command/registerCom
1010
import { CommandContext, setCommandContext } from './commandContext';
1111
import { Commands, registerCommands } from './commands';
1212
import { Configuration, configuration, IConfig } from './config/configuration';
13+
import { registerDevsphereCommands } from './config/devsphere-config/registerDevsphereCommands';
1314
import { ExtensionId, GlobalStateVersionKey } from './constants';
1415
import { Container } from './container';
1516
import { registerAnalyticsClient, registerErrorReporting, unregisterErrorReporting } from './errorReporting';
@@ -92,7 +93,7 @@ export async function activate(context: ExtensionContext) {
9293
// icon to appear in the activity bar
9394
activateBitbucketFeatures();
9495
activateYamlFeatures(context);
95-
96+
registerDevsphereCommands(context);
9697
Logger.info(
9798
`Atlassian for VS Code (v${atlascodeVersion}) activated in ${
9899
duration[0] * 1000 + Math.floor(duration[1] / 1000000)

0 commit comments

Comments
 (0)