[web] Create CSharp repos

This commit is contained in:
orosmatthew 2023-11-12 18:14:13 -05:00
parent 509c9bcdda
commit 7f2e4c815c
5 changed files with 76 additions and 10 deletions

View File

@ -7,6 +7,52 @@ import { dirname } from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import http from 'isomorphic-git/http/node'; import http from 'isomorphic-git/http/node';
async function addProblemsJava(opts: {
fs: memfs.IFs;
templateDir: string;
dir: string;
contest: { problems: { pascalName: string }[] };
}) {
const template = hostFs
.readFileSync(join(opts.templateDir, 'java/problem/problem.java'))
.toString();
opts.contest.problems.forEach((problem) => {
opts.fs.mkdirSync(join(opts.dir, problem.pascalName));
const filledTemplate = template.replaceAll('%%pascalName%%', problem.pascalName);
opts.fs.writeFileSync(
join(opts.dir, problem.pascalName, `${problem.pascalName}.java`),
filledTemplate
);
});
}
async function addProblemsCSharp(opts: {
fs: memfs.IFs;
templateDir: string;
dir: string;
contest: { problems: { pascalName: string }[] };
}) {
const project = hostFs
.readFileSync(join(opts.templateDir, 'csharp/problem/problem.csproj'))
.toString();
const template = hostFs
.readFileSync(join(opts.templateDir, 'csharp/problem/problem.cs'))
.toString();
opts.contest.problems.forEach((problem) => {
opts.fs.mkdirSync(join(opts.dir, problem.pascalName));
opts.fs.writeFileSync(
join(opts.dir, problem.pascalName, `${problem.pascalName}.csproj`),
project
);
const filledTemplate = template.replaceAll('%%pascalName%%', problem.pascalName);
opts.fs.writeFileSync(
join(opts.dir, problem.pascalName, `${problem.pascalName}.cs`),
filledTemplate
);
});
}
export async function createRepos(contestId: number) { export async function createRepos(contestId: number) {
const vol = new memfs.Volume(); const vol = new memfs.Volume();
const fs = createFsFromVolume(vol); const fs = createFsFromVolume(vol);
@ -22,19 +68,19 @@ export async function createRepos(contestId: number) {
const templateDir = join(dirname(fileURLToPath(import.meta.url)), '../../../../templates'); const templateDir = join(dirname(fileURLToPath(import.meta.url)), '../../../../templates');
const template = hostFs.readFileSync(join(templateDir, 'java/problem/Main.java')).toString();
contest.teams.forEach(async (team) => { contest.teams.forEach(async (team) => {
fs.mkdirSync(team.id.toString(), { recursive: true }); fs.mkdirSync(team.id.toString(), { recursive: true });
await git.init({ fs: fs, bare: false, defaultBranch: 'master', dir: team.id.toString() }); await git.init({ fs: fs, bare: false, defaultBranch: 'master', dir: team.id.toString() });
contest.problems.forEach((problem) => { if (team.language === 'Java') {
fs.mkdirSync(join(team.id.toString(), problem.pascalName)); addProblemsJava({ fs, templateDir, dir: team.id.toString(), contest });
const filledTemplate = template.replaceAll('%%pascalName%%', problem.pascalName); } else if (team.language === 'CSharp') {
fs.writeFileSync( addProblemsCSharp({ fs, templateDir, dir: team.id.toString(), contest });
join(team.id.toString(), problem.pascalName, `${problem.pascalName}.java`), const gitignore = hostFs.readFileSync(join(templateDir, 'csharp/.gitignore')).toString();
filledTemplate fs.writeFileSync(join(team.id.toString(), '.gitignore'), gitignore);
); } else {
}); console.error('Language not supported');
return;
}
await git.add({ fs: fs, dir: team.id.toString(), filepath: '.' }); await git.add({ fs: fs, dir: team.id.toString(), filepath: '.' });
await git.commit({ await git.commit({
fs: fs, fs: fs,

3
web/templates/csharp/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.sln
**/bin
**/obj

View File

@ -0,0 +1,7 @@
public class %%pascalName%%
{
static void Main(string[] args)
{
Console.WriteLine("Hello %%pascalName%%!");
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>