-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: launch custom profile menu (workaround) (#73)
* 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
1 parent
9b737cb
commit 33cfa26
Showing
6 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters