Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): extract source of contract in contract.yaml #4429

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/build-config-factory/src/generateBrickContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const {
bricks: { webpackContractsFactory },
} = require("@next-core/webpack-config-factory");

const globalContractRegExp =
/(?<=@contract\s+)(?:<source=(?:[^>]+)>\s+)?(?:[\w.]+)(?:@[\d.]+)?(?=\s+)/g;
const singleContractRegExp = /^(?:<source=([^>]+)>\s+)?([\w.]+)(?:@([\d.]+))?$/;

module.exports = function generateBrickContracts(dir, isProviderBricks) {
console.log("Analyzing brick contracts...");
const startTime = Date.now();
Expand Down Expand Up @@ -43,15 +47,16 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) {
const contractFiles = await globby(
path.posix.join(dir, "contracts.log/*/*.contracts")
);
const contractRegExp = /(?<=@contract\s+)(?:[\w.]+)(?:@[\d.]+)?(?=\s+)/g;
const contractRegExp = globalContractRegExp;
await Promise.all(
contractFiles.map(async (filePath) => {
const source = await fs.readFile(filePath, "utf-8");
const contracts = source.match(contractRegExp);
if (isProviderBricks) {
if (contracts) {
for (const item of contracts) {
const [contract, version] = item.split("@");
const [_full, source, contract, version] =
item.match(singleContractRegExp);
depsMap.set(
`${pkgLastName}.${contract
.split(".")
Expand All @@ -60,6 +65,7 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) {
.join("-api-")}`,
{
type: "contract",
source: source || "sdk",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为现在 VB 打包时会扫描项目依赖的构件的制品中的 contract.yaml 中记录的依赖契约的信息,然后合并打进 bootstrap.json 中,导致应用 storyboard 中 meta.contracts 字段包含大量并不需要的契约,这包括直接使用 sdk 和 provider 的用法,实际只有使用 useProvider("xx@yy") 才需要,因此增加 source 字段区分,届时打包扫描时忽略存在 sourcesource 不为 contract 的记录。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contract,
version: version || "*",
}
Expand All @@ -72,9 +78,11 @@ module.exports = function generateBrickContracts(dir, isProviderBricks) {
depsMap.set(
brick,
contracts.map((item) => {
const [contract, version] = item.split("@");
const [_full, source, contract, version] =
item.match(singleContractRegExp);
return {
type: "contract",
source: source || "sdk",
contract,
version: version || "*",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const validFlowApiProviderName =

function gatherContractComments({ provider, brickName, contractsEntries }) {
let contract = "";
let source = "provider";

// Flow Api Provider
if (provider.includes("@")) {
contract = provider.replace("@", ".").replace(":", "@");
source = "contract";
} else {
// SDK API Provider
const [namespace, apiFullName] = provider.split(".");
Expand All @@ -22,7 +24,7 @@ function gatherContractComments({ provider, brickName, contractsEntries }) {
service
)}.${changeCase.snakeCase(model)}.${changeCase.pascalCase(apiName)}`;
}
const comment = `/**! @contract ${contract} */`;
const comment = `/**! @contract <source=${source}> ${contract} */`;
const fileName = `${brickName}${contractFileExtName}`;
const contracts = contractsEntries.get(fileName) || [];

Expand Down
Loading