Skip to content

Commit 9c5f7ec

Browse files
authored
1 parent 7307293 commit 9c5f7ec

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
lines changed

src/vs/platform/userDataProfile/browser/userDataProfile.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,17 @@ export class BrowserUserDataProfilesService extends UserDataProfilesService impl
8888
}
8989

9090
protected override getStoredProfileAssociations(): StoredProfileAssociations {
91+
const migrateKey = 'profileAssociationsMigration';
9192
try {
9293
const value = window.localStorage.getItem(UserDataProfilesService.PROFILE_ASSOCIATIONS_KEY);
9394
if (value) {
94-
return revive(JSON.parse(value));
95+
let associations: StoredProfileAssociations = JSON.parse(value);
96+
if (!window.localStorage.getItem(migrateKey)) {
97+
associations = this.migrateStoredProfileAssociations(associations);
98+
this.saveStoredProfileAssociations(associations);
99+
window.localStorage.setItem(migrateKey, 'true');
100+
}
101+
return associations;
95102
}
96103
} catch (error) {
97104
/* ignore */

src/vs/platform/userDataProfile/common/userDataProfile.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,22 @@ export class UserDataProfilesService extends Disposable implements IUserDataProf
242242
}
243243
const workspaces = new ResourceMap<IUserDataProfile>();
244244
const emptyWindows = new Map<string, IUserDataProfile>();
245-
const defaultProfile = toUserDataProfile(hash(this.environmentService.userRoamingDataHome.path).toString(16), localize('defaultProfile', "Default"), this.environmentService.userRoamingDataHome);
245+
const defaultProfile = this.createDefaultProfile();
246246
profiles.unshift({ ...defaultProfile, extensionsResource: this.getDefaultProfileExtensionsLocation() ?? defaultProfile.extensionsResource, isDefault: true });
247247
if (profiles.length) {
248248
const profileAssociaitions = this.getStoredProfileAssociations();
249249
if (profileAssociaitions.workspaces) {
250-
for (const [workspacePath, profilePath] of Object.entries(profileAssociaitions.workspaces)) {
250+
for (const [workspacePath, profileId] of Object.entries(profileAssociaitions.workspaces)) {
251251
const workspace = URI.parse(workspacePath);
252-
const profileLocation = URI.parse(profilePath);
253-
const profile = profiles.find(p => this.uriIdentityService.extUri.isEqual(p.location, profileLocation));
252+
const profile = profiles.find(p => p.id === profileId);
254253
if (profile) {
255254
workspaces.set(workspace, profile);
256255
}
257256
}
258257
}
259258
if (profileAssociaitions.emptyWindows) {
260-
for (const [windowId, profilePath] of Object.entries(profileAssociaitions.emptyWindows)) {
261-
const profileLocation = URI.parse(profilePath);
262-
const profile = profiles.find(p => this.uriIdentityService.extUri.isEqual(p.location, profileLocation));
259+
for (const [windowId, profileId] of Object.entries(profileAssociaitions.emptyWindows)) {
260+
const profile = profiles.find(p => p.id === profileId);
263261
if (profile) {
264262
emptyWindows.set(windowId, profile);
265263
}
@@ -271,6 +269,10 @@ export class UserDataProfilesService extends Disposable implements IUserDataProf
271269
return this._profilesObject;
272270
}
273271

272+
private createDefaultProfile() {
273+
return toUserDataProfile('__default__profile__', localize('defaultProfile', "Default"), this.environmentService.userRoamingDataHome);
274+
}
275+
274276
async createTransientProfile(workspaceIdentifier?: IAnyWorkspaceIdentifier): Promise<IUserDataProfile> {
275277
const namePrefix = `Temp`;
276278
const nameRegEx = new RegExp(`${escapeRegExpCharacters(namePrefix)}\\s(\\d+)`);
@@ -548,16 +550,36 @@ export class UserDataProfilesService extends Disposable implements IUserDataProf
548550
private updateStoredProfileAssociations() {
549551
const workspaces: IStringDictionary<string> = {};
550552
for (const [workspace, profile] of this.profilesObject.workspaces.entries()) {
551-
workspaces[workspace.toString()] = profile.location.toString();
553+
workspaces[workspace.toString()] = profile.id;
552554
}
553555
const emptyWindows: IStringDictionary<string> = {};
554556
for (const [windowId, profile] of this.profilesObject.emptyWindows.entries()) {
555-
emptyWindows[windowId.toString()] = profile.location.toString();
557+
emptyWindows[windowId.toString()] = profile.id;
556558
}
557559
this.saveStoredProfileAssociations({ workspaces, emptyWindows });
558560
this._profilesObject = undefined;
559561
}
560562

563+
// TODO: @sandy081 Remove migration after couple of releases
564+
protected migrateStoredProfileAssociations(storedProfileAssociations: StoredProfileAssociations): StoredProfileAssociations {
565+
const workspaces: IStringDictionary<string> = {};
566+
const defaultProfile = this.createDefaultProfile();
567+
if (storedProfileAssociations.workspaces) {
568+
for (const [workspace, location] of Object.entries(storedProfileAssociations.workspaces)) {
569+
const uri = URI.parse(location);
570+
workspaces[workspace] = this.uriIdentityService.extUri.isEqual(uri, defaultProfile.location) ? defaultProfile.id : this.uriIdentityService.extUri.basename(uri);
571+
}
572+
}
573+
const emptyWindows: IStringDictionary<string> = {};
574+
if (storedProfileAssociations.emptyWindows) {
575+
for (const [workspace, location] of Object.entries(storedProfileAssociations.emptyWindows)) {
576+
const uri = URI.parse(location);
577+
emptyWindows[workspace] = this.uriIdentityService.extUri.isEqual(uri, defaultProfile.location) ? defaultProfile.id : this.uriIdentityService.extUri.basename(uri);
578+
}
579+
}
580+
return { workspaces, emptyWindows };
581+
}
582+
561583
protected getStoredProfiles(): StoredUserDataProfile[] { return []; }
562584
protected saveStoredProfiles(storedProfiles: StoredUserDataProfile[]): void { throw new Error('not implemented'); }
563585

src/vs/platform/userDataProfile/electron-main/userDataProfile.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,21 @@ export class UserDataProfilesMainService extends UserDataProfilesService impleme
4747

4848
protected override saveStoredProfiles(storedProfiles: StoredUserDataProfile[]): void {
4949
if (storedProfiles.length) {
50-
this.stateMainService.setItem(UserDataProfilesMainService.PROFILES_KEY, storedProfiles);
50+
this.stateMainService.setItem(UserDataProfilesMainService.PROFILES_KEY, storedProfiles.map(profile => ({ ...profile, location: this.uriIdentityService.extUri.basename(profile.location) })));
5151
} else {
5252
this.stateMainService.removeItem(UserDataProfilesMainService.PROFILES_KEY);
5353
}
5454
}
5555

56+
protected override getStoredProfiles(): StoredUserDataProfile[] {
57+
const storedProfiles = super.getStoredProfiles();
58+
if (!this.stateMainService.getItem<boolean>('userDataProfilesMigration', false)) {
59+
this.saveStoredProfiles(storedProfiles);
60+
this.stateMainService.setItem('userDataProfilesMigration', true);
61+
}
62+
return storedProfiles;
63+
}
64+
5665
protected override saveStoredProfileAssociations(storedProfileAssociations: StoredProfileAssociations): void {
5766
if (storedProfileAssociations.emptyWindows || storedProfileAssociations.workspaces) {
5867
this.stateMainService.setItem(UserDataProfilesMainService.PROFILE_ASSOCIATIONS_KEY, storedProfileAssociations);
@@ -72,7 +81,12 @@ export class UserDataProfilesMainService extends UserDataProfilesService impleme
7281
}, {});
7382
this.stateMainService.setItem(UserDataProfilesMainService.PROFILE_ASSOCIATIONS_KEY, <StoredProfileAssociations>{ workspaces });
7483
}
75-
return super.getStoredProfileAssociations();
84+
const associations = super.getStoredProfileAssociations();
85+
if (!this.stateMainService.getItem<boolean>(UserDataProfilesService.PROFILE_ASSOCIATIONS_MIGRATION_KEY, false)) {
86+
this.saveStoredProfileAssociations(associations);
87+
this.stateMainService.setItem(UserDataProfilesService.PROFILE_ASSOCIATIONS_MIGRATION_KEY, true);
88+
}
89+
return associations;
7690
}
7791

7892
}

src/vs/platform/userDataProfile/node/userDataProfile.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { revive } from 'vs/base/common/marshalling';
6+
import { isString } from 'vs/base/common/types';
77
import { URI, UriDto } from 'vs/base/common/uri';
88
import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
99
import { IFileService } from 'vs/platform/files/common/files';
@@ -29,9 +29,12 @@ export class ServerUserDataProfilesService extends BaseUserDataProfilesService i
2929

3030
}
3131

32+
type StoredUserDataProfileState = StoredUserDataProfile & { location: URI | string };
3233

3334
export class UserDataProfilesService extends ServerUserDataProfilesService implements IUserDataProfilesService {
3435

36+
protected static readonly PROFILE_ASSOCIATIONS_MIGRATION_KEY = 'profileAssociationsMigration';
37+
3538
constructor(
3639
@IStateService private readonly stateService: IStateService,
3740
@IUriIdentityService uriIdentityService: IUriIdentityService,
@@ -43,11 +46,14 @@ export class UserDataProfilesService extends ServerUserDataProfilesService imple
4346
}
4447

4548
protected override getStoredProfiles(): StoredUserDataProfile[] {
46-
return revive(this.stateService.getItem<UriDto<StoredUserDataProfile>[]>(UserDataProfilesService.PROFILES_KEY, []));
49+
const storedProfilesState = this.stateService.getItem<UriDto<StoredUserDataProfileState>[]>(UserDataProfilesService.PROFILES_KEY, []);
50+
return storedProfilesState.map(p => ({ ...p, location: isString(p.location) ? this.uriIdentityService.extUri.joinPath(this.profilesHome, p.location) : URI.revive(p.location) }));
4751
}
4852

4953
protected override getStoredProfileAssociations(): StoredProfileAssociations {
50-
return revive(this.stateService.getItem<UriDto<StoredProfileAssociations>>(UserDataProfilesService.PROFILE_ASSOCIATIONS_KEY, {}));
54+
const associations = this.stateService.getItem<StoredProfileAssociations>(UserDataProfilesService.PROFILE_ASSOCIATIONS_KEY, {});
55+
const migrated = this.stateService.getItem<boolean>(UserDataProfilesService.PROFILE_ASSOCIATIONS_MIGRATION_KEY, false);
56+
return migrated ? associations : this.migrateStoredProfileAssociations(associations);
5157
}
5258

5359
protected override getDefaultProfileExtensionsLocation(): URI {

src/vs/platform/userDataProfile/test/electron-main/userDataProfileMainService.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ suite('UserDataProfileMainService', () => {
8383

8484
testObject.setProfileForWorkspace(workspace, profile);
8585

86-
assert.deepStrictEqual(testObject.getProfileForWorkspace(workspace), profile);
86+
assert.strictEqual(testObject.getProfileForWorkspace(workspace)?.id, profile.id);
8787
});
8888

8989
test('set profile to a folder', async () => {
@@ -92,7 +92,7 @@ suite('UserDataProfileMainService', () => {
9292

9393
testObject.setProfileForWorkspace(workspace, profile);
9494

95-
assert.deepStrictEqual(testObject.getProfileForWorkspace(workspace), profile);
95+
assert.strictEqual(testObject.getProfileForWorkspace(workspace)?.id, profile.id);
9696
});
9797

9898
test('set profile to a window', async () => {
@@ -101,7 +101,7 @@ suite('UserDataProfileMainService', () => {
101101

102102
testObject.setProfileForWorkspace(workspace, profile);
103103

104-
assert.deepStrictEqual(testObject.getProfileForWorkspace(workspace), profile);
104+
assert.strictEqual(testObject.getProfileForWorkspace(workspace)?.id, profile.id);
105105
});
106106

107107
});

0 commit comments

Comments
 (0)