Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Support multiple instances

* Completely switch to state

* Update to work properly

* Move setting classes to their own models

* Update service names

* Only auto upload using focused window

* Resolve merge conflicts
  • Loading branch information
auxves authored and shanalikhan committed Jun 19, 2019
1 parent d0002f2 commit 272273e
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 243 deletions.
67 changes: 32 additions & 35 deletions src/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import * as vscode from "vscode";
import { Environment } from "./environmentPath";
import localize from "./localize";
import { AutoUploadService } from "./service/autoUploadService";
import { File, FileService } from "./service/fileService";
import { CustomConfig } from "./models/customConfig.model";
import { ExtensionConfig } from "./models/extensionConfig.model";
import { LocalConfig } from "./models/localConfig.model";
import { AutoUploadService } from "./service/autoUpload.service";
import { File, FileService } from "./service/file.service";
import { ExtensionInformation } from "./service/pluginService";
import { CustomSettings, ExtensionConfig, LocalConfig } from "./setting";
import { state } from "./state";

export default class Commons {
public static outputChannel: vscode.OutputChannel = null;
Expand Down Expand Up @@ -74,22 +77,15 @@ export default class Commons {

public ERROR_MESSAGE: string = localize("common.error.message");

constructor(
private en: Environment,
private context: vscode.ExtensionContext
) {
constructor() {
this.InitializeAutoUpload();
}

public async InitializeAutoUpload() {
const ignored = AutoUploadService.GetIgnoredItems(
await this.GetCustomSettings()
);
this.autoUploadService = new AutoUploadService({
en: this.en,
commons: this,
ignored
});
this.autoUploadService = new AutoUploadService(ignored);
}

public async HandleStartWatching() {
Expand All @@ -114,9 +110,9 @@ export default class Commons {
askToken: boolean,
askGist: boolean
): Promise<LocalConfig> {
const settings: LocalConfig = new LocalConfig();
const extSettings: ExtensionConfig = this.GetSettings();
const cusSettings: CustomSettings = await this.GetCustomSettings();
const settings = new LocalConfig();
const extSettings = this.GetSettings();
const cusSettings = await this.GetCustomSettings();

if (cusSettings.token === "") {
if (askToken === true) {
Expand Down Expand Up @@ -156,15 +152,15 @@ export default class Commons {
return settings;
}

public async GetCustomSettings(): Promise<CustomSettings> {
let customSettings: CustomSettings = new CustomSettings();
public async GetCustomSettings(): Promise<CustomConfig> {
let customSettings = new CustomConfig();
try {
const customExist: boolean = await FileService.FileExists(
this.en.FILE_CUSTOMIZEDSETTINGS
state.environment.FILE_CUSTOMIZEDSETTINGS
);
if (customExist) {
const customSettingStr: string = await FileService.ReadFile(
this.en.FILE_CUSTOMIZEDSETTINGS
state.environment.FILE_CUSTOMIZEDSETTINGS
);
const tempObj = JSON.parse(customSettingStr);

Expand All @@ -176,7 +172,7 @@ export default class Commons {
Commons.LogException(
e,
"Sync : Unable to read " +
this.en.FILE_CUSTOMIZEDSETTINGS_NAME +
state.environment.FILE_CUSTOMIZEDSETTINGS_NAME +
". Make sure its Valid JSON.",
true
);
Expand All @@ -191,17 +187,18 @@ export default class Commons {
}
}

public async SetCustomSettings(setting: CustomSettings): Promise<boolean> {
public async SetCustomSettings(setting: CustomConfig): Promise<boolean> {
try {
await FileService.WriteFile(
this.en.FILE_CUSTOMIZEDSETTINGS,
state.environment.FILE_CUSTOMIZEDSETTINGS,
JSON.stringify(setting, null, 4)
);
return true;
} catch (e) {
Commons.LogException(
e,
"Sync : Unable to write " + this.en.FILE_CUSTOMIZEDSETTINGS_NAME,
"Sync : Unable to write " +
state.environment.FILE_CUSTOMIZEDSETTINGS_NAME,
true
);
return false;
Expand All @@ -210,16 +207,16 @@ export default class Commons {

public async StartMigrationProcess(): Promise<boolean> {
const fileExist: boolean = await FileService.FileExists(
this.en.FILE_CUSTOMIZEDSETTINGS
state.environment.FILE_CUSTOMIZEDSETTINGS
);
let customSettings: CustomSettings = null;
let customSettings: CustomConfig = null;
const firstTime: boolean = !fileExist;
let fileChanged: boolean = firstTime;

if (fileExist) {
customSettings = await this.GetCustomSettings();
} else {
customSettings = new CustomSettings();
customSettings = new CustomConfig();
}
// vscode.workspace.getConfiguration().update("sync.version", undefined, true);

Expand All @@ -244,18 +241,18 @@ export default class Commons {
} else if (customSettings.version < Environment.CURRENT_VERSION) {
fileChanged = true;
// #TODO : Remove this in new update
const newIgnoredList = new CustomSettings().ignoreUploadFiles;
const newIgnoredList = new CustomConfig().ignoreUploadFiles;
newIgnoredList.forEach(m => {
if (customSettings.ignoreUploadFiles.indexOf(m) === -1) {
customSettings.ignoreUploadFiles.push(m);
}
});

if (this.context.globalState.get("synctoken")) {
const token = this.context.globalState.get("synctoken");
if (state.context.globalState.get("synctoken")) {
const token = state.context.globalState.get("synctoken");
if (token !== "") {
customSettings.token = String(token);
this.context.globalState.update("synctoken", "");
state.context.globalState.update("synctoken", "");
vscode.window.showInformationMessage(
localize("common.info.setToken")
);
Expand Down Expand Up @@ -337,16 +334,16 @@ export default class Commons {

try {
await Promise.all(allKeysUpdated);
if (this.context.globalState.get("syncCounter")) {
const counter = this.context.globalState.get("syncCounter");
if (state.context.globalState.get("syncCounter")) {
const counter = state.context.globalState.get("syncCounter");
let count: number = parseInt(counter + "", 10);
if (count % 450 === 0) {
this.DonateMessage();
}
count = count + 1;
this.context.globalState.update("syncCounter", count);
state.context.globalState.update("syncCounter", count);
} else {
this.context.globalState.update("syncCounter", 1);
state.context.globalState.update("syncCounter", 1);
}
return true;
} catch (err) {
Expand Down Expand Up @@ -394,7 +391,7 @@ export default class Commons {
return settings;
}

public async GetTokenAndSave(sett: CustomSettings): Promise<string> {
public async GetTokenAndSave(sett: CustomConfig): Promise<string> {
const opt = Commons.GetInputBox(true);

const token = ((await vscode.window.showInputBox(opt)) || "").trim();
Expand Down
7 changes: 4 additions & 3 deletions src/environmentPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { normalize, resolve } from "path";
import * as vscode from "vscode";
import { OsType } from "./enums";
import { state } from "./state";

export const SUPPORTED_OS: string[] = Object.keys(OsType)
.filter(k => !/\d/.test(k))
Expand Down Expand Up @@ -64,14 +65,14 @@ export class Environment {

public FOLDER_SNIPPETS: string = null;

constructor(private context: vscode.ExtensionContext) {
this.context.globalState.update("_", undefined); // Make sure the global state folder exists. This is needed for using this.context.globalStoragePath to access user folder
constructor() {
state.context.globalState.update("_", undefined); // Make sure the global state folder exists. This is needed for using this.context.globalStoragePath to access user folder

this.isPortable = !!process.env.VSCODE_PORTABLE;

this.OsType = process.platform as OsType;
if (!this.isPortable) {
this.PATH = resolve(this.context.globalStoragePath, "../../..").concat(
this.PATH = resolve(state.context.globalStoragePath, "../../..").concat(
normalize("/")
);
this.USER_FOLDER = resolve(this.PATH, "User").concat(normalize("/"));
Expand Down
7 changes: 6 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"use strict";

import * as vscode from "vscode";
import { Environment } from "./environmentPath";
import { init as initLocalize } from "./localize";
import { state } from "./state";
import { Sync } from "./sync";

export async function activate(context: vscode.ExtensionContext) {
state.context = context;
state.environment = new Environment();

await initLocalize();

const sync = new Sync(context);
const sync = new Sync();

sync.bootstrap();

Expand Down
6 changes: 6 additions & 0 deletions src/models/cloudSettings.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Environment } from "../environmentPath";

export class CloudSettings {
public lastUpload: Date = null;
public extensionVersion: string = "v" + Environment.getVersion();
}
92 changes: 32 additions & 60 deletions src/setting.ts → src/models/customConfig.model.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,32 @@
"use strict";
import { Environment } from "./environmentPath";

export class ExtensionConfig {
public gist: string = null;
public quietSync: boolean = false;
public removeExtensions: boolean = true;
public syncExtensions: boolean = true;
public autoDownload: boolean = false;
public autoUpload = false;
public forceDownload: boolean = false;
}

export class LocalConfig {
public publicGist: boolean = false;
public userName: string = null;
public name: string = null;
public extConfig: ExtensionConfig = new ExtensionConfig();
public customConfig: CustomSettings = new CustomSettings();
}

export class CloudSetting {
public lastUpload: Date = null;
public extensionVersion: string = "v" + Environment.getVersion();
}

export class KeyValue<T, S> {
constructor(public Key: T, public Value: S) {}
}

export class CustomSettings {
public ignoreUploadFiles: string[] = [
"state.*",
"syncLocalSettings.json",
".DS_Store",
"sync.lock",
"projects.json",
"projects_cache_vscode.json",
"projects_cache_git.json",
"projects_cache_svn.json",
"gpm_projects.json",
"gpm-recentItems.json"
];
public ignoreUploadFolders: string[] = ["workspaceStorage"];
public ignoreExtensions: string[] = [];
public gistDescription: string = "Visual Studio Code Settings Sync Gist";
public version: number = Environment.CURRENT_VERSION;
public token: string = "";
public downloadPublicGist: boolean = false;
public supportedFileExtensions: string[] = ["json", "code-snippets"];
public openTokenLink: boolean = true;
public disableUpdateMessage: boolean = false;
public lastUpload: Date = null;
public lastDownload: Date = null;
public githubEnterpriseUrl: string = null;
public askGistName: boolean = false;
public customFiles: { [key: string]: string } = {};
public hostName: string = null;
public universalKeybindings: boolean = false;
}
import { Environment } from "../environmentPath";

export class CustomConfig {
public ignoreUploadFiles: string[] = [
"state.*",
"syncLocalSettings.json",
".DS_Store",
"sync.lock",
"projects.json",
"projects_cache_vscode.json",
"projects_cache_git.json",
"projects_cache_svn.json",
"gpm_projects.json",
"gpm-recentItems.json"
];
public ignoreUploadFolders: string[] = ["workspaceStorage"];
public ignoreExtensions: string[] = [];
public gistDescription: string = "Visual Studio Code Settings Sync Gist";
public version: number = Environment.CURRENT_VERSION;
public token: string = "";
public downloadPublicGist: boolean = false;
public supportedFileExtensions: string[] = ["json", "code-snippets"];
public openTokenLink: boolean = true;
public disableUpdateMessage: boolean = false;
public lastUpload: Date = null;
public lastDownload: Date = null;
public githubEnterpriseUrl: string = null;
public askGistName: boolean = false;
public customFiles: { [key: string]: string } = {};
public hostName: string = null;
public universalKeybindings: boolean = false;
}
9 changes: 9 additions & 0 deletions src/models/extensionConfig.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class ExtensionConfig {
public gist = null;
public quietSync = false;
public removeExtensions = true;
public syncExtensions = true;
public autoDownload = false;
public autoUpload = false;
public forceDownload = false;
}
10 changes: 10 additions & 0 deletions src/models/localConfig.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CustomConfig } from "./customConfig.model";
import { ExtensionConfig } from "./extensionConfig.model";

export class LocalConfig {
public publicGist: boolean = false;
public userName: string = null;
public name: string = null;
public extConfig = new ExtensionConfig();
public customConfig = new CustomConfig();
}
10 changes: 10 additions & 0 deletions src/models/state.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ExtensionContext } from "vscode";
import Commons from "../commons";
import { Environment } from "../environmentPath";

export interface IExtensionState {
context?: ExtensionContext;
environment?: Environment;
commons?: Commons;
instanceID: string;
}
Loading

0 comments on commit 272273e

Please sign in to comment.