Skip to content

Commit

Permalink
Rename task and remove relative bundle url support
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Jan 18, 2024
1 parent 90909d1 commit f8eb27a
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {SemVer: Version} = semver;
import {promisify} from "node:util";
import path from "node:path/posix";
import {getLogger} from "@ui5/logger";
const log = getLogger("builder:processors:manifestTransformer");
const log = getLogger("builder:processors:manifestEnricher");

const APP_DESCRIPTOR_V22 = new Version("1.21.0");

Expand All @@ -14,10 +14,9 @@ const APP_DESCRIPTOR_V22 = new Version("1.21.0");
* @param {string} defaultBundleName
*/
function getLocale(bundleName, defaultBundleName) {
log.verbose("File name: " + bundleName);
log.verbose("Resource bundle detected on " + bundleName);
bundleName = path.basename(bundleName, ".properties");
defaultBundleName = path.basename(defaultBundleName, ".properties");

if (bundleName === defaultBundleName) {
return "";
} else {
Expand All @@ -39,46 +38,51 @@ function hasManifestTemplates(manifest) {
return JSON.stringify(manifest).match(rManifestTemplate);
}

/**
* Transforms "ui5://my/fancy/app/i18n/i18n.properties" to "my.fancy.app.i18n.i18n"
*
* @param {string} bundleUrl A bundle Url
* @returns {string}
*/
function getBundleNameByUrl(bundleUrl) {
return bundleUrl.split("ui5://")[1].replaceAll("/", ".").split(".properties")[0];
}

// fallbacklocale not default bundle ---- will not work
async function fileExists(stat, filePath) {
try {
return !!(await stat(filePath));
} catch (e) {
/**
* Determines if a bundle config should be enriched with supported locales
*
* @param {object} bundleConfig The bundle config
* @param {string} appId The app id
* @returns {boolean}
*/
function isAutofillRequired(bundleConfig, appId) {
const {supportedLocales, bundleUrl} = bundleConfig;
let {bundleName} = bundleConfig;

if (supportedLocales) {
return false;
}
}

async function isAutofillRequired(bundleConfig, appId, stat) {
const {supportedLocales, bundleUrl, bundleName} = bundleConfig;

if (bundleUrl && bundleUrl.startsWith("/")) {
log.verbose("manifest.json: bundleUrl '" + bundleUrl +
"' contains an absolute path, no supportedLocales are generated");
return false;
}

if (bundleUrl && bundleUrl.startsWith("ui5://") && !getBundleNameByUrl(bundleUrl).startsWith(appId)) {
if (bundleUrl && bundleUrl.startsWith("..")) {
log.verbose("manifest.json: bundleUrl '" + bundleUrl +
"' contains a relative path, no supportedLocales are generated");
return false;
}

if (bundleUrl && bundleUrl.startsWith("..")) {
const bundleExistsInProject = await fileExists(stat, bundleUrl);
if (!bundleExistsInProject) {
log.verbose("manifest.json: bundleUrl '" + bundleUrl +
"' is outside the current project, no supportedLocales are not generated");
return false;
}
if (bundleUrl && bundleUrl.startsWith("ui5://")) {
bundleName = getBundleNameByUrl(bundleUrl);
}

// sap.demo.components.appA
// sap.demo.i18n
if (bundleName && !bundleName.startsWith(appId)) {
return false;
}

if (supportedLocales) {
const logMsg = bundleUrl ? "bundleUrl '" + bundleUrl : "bundleName '" + bundleName;
log.verbose("manifest.json: " + logMsg +
"' contains a path which is not part of the project, no supportedLocales are generated");
return false;
}

Expand All @@ -97,7 +101,6 @@ function hasBundleTerminologies(bundleConfig) {
return bundleConfig?.terminologies && Object.keys(bundleConfig.terminologies).length > 0;
}


function getTerminologieBundles(bundleConfig, bundleConfigs) {
const terminologyBundles = [];
if (hasBundleTerminologies(bundleConfig)) {
Expand Down Expand Up @@ -188,7 +191,7 @@ function getSapUi5LibrariesBundles(manifest, bundleConfigs) {
bundleConfigs.push(sapui5LibraryBundleConfig);
}

async function getBundles(manifest, stat) {
function getBundles(manifest) {
const bundleConfigs = [];
const appType = getType(manifest);

Expand All @@ -200,55 +203,42 @@ async function getBundles(manifest, stat) {
getSapUi5ModelBundles(manifest, bundleConfigs);
}

// Challenge: async functions does not work for filter ----- still thinking
// return bundleConfigs
// .filter((bundleConfig) => isAutofillRequired(bundleConfig, manifest["sap.app"].id));

// Option 1:
const autofill = await Promise.all(bundleConfigs
.map(async (bundleConfig) => await isAutofillRequired(bundleConfig, manifest["sap.app"].id, stat)));

return bundleConfigs
.filter((bundleConfig, index) => autofill[index]);

// improvements
// map --> bei false undefined zurückgegen bei filter undefined werte rauswerfen/ true bundleconfig
// path.join is necessary when interpreting .. urls
return bundleConfigs.filter((bundleConfig) => isAutofillRequired(bundleConfig, manifest["sap.app"].id));
}

async function transformManifest(resource, fs, options) {
async function enrichManifest(resource, fs, options) {
// merge options with defaults
options = Object.assign({
prettyPrint: true,
}, options);

const content = await resource.getString();
const manifest = JSON.parse(content);
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);

if (!manifest._version) {
log.verbose("manifest.json: version is not defined. No supportedLocales are generated");
log.verbose("manifest.json: _version is not defined. No supportedLocales are generated");
return;
}

const descriptorVersion = new Version(manifest._version);
if (descriptorVersion.compare(APP_DESCRIPTOR_V22) === -1) {
log.verbose("manifest.json: version is lower than 1.21.0 so no supportedLocales can be generated");
log.verbose("manifest.json: _version is lower than 1.21.0 so no supportedLocales can be generated");
return;
}

const project = resource.getProject();
const namespace = project.getNamespace();

// in case of usage by server middleware --> isNamespaced ist false
const isNamespaced = namespace && resource.getPath().startsWith("/resources/" + namespace);

const bundles = await getBundles(manifest, stat);
const bundles = getBundles(manifest);

if (bundles.length === 0) {
return;
}

// check which locales are avalable
await Promise.all(bundles.map(async (bundleConfig) => {
const {bundleUrl, bundleName, fallbackLocale} = bundleConfig;
let defaultBundleName = bundleUrl;
Expand All @@ -273,18 +263,13 @@ async function transformManifest(resource, fs, options) {
} else {
pathToDefaultBundleName = defaultBundleName.replace(namespace.replace(/\./g, "/"), "");
}

log.verbose("File: " + defaultBundleName);
log.verbose("Path: " + pathToDefaultBundleName);

const generatedSupportedLocales = (await readdir(path.dirname(pathToDefaultBundleName)))
.filter((name) => name.endsWith(".properties") && name.startsWith(path.basename(defaultBundleName)))
.map((name) => {
log.verbose("File name: " + name);
return getLocale(name, defaultBundleName);
}).sort();

log.verbose("Generated locales: " + generatedSupportedLocales.toString());
log.verbose("Generated supported locales: " + generatedSupportedLocales.join(", ").toString());

if (fallbackLocale && !isFallbackLocalePartOfSupportedLocales(fallbackLocale, generatedSupportedLocales)) {
log.error("manifest.json: Generated supported locales ('" + generatedSupportedLocales.join("', '") + "') " +
Expand All @@ -306,7 +291,7 @@ async function transformManifest(resource, fs, options) {


/**
* @module @ui5/builder/processors/manifestTransformer
* @module @ui5/builder/processors/manifestEnricher
*/

/**
Expand All @@ -324,5 +309,5 @@ async function transformManifest(resource, fs, options) {
* @returns {Promise<@ui5/fs/Resource[]>} Promise resolving with the cloned resources
*/
export default function({resources, fs, options}) {
return Promise.all(resources.map(async (resource) => transformManifest(resource, fs, options)));
return Promise.all(resources.map(async (resource) => enrichManifest(resource, fs, options)));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import manifestTransformer from "../processors/manifestTransformer.js";
import manifestEnricher from "../processors/manifestEnricher.js";
import fsInterface from "@ui5/fs/fsInterface";
import {getLogger} from "@ui5/logger";
const log = getLogger("builder:task:transformerManifest");
const log = getLogger("builder:task:enrichManifest");

/* eslint "jsdoc/check-param-names": ["error", {"disableExtraPropertyReporting":true}] */
/**
Expand All @@ -24,7 +24,7 @@ export default async function({workspace, options}) {
// Note: all "manifest.json" files
return workspace.byGlob(`/resources/${namespace}/**/manifest.json`)
.then((resources) => {
return manifestTransformer({
return manifestEnricher({
resources,
fs: fsInterface(workspace),
options
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/taskRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const taskInfos = {
replaceCopyright: {path: "./replaceCopyright.js"},
replaceVersion: {path: "./replaceVersion.js"},
replaceBuildtime: {path: "./replaceBuildtime.js"},
enrichManifest: {path: "./enrichManifest.js"},
escapeNonAsciiCharacters: {path: "./escapeNonAsciiCharacters.js"},
executeJsdocSdkTransformation: {path: "./jsdoc/executeJsdocSdkTransformation.js"},
generateApiIndex: {path: "./jsdoc/generateApiIndex.js"},
generateJsdoc: {path: "./jsdoc/generateJsdoc.js"},
minify: {path: "./minify.js"},
buildThemes: {path: "./buildThemes.js"},
transformBootstrapHtml: {path: "./transformBootstrapHtml.js"},
transformManifest: {path: "./transformManifest.js"},
generateLibraryManifest: {path: "./generateLibraryManifest.js"},
generateVersionInfo: {path: "./generateVersionInfo.js"},
generateFlexChangesBundle: {path: "./bundlers/generateFlexChangesBundle.js"},
Expand Down
4 changes: 2 additions & 2 deletions test/lib/package-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("check number of exports", (t) => {
"processors/minifier",
"processors/libraryLessGenerator",
"processors/manifestCreator",
"processors/manifestTransformer",
"processors/manifestEnricher",
"processors/nonAsciiEscaper",
"processors/stringReplacer",
"processors/themeBuilder",
Expand All @@ -49,7 +49,7 @@ test("check number of exports", (t) => {
"tasks/replaceVersion",
"tasks/replaceBuildtime",
"tasks/transformBootstrapHtml",
"tasks/transformManifest",
"tasks/enrichManifest",

// Internal modules (only to be used by @ui5/* packages)
{exportedSpecifier: "internal/taskRepository", mappedModule: "../../lib/tasks/taskRepository.js"},
Expand Down
Loading

0 comments on commit f8eb27a

Please sign in to comment.