Skip to content

Commit ed4e082

Browse files
devversionmmalerba
authored andcommitted
fix(schematics): properly indent inline files (#12317)
* Properly indents files that are being inlined through the `resolvedFiles` object that been recently introduced
1 parent c5b87da commit ed4e082

File tree

7 files changed

+36
-27
lines changed

7 files changed

+36
-27
lines changed

src/lib/schematics/address-form/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import { FormBuilder, Validators } from '@angular/forms';
55
selector: '<%= selector %>',
66
<% if(inlineTemplate) { %>
77
template: `
8-
<%= resolvedFiles.template %>
8+
<%= indentTextContent(resolvedFiles.template, 4) %>
99
`,<% } else { %>
1010
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
11-
styles: [
12-
`
13-
<%= resolvedFiles.stylesheet %>
14-
`
15-
],<% } else { %>
11+
styles: [`
12+
<%= indentTextContent(resolvedFiles.stylesheet, 4) %>
13+
`],<% } else { %>
1614
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleext %>'],<% } %><% if(!!viewEncapsulation) { %>
1715
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1816
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>

src/lib/schematics/dashboard/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
55
@Component({
66
selector: '<%= selector %>',<% if(inlineTemplate) { %>
77
template: `
8-
<%= resolvedFiles.template %>
8+
<%= indentTextContent(resolvedFiles.template, 4) %>
99
`,<% } else { %>
1010
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
11-
styles: [
12-
`
13-
<%= resolvedFiles.stylesheet %>
14-
`
15-
],<% } else { %>
11+
styles: [`
12+
<%= indentTextContent(resolvedFiles.stylesheet, 4) %>
13+
`],<% } else { %>
1614
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleext %>'],<% } %><% if(!!viewEncapsulation) { %>
1715
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1816
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import { map } from 'rxjs/operators';
66
@Component({
77
selector: '<%= selector %>',<% if(inlineTemplate) { %>
88
template: `
9-
<%= resolvedFiles.template %>
9+
<%= indentTextContent(resolvedFiles.template, 4) %>
1010
`,<% } else { %>
1111
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
12-
styles: [
13-
`
14-
<%= resolvedFiles.stylesheet %>
15-
`
16-
],<% } else { %>
12+
styles: [`
13+
<%= indentTextContent(resolvedFiles.stylesheet, 4) %>
14+
`],<% } else { %>
1715
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleext %>'],<% } %><% if(!!viewEncapsulation) { %>
1816
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1917
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>

src/lib/schematics/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-dataso
55
@Component({
66
selector: '<%= selector %>',<% if(inlineTemplate) { %>
77
template: `
8-
<%= resolvedFiles.template %>
8+
<%= indentTextContent(resolvedFiles.template, 4) %>
99
`,<% } else { %>
1010
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
1111
styles: []<% } else { %>

src/lib/schematics/tree/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ export interface FlatTreeNode {
2525
@Component({
2626
selector: '<%= selector %>',<% if (inlineTemplate) { %>
2727
template: `
28-
<%= resolvedFiles.template %>
28+
<%= indentTextContent(resolvedFiles.template, 4) %>
2929
`,<% } else { %>
3030
templateUrl: './<%= dasherize(name) %>.component.html',<% } if (inlineStyle) { %>
31-
styles: [
32-
`
33-
<%= resolvedFiles.stylesheet %>
34-
`
35-
],<% } else { %>
31+
styles: [`
32+
<%= indentTextContent(resolvedFiles.stylesheet, 4) %>
33+
`],<% } else { %>
3634
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleext %>'],<% } %><% if (!!viewEncapsulation) { %>
3735
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
3836
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# NOTE
2-
This code is directly taken from [angular schematics package](https://github.com/angular/devkit/tree/master/packages/schematics/angular/utility).
2+
3+
The base utility functions are taken from [angular schematics package](https://github.com/angular/angular-cli/tree/master/packages/schematics/angular/utility).

src/lib/schematics/utils/devkit-utils/component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ function buildSelector(options: any, projectPrefix: string) {
105105
return selector;
106106
}
107107

108+
/**
109+
* Indents the text content with the amount of specified spaces. The spaces will be added after
110+
* every line-break. This utility function can be used inside of EJS templates to properly
111+
* include the additional files.
112+
*/
113+
function indentTextContent(text: string, numSpaces: number): string {
114+
// In the Material project there should be only LF line-endings, but the schematic files
115+
// are not being linted and therefore there can be also use CRLF or just CR line-endings.
116+
return text.replace(/(\r\n|\r|\n)/g, `$1${' '.repeat(numSpaces)}`);
117+
}
118+
108119
/**
109120
* Rule that copies and interpolates the files that belong to this schematic context. Additionally
110121
* a list of file paths can be passed to this rule in order to expose them inside the EJS
@@ -113,7 +124,9 @@ function buildSelector(options: any, projectPrefix: string) {
113124
* This allows inlining the external template or stylesheet files in EJS without having
114125
* to manually duplicate the file content.
115126
*/
116-
export function buildComponent(options: any, additionalFiles?: {[key: string]: string}): Rule {
127+
export function buildComponent(options: any,
128+
additionalFiles: {[key: string]: string} = {}): Rule {
129+
117130
return (host: Tree, context: FileSystemSchematicContext) => {
118131
const workspace = getWorkspace(host);
119132

@@ -166,6 +179,7 @@ export function buildComponent(options: any, additionalFiles?: {[key: string]: s
166179
options.inlineStyle ? filter(path => !path.endsWith('.__styleext__')) : noop(),
167180
options.inlineTemplate ? filter(path => !path.endsWith('.html')) : noop(),
168181
template({
182+
indentTextContent,
169183
resolvedFiles,
170184
...baseTemplateContext
171185
}),
@@ -180,3 +194,5 @@ export function buildComponent(options: any, additionalFiles?: {[key: string]: s
180194
])(host, context);
181195
};
182196
}
197+
198+
// TODO(paul): move this utility out of the `devkit-utils` because it's no longer taken from there.

0 commit comments

Comments
 (0)