src/goMain.ts: refactor welcome panel activation

The logic to activate and regsiter the welcome panel
is moved to a static method.

Change-Id: I1c194b53d0ded51e2f58a810d87e5b2b275e47f4
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/404378
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
diff --git a/src/commands/index.ts b/src/commands/index.ts
index 1d7c5a1..4a48fdd 100644
--- a/src/commands/index.ts
+++ b/src/commands/index.ts
@@ -23,3 +23,9 @@
 	ctx: vscode.ExtensionContext,
 	goCtx: GoExtensionContext
 ) => CommandCallback<T>;
+
+export function createRegisterCommand(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) {
+	return function registerCommand(name: string, fn: CommandFactory) {
+		ctx.subscriptions.push(vscode.commands.registerCommand(name, fn(ctx, goCtx)));
+	};
+}
diff --git a/src/goMain.ts b/src/goMain.ts
index b31c98f..7812323 100644
--- a/src/goMain.ts
+++ b/src/goMain.ts
@@ -126,19 +126,7 @@
 	const cfg = getGoConfig();
 	setLogConfig(cfg['logging']);
 
-	if (vscode.window.registerWebviewPanelSerializer) {
-		// Make sure we register a serializer in activation event
-		vscode.window.registerWebviewPanelSerializer(WelcomePanel.viewType, {
-			async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state: any) {
-				WelcomePanel.revive(webviewPanel, ctx.extensionUri);
-			}
-		});
-	}
-
-	// Show the Go welcome page on update.
-	if (!extensionInfo.isInCloudIDE) {
-		showGoWelcomePage(ctx);
-	}
+	WelcomePanel.activate(ctx, goCtx);
 
 	const configGOROOT = getGoConfig()['goroot'];
 	if (configGOROOT) {
@@ -199,7 +187,7 @@
 
 	initCoverageDecorators(ctx);
 
-	const registerCommand = createRegisterCommand(ctx, goCtx);
+	const registerCommand = commands.createRegisterCommand(ctx, goCtx);
 
 	registerCommand('go.languageserver.restart', commands.startLanguageServer);
 
@@ -209,7 +197,6 @@
 		vscode.workspace.onDidChangeConfiguration((e) => watchLanguageServerConfiguration(goCtx, e))
 	);
 
-	registerCommand('go.welcome', (ctx) => () => WelcomePanel.createOrShow(ctx.extensionUri));
 	registerCommand('go.environment.status', (_ctx, goCtx) => () => expandGoStatusBar(goCtx));
 
 	const testCodeLensProvider = new GoRunTestCodeLensProvider();
@@ -406,42 +393,6 @@
 	return extensionAPI;
 }
 
-function showGoWelcomePage(ctx: vscode.ExtensionContext) {
-	// Update this list of versions when there is a new version where we want to
-	// show the welcome page on update.
-	const showVersions: string[] = ['0.30.0'];
-	// TODO(hyangah): use the content hash instead of hard-coded string.
-	// https://github.com/golang/vscode-go/issue/1179
-	let goExtensionVersion = '0.30.0';
-	let goExtensionVersionKey = 'go.extensionVersion';
-	if (extensionInfo.isPreview) {
-		goExtensionVersion = '0.0.0';
-		goExtensionVersionKey = 'go.nightlyExtensionVersion';
-	}
-
-	const savedGoExtensionVersion = getFromGlobalState(goExtensionVersionKey, '');
-
-	if (shouldShowGoWelcomePage(showVersions, goExtensionVersion, savedGoExtensionVersion)) {
-		WelcomePanel.createOrShow(ctx.extensionUri);
-	}
-	if (goExtensionVersion !== savedGoExtensionVersion) {
-		updateGlobalState(goExtensionVersionKey, goExtensionVersion);
-	}
-}
-
-export function shouldShowGoWelcomePage(showVersions: string[], newVersion: string, oldVersion: string): boolean {
-	if (newVersion === oldVersion) {
-		return false;
-	}
-	const coercedNew = semver.coerce(newVersion);
-	const coercedOld = semver.coerce(oldVersion);
-	if (!coercedNew || !coercedOld) {
-		return true;
-	}
-	// Both semver.coerce(0.22.0) and semver.coerce(0.22.0-rc.1) will be 0.22.0.
-	return semver.gte(coercedNew, coercedOld) && showVersions.includes(coercedNew.toString());
-}
-
 export function deactivate() {
 	return Promise.all([
 		cancelRunningTests(),
@@ -665,9 +616,3 @@
 		delete process.env.GOROOT;
 	}
 }
-
-function createRegisterCommand(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) {
-	return function registerCommand(name: string, fn: commands.CommandFactory) {
-		ctx.subscriptions.push(vscode.commands.registerCommand(name, fn(ctx, goCtx)));
-	};
-}
diff --git a/src/welcome.ts b/src/welcome.ts
index e881e8e..b40aa7e 100644
--- a/src/welcome.ts
+++ b/src/welcome.ts
@@ -9,37 +9,65 @@
 
 import vscode = require('vscode');
 import path = require('path');
+import semver = require('semver');
 import { extensionId } from './const';
+import { GoExtensionContext } from './context';
+import { extensionInfo } from './config';
+import { getFromGlobalState, updateGlobalState } from './stateUtils';
+import { createRegisterCommand } from './commands';
+
 export class WelcomePanel {
+	public static activate(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) {
+		const registerCommand = createRegisterCommand(ctx, goCtx);
+		registerCommand('go.welcome', WelcomePanel.createOrShow);
+
+		if (vscode.window.registerWebviewPanelSerializer) {
+			// Make sure we register a serializer in activation event
+			vscode.window.registerWebviewPanelSerializer(WelcomePanel.viewType, {
+				async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel) {
+					WelcomePanel.revive(webviewPanel, ctx.extensionUri);
+				}
+			});
+		}
+
+		// Show the Go welcome page on update.
+		if (!extensionInfo.isInCloudIDE) {
+			showGoWelcomePage();
+		}
+	}
+
 	public static currentPanel: WelcomePanel | undefined;
 
 	public static readonly viewType = 'welcomeGo';
 
-	public static createOrShow(extensionUri: vscode.Uri) {
-		const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;
+	public static createOrShow(ctx: Pick<vscode.ExtensionContext, 'extensionUri'>) {
+		return () => {
+			const extensionUri = ctx.extensionUri;
+			const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;
 
-		// If we already have a panel, show it.
-		if (WelcomePanel.currentPanel) {
-			WelcomePanel.currentPanel.panel.reveal(column);
-			return;
-		}
-
-		// Otherwise, create a new panel.
-		const panel = vscode.window.createWebviewPanel(
-			WelcomePanel.viewType,
-			'Go for VS Code',
-			column || vscode.ViewColumn.One,
-			{
-				// Enable javascript in the webview
-				enableScripts: true,
-
-				// And restrict the webview to only loading content from our extension's directory.
-				localResourceRoots: [joinPath(extensionUri)]
+			// If we already have a panel, show it.
+			if (WelcomePanel.currentPanel) {
+				WelcomePanel.currentPanel.panel.reveal(column);
+				return;
 			}
-		);
-		panel.iconPath = joinPath(extensionUri, 'media', 'go-logo-blue.png');
 
-		WelcomePanel.currentPanel = new WelcomePanel(panel, extensionUri);
+			// Otherwise, create a new panel.
+			const panel = vscode.window.createWebviewPanel(
+				WelcomePanel.viewType,
+				'Go for VS Code',
+				column || vscode.ViewColumn.One,
+				{
+					// Enable javascript in the webview
+					enableScripts: true,
+
+					// And restrict the webview to only loading content from our extension's directory.
+					localResourceRoots: [joinPath(extensionUri)]
+				}
+			);
+			panel.iconPath = joinPath(extensionUri, 'media', 'go-logo-blue.png');
+
+			WelcomePanel.currentPanel = new WelcomePanel(panel, extensionUri);
+		};
 	}
 
 	public static revive(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
@@ -218,3 +246,39 @@
 	}
 	return uri.with({ path: vscode.Uri.file(path.join(uri.fsPath, ...pathFragment)).path });
 }
+
+function showGoWelcomePage() {
+	// Update this list of versions when there is a new version where we want to
+	// show the welcome page on update.
+	const showVersions: string[] = ['0.30.0'];
+	// TODO(hyangah): use the content hash instead of hard-coded string.
+	// https://github.com/golang/vscode-go/issue/1179
+	let goExtensionVersion = '0.30.0';
+	let goExtensionVersionKey = 'go.extensionVersion';
+	if (extensionInfo.isPreview) {
+		goExtensionVersion = '0.0.0';
+		goExtensionVersionKey = 'go.nightlyExtensionVersion';
+	}
+
+	const savedGoExtensionVersion = getFromGlobalState(goExtensionVersionKey, '');
+
+	if (shouldShowGoWelcomePage(showVersions, goExtensionVersion, savedGoExtensionVersion)) {
+		vscode.commands.executeCommand('go.welcome');
+	}
+	if (goExtensionVersion !== savedGoExtensionVersion) {
+		updateGlobalState(goExtensionVersionKey, goExtensionVersion);
+	}
+}
+
+export function shouldShowGoWelcomePage(showVersions: string[], newVersion: string, oldVersion: string): boolean {
+	if (newVersion === oldVersion) {
+		return false;
+	}
+	const coercedNew = semver.coerce(newVersion);
+	const coercedOld = semver.coerce(oldVersion);
+	if (!coercedNew || !coercedOld) {
+		return true;
+	}
+	// Both semver.coerce(0.22.0) and semver.coerce(0.22.0-rc.1) will be 0.22.0.
+	return semver.gte(coercedNew, coercedOld) && showVersions.includes(coercedNew.toString());
+}
diff --git a/test/integration/welcome.test.ts b/test/integration/welcome.test.ts
index 2834ac3..e3fd1d5 100644
--- a/test/integration/welcome.test.ts
+++ b/test/integration/welcome.test.ts
@@ -5,7 +5,7 @@
 
 import vscode = require('vscode');
 import assert from 'assert';
-import { shouldShowGoWelcomePage } from '../../src/goMain';
+import { shouldShowGoWelcomePage } from '../../src/welcome';
 import { extensionId } from '../../src/const';
 import { WelcomePanel } from '../../src/welcome';
 
@@ -62,7 +62,7 @@
 suite('joinPath Tests', () => {
 	test('WelcomePanel dataroot is set as expected', () => {
 		const uri = vscode.extensions.getExtension(extensionId).extensionUri;
-		WelcomePanel.createOrShow(uri);
+		WelcomePanel.createOrShow({ extensionUri: uri })();
 		const got = WelcomePanel.currentPanel.dataroot;
 		const want = vscode.Uri.joinPath(uri, 'media');
 		assert.strictEqual(got.toString(), want.toString());