mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 02:54:21 +08:00
refactor(@schematics/angular): use new workspace helpers in update-i18n migration
This commit is contained in:
parent
6ff04473ef
commit
c49ebe78a2
@ -5,162 +5,106 @@
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
import { JsonAstObject, logging } from '@angular-devkit/core';
|
||||
import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics';
|
||||
import { logging, workspaces } from '@angular-devkit/core';
|
||||
import { Rule, Tree } from '@angular-devkit/schematics';
|
||||
import { posix } from 'path';
|
||||
import { getWorkspacePath } from '../../utility/config';
|
||||
import { NodeDependencyType, addPackageJsonDependency, getPackageJsonDependency } from '../../utility/dependencies';
|
||||
import {
|
||||
findPropertyInAstObject,
|
||||
insertPropertyInAstObjectInOrder,
|
||||
removePropertyInAstObject,
|
||||
} from '../../utility/json-utils';
|
||||
import { latestVersions } from '../../utility/latest-versions';
|
||||
import { allTargetOptions, allWorkspaceTargets, updateWorkspace } from '../../utility/workspace';
|
||||
import { Builders } from '../../utility/workspace-models';
|
||||
import { getAllOptions, getProjectTarget, getTargets, getWorkspace } from './utils';
|
||||
|
||||
export function updateI18nConfig(): Rule {
|
||||
return (tree, context) => {
|
||||
// this is whole process of partial change writing and repeat loading/looping is only necessary
|
||||
// to workaround underlying issues with the recorder and ast helper functions
|
||||
|
||||
const workspacePath = getWorkspacePath(tree);
|
||||
let workspaceAst = getWorkspace(tree);
|
||||
|
||||
// Update extract targets
|
||||
const extractTargets = getTargets(workspaceAst, 'extract-i18n', Builders.ExtractI18n);
|
||||
if (extractTargets.length > 0) {
|
||||
const recorder = tree.beginUpdate(workspacePath);
|
||||
|
||||
for (const { target, project } of extractTargets) {
|
||||
addProjectI18NOptions(recorder, tree, target, project);
|
||||
removeExtracti18nDeprecatedOptions(recorder, target);
|
||||
return (tree, { logger }) =>
|
||||
updateWorkspace((workspace) => {
|
||||
// Process extraction targets first since they use browser option values
|
||||
for (const [, target, , project] of allWorkspaceTargets(workspace)) {
|
||||
switch (target.builder) {
|
||||
case Builders.ExtractI18n:
|
||||
addProjectI18NOptions(tree, target, project);
|
||||
removeExtracti18nDeprecatedOptions(target);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tree.commitUpdate(recorder);
|
||||
|
||||
// workspace was changed so need to reload
|
||||
workspaceAst = getWorkspace(tree);
|
||||
}
|
||||
|
||||
// Update base HREF values for existing configurations
|
||||
let recorder = tree.beginUpdate(workspacePath);
|
||||
for (const { target } of getTargets(workspaceAst, 'build', Builders.Browser)) {
|
||||
updateBaseHrefs(recorder, target);
|
||||
}
|
||||
for (const { target } of getTargets(workspaceAst, 'test', Builders.Karma)) {
|
||||
updateBaseHrefs(recorder, target);
|
||||
}
|
||||
tree.commitUpdate(recorder);
|
||||
|
||||
// Remove i18n format option
|
||||
workspaceAst = getWorkspace(tree);
|
||||
recorder = tree.beginUpdate(workspacePath);
|
||||
for (const { target } of getTargets(workspaceAst, 'build', Builders.Browser)) {
|
||||
removeFormatOption(recorder, target);
|
||||
}
|
||||
for (const { target } of getTargets(workspaceAst, 'test', Builders.Karma)) {
|
||||
removeFormatOption(recorder, target);
|
||||
}
|
||||
tree.commitUpdate(recorder);
|
||||
|
||||
// Add new i18n options to build target configurations
|
||||
workspaceAst = getWorkspace(tree);
|
||||
recorder = tree.beginUpdate(workspacePath);
|
||||
for (const { target } of getTargets(workspaceAst, 'build', Builders.Browser)) {
|
||||
addBuilderI18NOptions(recorder, target, context.logger);
|
||||
}
|
||||
tree.commitUpdate(recorder);
|
||||
|
||||
// Add new i18n options to test target configurations
|
||||
workspaceAst = getWorkspace(tree);
|
||||
recorder = tree.beginUpdate(workspacePath);
|
||||
for (const { target } of getTargets(workspaceAst, 'test', Builders.Karma)) {
|
||||
addBuilderI18NOptions(recorder, target, context.logger);
|
||||
}
|
||||
tree.commitUpdate(recorder);
|
||||
|
||||
return tree;
|
||||
};
|
||||
for (const [, target] of allWorkspaceTargets(workspace)) {
|
||||
switch (target.builder) {
|
||||
case Builders.Browser:
|
||||
case Builders.Karma:
|
||||
updateBaseHrefs(target);
|
||||
removeFormatOption(target);
|
||||
addBuilderI18NOptions(target, logger);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function addProjectI18NOptions(
|
||||
recorder: UpdateRecorder,
|
||||
tree: Tree,
|
||||
builderConfig: JsonAstObject,
|
||||
projectConfig: JsonAstObject,
|
||||
builderConfig: workspaces.TargetDefinition,
|
||||
projectConfig: workspaces.ProjectDefinition,
|
||||
) {
|
||||
const browserConfig = getProjectTarget(projectConfig, 'build', Builders.Browser);
|
||||
if (!browserConfig || browserConfig.kind !== 'object') {
|
||||
const browserConfig = projectConfig.targets.get('build');
|
||||
if (!browserConfig || browserConfig.builder !== Builders.Browser) {
|
||||
return;
|
||||
}
|
||||
|
||||
// browser builder options
|
||||
let locales: Record<string, string | { translation: string; baseHref: string }> | undefined;
|
||||
const options = getAllOptions(browserConfig);
|
||||
for (const option of options) {
|
||||
const localeId = findPropertyInAstObject(option, 'i18nLocale');
|
||||
if (!localeId || localeId.kind !== 'string') {
|
||||
for (const [, options] of allTargetOptions(browserConfig)) {
|
||||
const localeId = options.i18nLocale;
|
||||
if (typeof localeId !== 'string') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const localeFile = findPropertyInAstObject(option, 'i18nFile');
|
||||
if (!localeFile || localeFile.kind !== 'string') {
|
||||
const localeFile = options.i18nFile;
|
||||
if (typeof localeFile !== 'string') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const localIdValue = localeId.value;
|
||||
const localeFileValue = localeFile.value;
|
||||
|
||||
const baseHref = findPropertyInAstObject(option, 'baseHref');
|
||||
let baseHrefValue;
|
||||
if (baseHref) {
|
||||
if (baseHref.kind === 'string' && baseHref.value !== `/${localIdValue}/`) {
|
||||
baseHrefValue = baseHref.value;
|
||||
let baseHref = options.baseHref;
|
||||
if (typeof baseHref === 'string') {
|
||||
// If the configuration baseHref is already the default locale value, do not include it
|
||||
if (baseHref === `/${localeId}/`) {
|
||||
baseHref = undefined;
|
||||
}
|
||||
} else {
|
||||
// If the configuration does not contain a baseHref, ensure the main option value is used.
|
||||
baseHrefValue = '';
|
||||
baseHref = '';
|
||||
}
|
||||
|
||||
if (!locales) {
|
||||
locales = {
|
||||
[localIdValue]:
|
||||
baseHrefValue === undefined
|
||||
? localeFileValue
|
||||
[localeId]:
|
||||
baseHref === undefined
|
||||
? localeFile
|
||||
: {
|
||||
translation: localeFileValue,
|
||||
baseHref: baseHrefValue,
|
||||
translation: localeFile,
|
||||
baseHref,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
locales[localIdValue] =
|
||||
baseHrefValue === undefined
|
||||
? localeFileValue
|
||||
locales[localeId] =
|
||||
baseHref === undefined
|
||||
? localeFile
|
||||
: {
|
||||
translation: localeFileValue,
|
||||
baseHref: baseHrefValue,
|
||||
translation: localeFile,
|
||||
baseHref,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (locales) {
|
||||
// Get sourceLocale from extract-i18n builder
|
||||
const i18nOptions = getAllOptions(builderConfig);
|
||||
const i18nOptions = [...allTargetOptions(builderConfig)];
|
||||
const sourceLocale = i18nOptions
|
||||
.map(o => {
|
||||
const sourceLocale = findPropertyInAstObject(o, 'i18nLocale');
|
||||
.map(([, o]) => o.i18nLocale)
|
||||
.find(x => !!x && typeof x === 'string');
|
||||
|
||||
return sourceLocale && sourceLocale.value;
|
||||
})
|
||||
.find(x => !!x);
|
||||
|
||||
// Add i18n project configuration
|
||||
insertPropertyInAstObjectInOrder(recorder, projectConfig, 'i18n', {
|
||||
projectConfig.extensions['i18n'] = {
|
||||
locales,
|
||||
// tslint:disable-next-line: no-any
|
||||
sourceLocale: sourceLocale as any,
|
||||
}, 6);
|
||||
...(sourceLocale ? { sourceLocale } : {}),
|
||||
};
|
||||
|
||||
// Add @angular/localize if not already a dependency
|
||||
if (!getPackageJsonDependency(tree, '@angular/localize')) {
|
||||
@ -174,120 +118,92 @@ function addProjectI18NOptions(
|
||||
}
|
||||
|
||||
function addBuilderI18NOptions(
|
||||
recorder: UpdateRecorder,
|
||||
builderConfig: JsonAstObject,
|
||||
builderConfig: workspaces.TargetDefinition,
|
||||
logger: logging.LoggerApi,
|
||||
) {
|
||||
const options = getAllOptions(builderConfig);
|
||||
for (const [, options] of allTargetOptions(builderConfig)) {
|
||||
const localeId = options.i18nLocale;
|
||||
const i18nFile = options.i18nFile;
|
||||
|
||||
for (const option of options) {
|
||||
const localeId = findPropertyInAstObject(option, 'i18nLocale');
|
||||
const i18nFile = findPropertyInAstObject(option, 'i18nFile');
|
||||
|
||||
const outputPath = findPropertyInAstObject(option, 'outputPath');
|
||||
const outputPath = options.outputPath;
|
||||
if (
|
||||
localeId &&
|
||||
localeId.kind === 'string' &&
|
||||
typeof localeId === 'string' &&
|
||||
i18nFile &&
|
||||
outputPath &&
|
||||
outputPath.kind === 'string'
|
||||
typeof outputPath === 'string'
|
||||
) {
|
||||
if (outputPath.value.match(new RegExp(`[/\\\\]${localeId.value}[/\\\\]?$`))) {
|
||||
const newOutputPath = outputPath.value.replace(
|
||||
new RegExp(`[/\\\\]${localeId.value}[/\\\\]?$`),
|
||||
if (outputPath.match(new RegExp(`[/\\\\]${localeId}[/\\\\]?$`))) {
|
||||
const newOutputPath = outputPath.replace(
|
||||
new RegExp(`[/\\\\]${localeId}[/\\\\]?$`),
|
||||
'',
|
||||
);
|
||||
const { start, end } = outputPath;
|
||||
recorder.remove(start.offset, end.offset - start.offset);
|
||||
recorder.insertLeft(start.offset, `"${newOutputPath}"`);
|
||||
options.outputPath = newOutputPath;
|
||||
} else {
|
||||
logger.warn(
|
||||
`Output path value "${outputPath.value}" for locale "${localeId.value}" is not supported with the new localization system. ` +
|
||||
`Output path value "${outputPath}" for locale "${localeId}" is not supported with the new localization system. ` +
|
||||
`With the current value, the localized output would be written to "${posix.join(
|
||||
outputPath.value,
|
||||
localeId.value,
|
||||
outputPath,
|
||||
localeId,
|
||||
)}". ` +
|
||||
`Keeping existing options for the target configuration of locale "${localeId.value}".`,
|
||||
`Keeping existing options for the target configuration of locale "${localeId}".`,
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (localeId && localeId.kind === 'string') {
|
||||
if (typeof localeId === 'string') {
|
||||
// add new localize option
|
||||
insertPropertyInAstObjectInOrder(recorder, option, 'localize', [localeId.value], 12);
|
||||
removePropertyInAstObject(recorder, option, 'i18nLocale');
|
||||
options.localize = [localeId];
|
||||
delete options.i18nLocale;
|
||||
}
|
||||
|
||||
if (i18nFile) {
|
||||
removePropertyInAstObject(recorder, option, 'i18nFile');
|
||||
if (i18nFile !== undefined) {
|
||||
delete options.i18nFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeFormatOption(
|
||||
recorder: UpdateRecorder,
|
||||
builderConfig: JsonAstObject,
|
||||
) {
|
||||
const options = getAllOptions(builderConfig);
|
||||
|
||||
for (const option of options) {
|
||||
function removeFormatOption(builderConfig: workspaces.TargetDefinition) {
|
||||
for (const [, options] of allTargetOptions(builderConfig)) {
|
||||
// The format is always auto-detected now
|
||||
const i18nFormat = findPropertyInAstObject(option, 'i18nFormat');
|
||||
if (i18nFormat) {
|
||||
removePropertyInAstObject(recorder, option, 'i18nFormat');
|
||||
}
|
||||
delete options.i18nFormat;
|
||||
}
|
||||
}
|
||||
|
||||
function updateBaseHrefs(
|
||||
recorder: UpdateRecorder,
|
||||
builderConfig: JsonAstObject,
|
||||
builderConfig: workspaces.TargetDefinition,
|
||||
) {
|
||||
const options = getAllOptions(builderConfig);
|
||||
const mainOptions = findPropertyInAstObject(builderConfig, 'options');
|
||||
const mainBaseHref =
|
||||
mainOptions &&
|
||||
mainOptions.kind === 'object' &&
|
||||
findPropertyInAstObject(mainOptions, 'baseHref');
|
||||
const mainBaseHref = builderConfig.options?.baseHref;
|
||||
const hasMainBaseHref =
|
||||
!!mainBaseHref && mainBaseHref.kind === 'string' && mainBaseHref.value !== '/';
|
||||
!!mainBaseHref && typeof mainBaseHref === 'string' && mainBaseHref !== '/';
|
||||
|
||||
for (const option of options) {
|
||||
const localeId = findPropertyInAstObject(option, 'i18nLocale');
|
||||
const i18nFile = findPropertyInAstObject(option, 'i18nFile');
|
||||
for (const [, options] of allTargetOptions(builderConfig)) {
|
||||
const localeId = options.i18nLocale;
|
||||
const i18nFile = options.i18nFile;
|
||||
|
||||
// localize base HREF values are controlled by the i18n configuration
|
||||
const baseHref = findPropertyInAstObject(option, 'baseHref');
|
||||
if (localeId && i18nFile && baseHref) {
|
||||
const baseHref = options.baseHref;
|
||||
if (localeId !== undefined && i18nFile !== undefined && baseHref !== undefined) {
|
||||
// if the main option set has a non-default base href,
|
||||
// ensure that the augmented base href has the correct base value
|
||||
if (hasMainBaseHref) {
|
||||
const { start, end } = baseHref;
|
||||
recorder.remove(start.offset, end.offset - start.offset);
|
||||
recorder.insertLeft(start.offset, `"/"`);
|
||||
options.baseHref = '/';
|
||||
} else {
|
||||
removePropertyInAstObject(recorder, option, 'baseHref');
|
||||
delete options.baseHref;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeExtracti18nDeprecatedOptions(recorder: UpdateRecorder, builderConfig: JsonAstObject) {
|
||||
const options = getAllOptions(builderConfig);
|
||||
|
||||
for (const option of options) {
|
||||
function removeExtracti18nDeprecatedOptions(builderConfig: workspaces.TargetDefinition) {
|
||||
for (const [, options] of allTargetOptions(builderConfig)) {
|
||||
// deprecated options
|
||||
removePropertyInAstObject(recorder, option, 'i18nLocale');
|
||||
const i18nFormat = option.properties.find(({ key }) => key.value === 'i18nFormat');
|
||||
delete options.i18nLocale;
|
||||
|
||||
if (i18nFormat) {
|
||||
if (options.i18nFormat !== undefined) {
|
||||
// i18nFormat has been changed to format
|
||||
const key = i18nFormat.key;
|
||||
const offset = key.start.offset + 1;
|
||||
recorder.remove(offset, key.value.length);
|
||||
recorder.insertLeft(offset, 'format');
|
||||
options.format = options.i18nFormat;
|
||||
delete options.i18nFormat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user