Skip to content

Commit

Permalink
Feat: Support code-insiders (#50)
Browse files Browse the repository at this point in the history
* (feat) use code or code-insiders based on where the extension is running

* 2.2.0
  • Loading branch information
mrsauravsahu authored Nov 2, 2021
1 parent a17a806 commit 7b8327b
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion 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.1.2",
"version": "2.2.0",
"repository": {
"url": "https://github.com/mrsauravsahu/vscode-manager",
"type": "git"
Expand Down
8 changes: 5 additions & 3 deletions src/commands/clone-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Command} from '../types'

export const cloneProfileCommand: Command = {
name: commands.cloneProfile,
handler: ({provider, services: [_, __, commandGeneratorService]}) => async (customProfile: CustomProfile) => vscode.window.withProgress({
handler: ({provider, services: {commandGeneratorService, commandMetaService}}) => async (customProfile: CustomProfile) => vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: 'Clone',
cancellable: false,
Expand All @@ -30,8 +30,10 @@ export const cloneProfileCommand: Command = {
await fs.promises.copyFile(path.join(originalProfilePath, 'data', 'User', 'settings.json'),
path.join(clonedProfilePath, 'data', 'User', 'settings.json'))

const codeBin = await commandMetaService.getProgramBasedOnMetaAsync('code')

// Get extensions
const {command: getExtensionsCommand, shell} = commandGeneratorService.generateCommand('code', `--user-data-dir '${path.join(originalProfilePath, 'data')}' --extensions-dir '${path.join(originalProfilePath, 'extensions')}' --list-extensions`)
const {command: getExtensionsCommand, shell} = commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(originalProfilePath, 'data')}' --extensions-dir '${path.join(originalProfilePath, 'extensions')}' --list-extensions`)

progress.report({increment: 10, message: 'Retrieving extensions from old profile...'})
const getExtensionsCommandOutput = await child_process.exec(getExtensionsCommand, {shell})
Expand All @@ -50,7 +52,7 @@ export const cloneProfileCommand: Command = {
}) ?? []

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

progress.report({
increment: 50,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/create-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {Command} from '../types'

export const createProfileCommand: Command = {
name: commands.createProfile,
handler: ({provider, services: [customProfileService, _, commandGeneratorService], treeView}) => async () => {
handler: ({provider, services: {customProfileService, commandGeneratorService}, treeView}) => async () => {
const newProfileName = uniqueNamesGenerator({
dictionaries: [adjectives, animals],
separator: '-',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delete-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {CustomProfile} from '../models/custom-profile'

export const deleteProfileCommand: Command = {
name: commands.deleteProfile,
handler: ({services: [customProfileService, ..._], treeView, provider}) => async (customProfile: CustomProfile) => {
handler: ({services: {customProfileService}, treeView, provider}) => async (customProfile: CustomProfile) => {
const {name} = customProfile
if (name === profiles.default) {
await vscode.window.showErrorMessage('Cannot delete the default profile')
Expand Down
11 changes: 6 additions & 5 deletions src/commands/launch-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import {Command, CustomProfileDetails} from '../types'

export const launchProfileCommand: Command = {
name: commands.launchProfile,
handler: ({services: [customProfileService, _, commandGeneratorService]}) => (arg: CustomProfile | {fsPath: string}) => vscode.window.withProgress({
handler: ({services: {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('code',
const {command: launchCommand, shell} = commandGeneratorService.generateCommand(codeBin,
`--user-data-dir '${path.join(rootStoragePath, name, 'data')}' --extensions-dir '${path.join(rootStoragePath, name, 'extensions')}' -n`)
await child_process.exec(launchCommand, {shell})
}
Expand Down Expand Up @@ -83,23 +84,23 @@ export const launchProfileCommand: Command = {
progress.report({increment: 50, message: 'installing extensions...'})

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

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

await Promise.all(installExtensionPromises)

const launchCommand
= commandGeneratorService.generateCommand('code', `--user-data-dir '${path.join(rootStoragePath, profileName, 'data')}' --extensions-dir '${path.join(rootStoragePath, profileName, 'extensions')}' -n`)
= commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(rootStoragePath, profileName, 'data')}' --extensions-dir '${path.join(rootStoragePath, 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('code', `--user-data-dir '${path.join(rootStoragePath, profileName, 'data')}' --extensions-dir '${path.join(rootStoragePath, profileName, 'extensions')}' -n`)
const launchCommand = commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(rootStoragePath, profileName, 'data')}' --extensions-dir '${path.join(rootStoragePath, 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.')
Expand Down
2 changes: 1 addition & 1 deletion src/commands/refresh-profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Command} from '../types'

export const refreshProfilesCommand: Command = {
name: commands.refreshProfiles,
handler: ({treeView, provider, services: [customProfileService, ..._]}) => () => {
handler: ({treeView, provider, services: {customProfileService}}) => () => {
provider.refresh()

treeView.message = customProfileService.getAll().length === 0 ? strings.noProfiles : undefined
Expand Down
2 changes: 1 addition & 1 deletion src/commands/rename-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Command} from '../types'

export const renameProfileCommand: Command = {
name: commands.renameProfile,
handler: ({services: [_, __, commandGeneratorService]}) => async (customProfile: CustomProfile) => {
handler: ({services: {commandGeneratorService}}) => async (customProfile: CustomProfile) => {
const {name} = customProfile

const value = await vscode.window.showInputBox({
Expand Down
17 changes: 12 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {FeaturedProfileService} from './services/featured-profile.service'
import {FeaturedProfilesProvider} from './providers/featured-profiles.provider'
import {FeaturedProfileContentProvider} from './providers/featured-profile-content.provider'
import {CommandGeneratorService} from './services/command-generator.service'
import {CommandMetaService} from './services/command-meta.service'

// This method is called when your extension is activated
// your extension is activated the very first time the command is executed
export async function activate(context: vscode.ExtensionContext) {
// TODO: Make rootPath cross platform
const commandMetaService = new CommandMetaService()
const commandGeneratorService = new CommandGeneratorService()
const customProfileService = new CustomProfileService(commandGeneratorService)
const customProfileService = new CustomProfileService(commandGeneratorService, commandMetaService)
const customProfilesProvider = new CustomProfilesProvider(context, customProfileService)

vscode.window.registerTreeDataProvider('customProfiles', customProfilesProvider)
Expand Down Expand Up @@ -45,16 +47,16 @@ export async function activate(context: vscode.ExtensionContext) {
customProfilesProvider.refresh()

/* FEATURED PROFILES SECTION */
const featuredProfilesService = new FeaturedProfileService()
const featuredProfilesProvider = new FeaturedProfilesProvider(featuredProfilesService)
const featuredProfileService = new FeaturedProfileService()
const featuredProfilesProvider = new FeaturedProfilesProvider(featuredProfileService)
vscode.window.createTreeView(constants.views.featuredProfiles, {
treeDataProvider: featuredProfilesProvider,
})

await featuredProfilesProvider.refresh()

context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(constants.uriSchemes.featuredProfile,
new FeaturedProfileContentProvider(featuredProfilesService),
new FeaturedProfileContentProvider(featuredProfileService),
))

// Register commands
Expand All @@ -64,7 +66,12 @@ export async function activate(context: vscode.ExtensionContext) {
command.handler({
context,
provider: customProfilesProvider,
services: [customProfileService, featuredProfilesService, commandGeneratorService],
services: {
customProfileService,
featuredProfileService,
commandGeneratorService,
commandMetaService,
},
treeView: customProfilesExplorer,
}),
)
Expand Down
15 changes: 15 additions & 0 deletions src/services/command-meta.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as vscode from 'vscode'

export class CommandMetaService {
async getProgramBasedOnMetaAsync(programName: string): Promise<string> {
if (programName === 'code') {
if (vscode.env.appName === 'Visual Studio Code - Insiders') {
return 'code-insiders'
}

return 'code'
}

return programName
}
}
8 changes: 6 additions & 2 deletions src/services/custom-profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import * as json5 from 'json5'
import * as constants from '../constants'
import {CustomProfile} from '../models/custom-profile'
import {CommandGeneratorService} from './command-generator.service'
import {CommandMetaService} from './command-meta.service'

export class CustomProfileService {
public constructor(private readonly commandGeneratorService: CommandGeneratorService) {}
public constructor(private readonly commandGeneratorService: CommandGeneratorService,
private readonly commandMetaService: CommandMetaService) {}

getAll(): CustomProfile[] {
const {rootStoragePath} = constants
Expand Down Expand Up @@ -60,8 +62,10 @@ export class CustomProfileService {
await vscode.window.showInformationMessage('The profile contains invalid user settings.')
}

const codeBin = await this.commandMetaService.getProgramBasedOnMetaAsync('code')

// Get extensions
const {command: getExtensionsCommand, shell} = this.commandGeneratorService.generateCommand('code', `--user-data-dir '${path.join(constants.rootStoragePath, profileName, 'data')}' --extensions-dir '${path.join(constants.rootStoragePath, profileName, 'extensions')}' --list-extensions`)
const {command: getExtensionsCommand, shell} = this.commandGeneratorService.generateCommand(codeBin, `--user-data-dir '${path.join(constants.rootStoragePath, profileName, 'data')}' --extensions-dir '${path.join(constants.rootStoragePath, profileName, 'extensions')}' --list-extensions`)
const getExtensionsCommandOutput = await child_process.exec(getExtensionsCommand, {shell})
const extensions = getExtensionsCommandOutput
.stdout
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vscode from 'vscode'
import {CustomProfilesProvider} from './custom-profile-tree'
import {CustomProfile} from './models/custom-profile'
import {CommandGeneratorService} from './services/command-generator.service'
import {CommandMetaService} from './services/command-meta.service'
import {CustomProfileService} from './services/custom-profile.service'
import {FeaturedProfileService} from './services/featured-profile.service'

Expand All @@ -16,7 +17,12 @@ export type HandlerArgs = {
context: vscode.ExtensionContext;
treeView: vscode.TreeView<CustomProfile>;
provider: CustomProfilesProvider;
services: [CustomProfileService, FeaturedProfileService, CommandGeneratorService];
services: {
customProfileService: CustomProfileService;
featuredProfileService: FeaturedProfileService;
commandGeneratorService: CommandGeneratorService;
commandMetaService: CommandMetaService;
};
}

export type CommandHandler = ((args: HandlerArgs) => (...args: any[]) => any)
Expand Down

0 comments on commit 7b8327b

Please sign in to comment.