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

Allow dictionaries to specify minimum compatible Yomitan version #1750

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions ext/data/schemas/dictionary-index-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"type": "string",
"description": "Revision of the dictionary. This value is displayed, and used to check for dictionary updates."
},
"minimumYomitanVersion": {
"type": "string",
"description": "Minimum version of Yomitan that is compatible with this dictionary."
},
"sequenced": {
"type": "boolean",
"default": false,
Expand Down
14 changes: 12 additions & 2 deletions ext/js/dictionary/dictionary-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ZipReader as ZipReader0,
configure,
} from '../../lib/zip.js';
import {compareRevisions} from './dictionary-data-util.js';
import {ExtensionError} from '../core/extension-error.js';
import {parseJson} from '../core/json.js';
import {toError} from '../core/to-error.js';
Expand Down Expand Up @@ -180,8 +181,9 @@ export class DictionaryImporter {
}
}

const yomitanVersion = details.yomitanVersion;
/** @type {import('dictionary-importer').SummaryDetails} */
const summaryDetails = {prefixWildcardsSupported, counts, styles};
const summaryDetails = {prefixWildcardsSupported, counts, styles, yomitanVersion};

const summary = this._createSummary(dictionaryTitle, version, index, summaryDetails);
await dictionaryDatabase.bulkAdd('dictionaries', [summary], 0, 1);
Expand Down Expand Up @@ -328,7 +330,15 @@ export class DictionaryImporter {
styles,
};

const {author, url, description, attribution, frequencyMode, isUpdatable, sourceLanguage, targetLanguage} = index;
const {minimumYomitanVersion, author, url, description, attribution, frequencyMode, isUpdatable, sourceLanguage, targetLanguage} = index;
if (typeof minimumYomitanVersion === 'string') {
if (details.yomitanVersion === '0.0.0.0') {
// Running a development version of Yomitan
} else if (compareRevisions(details.yomitanVersion, minimumYomitanVersion)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was chrome.runtime not available in this context, or was the purpose of calling it where you did a performance optimization?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, not available. I initially tried to use chrome.runtime here within the DictionaryImporter class, but it caused an error (ReferenceError: chrome is not defined).

throw new Error(`Dictionary is incompatible with this version of Yomitan (${details.yomitanVersion}; minimum required: ${minimumYomitanVersion})`);
}
summary.minimumYomitanVersion = minimumYomitanVersion;
}
if (typeof author === 'string') { summary.author = author; }
if (typeof url === 'string') { summary.url = url; }
if (typeof description === 'string') { summary.description = description; }
Expand Down
1 change: 1 addition & 0 deletions ext/js/pages/settings/dictionary-import-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ export class DictionaryImportController {
const optionsFull = await this._settingsController.getOptionsFull();
const importDetails = {
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported,
yomitanVersion: chrome.runtime.getManifest().version,
};

for (let i = 0; i < importProgressTracker.dictionaryCount; ++i) {
Expand Down
8 changes: 4 additions & 4 deletions test/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Database', () => {
// Setup database
const dictionaryDatabase = new DictionaryDatabase();
/** @type {import('dictionary-importer').ImportDetails} */
const defaultImportDetails = {prefixWildcardsSupported: false};
const defaultImportDetails = {prefixWildcardsSupported: false, yomitanVersion: '0.0.0.0'};

// Database not open
await expect.soft(dictionaryDatabase.deleteDictionary(title, 1000, () => {})).rejects.toThrow('Database not open');
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('Database', () => {
const testDictionarySource = await createTestDictionaryArchiveData(name);

/** @type {import('dictionary-importer').ImportDetails} */
const detaultImportDetails = {prefixWildcardsSupported: false};
const detaultImportDetails = {prefixWildcardsSupported: false, yomitanVersion: '0.0.0.0'};
await expect.soft(createDictionaryImporter(expect).importDictionary(dictionaryDatabase, testDictionarySource, detaultImportDetails)).rejects.toThrow('Dictionary has invalid data');
await dictionaryDatabase.close();
});
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('Database', () => {
const {result: importDictionaryResult, errors: importDictionaryErrors} = await dictionaryImporter.importDictionary(
dictionaryDatabase,
testDictionarySource,
{prefixWildcardsSupported: true},
{prefixWildcardsSupported: true, yomitanVersion: '0.0.0.0'},
);

if (importDictionaryResult) {
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('Database', () => {

// Import data
const dictionaryImporter = createDictionaryImporter(expect);
await dictionaryImporter.importDictionary(dictionaryDatabase, testDictionarySource, {prefixWildcardsSupported: true});
await dictionaryImporter.importDictionary(dictionaryDatabase, testDictionarySource, {prefixWildcardsSupported: true, yomitanVersion: '0.0.0.0'});

// Clear
switch (clearMethod) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/translator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function createTranslatorContext(dictionaryDirectory, dictionaryNam
const {errors, result} = await dictionaryImporter.importDictionary(
dictionaryDatabase,
testDictionaryData,
{prefixWildcardsSupported: true},
{prefixWildcardsSupported: true, yomitanVersion: '0.0.0.0'},
);

expect(errors.length).toEqual(0);
Expand Down
1 change: 1 addition & 0 deletions types/ext/dictionary-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Index = {
version?: IndexVersion;
title: string;
revision: string;
minimumYomitanVersion?: string;
sequenced?: boolean;
isUpdatable?: true;
indexUrl?: string;
Expand Down
3 changes: 3 additions & 0 deletions types/ext/dictionary-importer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ export type ImportResult = {

export type ImportDetails = {
prefixWildcardsSupported: boolean;
yomitanVersion: string;
};

export type Summary = {
title: string;
revision: string;
sequenced: boolean;
minimumYomitanVersion?: string;
version: number;
importDate: number;
prefixWildcardsSupported: boolean;
Expand All @@ -67,6 +69,7 @@ export type SummaryDetails = {
prefixWildcardsSupported: boolean;
counts: SummaryCounts;
styles: string;
yomitanVersion: string;
};

export type SummaryCounts = {
Expand Down
Loading