Skip to content

Commit

Permalink
Reorganise build
Browse files Browse the repository at this point in the history
Signed-off-by: John Gomersall <[email protected]>
  • Loading branch information
john-gom committed Aug 23, 2024
1 parent a0306f4 commit f4c173a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ node_modules
/playwright-report/
/blob-report/
/playwright/.cache/
/theme/off/messages/
# These are generated at build time by the build_languages script
/theme/off/common/messages/
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"java.compile.nullAnalysis.mode": "automatic"
"java.compile.nullAnalysis.mode": "automatic",
"cSpell.words": [
"keycloak"
]
}
45 changes: 3 additions & 42 deletions build-scripts/build_languages.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { writeFileSync, appendFileSync, readFileSync, existsSync, mkdirSync, copyFileSync } from 'fs';
import { writeFileSync, readFileSync, existsSync, mkdirSync, copyFileSync } from 'fs';
import { getLanguages } from './utils.mjs';

const runtimeDir = 'runtime-scripts';
const themeDir = 'theme/off/common';

const languages = JSON.parse(readFileSync('build-scripts/languages.json'));
const countries = JSON.parse(readFileSync('build-scripts/countries.json'));

const languageList = {};
for (const [ key, language ] of Object.entries(languages)) {
if (key === 'en:unknown-language') continue;

const code = language.language_code_2.en;
const name = (language.name?.[code] ?? language.name.en ?? key).replaceAll("'","''");
languageList[code] = name;
}
const {languages, languageList} = getLanguages();
const languageMessages = '\n# The following are obtained from the OFF languages taxonomy\n' +
Object.entries(languageList).map(([key,value]) => `locale_${key}=${value}`).sort().join('\n');

Expand All @@ -38,37 +30,6 @@ for (const [ key, language ] of Object.entries(languages)) {
'\n# The following are obtained from the OFF countries taxonomy\n' +
countryMessages.sort().join('\n') + languageMessages);
}
const countryOptions = {};
const countryList = {};
// Try and sort the country list to avoid excess diffs
for (const [ countryId, country ] of Object.entries(countries).sort((a,b) => a[0].localeCompare(b[0]))) {
if (!country.country_code_2?.en) {
console.warn(countryId);
continue;
}
const countryCode = country.country_code_2.en;
// Currently get english name for sorting until Keycloak fixes sorting by localized name
const countryName = country.name.en;
countryOptions[countryCode] = '${country_' + countryCode + '}';
countryList[countryCode] = countryName;
}

// Currently sort countries by english name until keycloak supports sorting by localized name
const sortedCountryCodes = Object.entries(countryList).sort((a,b) => a[1].localeCompare(b[1])).map((entry) => entry[0]);
const sortedLanguageCodes = Object.entries(languageList).sort((a,b) => a[1].localeCompare(b[1])).map((entry) => entry[0]);

// Add dummy language to show property names
sortedLanguageCodes.push('xx');
copyFileSync('build-scripts/messages_xx.properties', `${themeDir}/messages/messages_xx.properties`);

const realmSettings = {
supportedLocales: sortedLanguageCodes
}
writeFileSync(`runtime-scripts/realm_settings.json`,JSON.stringify(realmSettings, undefined, 2));
writeFileSync(`${themeDir}/theme.properties`,`locales=${sortedLanguageCodes.join(',')}\n`);

const userProfile = JSON.parse(readFileSync(`${runtimeDir}/users_profile.json`));
const countryAttribute = userProfile.attributes.find((a) => a.name === 'country');
countryAttribute.validations.options.options = sortedCountryCodes;
countryAttribute.annotations.inputOptionLabels = countryOptions;
writeFileSync(`${runtimeDir}/users_profile.json`, JSON.stringify(userProfile, undefined, 2));
41 changes: 41 additions & 0 deletions build-scripts/refresh_messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { writeFileSync, readFileSync, existsSync, readdirSync } from 'fs';
import { getLanguages } from './utils.mjs';

const baseThemeDir = 'theme';
const offMessagesDir = `src/messages`;
Expand Down Expand Up @@ -74,4 +75,44 @@ writeFileSync('build-scripts/messages_xx.properties', xxMessages.join('\n'));
fetch('https://static.openfoodfacts.org/data/taxonomies/languages.json').then(async (response) => {
writeFileSync('build-scripts/languages.json', await response.text());
writeFileSync('build-scripts/countries.json', await (await fetch('https://static.openfoodfacts.org/data/taxonomies/countries.json')).text());

const runtimeDir = 'runtime-scripts';
const themeDir = 'theme/off/common';

const {languageList} = getLanguages();
const countries = JSON.parse(readFileSync('build-scripts/countries.json'));

const countryOptions = {};
const countryList = {};
// Try and sort the country list to avoid excess diffs
for (const [ countryId, country ] of Object.entries(countries).sort((a,b) => a[0].localeCompare(b[0]))) {
if (!country.country_code_2?.en) {
console.warn(`No 2 letter code for: ${countryId}`);
continue;
}
const countryCode = country.country_code_2.en;
// Currently get english name for sorting until Keycloak fixes sorting by localized name
const countryName = country.name.en;
countryOptions[countryCode] = '${country_' + countryCode + '}';
countryList[countryCode] = countryName;
}

// Currently sort countries by english name until keycloak supports sorting by localized name
const sortedCountryCodes = Object.entries(countryList).sort((a,b) => a[1].localeCompare(b[1])).map((entry) => entry[0]);
const sortedLanguageCodes = Object.entries(languageList).sort((a,b) => a[1].localeCompare(b[1])).map((entry) => entry[0]);

// Add dummy language to show property names
sortedLanguageCodes.push('xx');

const realmSettings = {
supportedLocales: sortedLanguageCodes
}
writeFileSync(`runtime-scripts/realm_settings.json`,JSON.stringify(realmSettings, undefined, 2));
writeFileSync(`${themeDir}/theme.properties`,`locales=${sortedLanguageCodes.join(',')}\n`);

const userProfile = JSON.parse(readFileSync(`${runtimeDir}/users_profile.json`));
const countryAttribute = userProfile.attributes.find((a) => a.name === 'country');
countryAttribute.validations.options.options = sortedCountryCodes;
countryAttribute.annotations.inputOptionLabels = countryOptions;
writeFileSync(`${runtimeDir}/users_profile.json`, JSON.stringify(userProfile, undefined, 2));
});
16 changes: 16 additions & 0 deletions build-scripts/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { readFileSync } from 'fs';

export function getLanguages() {
const languages = JSON.parse(readFileSync('build-scripts/languages.json'));

const languageList = {};
for (const [ key, language ] of Object.entries(languages)) {
if (key === 'en:unknown-language') continue;

const code = language.language_code_2.en;
const name = (language.name?.[code] ?? language.name.en ?? key).replaceAll("'","''");
languageList[code] = name;
}

return {languages, languageList};
}

0 comments on commit f4c173a

Please sign in to comment.