diff --git a/web/src/lib/server/repos.ts b/web/src/lib/server/repos.ts index 1a48e3b..60e987b 100644 --- a/web/src/lib/server/repos.ts +++ b/web/src/lib/server/repos.ts @@ -1,27 +1,25 @@ import { db } from '$lib/server/prisma'; -import hostFs from 'fs-extra'; import memfs, { createFsFromVolume } from 'memfs'; import { join } from 'path'; import git from 'isomorphic-git'; -import { dirname } from 'path'; -import { fileURLToPath } from 'url'; import http from 'isomorphic-git/http/node'; +import { + templateCSharpGitIgnore, + templateCSharpProblem, + templateCSharpProblemProj, + templateJavaProblem +} from './templates'; type OptsAddProblems = { fs: memfs.IFs; - templateDir: string; dir: string; contest: { problems: { pascalName: string }[] }; }; async function addProblemsJava(opts: OptsAddProblems) { - 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); + const filledTemplate = templateJavaProblem.replaceAll('%%pascalName%%', problem.pascalName); opts.fs.writeFileSync( join(opts.dir, problem.pascalName, `${problem.pascalName}.java`), filledTemplate @@ -30,19 +28,13 @@ async function addProblemsJava(opts: OptsAddProblems) { } async function addProblemsCSharp(opts: OptsAddProblems) { - 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 + templateCSharpProblemProj ); - const filledTemplate = template.replaceAll('%%pascalName%%', problem.pascalName); + const filledTemplate = templateCSharpProblem.replaceAll('%%pascalName%%', problem.pascalName); opts.fs.writeFileSync( join(opts.dir, problem.pascalName, `${problem.pascalName}.cs`), filledTemplate @@ -63,17 +55,14 @@ export async function createRepos(contestId: number) { return; } - const templateDir = join(dirname(fileURLToPath(import.meta.url)), '../../../templates'); - contest.teams.forEach(async (team) => { fs.mkdirSync(team.id.toString(), { recursive: true }); await git.init({ fs: fs, bare: false, defaultBranch: 'master', dir: team.id.toString() }); if (team.language === 'Java') { - addProblemsJava({ fs, templateDir, dir: team.id.toString(), contest }); + addProblemsJava({ fs, dir: team.id.toString(), contest }); } else if (team.language === 'CSharp') { - addProblemsCSharp({ fs, templateDir, dir: team.id.toString(), contest }); - const gitignore = hostFs.readFileSync(join(templateDir, 'csharp/.gitignore')).toString(); - fs.writeFileSync(join(team.id.toString(), '.gitignore'), gitignore); + addProblemsCSharp({ fs, dir: team.id.toString(), contest }); + fs.writeFileSync(join(team.id.toString(), '.gitignore'), templateCSharpGitIgnore); } else { console.error('Language not supported'); return; @@ -89,7 +78,9 @@ export async function createRepos(contestId: number) { fs: fs, http, dir: team.id.toString(), - url: `http://localhost:${process.env.GIT_PORT}/${contest.id.toString()}/${team.id.toString()}` + url: `http://localhost:${ + process.env.GIT_PORT ?? 7006 + }/${contest.id.toString()}/${team.id.toString()}` }); }); } diff --git a/web/src/lib/server/templates.ts b/web/src/lib/server/templates.ts new file mode 100644 index 0000000..5f0f0f5 --- /dev/null +++ b/web/src/lib/server/templates.ts @@ -0,0 +1,28 @@ +export const templateJavaProblem = `public class %%pascalName%% { + public static void main(String[] args) { + System.out.println("Hello %%pascalName%%!"); + } +}`; + +export const templateCSharpGitIgnore = `*.sln +**/bin +**/obj`; + +export const templateCSharpProblem = `public class %%pascalName%% +{ + static void Main(string[] args) + { + Console.WriteLine("Hello %%pascalName%%!"); + } +}`; + +export const templateCSharpProblemProj = ` + + + Exe + net7.0 + enable + enable + + +`; diff --git a/web/templates/csharp/.gitignore b/web/templates/csharp/.gitignore deleted file mode 100644 index d2fb573..0000000 --- a/web/templates/csharp/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.sln -**/bin -**/obj \ No newline at end of file diff --git a/web/templates/csharp/problem/problem.cs b/web/templates/csharp/problem/problem.cs deleted file mode 100644 index ab329bc..0000000 --- a/web/templates/csharp/problem/problem.cs +++ /dev/null @@ -1,7 +0,0 @@ -public class %%pascalName%% -{ - static void Main(string[] args) - { - Console.WriteLine("Hello %%pascalName%%!"); - } -} \ No newline at end of file diff --git a/web/templates/csharp/problem/problem.csproj b/web/templates/csharp/problem/problem.csproj deleted file mode 100644 index d832f00..0000000 --- a/web/templates/csharp/problem/problem.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - Exe - net7.0 - enable - enable - - - \ No newline at end of file diff --git a/web/templates/java/problem/problem.java b/web/templates/java/problem/problem.java deleted file mode 100644 index cfac89c..0000000 --- a/web/templates/java/problem/problem.java +++ /dev/null @@ -1,5 +0,0 @@ -public class %%pascalName%% { - public static void main(String[] args) { - System.out.println("Hello %%pascalName%%!"); - } -} \ No newline at end of file