Skip to content

Commit

Permalink
Bugfix / fix tn import (#7566)
Browse files Browse the repository at this point in the history
* add fixes for invalid localization (#7564) (#7565)

Co-authored-by: PhotoNomad0 <bruce.mclean@unfoldingword.org>

* add fixes for tn import and migration errors

* add fixes for tn import

---------

Co-authored-by: PhotoNomad0 <bruce.mclean@unfoldingword.org>
  • Loading branch information
PhotoNomad0 and PhotoNomad0 committed Mar 11, 2024
1 parent 04921d4 commit 0b8d6cd
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 100 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"string-punctuation-tokenizer": "^2.2.0",
"sudo-prompt": "6.2.1",
"tc-electron-env": "0.10.0",
"tc-source-content-updater": "1.4.23",
"tc-source-content-updater": "1.4.24",
"tc-strings": "0.1.7",
"tc-tool": "4.1.0",
"tc-ui-toolkit": "6.2.8",
Expand Down
188 changes: 96 additions & 92 deletions src/js/helpers/ResourcesHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1416,114 +1416,118 @@ export function preserveNeededOrigLangVersions(languageId, resourceId, resourceP
* restores missing resources by language and bible and lexicon
*/
export function getMissingResources() {
const tcResourcesLanguages = getFilteredSubFolders(STATIC_RESOURCES_PATH);

for ( const languageId of tcResourcesLanguages) {
LogHighlighted(` Checking for missing ${languageId} resources`);
const staticLanguageResource = path.join(STATIC_RESOURCES_PATH, languageId);
const userLanguageResource = path.join(USER_RESOURCES_PATH, languageId);
const resourceTypes = getFilteredSubFolders(staticLanguageResource);

for ( const resourceType of resourceTypes) {// resourceType: bibles, lexicons or translationHelps
const resourceTypePath = path.join(staticLanguageResource, resourceType);
const resourceIds = getFilteredSubFolders(resourceTypePath);

for ( const resourceId of resourceIds) {// resourceId: udb, ult, ugl, translationWords, translationNotes
const userResourcePath = path.join(userLanguageResource, resourceType, resourceId);
const staticResourcePath = path.join(staticLanguageResource, resourceType, resourceId);

if (resourceType === 'lexicons') {
// check for lexicons packaged with tc executable.
checkForNewLexicons(languageId);
extractZippedResourceContent(userResourcePath, resourceType === 'bibles');
} else if (!fs.existsSync(userResourcePath)) {// if resource isn't found in user resources folder.
copyAndExtractResource(staticResourcePath, userResourcePath, languageId, resourceId, resourceType);
} else { // compare resources manifest modified time
const owners = ResourceAPI.getLatestVersionsAndOwners(staticResourcePath) || {};
const ownersKeys = Object.keys(owners);

if (!ownersKeys || !ownersKeys.length) {
console.warn(`getMissingResources() - Could not find latest for ${staticResourcePath}`);
}

for (const owner of ownersKeys) {
if (!owner) {
console.error(`getMissingResources() - skipping empty owner for ${staticResourcePath}`);
continue;
try {
const tcResourcesLanguages = getFilteredSubFolders(STATIC_RESOURCES_PATH);

for (const languageId of tcResourcesLanguages) {
LogHighlighted(` Checking for missing ${languageId} resources`);
const staticLanguageResource = path.join(STATIC_RESOURCES_PATH, languageId);
const userLanguageResource = path.join(USER_RESOURCES_PATH, languageId);
const resourceTypes = getFilteredSubFolders(staticLanguageResource);

for (const resourceType of resourceTypes) {// resourceType: bibles, lexicons or translationHelps
const resourceTypePath = path.join(staticLanguageResource, resourceType);
const resourceIds = getFilteredSubFolders(resourceTypePath);

for (const resourceId of resourceIds) {// resourceId: udb, ult, ugl, translationWords, translationNotes
const userResourcePath = path.join(userLanguageResource, resourceType, resourceId);
const staticResourcePath = path.join(staticLanguageResource, resourceType, resourceId);

if (resourceType === 'lexicons') {
// check for lexicons packaged with tc executable.
checkForNewLexicons(languageId);
extractZippedResourceContent(userResourcePath, resourceType === 'bibles');
} else if (!fs.existsSync(userResourcePath)) {// if resource isn't found in user resources folder.
copyAndExtractResource(staticResourcePath, userResourcePath, languageId, resourceId, resourceType);
} else { // compare resources manifest modified time
const owners = ResourceAPI.getLatestVersionsAndOwners(staticResourcePath) || {};
const ownersKeys = Object.keys(owners);

if (!ownersKeys || !ownersKeys.length) {
console.warn(`getMissingResources() - Could not find latest for ${staticResourcePath}`);
}

const staticResourceVersionPath = owners[owner];
const version = path.basename(staticResourceVersionPath);
const userResourceVersionPath = path.join(userResourcePath, version);
const userResourceExists = fs.existsSync(userResourceVersionPath);
let isOldResource = false;
const filename = 'manifest.json';
const staticResourceManifestPath = path.join(staticResourceVersionPath, filename);

if (userResourceExists) {
const userResourceManifestPath = path.join(userResourceVersionPath, filename);

if (fs.existsSync(userResourceManifestPath) && fs.existsSync(staticResourceManifestPath)) {
const { catalog_modified_time: userModifiedTime } = fs.readJsonSync(userResourceManifestPath) || {};
const { catalog_modified_time: staticModifiedTime } = fs.readJsonSync(staticResourceManifestPath) || {};
isOldResource = !userModifiedTime || (userModifiedTime < staticModifiedTime);

if (isOldResource) {
console.log('getMissingResources() - userModifiedTime: ' + userModifiedTime);
console.log('getMissingResources() - staticModifiedTime: ' + staticModifiedTime);
}
} else if (!fs.existsSync(userResourceManifestPath)) {
if (fs.existsSync(staticResourceManifestPath)) {
console.log('getMissingResources() - missing user manifest: ' + userResourceManifestPath);
console.log('getMissingResources() - but found static manifest: ' + staticResourceManifestPath);
isOldResource = true;
} else { // if no manifest.json, fall back to checking versions
const userVersion = path.basename(userResourceVersionPath);
const staticVersion = path.basename(staticResourceVersionPath);
isOldResource = ResourceAPI.compareVersions(userVersion, staticVersion) < 0;
for (const owner of ownersKeys) {
if (!owner) {
console.error(`getMissingResources() - skipping empty owner for ${staticResourcePath}`);
continue;
}

const staticResourceVersionPath = owners[owner];
const version = path.basename(staticResourceVersionPath);
const userResourceVersionPath = path.join(userResourcePath, version);
const userResourceExists = fs.existsSync(userResourceVersionPath);
let isOldResource = false;
const filename = 'manifest.json';
const staticResourceManifestPath = path.join(staticResourceVersionPath, filename);

if (userResourceExists) {
const userResourceManifestPath = path.join(userResourceVersionPath, filename);

if (fs.existsSync(userResourceManifestPath) && fs.existsSync(staticResourceManifestPath)) {
const { catalog_modified_time: userModifiedTime } = fs.readJsonSync(userResourceManifestPath) || {};
const { catalog_modified_time: staticModifiedTime } = fs.readJsonSync(staticResourceManifestPath) || {};
isOldResource = !userModifiedTime || (userModifiedTime < staticModifiedTime);

if (isOldResource) {
console.log('getMissingResources() - userVersion: ' + userVersion);
console.log('getMissingResources() - staticVersion: ' + staticVersion);
console.log('getMissingResources() - userModifiedTime: ' + userModifiedTime);
console.log('getMissingResources() - staticModifiedTime: ' + staticModifiedTime);
}
} else if (!fs.existsSync(userResourceManifestPath)) {
if (fs.existsSync(staticResourceManifestPath)) {
console.log('getMissingResources() - missing user manifest: ' + userResourceManifestPath);
console.log('getMissingResources() - but found static manifest: ' + staticResourceManifestPath);
isOldResource = true;
} else { // if no manifest.json, fall back to checking versions
const userVersion = path.basename(userResourceVersionPath);
const staticVersion = path.basename(staticResourceVersionPath);
isOldResource = ResourceAPI.compareVersions(userVersion, staticVersion) < 0;

if (isOldResource) {
console.log('getMissingResources() - userVersion: ' + userVersion);
console.log('getMissingResources() - staticVersion: ' + staticVersion);
}
}
}
} else { // resource folder was empty
isOldResource = true;
}
} else { // resource folder was empty
isOldResource = true;
}

preserveNeededOrigLangVersions(languageId, resourceId, userResourcePath, USER_RESOURCES_PATH);

if (isOldResource) {
console.log(`getMissingResources() - checking ${languageId} ${resourceId} ${owner} - no resource found`);

// if (deleteOldResources) {
// console.log('getMissingResources() - deleting old resources folder: ' + userResourcePath);
// fs.removeSync(userResourcePath);
// }
console.log('getMissingResources() - unzipping static resources');
copyAndExtractResource(staticResourcePath, userResourcePath, languageId, resourceId, resourceType, owner);
} else { // if folder empty, then copy over current resource
const versions = ResourceAPI.listVersions(userResourcePath, owner);
const emptyResourceFolder = !versions.length;
let installResource = emptyResourceFolder;

if (!emptyResourceFolder) { // make sure bundled version is installed
const staticResourceVersionPath = ResourceAPI.getLatestVersion(staticResourcePath, owner);
const bundledVersion = path.basename(staticResourceVersionPath);
const destinationPath = path.join(userResourcePath, bundledVersion);
installResource = !fs.existsSync(destinationPath);
}
preserveNeededOrigLangVersions(languageId, resourceId, userResourcePath, USER_RESOURCES_PATH);

if (isOldResource) {
console.log(`getMissingResources() - checking ${languageId} ${resourceId} ${owner} - no resource found`);

if (installResource) {
console.log('getMissingResources() - unzipping missing static resources');
// if (deleteOldResources) {
// console.log('getMissingResources() - deleting old resources folder: ' + userResourcePath);
// fs.removeSync(userResourcePath);
// }
console.log('getMissingResources() - unzipping static resources');
copyAndExtractResource(staticResourcePath, userResourcePath, languageId, resourceId, resourceType, owner);
} else { // if folder empty, then copy over current resource
const versions = ResourceAPI.listVersions(userResourcePath, owner);
const emptyResourceFolder = !versions.length;
let installResource = emptyResourceFolder;

if (!emptyResourceFolder) { // make sure bundled version is installed
const staticResourceVersionPath = ResourceAPI.getLatestVersion(staticResourcePath, owner);
const bundledVersion = path.basename(staticResourceVersionPath);
const destinationPath = path.join(userResourcePath, bundledVersion);
installResource = !fs.existsSync(destinationPath);
}

if (installResource) {
console.log('getMissingResources() - unzipping missing static resources');
copyAndExtractResource(staticResourcePath, userResourcePath, languageId, resourceId, resourceType, owner);
}
}
}
}
}
}
}
} catch (e) {
console.error(`getMissingResources() - exception occurred`, e);
}
}

Expand Down

0 comments on commit 0b8d6cd

Please sign in to comment.