refactor(@schematics/angular): use new workspace helpers in update-i18n migration

This commit is contained in:
Charles Lyding 2020-09-23 16:52:40 -04:00 committed by Alan Agius
parent 6ff04473ef
commit c49ebe78a2

View File

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