MCPcopy Create free account
hub / github.com/Tatamo/atcoder-cli / installTaskTemplate

Function installTaskTemplate

src/template.ts:136–194  ·  view source on GitHub ↗
(detailed_task: DetailedTask, paths: { contest: string, task: string }, log: boolean = false)

Source from the content-addressed store, hash-verified

134 * @param log default=false trueなら通常ログを標準出力に表示させる falseの場合はエラーログのみをエラー出力に表示
135 */
136export async function installTaskTemplate(detailed_task: DetailedTask, paths: { contest: string, task: string }, log: boolean = false) {
137 const {task, index, contest, template} = detailed_task;
138 if (template === undefined) throw new Error("no template is given");
139 const task_template = template.task;
140 // 現在のディレクトリを記憶しつつ展開先ディレクトリに移動する
141 const pwd = process.cwd();
142 process.chdir(paths.task);
143 const template_dir = resolve(await getConfigDirectory(), template.name);
144 const fs = await importFsExtra();
145 // プログラムファイルのコピー
146 for (const file of task_template.program) {
147 const source = resolve(template_dir, typeof file === "string" ? file : file[0]);
148 const dest = resolve(process.cwd(), typeof file === "string" ? file : formatTaskDirname(file[1], task, index, contest));
149 try {
150 // ファイルの上書きは行わず、既にファイルが存在する場合はエラーを発生させる
151 await fs.copy(source, dest, {overwrite: false, errorOnExist: true});
152 if (log) console.log(`"${source}" -> "${dest}"`)
153 } catch (e) {
154 // ファイルのコピーを行わなかったことを通知
155 console.error(`Skip: "${source}" -> "${dest}"`);
156 }
157 }
158
159 // 静的ファイルのコピー
160 // 同名ファイルが存在した場合は上書きされる
161 if (task_template.static !== undefined) {
162 for (const file of task_template.static) {
163 const source = resolve(template_dir, typeof file === "string" ? file : file[0]);
164 const dest = resolve(process.cwd(), typeof file === "string" ? file : formatTaskDirname(file[1], task, index, contest));
165 try {
166 await fs.copy(source, dest);
167 } catch (e) {
168 console.error(e.toString());
169 }
170 if (log) console.log(`"${source}" -> "${dest}"`)
171 }
172 }
173
174 // コマンドの実行
175 if (task_template.cmd !== undefined) {
176 if (log) console.log(`Command:\n exec \`${task_template.cmd}\``);
177 // 環境変数としてパラメータを利用可能にする
178 const env = {
179 ...process.env,
180 TEMPLATE_DIR: template_dir,
181 TASK_DIR: paths.task,
182 TASK_ID: task.id,
183 TASK_INDEX: index.toString(),
184 CONTEST_DIR: paths.contest,
185 CONTEST_ID: contest.id
186 };
187 const {stdout, stderr} = await promisify(child_process.exec)(task_template.cmd, {env});
188 if (log && stdout !== "") console.log(stdout);
189 if (stderr !== "") console.error(stderr);
190 }
191
192 // もとのディレクトリに戻る
193 process.chdir(pwd);

Callers 1

installTaskFunction · 0.90

Calls 3

getConfigDirectoryFunction · 0.90
importFsExtraFunction · 0.90
formatTaskDirnameFunction · 0.90

Tested by

no test coverage detected