netcore-blueprint
Version:
A custom project blueprint
191 lines (149 loc) • 5.01 kB
JavaScript
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { addProjectReferencesToCsproj, copyRecursive } = require('../lib/replacer');
// =======================
// FIXED TEMPLATE
// =======================
const sourceModuleName = "AiAssistantModule";
const assistantName = "__ASSISTANT_NAME__";
const force = process.argv.includes('--force');
// =======================
// FIND SOLUTION ROOT
// =======================
const solutionDir = process.cwd(); // đang ở root chứa .sln
const solutionFile = fs.readdirSync(solutionDir)
.find(f => f.endsWith('.sln') || f.endsWith('.slnx'));
if (!solutionFile) {
console.error("❌ Could not find .sln file in parent directories.");
process.exit(1);
}
// =======================
// PATHS
// =======================
const templateRoot = path.join(
__dirname,
"..",
"Modules",
sourceModuleName
);
const targetModuleRoot = path.join(
solutionDir,
'Modules',
sourceModuleName
);
const hostApiProjectPath = path.join(
solutionDir,
'Host',
'Host.API',
'Host.API.csproj'
);
// =======================
// GUARDS
// =======================
if (!fs.existsSync(templateRoot)) {
console.error(`❌ Template not found: ${templateRoot}`);
process.exit(1);
}
if (fs.existsSync(targetModuleRoot)) {
if (!force) {
console.error(`❌ Target module already exists: ${targetModuleRoot}`);
console.error(` Use --force to overwrite.`);
process.exit(1);
}
console.log(`🔥 --force detected. Removing existing module: ${targetModuleRoot}`);
fs.rmSync(targetModuleRoot, { recursive: true, force: true });
}
console.log(`📦 Creating assistant module`);
console.log(`📁 Target: ${targetModuleRoot}`);
console.log(`🧩 Solution: ${solutionFile}`);
// =======================
// 1. Create root
// =======================
fs.mkdirSync(targetModuleRoot, { recursive: true });
// =======================
// 2. ENGINE PROJECTS
// =======================
const engineRoot = path.join(targetModuleRoot, "Engine");
fs.mkdirSync(engineRoot, { recursive: true });
const engineProjects = [
`${sourceModuleName}.API`,
`${sourceModuleName}.Core`,
`${sourceModuleName}.Infrastructure`
];
engineProjects.forEach(name => {
console.log(`📦 Creating Engine project: ${name}`);
execSync(`dotnet new classlib -n ${name}`, {
cwd: engineRoot,
stdio: 'inherit'
});
const projectPath = path.join(engineRoot, name);
// remove default file
const defaultFile = path.join(projectPath, 'Class1.cs');
if (fs.existsSync(defaultFile)) {
fs.unlinkSync(defaultFile);
}
// copy template
const templatePath = path.join(templateRoot, "Engine", name);
if (fs.existsSync(templatePath)) {
console.log(`📄 Copying template: ${templatePath}`);
copyRecursive(templatePath, projectPath);
}
});
// =======================
// 3. ASSISTANT PROJECT
// =======================
const assistantRoot = path.join(targetModuleRoot, "Assistants");
fs.mkdirSync(assistantRoot, { recursive: true });
console.log(`📦 Creating Assistant project: ${assistantName}`);
execSync(`dotnet new classlib -n ${assistantName}`, {
cwd: assistantRoot,
stdio: 'inherit'
});
const assistantProjectPath = path.join(assistantRoot, assistantName);
// remove default file
const defaultFile = path.join(assistantProjectPath, 'Class1.cs');
if (fs.existsSync(defaultFile)) {
fs.unlinkSync(defaultFile);
}
// copy template
const assistantTemplate = path.join(
templateRoot,
"Assistants",
assistantName
);
if (fs.existsSync(assistantTemplate)) {
console.log(`📄 Copying assistant template`);
copyRecursive(assistantTemplate, assistantProjectPath);
}
// =======================
// 4. ADD TO SOLUTION
// =======================
const allProjects = [
...engineProjects.map(p => path.join(engineRoot, p, `${p}.csproj`)),
path.join(assistantProjectPath, `${assistantName}.csproj`)
];
allProjects.forEach(csproj => {
console.log(`➕ Adding to solution: ${csproj}`);
execSync(`dotnet sln "${solutionFile}" add "${csproj}"`, {
stdio: 'inherit'
});
});
// =======================
// 5. BUILD
// =======================
console.log('🔄 Restoring...');
execSync(`dotnet restore "${solutionFile}"`, { stdio: 'inherit' });
console.log('🏗️ Building...');
execSync(`dotnet build "${solutionFile}"`, { stdio: 'inherit' });
// =======================
// 6. ADD REFERENCES
// =======================
addProjectReferencesToCsproj(hostApiProjectPath, [
`..\\..\\Modules\\${sourceModuleName}\\Engine\\${sourceModuleName}.API\\${sourceModuleName}.API.csproj`,
`..\\..\\Modules\\${sourceModuleName}\\Engine\\${sourceModuleName}.Core\\${sourceModuleName}.Core.csproj`,
`..\\..\\Modules\\${sourceModuleName}\\Engine\\${sourceModuleName}.Infrastructure\\${sourceModuleName}.Infrastructure.csproj`,
`..\\..\\Modules\\${sourceModuleName}\\Assistants\\${assistantName}\\${assistantName}.csproj`
]);
console.log(`🎉 DONE`);