Skip to content
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
76 changes: 41 additions & 35 deletions generators/repo/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from "fs/promises";
import ansiEscapes from "ansi-escapes";
import chalk from "chalk";
import { cleanDirectoryPath, ensureRelativeBasePath, copyFile, createRepoUrlFromRemote, ensureDirectoryPath, getGlobFiles, getRepoPropsFromRemote, isStringNullOrEmpty, RepoProps, writeHeader,isFilePath } from "../common/util";
import { AssetRule, GitRemote, RepomanCommand, RepomanCommandOptions, RepoManifest } from "../models";
import { AssetRule, RewriteRule, GitRemote, RepomanCommand, RepomanCommandOptions, RepoManifest } from "../models";
import { GitRepo } from "../tools/git";

export interface GenerateCommandOptions extends RepomanCommandOptions {
Expand Down Expand Up @@ -39,7 +39,7 @@ export class GenerateCommand implements RepomanCommand {
private outputPath: string;
private generatePath: string;
private assetRules: AssetRule[];
private rewritePatterns: string[];
private rewriteRules: RewriteRule[];

constructor(private options: GenerateCommandOptions) {
this.sourcePath = path.resolve(path.normalize(options.source));
Expand All @@ -61,7 +61,7 @@ export class GenerateCommand implements RepomanCommand {
});
}

this.rewritePatterns = this.manifest.repo.rewrite?.patterns || ["**/*.@(yml|yaml)"];
this.rewriteRules =(this.manifest.repo.rewrite) ? [...this.manifest.repo.rewrite?.rules] : [];
}
catch (err) {
console.error(chalk.red(`Repo template manifest not found at '${this.templateFile}'`));
Expand All @@ -86,7 +86,10 @@ export class GenerateCommand implements RepomanCommand {
}

console.info();
await this.rewritePaths();

for (const rule of this.rewriteRules) {
await this.processRewriteRule(rule);
}
console.info(chalk.cyan('Repo generation completed.'));
console.info();

Expand Down Expand Up @@ -375,38 +378,41 @@ export class GenerateCommand implements RepomanCommand {
}
}

private rewritePaths = async () => {
console.info(chalk.cyan("Rewriting relative paths found in files"));

const globOptions: IOptions = { cwd: this.generatePath, matchBase: true };
const tasks = this.rewritePatterns.map(pattern => getGlobFiles(pattern, globOptions));
const files = (await Promise.all(tasks)).flat();

const pathRegex = new RegExp(/((?:\.{1,2}[\/\\]{1,2})+[^'"\s]*)/gm);

for (const filePath of files) {
const destFilePath = path.join(this.generatePath, filePath);
const destFolderPath = path.dirname(destFilePath)

if (this.options.debug) {
console.debug(chalk.whiteBright(`Processing file: ${destFilePath}`));
private processRewriteRule = async(rule: RewriteRule) => {
const globOptions: IOptions = {
cwd: this.generatePath,
ignore: rule.ignore,
matchBase: true,
nodir: true
};
const patterns = rule.patterns ?? [];
if(patterns.length == 0){
console.warn(chalk.yellowBright(`Skipping Rewrite Rule ${rule.from} => ${rule.to}. No pattern found. Add a pattern of '**/*' to apply this rule to all files.`));
}
for (const pattern of patterns) {
const files = await getGlobFiles(pattern, globOptions);
for (const filePath of files) {
await this.rewritePath(rule, filePath);
}
}
}

const buffer = await fs.readFile(destFilePath);
let contents = buffer.toString('utf8');
private rewritePath = async(rule: RewriteRule, filePath: string) => {
const destFilePath = path.join(this.generatePath, filePath);
const destFolderPath = path.dirname(destFilePath);
const buffer = await fs.readFile(destFilePath);
let contents = buffer.toString('utf8');
if(contents.indexOf(rule.from) == -1) return;

// Replace relative path updates
for (const rule of this.assetRules) {
if (this.options.debug) {
console.debug(chalk.white(`- Processing ${rule.from} => ${rule.to}`));
}
contents = contents.replaceAll(rule.from, rule.to);
}
console.info(chalk.cyan(` -> Rewriting relative paths ${rule.from} => ${rule.to} for file "${filePath}"`));
contents = contents.replaceAll(rule.from, rule.to);

// Normalize transformed paths
const matches = contents.match(pathRegex);
if (matches && matches.length > 0) {
for (const match of matches) {
// Normalize transformed paths
const pathRegex = new RegExp(/((?:\.{1,2}[\/\\]{1,2})+[^'"\s]*)/gm);
const matches = contents.match(pathRegex);
if (matches && matches.length > 0) {
for (const match of matches) {
if(match.indexOf(rule.to) > -1){
// Generate the absolute path to the referenced match
let refPath = path.resolve(destFolderPath, path.normalize(match))
// Generate the relative path between the current processed file dir path & the referenced match path
Expand All @@ -418,12 +424,12 @@ export class GenerateCommand implements RepomanCommand {
contents = contents.replaceAll(match, relativePath);

if (this.options.debug) {
console.log(chalk.grey(` -> Rewriting relative path ${match} => ${relativePath}`));
console.log(chalk.grey(` -> Rewriting relative path ${match} => ${relativePath} in ${destFilePath}`));
}
}
}

await fs.writeFile(destFilePath, contents);
}
await fs.writeFile(destFilePath, contents);

}
}
4 changes: 3 additions & 1 deletion generators/repo/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface RepoManifest {

assets: AssetRule[]
rewrite?: {
patterns: string[]
rules: RewriteRule[]
}
}
}
Expand All @@ -28,6 +28,8 @@ export interface AssetRule {
ignore?: string[]
}

export type RewriteRule = AssetRule;

export interface RepomanCommandOptions {
debug: true
[key: string]: any
Expand Down
23 changes: 15 additions & 8 deletions templates/todo/projects/csharp-cosmos-sql/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/csharp-cosmos-sql
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rule for Path Rewrite Only
- from: ../../../../common/infra/bicep
to: ./
ignore:
- "**/*"

# openapi.yaml to root
- from: ../../api/common
Expand Down
23 changes: 15 additions & 8 deletions templates/todo/projects/csharp-mongo/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/csharp
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rule for Path Rewrite Only
- from: ../../../../common/infra/bicep
to: ./
ignore:
- "**/*"

# openapi.yaml to root
- from: ../../api/common
Expand Down
23 changes: 15 additions & 8 deletions templates/todo/projects/csharp-sql/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/csharp-sql
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rule for Path Rewrite Only
- from: ../../../../common/infra/bicep
to: ./
ignore:
- "**/*"

# openapi.yaml to root
- from: ../../api/common
Expand Down
23 changes: 15 additions & 8 deletions templates/todo/projects/nodejs-mongo-aca/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/js
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rule for Path Rewrite Only
- from: ../../../../../common/infra/bicep
to: ./
ignore:
- "**/*"

# openapi.yaml to root
- from: ../../api/common
Expand Down
23 changes: 15 additions & 8 deletions templates/todo/projects/nodejs-mongo-swa-func/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/js
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rule for Path Rewrite Only
- from: ../../../../common/infra/bicep
to: ./
ignore:
- "**/*"

# openapi.yaml to root
- from: ../../api/common
Expand Down
35 changes: 15 additions & 20 deletions templates/todo/projects/nodejs-mongo/.repo/bicep/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/js
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rules for Path Rewrite Only
- from: ../../../../../../common/infra/bicep
to: ./
ignore:
- ".repo/**/*"

- from: ../../api/js
to: ./src/api
ignore:
- ".repo/**/*"

- from: ../../web/react-fluent
to: ./src/web
ignore:
- ".repo/**/*"
# End Path Rewrite Rules

- from: ./../../
to: ./
ignore:
Expand Down
23 changes: 15 additions & 8 deletions templates/todo/projects/python-mongo-aca/repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ repo:
branch: staging

rewrite:
patterns:
- "**/azure.@(yml|yaml)"
- "**/*.bicep"
rules:
- from: ../../../../../common/infra/bicep
to: ./
patterns:
- "**/*.bicep"

- from: ../../api/python
to: ./src/api
patterns:
- "**/azure.@(yml|yaml)"

- from: ../../web/react-fluent
to: ./src/web
patterns:
- "**/azure.@(yml|yaml)"

assets:
# Common assets
# Rule for Path Rewrite Only
- from: ../../../../../common/infra/bicep
to: ./
ignore:
- "**/*"

# openapi.yaml to root
- from: ../../api/common
Expand Down
Loading