Skip to content

Commit d8be642

Browse files
committed
feat(core): schematic migrations
1 parent 54c5df1 commit d8be642

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

libs/core/schematics/migrations.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"schematics": {
3+
"migration-0.36": {
4+
"version": "0.36",
5+
"description": "Removes icon font files, font styles, styles from config and notifies about necessity of running add schematics again.",
6+
"factory": "./migrations/migration-0.36/index"
7+
}
8+
}
9+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { isJsonArray, isJsonObject } from '@angular-devkit/core';
2+
import { Rule, SchematicContext, Tree, chain } from '@angular-devkit/schematics';
3+
import { updateWorkspace } from '@schematics/angular/utility/workspace';
4+
5+
import { findStylesheetFiles } from '../../utils/file-utils';
6+
7+
export default function (): Rule {
8+
return chain([removeStylesFromConfig(), removeIconFonts(), removeFontStyles(), noticeAddSchematics()]);
9+
}
10+
11+
function removeStylesFromConfig(): Rule {
12+
return async (_: Tree, context: SchematicContext) =>
13+
updateWorkspace((workspace) => {
14+
workspace.projects.forEach((project) => {
15+
project.targets.forEach((target) => {
16+
const styles = target.options?.styles;
17+
18+
if (!target.options?.styles || !styles || !isJsonArray(styles)) {
19+
return;
20+
}
21+
22+
const stylesToRemove = [
23+
'node_modules/fundamental-styles/dist/icon.css',
24+
'./node_modules/@sap-theming/theming-base-content/content/Base/baseLib/sap_fiori_3/css_variables.css',
25+
'./node_modules/fundamental-styles/dist/theming/sap_fiori_3.css'
26+
];
27+
28+
stylesToRemove.forEach((styleToRemove) => {
29+
const indexInStylesArray = styles.findIndex(
30+
(configStyle) => typeof configStyle === 'string' && configStyle === styleToRemove
31+
);
32+
if (indexInStylesArray > -1) {
33+
styles.splice(indexInStylesArray, 1);
34+
context.logger.info(`✅️ Removed '${styleToRemove}' style from angular.json`);
35+
}
36+
});
37+
38+
target.options.styles = styles;
39+
});
40+
});
41+
});
42+
}
43+
44+
function removeIconFonts(): Rule {
45+
return (tree: Tree, context: SchematicContext) =>
46+
updateWorkspace((workspace) => {
47+
workspace.projects.forEach((project) => {
48+
project.targets.forEach((target) => {
49+
const styles = target.options?.styles;
50+
51+
if (!target.options?.styles || !styles || !isJsonArray(styles)) {
52+
return;
53+
}
54+
55+
const iconFonts = ['sap_fiori_3_fonts', 'sap_horizon_fonts'];
56+
57+
iconFonts.forEach((fontFile) => {
58+
// Remove file
59+
const fontFileWithPath = `${project.sourceRoot}/theming/${fontFile}.css`;
60+
if (tree.exists(fontFileWithPath)) {
61+
tree.delete(fontFileWithPath);
62+
context.logger.info(`✅️ Removed ${fontFile}.css file.`);
63+
}
64+
65+
// Remove from config
66+
const indexInStylesArray = styles.findIndex(
67+
(style) => isJsonObject(style) && style?.bundleName === `${fontFile}`
68+
);
69+
if (indexInStylesArray > -1) {
70+
styles.splice(indexInStylesArray, 1);
71+
context.logger.info(`✅️ Removed '${fontFile}' style from angular.json`);
72+
}
73+
});
74+
75+
target.options.styles = styles;
76+
});
77+
});
78+
});
79+
}
80+
81+
function removeFontStyles(): Rule {
82+
return (tree: Tree, context: SchematicContext) => {
83+
const styleSheets = findStylesheetFiles(tree);
84+
85+
if (styleSheets.length > 0) {
86+
const FONT_FACE_REGEX = /(@font-face)[\w\s]?{[s\S\W\w]+?(?=\s}\s)?\s?}[\n\s]*/gi;
87+
88+
styleSheets.forEach((styleSheet) => {
89+
let buffer = tree.read(styleSheet);
90+
if (!buffer) {
91+
return;
92+
}
93+
94+
let contents = buffer.toString();
95+
96+
if (FONT_FACE_REGEX.test(contents)) {
97+
const matches = contents
98+
.match(FONT_FACE_REGEX)!
99+
.filter((m) => m.match(/~@sap-theming.*\.woff/)?.length);
100+
101+
if (matches.length === 0) {
102+
return;
103+
}
104+
105+
matches.forEach((match) => {
106+
contents = contents.replace(match, '');
107+
});
108+
109+
tree.overwrite(styleSheet, contents);
110+
context.logger.info(`✅️ Removed font styles from stylesheets.`);
111+
}
112+
});
113+
}
114+
115+
context.logger.info(
116+
`⚠️ Notice: If you have imported font styles from @sap-theming, please remove it since now it controlled by fundamental-ngx`
117+
);
118+
119+
return tree;
120+
};
121+
}
122+
123+
function noticeAddSchematics(): Rule {
124+
return (_: Tree, context: SchematicContext) => {
125+
context.logger.info(`ℹ️ Now you have run ng-add schematics once again to setup things in the right way.`);
126+
127+
// Unfortunately we cannot run it on our own because ng-update doesn't respect schema so there is no way to get options here and pass them further
128+
};
129+
}

libs/core/src/lib/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "@fundamental-ngx/core",
33
"version": "VERSION_PLACEHOLDER",
44
"schematics": "./schematics/collection.json",
5+
"ng-update": {
6+
"migrations": "./schematics/migrations.json"
7+
},
58
"description": "Fundamental Library for Angular - core",
69
"license": "Apache-2.0",
710
"homepage": "https://sap.github.io/fundamental-ngx/home",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
"compile-typedoc-platform": "rm -rf apps/docs/src/assets/typedoc/platform && typedoc --out apps/docs/src/assets/typedoc/platform libs/platform/src/lib --tsconfig libs/platform/src/lib/tsconfig.lib.json --hideGenerator --theme apps/docs/src/fd-typedoc",
7474
"compile-typedoc-json": "typedoc --json apps/docs/src/assets/typedoc/typedoc.json --hideGenerator --theme apps/docs/src/fd-typedoc libs/core/src/lib",
7575
"build:schematics": "./node_modules/.bin/tsc -p libs/core/tsconfig.schematics.json && yarn run copy:schemas && yarn run copy:collection",
76-
"copy:schemas": "cp libs/core/schematics/ng-add/schema.json dist/libs/core/schematics/ng-add/schema.json && yarn run copy:core-migration-shema",
77-
"copy:core-migration-shema": "cp libs/core/schematics/migrations/migrate-theme-fonts/schema.json dist/libs/core/schematics/migrations/migrate-theme-fonts/schema.json",
76+
"copy:schemas": "cp libs/core/schematics/ng-add/schema.json dist/libs/core/schematics/ng-add/schema.json && yarn run copy:core-migration-schema",
77+
"copy:core-migration-schema": "cp libs/core/schematics/migrations.json dist/libs/core/schematics/migrations.json",
7878
"copy:collection": "cp libs/core/schematics/collection.json dist/libs/core/schematics/collection.json",
7979
"build:platform-schematics": "./node_modules/.bin/tsc -p libs/platform/tsconfig.schematics.json && yarn run copy:platform-add-schemas && yarn run copy:platform-update-schemas && yarn run copy:platform-collection && yarn run copy:platform-translations && yarn run copy:migration-collection",
8080
"copy:platform-add-schemas": "cp libs/platform/schematics/ng-add/schema.json dist/libs/platform/schematics/ng-add/schema.json",

0 commit comments

Comments
 (0)