Skip to content

Commit

Permalink
Use filenamePatterns in seti update script. Fixes #131650
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Aug 31, 2021
1 parent 2b0b403 commit 5541826
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 74 deletions.
156 changes: 90 additions & 66 deletions extensions/theme-seti/build/update-icon-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

'use strict';

let path = require('path');
let fs = require('fs');
let https = require('https');
let url = require('url');
const path = require('path');
const fs = require('fs');
const https = require('https');
const url = require('url');
const minimatch = require('minimatch');

// list of languagesId not shipped with VSCode. The information is used to associate an icon with a language association
// Please try and keep this list in alphabetical order! Thank you.
let nonBuiltInLanguages = { // { fileNames, extensions }
const nonBuiltInLanguages = { // { fileNames, extensions }
"argdown": { extensions: ['ad', 'adown', 'argdown', 'argdn'] },
"bicep": { extensions: ['bicep'] },
"elixir": { extensions: ['ex'] },
Expand Down Expand Up @@ -41,13 +42,13 @@ let nonBuiltInLanguages = { // { fileNames, extensions }
};

// list of languagesId that inherit the icon from another language
let inheritIconFromLanguage = {
const inheritIconFromLanguage = {
"jsonc": 'json',
"postcss": 'css',
"django-html": 'html'
}

let FROM_DISK = true; // set to true to take content from a repo checked out next to the vscode repo
const FROM_DISK = true; // set to true to take content from a repo checked out next to the vscode repo

let font, fontMappingsFile, fileAssociationFile, colorsFile;
if (!FROM_DISK) {
Expand All @@ -63,10 +64,10 @@ if (!FROM_DISK) {
}

function getCommitSha(repoId) {
let commitInfo = 'https://api.github.com/repos/' + repoId + '/commits/master';
const commitInfo = 'https://api.github.com/repos/' + repoId + '/commits/master';
return download(commitInfo).then(function (content) {
try {
let lastCommit = JSON.parse(content);
const lastCommit = JSON.parse(content);
return Promise.resolve({
commitSha: lastCommit.sha,
commitDate: lastCommit.commit.author.date
Expand All @@ -86,8 +87,8 @@ function download(source) {
return readFile(source);
}
return new Promise((c, e) => {
let _url = url.parse(source);
let options = { host: _url.host, port: _url.port, path: _url.path, headers: { 'User-Agent': 'NodeJS' } };
const _url = url.parse(source);
const options = { host: _url.host, port: _url.port, path: _url.path, headers: { 'User-Agent': 'NodeJS' } };
let content = '';
https.get(options, function (response) {
response.on('data', function (data) {
Expand Down Expand Up @@ -122,7 +123,7 @@ function downloadBinary(source, dest) {
https.get(source, function (response) {
switch (response.statusCode) {
case 200: {
let file = fs.createWriteStream(dest);
const file = fs.createWriteStream(dest);
response.on('data', function (chunk) {
file.write(chunk);
}).on('end', function () {
Expand Down Expand Up @@ -157,9 +158,9 @@ function copyFile(fileName, dest) {
cbCalled = true;
}
}
let rd = fs.createReadStream(fileName);
const rd = fs.createReadStream(fileName);
rd.on("error", handleError);
let wr = fs.createWriteStream(dest);
const wr = fs.createWriteStream(dest);
wr.on("error", handleError);
wr.on("close", function () {
if (!cbCalled) {
Expand All @@ -174,8 +175,8 @@ function copyFile(fileName, dest) {
function darkenColor(color) {
let res = '#';
for (let i = 1; i < 7; i += 2) {
let newVal = Math.round(parseInt('0x' + color.substr(i, 2), 16) * 0.9);
let hex = newVal.toString(16);
const newVal = Math.round(parseInt('0x' + color.substr(i, 2), 16) * 0.9);
const hex = newVal.toString(16);
if (hex.length === 1) {
res += '0';
}
Expand All @@ -195,39 +196,45 @@ function mergeMapping(to, from, property) {
}

function getLanguageMappings() {
let langMappings = {};
let allExtensions = fs.readdirSync('..');
const langMappings = {};
const allExtensions = fs.readdirSync('..');
for (let i = 0; i < allExtensions.length; i++) {
let dirPath = path.join('..', allExtensions[i], 'package.json');
const dirPath = path.join('..', allExtensions[i], 'package.json');
if (fs.existsSync(dirPath)) {
let content = fs.readFileSync(dirPath).toString();
let jsonContent = JSON.parse(content);
let languages = jsonContent.contributes && jsonContent.contributes.languages;
const content = fs.readFileSync(dirPath).toString();
const jsonContent = JSON.parse(content);
const languages = jsonContent.contributes && jsonContent.contributes.languages;
if (Array.isArray(languages)) {
for (let k = 0; k < languages.length; k++) {
let languageId = languages[k].id;
const languageId = languages[k].id;
if (languageId) {
let extensions = languages[k].extensions;
let mapping = {};
const extensions = languages[k].extensions;
const mapping = {};
if (Array.isArray(extensions)) {
mapping.extensions = extensions.map(function (e) { return e.substr(1).toLowerCase(); });
}
let filenames = languages[k].filenames;
const filenames = languages[k].filenames;
if (Array.isArray(filenames)) {
mapping.fileNames = filenames.map(function (f) { return f.toLowerCase(); });
}
let existing = langMappings[languageId];
const filenamePatterns = languages[k].filenamePatterns;
if (Array.isArray(filenamePatterns)) {
mapping.filenamePatterns = filenamePatterns.map(function (f) { return f.toLowerCase(); });
}
const existing = langMappings[languageId];

if (existing) {
// multiple contributions to the same language
// give preference to the contribution wth the configuration
if (languages[k].configuration) {
mergeMapping(mapping, existing, 'extensions');
mergeMapping(mapping, existing, 'fileNames');
mergeMapping(mapping, existing, 'filenamePatterns');
langMappings[languageId] = mapping;
} else {
mergeMapping(existing, mapping, 'extensions');
mergeMapping(existing, mapping, 'fileNames');
mergeMapping(existing, mapping, 'filenamePatterns');
}
} else {
langMappings[languageId] = mapping;
Expand All @@ -237,60 +244,58 @@ function getLanguageMappings() {
}
}
}
for (let languageId in nonBuiltInLanguages) {
for (const languageId in nonBuiltInLanguages) {
langMappings[languageId] = nonBuiltInLanguages[languageId];
}
return langMappings;
}



exports.copyFont = function () {
return downloadBinary(font, './icons/seti.woff');
};

exports.update = function () {

console.log('Reading from ' + fontMappingsFile);
let def2Content = {};
let ext2Def = {};
let fileName2Def = {};
let def2ColorId = {};
let colorId2Value = {};
let lang2Def = {};
const def2Content = {};
const ext2Def = {};
const fileName2Def = {};
const def2ColorId = {};
const colorId2Value = {};
const lang2Def = {};

function writeFileIconContent(info) {
let iconDefinitions = {};
let allDefs = Object.keys(def2Content).sort();
const iconDefinitions = {};
const allDefs = Object.keys(def2Content).sort();

for (let i = 0; i < allDefs.length; i++) {
let def = allDefs[i];
let entry = { fontCharacter: def2Content[def] };
let colorId = def2ColorId[def];
const def = allDefs[i];
const entry = { fontCharacter: def2Content[def] };
const colorId = def2ColorId[def];
if (colorId) {
let colorValue = colorId2Value[colorId];
const colorValue = colorId2Value[colorId];
if (colorValue) {
entry.fontColor = colorValue;

let entryInverse = { fontCharacter: entry.fontCharacter, fontColor: darkenColor(colorValue) };
const entryInverse = { fontCharacter: entry.fontCharacter, fontColor: darkenColor(colorValue) };
iconDefinitions[def + '_light'] = entryInverse;
}
}
iconDefinitions[def] = entry;
}

function getInvertSet(input) {
let result = {};
for (let assoc in input) {
let invertDef = input[assoc] + '_light';
const result = {};
for (const assoc in input) {
const invertDef = input[assoc] + '_light';
if (iconDefinitions[invertDef]) {
result[assoc] = invertDef;
}
}
return result;
}

let res = {
const res = {
information_for_contributors: [
'This file has been generated from data in https://github.com/jesseweed/seti-ui',
'- icon definitions: https://github.com/jesseweed/seti-ui/blob/master/styles/_fonts/seti.less',
Expand Down Expand Up @@ -321,7 +326,7 @@ exports.update = function () {
version: 'https://github.com/jesseweed/seti-ui/commit/' + info.commitSha,
};

let path = './icons/vs-seti-icon-theme.json';
const path = './icons/vs-seti-icon-theme.json';
fs.writeFileSync(path, JSON.stringify(res, null, '\t'));
console.log('written ' + path);
}
Expand All @@ -330,18 +335,18 @@ exports.update = function () {
let match;

return download(fontMappingsFile).then(function (content) {
let regex = /@([\w-]+):\s*'(\\E[0-9A-F]+)';/g;
let contents = {};
const regex = /@([\w-]+):\s*'(\\E[0-9A-F]+)';/g;
const contents = {};
while ((match = regex.exec(content)) !== null) {
contents[match[1]] = match[2];
}

return download(fileAssociationFile).then(function (content) {
let regex2 = /\.icon-(?:set|partial)\(['"]([\w-\.+]+)['"],\s*['"]([\w-]+)['"],\s*(@[\w-]+)\)/g;
const regex2 = /\.icon-(?:set|partial)\(['"]([\w-\.+]+)['"],\s*['"]([\w-]+)['"],\s*(@[\w-]+)\)/g;
while ((match = regex2.exec(content)) !== null) {
let pattern = match[1];
const pattern = match[1];
let def = '_' + match[2];
let colorId = match[3];
const colorId = match[3];
let storedColorId = def2ColorId[def];
let i = 1;
while (storedColorId && colorId !== storedColorId) { // different colors for the same def?
Expand All @@ -364,20 +369,30 @@ exports.update = function () {
}
}
// replace extensions for languageId
let langMappings = getLanguageMappings();
const langMappings = getLanguageMappings();
for (let lang in langMappings) {
let mappings = langMappings[lang];
let exts = mappings.extensions || [];
let fileNames = mappings.fileNames || [];
const mappings = langMappings[lang];
const exts = mappings.extensions || [];
const fileNames = mappings.fileNames || [];
const filenamePatterns = mappings.filenamePatterns || [];
let preferredDef = null;
// use the first file association for the preferred definition
// use the first file extension association for the preferred definition
for (let i1 = 0; i1 < exts.length && !preferredDef; i1++) {
preferredDef = ext2Def[exts[i1]];
}
// use the first file association for the preferred definition
// use the first file name association for the preferred definition, if not availbale
for (let i1 = 0; i1 < fileNames.length && !preferredDef; i1++) {
preferredDef = fileName2Def[fileNames[i1]];
}
for (let i1 = 0; i1 < filenamePatterns.length && !preferredDef; i1++) {
let pattern = filenamePatterns[i1];
for (const name in fileName2Def) {
if (minimatch(name, pattern)) {
preferredDef = fileName2Def[name];
break;
}
}
}
if (preferredDef) {
lang2Def[lang] = preferredDef;
if (!nonBuiltInLanguages[lang] && !inheritIconFromLanguage[lang]) {
Expand All @@ -393,12 +408,21 @@ exports.update = function () {
delete fileName2Def[fileNames[i2]];
}
}
for (let i2 = 0; i2 < filenamePatterns.length; i2++) {
let pattern = filenamePatterns[i2];
// remove the filenamePatterns association, unless it is different from the preferred
for (const name in fileName2Def) {
if (minimatch(name, pattern) && fileName2Def[name] === preferredDef) {
delete fileName2Def[name];
}
}
}
}
}
}
for (let lang in inheritIconFromLanguage) {
let superLang = inheritIconFromLanguage[lang];
let def = lang2Def[superLang];
for (const lang in inheritIconFromLanguage) {
const superLang = inheritIconFromLanguage[lang];
const def = lang2Def[superLang];
if (def) {
lang2Def[lang] = def;
} else {
Expand All @@ -409,17 +433,17 @@ exports.update = function () {


return download(colorsFile).then(function (content) {
let regex3 = /(@[\w-]+):\s*(#[0-9a-z]+)/g;
const regex3 = /(@[\w-]+):\s*(#[0-9a-z]+)/g;
while ((match = regex3.exec(content)) !== null) {
colorId2Value[match[1]] = match[2];
}
return getCommitSha('jesseweed/seti-ui').then(function (info) {
try {
writeFileIconContent(info);

let cgmanifestPath = './cgmanifest.json';
let cgmanifest = fs.readFileSync(cgmanifestPath).toString();
let cgmanifestContent = JSON.parse(cgmanifest);
const cgmanifestPath = './cgmanifest.json';
const cgmanifest = fs.readFileSync(cgmanifestPath).toString();
const cgmanifestContent = JSON.parse(cgmanifest);
cgmanifestContent['registrations'][0]['component']['git']['commitHash'] = info.commitSha;
fs.writeFileSync(cgmanifestPath, JSON.stringify(cgmanifestContent, null, '\t'));
console.log('updated ' + cgmanifestPath);
Expand Down
Binary file modified extensions/theme-seti/icons/seti.woff
Binary file not shown.
10 changes: 2 additions & 8 deletions extensions/theme-seti/icons/vs-seti-icon-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -1791,10 +1791,6 @@
"workspace.bazel": "_bazel",
"bower.json": "_bower",
"docker-healthcheck": "_docker_2",
"docker-compose.yml": "_docker_3",
"docker-compose.yaml": "_docker_3",
"docker-compose.override.yml": "_docker_3",
"docker-compose.override.yaml": "_docker_3",
"firebase.json": "_firebase",
"geckodriver": "_firefox",
"gruntfile.js": "_grunt",
Expand Down Expand Up @@ -1878,6 +1874,7 @@
"typescript": "_typescript",
"typescriptreact": "_typescript",
"xml": "_xml",
"dockercompose": "_docker_3",
"yaml": "_yml",
"argdown": "_argdown",
"bicep": "_bicep",
Expand Down Expand Up @@ -2189,6 +2186,7 @@
"typescript": "_typescript_light",
"typescriptreact": "_typescript_light",
"xml": "_xml_light",
"dockercompose": "_docker_3_light",
"yaml": "_yml_light",
"argdown": "_argdown_light",
"bicep": "_bicep_light",
Expand Down Expand Up @@ -2241,10 +2239,6 @@
"workspace.bazel": "_bazel_light",
"bower.json": "_bower_light",
"docker-healthcheck": "_docker_2_light",
"docker-compose.yml": "_docker_3_light",
"docker-compose.yaml": "_docker_3_light",
"docker-compose.override.yml": "_docker_3_light",
"docker-compose.override.yaml": "_docker_3_light",
"firebase.json": "_firebase_light",
"geckodriver": "_firefox_light",
"gruntfile.js": "_grunt_light",
Expand Down

0 comments on commit 5541826

Please sign in to comment.