Skip to content

Commit

Permalink
Fix: launch custom profile menu (workaround) (#73)
Browse files Browse the repository at this point in the history
* fix: create a new command to fix launching profile.json files

* fix: remove offending launch custom profiles from vscode-manager's profile.json
  • Loading branch information
mrsauravsahu committed Jul 30, 2023
1 parent 9b737cb commit 33cfa26
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
1 change: 0 additions & 1 deletion .vscode/profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"extensions": [
"dbaeumer.vscode-eslint",
"Gruntfuggly.todo-tree",
"jolaleye.horizon-theme-vscode",
"mrsauravsahu.vscode-manager",
"PKief.material-icon-theme"
]
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "mrsauravsahu",
"icon": "resources/icon.png",
"description": "Manage VSCode custom profiles with isolated settings and extensions",
"version": "2.3.2",
"version": "2.3.3",
"repository": {
"url": "https://github.com/mrsauravsahu/vscode-manager",
"type": "git"
Expand All @@ -31,8 +31,8 @@
"menus": {
"explorer/context": [
{
"when": "resourceLangId == json && resourceFilename =~ /profile.json$/ && resourceDirname =~ /.vscode$/",
"command": "vscode-manager.commands.launchProfile",
"when": "resourceFilename =~ /profile.json$/ && resourceDirname =~ /.vscode$/",
"command": "vscode-manager.commands.launchProfileJson",
"group": "customProfiles"
}
],
Expand Down Expand Up @@ -77,6 +77,10 @@
]
},
"commands": [
{
"command": "vscode-manager.commands.launchProfileJson",
"title": "Launch Custom Profile"
},
{
"command": "vscode-manager.commands.launchProfile",
"title": "Launch Custom Profile",
Expand Down
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {refreshProfilesCommand} from './refresh-profiles'
import {renameProfileCommand} from './rename-profile'
import {selectProfileCommand} from './select-profile'
import {launchProfileCommand} from './launch-profile'
import {launchProfileJsonCommand} from './launch-profile-json'
import {selectFeaturedProfileCommand} from './select-featured-profile'
import {requestFeaturedProfileCommand} from './request-featured-profile'

Expand All @@ -18,6 +19,7 @@ export const commands = [
refreshProfilesCommand,
selectProfileCommand,
launchProfileCommand,
launchProfileJsonCommand,
selectFeaturedProfileCommand,
requestFeaturedProfileCommand,
]
Expand Down
113 changes: 113 additions & 0 deletions src/commands/launch-profile-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import * as fs from 'fs'
import * as path from 'path'
import * as child_process from 'child-process-promise'
import * as vscode from 'vscode'

import {commands} from '../constants'
import {CustomProfile} from '../models/custom-profile'
import {Command, CustomProfileDetails} from '../types'

export const launchProfileJsonCommand: Command = {
name: commands.launchProfileJson,
handler: ({services: {extensionMetaService, customProfileService, commandGeneratorService, commandMetaService}}) => (arg: CustomProfile | {fsPath: string}) => vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: 'Launching Custom Profile',
cancellable: false,
}, async progress => {
const codeBin = await commandMetaService.getProgramBasedOnMetaAsync('code')
if (arg instanceof CustomProfile) {
// Custom profile launch
const {name} = arg

if (name === 'default') {
await child_process.exec('code -n')
} else {
const {command: launchCommand, shell} = commandGeneratorService.generateCommand(codeBin,
`--user-data-dir '${path.join(extensionMetaService.globalProfilesLocation, name, 'data')}' --extensions-dir '${path.join(extensionMetaService.globalProfilesLocation, name, 'extensions')}' -n`)
await child_process.exec(launchCommand, {shell})
}
} else {
// Custom profile launch from .json file
// vscode.window.showInformationMessage(arg.path);
const {fsPath: profilePath} = arg

// Check if this profile file has same value as
// the generated value from profile name;
if (!fs.existsSync(profilePath)) {
await vscode.window.showInformationMessage('Unable to find the custom profile details file.')
}

const profileDetailsString = await fs.promises.readFile(profilePath, {encoding: 'utf-8'})

let profileDetailsJson: any = {}
try {
profileDetailsJson = JSON.parse(profileDetailsString)
} catch {
await vscode.window.showInformationMessage('The selected profile details file has invalid data. Please use a valid JSON file.')
return
}

// TODO: Validate profileDetailsJson
// TODO: Add strong type for profile details json
const {
name: profileName,
userSettings,
extensions,
} = profileDetailsJson as CustomProfileDetails

const profileRootPath = path.join(extensionMetaService.globalProfilesLocation, profileName)
// TODO: Check if profile exists
const profileExists = fs.existsSync(profileRootPath)

if (!profileExists) {
// Await vscode.window.showInformationMessage('The selected profile does not exist. Creating it now.')

progress.report({increment: 10, message: 'creating Custom Profile folder...'})
await fs.promises.mkdir(profileRootPath)

progress.report({increment: 30, message: 'copying Custom Profile user settings...'})

const {command: createDirCommand, shell} = commandGeneratorService.generateCommand('mkdir',
path.join(extensionMetaService.globalProfilesLocation, profileName, 'data', 'User'), {
Linux: '-p',
Darwin: '-p',
Windows_NT: undefined,
})
await child_process.exec(createDirCommand, {shell})

await fs.promises.writeFile(
path.join(extensionMetaService.globalProfilesLocation, profileName, 'data', 'User', 'settings.json'),
JSON.stringify(userSettings, undefined, 2),
{encoding: 'utf-8'},
)

progress.report({increment: 50, message: 'installing extensions...'})

const installExtensionPromises = extensions.map(async extension => {
const {command: extensionInstallCommand, shell} = commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(extensionMetaService.globalProfilesLocation, profileName, 'data')}' --extensions-dir '${path.join(extensionMetaService.globalProfilesLocation, profileName, 'extensions')}' --install-extension ${extension}`)

return child_process.exec(extensionInstallCommand, {shell})
})

await Promise.all(installExtensionPromises)

const launchCommand
= commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(extensionMetaService.globalProfilesLocation, profileName, 'data')}' --extensions-dir '${path.join(extensionMetaService.globalProfilesLocation, profileName, 'extensions')}' -n`)

await child_process.exec(launchCommand.command, {shell: launchCommand.shell})
} else {
const alreadyPresentJsonString = await customProfileService.generateProfileJson(profileName)

const profileDetailsJsonString = JSON.stringify(profileDetailsJson, undefined, 2)
if (profileDetailsJsonString === alreadyPresentJsonString) {
const launchCommand = commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(extensionMetaService.globalProfilesLocation, profileName, 'data')}' --extensions-dir '${path.join(extensionMetaService.globalProfilesLocation, profileName, 'extensions')}' -n`)
await child_process.exec(launchCommand.command, {shell: launchCommand.shell})
} else {
await vscode.window.showInformationMessage('Please use a different name. Another profile with the same name already exists, but with different settings.')
}
}
}

return Promise.resolve()
}),
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const rootStoragePath = path.join(os.homedir(), '.config', app)

export const commands = {
launchProfile: `${app}.commands.launchProfile`,
launchProfileJson: `${app}.commands.launchProfileJson`,
selectProfile: `${app}.commands.selectProfile`,
selectFeaturedProfile: `${app}.commands.selectFeaturedProfile`,
createProfile: `${app}.commands.createProfile`,
Expand Down

0 comments on commit 33cfa26

Please sign in to comment.