diff --git a/src/pragmaUtil.ts b/src/pragmaUtil.ts index 1d76864e..3f65d1fd 100644 --- a/src/pragmaUtil.ts +++ b/src/pragmaUtil.ts @@ -1,6 +1,8 @@ +import { ExtensionContext } from "vscode"; import { OsType } from "./enums"; import { osTypeFromString, SUPPORTED_OS } from "./environmentPath"; import localize from "./localize"; +import { FileService } from "./service/fileService"; /** * Comment/Uncomment lines if matches OS name or Hostname. @@ -122,7 +124,10 @@ export default class PragmaUtil { * @returns {string} * @memberof PragmaUtil */ - public static processBeforeUpload(settingsContent: string): string { + public static async processBeforeUpload( + settingsContent: string, + context: ExtensionContext + ): Promise { const lines = settingsContent.split("\n"); let osMatch: RegExpMatchArray; let osFromPragma: string; @@ -136,6 +141,17 @@ export default class PragmaUtil { const parsedLines: string[] = []; let currentLine = ""; + let settingsFileExists = false; + + if (context !== null) { + settingsFileExists = await FileService.FileExists( + `${context.globalStoragePath}/settings.sync` + ); + } + + // Compare each line between new content and existing settings file + let shouldUpload = false; + for (let index = 0; index < lines.length; index++) { currentLine = lines[index]; @@ -196,7 +212,28 @@ export default class PragmaUtil { } } - return parsedLines.join("\n"); + const result = parsedLines.join("\n"); + + if (settingsFileExists && context !== null) { + try { + const localSettingFile = await FileService.ReadFile( + `${context.globalStoragePath}/settings.sync` + ); + shouldUpload = localSettingFile !== result; + } catch (error) { + console.warn("Sync: Could not read local settings file", error.message); + } + } + + if ((!settingsFileExists || shouldUpload) && context !== null) { + // Create or update local settings file + await FileService.WriteFile( + `${context.globalStoragePath}/settings.sync`, + result + ); + } + + return [result, shouldUpload]; } public static getIgnoredBlocks(content: string): string { @@ -232,14 +269,21 @@ export default class PragmaUtil { private static readonly EnvPragmaWhiteSpacesSupportRegExp = /(?:env=(.+)host=)|(?:env=(.+)os=)|env=(.+)\n?/; private static toggleComments(line: string, shouldComment: boolean) { - if (shouldComment && !line.trim().startsWith("//")) { - return " //" + line; // 2 spaces as formmating + const isCommented = line.trim().startsWith("//"); + if (shouldComment) { + if (!isCommented) { + return " //" + line; // 2 spaces as formating + } } else { - return line.replace("//", ""); + if (isCommented) { + return line.replace("//", ""); + } } + + return line; } - // checks and advance index + // Checks and advance line reading index private static checkNextLines( lines: string[], parsedLines: string[], diff --git a/src/sync.ts b/src/sync.ts index cd498faa..7e21adec 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -21,7 +21,10 @@ import PragmaUtil from "./pragmaUtil"; let globalCommonService: Commons; export class Sync { - constructor(private context: vscode.ExtensionContext) {} + constructor(private context: vscode.ExtensionContext) { + // Check global storage dir + FileService.CreateDirectory(context.globalStoragePath); + } /** * Run when extension have been activated */ @@ -100,7 +103,11 @@ export class Sync { localConfig.customConfig.token, localConfig.customConfig.githubEnterpriseUrl ); - await startGitProcess(localConfig.extConfig, localConfig.customConfig); + await startGitProcess.call( + this, + localConfig.extConfig, + localConfig.customConfig + ); } catch (error) { Commons.LogException(error, globalCommonService.ERROR_MESSAGE, true); return; @@ -219,25 +226,36 @@ export class Sync { for (const snippetFile of contentFiles) { if (snippetFile.fileName !== env.FILE_KEYBINDING_MAC) { if (snippetFile.content !== "") { + let shouldUpload = true; + if (snippetFile.fileName === env.FILE_KEYBINDING_NAME) { snippetFile.gistName = env.OsType === OsType.Mac ? env.FILE_KEYBINDING_MAC : env.FILE_KEYBINDING_DEFAULT; } - allSettingFiles.push(snippetFile); - } - } - if (snippetFile.fileName === env.FILE_SETTING_NAME) { - try { - snippetFile.content = PragmaUtil.processBeforeUpload( - snippetFile.content - ); - } catch (e) { - Commons.LogException(null, e.message, true); - console.error(e); - return; + if (snippetFile.fileName === env.FILE_SETTING_NAME) { + try { + const [ + content, + shouldUploadSettingsFile + ] = await PragmaUtil.processBeforeUpload( + snippetFile.content, + this.context + ); + snippetFile.content = content; + shouldUpload = shouldUploadSettingsFile; + } catch (e) { + Commons.LogException(null, e.message, true); + console.error(e); + return; + } + } + + if (shouldUpload) { + allSettingFiles.push(snippetFile); + } } } } diff --git a/test/pragmaUtil/index.ts b/test/pragmaUtil/index.ts index 1b1b2b31..df15661c 100644 --- a/test/pragmaUtil/index.ts +++ b/test/pragmaUtil/index.ts @@ -13,23 +13,26 @@ describe("Process before upload", function() { ); }); - it("should trim os, host and env", () => { - expect(PragmaUtil.processBeforeUpload(testSettings)).to.match( - /@sync os=linux host=trim env=TEST_ENV/ - ); + it("should trim os, host and env", async () => { + const [result] = await PragmaUtil.processBeforeUpload(testSettings, null); + await expect(result).to.match(/@sync os=linux host=trim env=TEST_ENV/); }); - it("should uncomment all lines", () => { + it("should uncomment all lines", async () => { const commentedSettings = ` // @sync os=linux // "window": 1, // @sync os=mac - // "mac": 1 + // "server": "http://exmaple.com `; - expect(PragmaUtil.processBeforeUpload(commentedSettings)) + const [result] = await PragmaUtil.processBeforeUpload( + commentedSettings, + null + ); + await expect(result) .to.match(/\s+"window"/) - .and.to.match(/\s+"mac"/); + .and.to.match(/\s+"server"/); }); it("should uncomment lines before write file for os=linux", () => { @@ -47,11 +50,11 @@ describe("Process before upload", function() { ); expect(processed) .to.match(/\s+"linux"/) - .and.to.match(/.+\/\/"mac"/); + .and.to.match(/\s+\/\/\s+"mac"/); }); - it("should not comment os=linux settings lines", () => { - let processed = PragmaUtil.processBeforeUpload(testSettings); + it("should not comment os=linux settings lines", async () => { + let [processed] = await PragmaUtil.processBeforeUpload(testSettings, null); processed = PragmaUtil.processBeforeWrite( processed, processed, @@ -61,11 +64,14 @@ describe("Process before upload", function() { expect(processed).to.match(/\s+"not_commented"/); }); - it("should leave only settings that matches with os=mac host=mac2 env=TEST_ENV", () => { - const processed = PragmaUtil.processBeforeUpload(testSettings); + it("should leave only settings that matches with os=mac host=mac2 env=TEST_ENV", async () => { + const [processed] = await PragmaUtil.processBeforeUpload( + testSettings, + null + ); // tslint:disable-next-line:no-string-literal process.env["TEST_ENV"] = "1"; - expect( + await expect( PragmaUtil.processBeforeWrite(processed, processed, OsType.Mac, "mac2") ) .to.match(/\n\s+"mac2"/) @@ -98,5 +104,5 @@ describe("Process before upload", function() { .and.to.match(/\/{2}\s+"setting"/) .and.to.match(/\/{2}\s+},/) .and.to.match(/\s+"mac"/); - }) + }); }); diff --git a/test/pragmaUtil/testSettings.txt b/test/pragmaUtil/testSettings.txt index de217cce..14f05281 100644 --- a/test/pragmaUtil/testSettings.txt +++ b/test/pragmaUtil/testSettings.txt @@ -18,6 +18,9 @@ // @sync host=mac2 os=mac env=TEST_ENV //"mac2": 3, + // @sync host=mac2 + //"server": "http://example.com", + // @sync os=mac "mactest": "",