This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: dynamically generate locales
- Loading branch information
Showing
30 changed files
with
1,291 additions
and
603 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import chalk from "chalk"; | ||
import { Logger } from "../../../logger"; | ||
|
||
import { runDartBuildRunner } from "./tasks/build_runner"; | ||
import { generateLocales } from "./tasks/locale"; | ||
import { generateMeta } from "./tasks/meta"; | ||
|
||
const logger = new Logger("generator:run"); | ||
|
||
const tasks: (() => Promise<void>)[] = [ | ||
runDartBuildRunner, | ||
generateLocales, | ||
generateMeta, | ||
]; | ||
|
||
export const generate = async () => { | ||
logger.log(`Starting ${chalk.cyanBright(tasks.length)} tasks...`); | ||
|
||
await Promise.all(tasks.map((x) => x())); | ||
|
||
logger.log(`Finished running ${chalk.cyanBright(tasks.length)} tasks`); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { dirname, join } from "path"; | ||
import chalk from "chalk"; | ||
import got from "got"; | ||
import { Octokit } from "@octokit/rest"; | ||
import { ensureDir, readFile, writeFile, lstat } from "fs-extra"; | ||
import { config } from "../../../../config"; | ||
import { Repos } from "../../../../constants"; | ||
import { Logger } from "../../../../logger"; | ||
|
||
const logger = new Logger("generator:run:locale"); | ||
|
||
export const localeFile = join( | ||
config.base, | ||
"packages/utilx/lib/generated/locale.g.dart" | ||
); | ||
|
||
const reservedIdentifiers = ["as", "do", "in", "is"]; | ||
|
||
export const generateLocales = async () => { | ||
let previousContent: string | undefined; | ||
try { | ||
previousContent = (await readFile(localeFile)).toString(); | ||
} catch (err: any) { | ||
if (err.code != "ENOENT") { | ||
throw err; | ||
} | ||
} | ||
|
||
const previousSha = previousContent?.match( | ||
/\/\/ SHA: (\b[0-9a-f]{5,40}\b)/ | ||
)?.[1]; | ||
const previousId = previousContent?.match( | ||
/\/\/ ID: ([A-Za-z0-9+\/=]+)/ | ||
)?.[1]; | ||
|
||
const fileStat = await lstat(__filename); | ||
const latestId = Buffer.from( | ||
`${fileStat.ctimeMs}-${fileStat.mtimeMs}` | ||
).toString("base64"); | ||
|
||
const octokit = new Octokit(); | ||
const { | ||
data: [{ sha: latestSha }], | ||
} = await octokit.request("GET /repos/{owner}/{repo}/commits", { | ||
...Repos.extensionsStore.repo, | ||
per_page: 1, | ||
}); | ||
|
||
if (previousSha === latestSha && previousId === latestId) { | ||
logger.log(`Aborting update due to matching SHA and ID`); | ||
return; | ||
} | ||
|
||
const { body } = await got.get( | ||
Repos.extensionsStore.languagesJson(latestSha), | ||
{ | ||
responseType: "json", | ||
} | ||
); | ||
|
||
const { | ||
countries, | ||
languages, | ||
}: { | ||
countries: Record<string, string>; | ||
languages: Record<string, string>; | ||
} = body as any; | ||
|
||
const content = ` | ||
// SHA: ${latestSha} | ||
// ID: ${latestId} | ||
// Generated file. Do not edit. | ||
${getLanguagesClass(languages)} | ||
${getCountriesClass(countries)} | ||
`.trim(); | ||
|
||
await ensureDir(dirname(localeFile)); | ||
await writeFile(localeFile, content); | ||
logger.log(`Generated ${chalk.cyanBright(localeFile)}`); | ||
}; | ||
|
||
const getLanguagesClass = (languages: Record<string, string>) => { | ||
return ` | ||
enum LanguageCodes { | ||
${Object.keys(languages) | ||
.map((x) => ` ${escapeIdentifier(x)}`) | ||
.join(",\n")} | ||
} | ||
extension LanguageCodesUtils on LanguageCodes { | ||
String get code => name.replaceFirst(RegExp(r'_$'), ''); | ||
String get language => LanguageCodesUtils.codeNameMap[this]!; | ||
} | ||
abstract class LanguageUtils { | ||
static Map<LanguageCodes, String> codeNameMap = | ||
<LanguageCodes, String>{ | ||
${Object.entries(languages) | ||
.map( | ||
([code, lang]) => | ||
` LanguageCodes.${escapeIdentifier(code)}: '${lang.replace( | ||
/'/, | ||
"\\'" | ||
)}'` | ||
) | ||
.join(",\n")} | ||
}; | ||
static final Map<String, LanguageCodes> nameCodeMap = | ||
LanguageCodes.values | ||
.asMap() | ||
.map( | ||
(final int k, final LanguageCodes v) => | ||
MapEntry<String, LanguageCodes>(v.name, v), | ||
) | ||
.cast<String, LanguageCodes>(); | ||
} | ||
`.trim(); | ||
}; | ||
|
||
const getCountriesClass = (countries: Record<string, string>) => { | ||
return ` | ||
enum CountryCodes { | ||
${Object.keys(countries) | ||
.map((x) => ` ${escapeIdentifier(x)}`) | ||
.join(",\n")} | ||
} | ||
extension CountryCodesUtils on CountryCodes { | ||
String get code => name.replaceFirst(RegExp(r'_$'), ''); | ||
String get language => CountryCodesUtils.codeNameMap[this]!; | ||
} | ||
abstract class CountryUtils { | ||
static Map<CountryCodes, String> codeNameMap = | ||
<CountryCodes, String>{ | ||
${Object.entries(countries) | ||
.map( | ||
([code, country]) => | ||
` CountryCodes.${escapeIdentifier(code)}: '${country.replace( | ||
/'/, | ||
"\\'" | ||
)}'` | ||
) | ||
.join(",\n")} | ||
}; | ||
static final Map<String, CountryCodes> nameCodeMap = | ||
CountryCodes.values | ||
.asMap() | ||
.map( | ||
(final int k, final CountryCodes v) => | ||
MapEntry<String, CountryCodes>(v.name, v), | ||
) | ||
.cast<String, CountryCodes>(); | ||
} | ||
`.trim(); | ||
}; | ||
|
||
const escapeIdentifier = (value: string) => | ||
reservedIdentifiers.includes(value) ? `${value}_` : value; |
18 changes: 10 additions & 8 deletions
18
cli/commands/meta/generate/index.ts → cli/commands/generator/run/tasks/meta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
import { dirname, join } from "path"; | ||
import chalk from "chalk"; | ||
import { ensureDir, readFile, writeFile } from "fs-extra"; | ||
import { parse as yaml } from "yaml"; | ||
import { config } from "../../../config"; | ||
import { Logger } from "../../../logger"; | ||
import { config } from "../../../../config"; | ||
import { Logger } from "../../../../logger"; | ||
|
||
const logger = new Logger("meta:generate"); | ||
const logger = new Logger("generator:run:meta"); | ||
|
||
export const generate = async () => { | ||
const metaFile = join(config.base, "assets/data/meta.json"); | ||
|
||
export const generateMeta = async () => { | ||
const pubspecPath = join(config.base, "pubspec.yaml"); | ||
logger.log(`Reading pubspec.yaml from ${pubspecPath}`); | ||
|
||
const pubspec = yaml((await readFile(pubspecPath)).toString()); | ||
|
||
const outPath = join(config.base, "assets/data/meta.json"); | ||
await ensureDir(dirname(outPath)); | ||
await ensureDir(dirname(metaFile)); | ||
await writeFile( | ||
outPath, | ||
metaFile, | ||
JSON.stringify({ | ||
name: pubspec.description, | ||
code: pubspec.name, | ||
version: pubspec.version, | ||
}) | ||
); | ||
|
||
logger.log(`Generated ${outPath}`); | ||
logger.log(`Generated ${chalk.cyanBright(metaFile)}`); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const Repos = { | ||
extensionsStore: { | ||
repo: { | ||
owner: "yukino-app", | ||
repo: "extensions-store", | ||
}, | ||
commitsApiURL() { | ||
return `https://api.github.com/repos/${this.repo.owner}/${this.repo.repo}/commits?per_page=1`; | ||
}, | ||
languagesJson(sha: string) { | ||
return `https://raw.githubusercontent.com/${this.repo.owner}/${this.repo.repo}/${sha}/lib/assets/languages.json`; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.