Skip to content

Commit

Permalink
Merge pull request #4429 from easyops-cn/steve/v2-source-of-contract
Browse files Browse the repository at this point in the history
fix(): extract source of contract in contract.yaml
  • Loading branch information
WHChen-Alex authored Aug 26, 2024
2 parents d7b2074 + 4701e4c commit 41e65ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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",
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

0 comments on commit 41e65ec

Please sign in to comment.