Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸš€ Add command to increase project version and implement version increment logic, Closes #94 #387

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/constants/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const Commands = {
// Rename
renameProject: `${EXTENSION_NAME}.renameProject`,

// Increase version
increaseVersion: `${EXTENSION_NAME}.increaseVersion`,

// Grant API permissions
grantAPIPermissions: `${EXTENSION_NAME}.grantAPIPermissions`,

Expand Down
24 changes: 24 additions & 0 deletions src/panels/CommandPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Subscription } from '../models';
import { Extension } from '../services/dataType/Extension';
import { getExtensionSettings } from '../utils';
import { Notifications } from '../services/dataType/Notifications';
import { increaseVersion } from '../utils/increaseVersion';


export class CommandPanel {
Expand All @@ -29,6 +30,9 @@ export class CommandPanel {
subscriptions.push(
commands.registerCommand(Commands.welcome, () => commands.executeCommand('workbench.action.openWalkthrough', 'm365pnp.viva-connections-toolkit#spfx-toolkit-intro', false))
);
subscriptions.push(
commands.registerCommand(Commands.increaseVersion, CommandPanel.increaseVersion)
);

CommandPanel.init();
}
Expand Down Expand Up @@ -302,6 +306,7 @@ export class CommandPanel {
actionCommands.push(new ActionTreeItem('Upgrade project SPFx version', '', { name: 'arrow-up', custom: false }, undefined, Commands.upgradeProject));
actionCommands.push(new ActionTreeItem('Validate project correctness', '', { name: 'check-all', custom: false }, undefined, Commands.validateProject));
actionCommands.push(new ActionTreeItem('Rename project', '', { name: 'whole-word', custom: false }, undefined, Commands.renameProject));
actionCommands.push(new ActionTreeItem('Increase project version', '', { name: 'arrow-up', custom: false }, undefined, Commands.increaseVersion));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets pick a different icon. This one is already used in upgrade action which may be just confusing.
we may use any icon from this
https://microsoft.github.io/vscode-codicons/dist/codicon.html
what do you think about fold-up icon πŸ€”


if (EnvironmentInformation.account) {
actionCommands.push(new ActionTreeItem('Grant API permissions', '', { name: 'workspace-trusted', custom: false }, undefined, Commands.grantAPIPermissions));
Expand Down Expand Up @@ -352,4 +357,23 @@ export class CommandPanel {
private static showWelcome() {
commands.executeCommand('setContext', ContextKeys.showWelcome, true);
}

/**
* Increases the version of the project.
*/
public static async increaseVersion() {
const versionType = await window.showQuickPick(['major', 'minor', 'patch'], {
placeHolder: 'Select the version type to increase',
ignoreFocusOut: true,
canPickMany: false,
title: 'Increase Version'
});

if (!versionType) {
return;
}

await increaseVersion(versionType as 'major' | 'minor' | 'patch');
Notifications.info('Version increased successfully.');
}
}
39 changes: 39 additions & 0 deletions src/utils/increaseVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { workspace } from 'vscode';


export async function increaseVersion(versionType: 'major' | 'minor' | 'patch') {
const wsFolder = workspace.workspaceFolders?.[0];
if (!wsFolder) {
throw new Error('Workspace folder not found');
}

const packageJsonPath = join(wsFolder.uri.fsPath, 'package.json');
const packageSolutionPath = join(wsFolder.uri.fsPath, 'config/package-solution.json');
Comment on lines +7 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not work when the SPFx project was created by TeamsToolkit. We should also support that case.
In case TT created a project that uses SPFx project then the SPFx part is in a src subcatalog.
I took a screenshot of the result when we use SPFx Toolkit in a project created by TT.
image

We already have some parts in SPFx Toolkit that add this support and check. Like here

let launchFiles = await workspace.findFiles('.vscode/launch.json', '**/node_modules/**');
if (!launchFiles || launchFiles.length <= 0) {
launchFiles = await workspace.findFiles('src/.vscode/launch.json', '**/node_modules/**');
}


const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
const packageSolution = JSON.parse(readFileSync(packageSolutionPath, 'utf8'));

const newVersion = incrementVersion(packageJson.version, versionType);
packageJson.version = newVersion;
packageSolution.solution.version = `${newVersion}.0`;

writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
writeFileSync(packageSolutionPath, JSON.stringify(packageSolution, null, 2));
}

function incrementVersion(version: string, versionType: 'major' | 'minor' | 'patch'): string {
const [major, minor, patch] = version.split('.').map(Number);

switch (versionType) {
case 'major':
return `${major + 1}.0.0`;
case 'minor':
return `${major}.${minor + 1}.0`;
case 'patch':
return `${major}.${minor}.${patch + 1}`;
default:
throw new Error('Invalid version type');
}
}