Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #754 #867

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
70d4dc1
Updated: German translation
Blackcatz1911 Sep 17, 2018
54752d3
#641
shanalikhan Sep 18, 2018
38b3b3c
Add support for VSCodium
stripedpajamas Sep 19, 2018
1948e5b
Corrected the message to be displayed
tkrtmy Sep 26, 2018
bef2a5d
Add Japanese translation file
tkrtmy Sep 26, 2018
59fb77d
=Adding HostName property. Get hostname using 'os.hostname()'
protiumx Oct 5, 2018
84ba33b
=Need definition for supported OS's. Using strict match regex to get …
protiumx Oct 5, 2018
072ba1c
Added English message for OSNotSupported message while processing upl…
protiumx Oct 7, 2018
fe6db09
mocha and chai packages added as dev dependencies. Test script not wo…
protiumx Oct 7, 2018
3782c76
Moving pragma functions to a separated file
protiumx Oct 7, 2018
9d56d30
Added Pragma util static class. Support for host, env and os values i…
protiumx Oct 7, 2018
741351c
Added hostName property to CustomSettings
protiumx Oct 7, 2018
1664358
Tests files added. Estructure test per feature.
protiumx Oct 7, 2018
681066e
Added some documentation. hostName property on custom config.
protiumx Oct 7, 2018
ac8be0d
uncomment lines function. Uncomment all @sync settings before upload.…
protiumx Oct 8, 2018
484ed0d
Do replace ments one time per setting pragma. Test uncoment function.…
protiumx Oct 8, 2018
9ba0d02
Test Directory
shanaccelirate Oct 8, 2018
a8ed581
Merge branch 'v3.2' of https://github.com/shanalikhan/code-settings-sync
protiumx Oct 8, 2018
692d2f2
Add SUPPORTED_OS and osTypeFromString to environmentPath.ts as reques…
protiumx Oct 8, 2018
123c68a
Check valid JSON before writting file. All the comments and trilling …
protiumx Oct 8, 2018
517de3d
Chatch exception during upload. Should it abort upload?
protiumx Oct 8, 2018
9542ea1
Adding VS Code tests in typescript. Run test changing launch mode to …
protiumx Oct 8, 2018
b0c5c68
merge with remote master
protiumx Oct 24, 2018
5755226
Fix bug with the first line of settings.json.
protiumx Oct 24, 2018
28278a0
Read local file in order to process ignored settings before writing t…
protiumx Oct 24, 2018
338d9cc
Addapt test to changes.
protiumx Oct 24, 2018
511f1e0
Get tabs or spaces and line breaks for each ignored line.
protiumx Oct 24, 2018
b4d376d
Optimize images (#740)
MarvinJWendt Dec 18, 2018
1175578
fix(package): update temp to version 0.9.0 (#737)
greenkeeper[bot] Jan 8, 2019
b73440e
Merge branch 'v3.2.5' of https://github.com/shanalikhan/code-settings…
protiumx Jan 14, 2019
384c8a4
Remove unnecesary tests
protiumx Jan 14, 2019
569e243
New Approach for parsing pragmas
protiumx Jan 14, 2019
51866a4
Support for Brackets and unifying repeated code
protiumx Jan 14, 2019
a52b1a4
Test for multi-line settings supporting curly braces and brackets. Ch…
protiumx Jan 14, 2019
192049b
Remove unused function. Add new lines after ignored ettings block.
protiumx Jan 14, 2019
f8d5a47
Merge tag 'v3.2.5' of https://github.com/shanalikhan/code-settings-sync
protiumx Feb 18, 2019
33c9924
Comments added
protiumx May 1, 2019
4d41e2c
Merge branch 'v3.3.0' of https://github.com/shanalikhan/code-settings…
protiumx May 1, 2019
201f802
Fix #754
protiumx May 1, 2019
99713af
Fix #754: Sync object
protiumx May 2, 2019
0d2feac
Update tests
protiumx May 2, 2019
a6adea3
Merge 865
protiumx May 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions src/pragmaUtil.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<any[]> {
const lines = settingsContent.split("\n");
let osMatch: RegExpMatchArray;
let osFromPragma: string;
Expand All @@ -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];

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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[],
Expand Down
46 changes: 32 additions & 14 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
36 changes: 21 additions & 15 deletions test/pragmaUtil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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,
Expand All @@ -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"/)
Expand Down Expand Up @@ -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"/);
})
});
});
3 changes: 3 additions & 0 deletions test/pragmaUtil/testSettings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",

Expand Down