2019-09-20 11:05:45 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @fileoverview Schematics for ng-new project that builds with Bazel.
|
|
|
|
*/
|
|
|
|
|
2020-05-05 05:47:12 -04:00
|
|
|
import {virtualFs, workspaces} from '@angular-devkit/core';
|
|
|
|
import {chain, Rule, SchematicsException, Tree} from '@angular-devkit/schematics';
|
|
|
|
import {getWorkspace} from '@schematics/angular/utility/workspace';
|
|
|
|
import {Builders} from '@schematics/angular/utility/workspace-models';
|
2019-09-20 11:05:45 -04:00
|
|
|
|
|
|
|
import {Schema} from './schema';
|
|
|
|
|
|
|
|
|
|
|
|
export const localizePolyfill = `import '@angular/localize/init';`;
|
|
|
|
|
2020-05-05 05:47:12 -04:00
|
|
|
function getRelevantTargetDefinitions(
|
|
|
|
project: workspaces.ProjectDefinition, builderName: Builders): workspaces.TargetDefinition[] {
|
|
|
|
const definitions: workspaces.TargetDefinition[] = [];
|
|
|
|
project.targets.forEach((target: workspaces.TargetDefinition): void => {
|
|
|
|
if (target.builder === builderName) {
|
|
|
|
definitions.push(target);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return definitions;
|
|
|
|
}
|
2019-09-20 11:05:45 -04:00
|
|
|
|
2020-05-05 05:47:12 -04:00
|
|
|
function getOptionValuesForTargetDefinition(
|
|
|
|
definition: workspaces.TargetDefinition, optionName: string): string[] {
|
|
|
|
const optionValues: string[] = [];
|
|
|
|
if (definition.options && optionName in definition.options) {
|
|
|
|
let optionValue: unknown = definition.options[optionName];
|
|
|
|
if (typeof optionValue === 'string') {
|
|
|
|
optionValues.push(optionValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!definition.configurations) {
|
|
|
|
return optionValues;
|
|
|
|
}
|
|
|
|
Object.values(definition.configurations)
|
|
|
|
.forEach((configuration: Record<string, unknown>|undefined): void => {
|
|
|
|
if (configuration && optionName in configuration) {
|
|
|
|
const optionValue: unknown = configuration[optionName];
|
|
|
|
if (typeof optionValue === 'string') {
|
|
|
|
optionValues.push(optionValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-09-20 11:05:45 -04:00
|
|
|
return optionValues;
|
|
|
|
}
|
|
|
|
|
2020-05-05 05:47:12 -04:00
|
|
|
function getFileListForRelevantTargetDefinitions(
|
|
|
|
project: workspaces.ProjectDefinition, builderName: Builders, optionName: string): string[] {
|
|
|
|
const fileList: string[] = [];
|
|
|
|
const definitions = getRelevantTargetDefinitions(project, builderName);
|
|
|
|
definitions.forEach((definition: workspaces.TargetDefinition): void => {
|
|
|
|
const optionValues = getOptionValuesForTargetDefinition(definition, optionName);
|
|
|
|
optionValues.forEach((filePath: string): void => {
|
|
|
|
if (fileList.indexOf(filePath) === -1) {
|
|
|
|
fileList.push(filePath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return fileList;
|
|
|
|
}
|
2019-09-20 11:05:45 -04:00
|
|
|
|
2020-05-05 05:47:12 -04:00
|
|
|
function prependToTargetFiles(
|
|
|
|
project: workspaces.ProjectDefinition, builderName: Builders, optionName: string, str: string) {
|
2019-09-20 11:05:45 -04:00
|
|
|
return (host: Tree) => {
|
2020-05-05 05:47:12 -04:00
|
|
|
const fileList = getFileListForRelevantTargetDefinitions(project, builderName, optionName);
|
2019-09-20 11:05:45 -04:00
|
|
|
|
2020-05-05 05:47:12 -04:00
|
|
|
fileList.forEach((path: string): void => {
|
2019-09-20 11:05:45 -04:00
|
|
|
const data = host.read(path);
|
|
|
|
if (!data) {
|
|
|
|
// If the file doesn't exist, just ignore it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = virtualFs.fileBufferToString(data);
|
|
|
|
if (content.includes(localizePolyfill) ||
|
|
|
|
content.includes(localizePolyfill.replace(/'/g, '"'))) {
|
|
|
|
// If the file already contains the polyfill (or variations), ignore it too.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add string at the start of the file.
|
|
|
|
const recorder = host.beginUpdate(path);
|
|
|
|
recorder.insertLeft(0, str);
|
|
|
|
host.commitUpdate(recorder);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function(options: Schema): Rule {
|
2020-05-05 05:47:12 -04:00
|
|
|
return async (host: Tree) => {
|
2019-09-20 11:05:45 -04:00
|
|
|
if (!options.name) {
|
2020-05-05 05:47:12 -04:00
|
|
|
throw new SchematicsException('Option "name" is required.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const workspace = await getWorkspace(host);
|
|
|
|
const project: workspaces.ProjectDefinition|undefined = workspace.projects.get(options.name);
|
|
|
|
if (!project) {
|
|
|
|
throw new SchematicsException(`Invalid project name (${options.name})`);
|
2019-09-20 11:05:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const localizeStr =
|
|
|
|
`/***************************************************************************************************
|
|
|
|
* Load \`$localize\` onto the global scope - used if i18n tags appear in Angular templates.
|
|
|
|
*/
|
|
|
|
${localizePolyfill}
|
|
|
|
`;
|
|
|
|
|
|
|
|
return chain([
|
2020-05-05 05:47:12 -04:00
|
|
|
prependToTargetFiles(project, Builders.Browser, 'polyfills', localizeStr),
|
|
|
|
prependToTargetFiles(project, Builders.Server, 'main', localizeStr),
|
2019-09-20 11:05:45 -04:00
|
|
|
]);
|
|
|
|
};
|
|
|
|
}
|