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 701 #750

Merged
merged 33 commits into from
Jan 15, 2019
Merged
Changes from 1 commit
Commits
Show all changes
33 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
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
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
Prev Previous commit
Next Next commit
Added Pragma util static class. Support for host, env and os values i…
…n every orther. Remove whitespaces before upload. Alert user if OS value is not a valid OS.
  • Loading branch information
protiumx committed Oct 7, 2018
commit 9d56d30595d0a81b8cdb27f3bad993f3d6e63afc
229 changes: 229 additions & 0 deletions src/pragmaUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import { OsType } from "./enums";
import localize from "./localize";

const SUPPORTED_OS = ["windows", "linux", "mac"];

export function GetOsEnum(osName: string): OsType {
switch (osName.toLowerCase()) {
case "windows":
return OsType.Windows;
case "linux":
return OsType.Linux;
case "mac":
return OsType.Mac;
}
}

/**
* Comment/Uncomment lines if matches OS name or Hostname.
* Usage: @sync os=[OS name] host=[hostName]
* Notes: Hostname must be defined in sync.host setting. It could be used for parse any JSON valid string.
* @export
* @class PragmaUtil
*/
export default class PragmaUtil {
/**
* Process @sync pragma statements before file is saved.
* Comment lines that don't match with the user OS or host value.
* @static
* @param {string} settingsContent a valid JSON string
* @returns {string}
* @memberof PragmaUtil
*/
public static processBeforeWrite(
settingsContent: string,
osType: OsType,
hostName: string
): string {
let result: string = settingsContent;

const pragmaSettingsBlocks: RegExpMatchArray = result.match(
this.PragmaRegExp
);

if (pragmaSettingsBlocks !== null) {
for (let block of pragmaSettingsBlocks) {
// line e.g.: // @sync os=windows host=Laptop\n"window.menuBarVisibility": "none",

try {
// check OS pragma
const osMatch: RegExpMatchArray = block.match(/os=(\w+)/);
if (osMatch !== null) {
const osFromPragma = osMatch[1].toLowerCase();

if (!SUPPORTED_OS.includes(osFromPragma)) {
continue;
}
if (GetOsEnum(osFromPragma) !== osType) {
result = result.replace(block, this.commentLineAfterBreak(block));
continue; // no need to lookup the host name
}
}

// check host pragma
const hostMatch: RegExpMatchArray = block.match(/host=(\S+)/);
if (hostMatch !== null) {
const hostFromPragma = hostMatch[1];
if (
hostName === null ||
hostName === "" ||
hostFromPragma.toLowerCase() !== hostName.toLowerCase()
) {
result = result.replace(block, this.commentLineAfterBreak(block));
continue;
}
}

// check env pragma
const envMatch: RegExpMatchArray = block.match(/env=(\S+)/);
if (envMatch !== null) {
const envFromPragma = envMatch[1];
if (!process.env[envFromPragma.toUpperCase()]) {
result = result.replace(block, this.commentLineAfterBreak(block));
}
}
} catch (e) {
continue;
}
}
}

result = this.removeIgnoreBlocks(result);

return result;
}

/**
* Remove @sync-ignore settings before upload.
*
* @static
* @param {string} settingsContent
* @param {require('vscode').window} window
* @returns {string}
* @memberof PragmaUtil
*/
public static processBeforeUpload(settingsContent: string, window): string {
let result: string = settingsContent;
result = this.removeIgnoreBlocks(result);

const lines = result.split("\n");

// alert not supported OS
const pragmaMatches: RegExpMatchArray = result.match(this.PragmaRegExp);
if (pragmaMatches) {
for (let block of pragmaMatches) {
try {
let newBlock: string;
const osMatch: RegExpMatchArray = block.match(
this.OSPragmaWhiteSpacesSupportRegExp
);
if (osMatch !== null) {
const osFromPragma = osMatch[1] || osMatch[2] || osMatch[3];

if (osFromPragma !== "" && /\s/.test(osFromPragma)) {
newBlock = block.replace(osFromPragma, osFromPragma.trimLeft());
result = result.replace(block, newBlock);
block = newBlock;
}

const trimmed = osFromPragma.toLowerCase().trim();
if (!SUPPORTED_OS.includes(trimmed)) {
console.warn("Sync: Invalid OS", osFromPragma);
if (window !== null) {
window.showWarningMessage(
localize(
"cmd.updateSettings.warning.OSNotSupported",
trimmed,
lines.indexOf(block)
)
);
}
}
}

const hostMatch: RegExpMatchArray = block.match(
this.HostPragmaWhiteSpacesSupportRegExp
);
if (hostMatch !== null) {
const hostFromPragma = hostMatch[1] || hostMatch[2] || hostMatch[3];
if (hostFromPragma !== "" && /\s/.test(hostFromPragma)) {
newBlock = block.replace(
hostFromPragma,
hostFromPragma.trimLeft()
);
result = result.replace(block, newBlock);

block = newBlock;
}
}

const envMatch: RegExpMatchArray = block.match(
this.EnvPragmaWhiteSpacesSupportRegExp
);
if (envMatch !== null) {
const envFromPragma = envMatch[1] || envMatch[2] || envMatch[3];
if (envFromPragma !== "" && /\s/.test(envFromPragma)) {
result = result.replace(
block,
block.replace(envFromPragma, envFromPragma.trimLeft())
);
}
}
} catch (e) {
console.log("Sync: Proccess before upload error.", e.message);
continue;
}
}
}

return result;
}

public static removeIgnoreBlocks(settingsContent: string): string {
let result: string = settingsContent;
result = result.replace(/\@sync ignore/g, "@sync-ignore");
const ignoreSettingsBlocks: RegExpMatchArray = result.match(
this.IgnorePragmaRegExp
);

if (ignoreSettingsBlocks !== null) {
for (const block of ignoreSettingsBlocks) {
result = result.replace(block, "");
}
}

return result;
}

public static matchPragmaSettings(settingsContent: string): RegExpMatchArray {
return settingsContent.match(this.PragmaRegExp);
}

/**
* Insert Javascript comment slashes
*
* @private
* @param {string} settingContent
* @param {string} line
* @returns {strign}
* @memberof PragmaUtil
*/
public static commentLineAfterBreak(block: string): string {
const settingLine = block.match(/\n[ \t]*(.+)/);
if (
settingLine !== null &&
settingLine[1] &&
!settingLine[1].startsWith("//")
) {
return block.replace(settingLine[1], l => "// " + l);
}

return block;
}

private static readonly PragmaRegExp: RegExp = /\/\/[ \t]*\@sync[ \t]+(?:os=.+[ \t]*)?(?:host=.+[ \t]*)?(?:env=.+[ \t]*)?\n[ \t]*.+,?/g;
private static readonly IgnorePragmaRegExp: RegExp = /\/\/[ \t]*\@sync-ignore.*\n.+,?/g;
private static readonly HostPragmaWhiteSpacesSupportRegExp = /(?:host=(.+)os=)|(?:host=(.+)env=)|host=(.+)\n?/;
private static readonly OSPragmaWhiteSpacesSupportRegExp = /(?:os=(.+)host=)|(?:os=(.+)env=)|os=(.+)\n?/;
private static readonly EnvPragmaWhiteSpacesSupportRegExp = /(?:env=(.+)host=)|(?:env=(.+)os=)|env=(.+)\n?/;
}