From 9f8a3ad6cb044b3c3d4df5c6d21b5b90e5c7863e Mon Sep 17 00:00:00 2001 From: "yamamoto.taizo" Date: Fri, 15 Oct 2021 19:21:06 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E3=82=B5=E3=82=A4=E3=83=88?= =?UTF-8?q?=E3=81=A8=E8=A8=80=E8=AA=9E=E3=81=AE=E6=8A=BD=E8=B1=A1=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AcTsExtension.ts | 59 ++++++++++++++++++-------------------------- src/AtCoder.ts | 14 +++++++++-- src/Python.ts | 10 ++++++++ src/TypeScript.ts | 11 +++++++++ src/Yukicoder.ts | 14 +++++++++-- 5 files changed, 69 insertions(+), 39 deletions(-) diff --git a/src/AcTsExtension.ts b/src/AcTsExtension.ts index ac44880..7906504 100644 --- a/src/AcTsExtension.ts +++ b/src/AcTsExtension.ts @@ -12,12 +12,14 @@ import { python } from './Python'; export interface Coder { // prop + name: string; contestregexp: RegExp; contestmessage: string; taskregexp: RegExp; taskmessage?: string; // method + isCoder(): boolean; checkLogin(): void; initProp(withtask: boolean): void; loginSite(): void; @@ -31,7 +33,12 @@ export interface Coder { // python / typescript export interface Lang { + // prop + name: string; + extension: string; + // method + isLang(): boolean; checkLang(): void; testLang(debug: boolean): any; }; @@ -43,8 +50,6 @@ class AcTsExtension { public appname: string; public appid: string; public configfile: string; - public sites: string[]; - public extensions: string[]; // context public vscodeextensionpath: string; @@ -59,8 +64,12 @@ class AcTsExtension { public extension: string; // prop + public coders: Coder[]; public coder: Coder; + public langs: Lang[]; public lang: Lang; + public sites: string[]; + public extensions: string[]; public tasktmplfile: string; public usertasktmplfile: string; public taskpath: string; @@ -87,11 +96,13 @@ class AcTsExtension { this.appid = "ac-ts-extension"; this.configfile = `${process.env.USERPROFILE}\\.${this.appid}.json`; - // site specific - this.sites = ["atcoder", "yukicoder"]; + // coders and sites + this.coders = [atcoder, yukicoder]; + this.sites = this.coders.map(val => val.name);; - // lang specific - this.extensions = [".ts", ".py"]; + // langs and extensions + this.langs = [typescript, python]; + this.extensions = this.langs.map(val => val.extension); // init context this.channel = vscode.window.createOutputChannel(this.appname); @@ -126,12 +137,8 @@ class AcTsExtension { this.timeout = 5000; // site specific - if (this.isAtcoder()) { this.coder = atcoder; } - if (this.isYukicoder()) { this.coder = yukicoder; } - - // lang specific - if (this.isTypeScript()) { this.lang = typescript; } - if (this.isPython()) { this.lang = python; } + this.coder = this.coders.find(val => val.isCoder()); + this.lang = this.langs.find(val => val.isLang()); // check and init coder this.coder.checkLogin(); @@ -153,7 +160,7 @@ class AcTsExtension { await this.initProp(false); // login site - this.coder.loginSite(); + await this.coder.loginSite(); actsextension.channel.appendLine(`---- SUCCESS: ${this.site} done ----`); } @@ -463,35 +470,17 @@ class AcTsExtension { this.contest = json.contest || ""; this.task = json.task || ""; this.extension = json.extension; - atcoder.loadConfig(json); - yukicoder.loadConfig(json); + this.coders.forEach(val => val.loadConfig(json)); } public saveConfig() { - const app = { + const json = { site: this.site, contest: this.contest, task: this.task, extension: this.extension }; - atcoder.saveConfig(app); - yukicoder.saveConfig(app); - fs.writeFileSync(this.configfile, JSON.stringify(app)); - } - - // site specific - public isAtcoder(): boolean { - return this.site === "atcoder"; - } - public isYukicoder(): boolean { - return this.site === "yukicoder"; - } - - // lang specific - public isTypeScript(): boolean { - return this.extension === ".ts"; - } - public isPython(): boolean { - return this.extension === ".py"; + this.coders.forEach(val => val.saveConfig(json)); + fs.writeFileSync(this.configfile, JSON.stringify(json)); } // message diff --git a/src/AtCoder.ts b/src/AtCoder.ts index dffcb3a..7c8dead 100644 --- a/src/AtCoder.ts +++ b/src/AtCoder.ts @@ -3,6 +3,8 @@ import * as fs from "fs"; import superagent from "superagent"; import * as cheerio from "cheerio"; import { actsextension, Coder } from './AcTsExtension'; +import { typescript } from './TypeScript'; +import { python } from './python'; class AtCoder implements Coder { @@ -17,11 +19,15 @@ class AtCoder implements Coder { submissionsurl: string; // implements + + // prop + name = "atcoder"; contestregexp: RegExp; contestmessage: string; taskregexp: RegExp; taskmessage?: string; + // method constructor() { this.contestregexp = /^(.+)$/; @@ -42,6 +48,10 @@ class AtCoder implements Coder { } + isCoder():boolean { + return actsextension.site === "atcoder"; + } + checkLogin() { if (!this.username || !this.password) { @@ -211,8 +221,8 @@ class AtCoder implements Coder { } getLanguageId(): number { - if (actsextension.isTypeScript()) { return 4057; } - if (actsextension.isPython()) { return 4006; } + if (typescript.isLang()) { return 4057; } + if (python.isLang()) { return 4006; } return 0; } diff --git a/src/Python.ts b/src/Python.ts index 8f8274c..0589717 100644 --- a/src/Python.ts +++ b/src/Python.ts @@ -6,6 +6,12 @@ import { actsextension, Lang } from './AcTsExtension'; class Python implements Lang { // implements + + // prop + name = "python"; + extension = ".py"; + + // method checkLang(): void { // throw if non-zero returned @@ -19,6 +25,10 @@ class Python implements Lang { } } + isLang():boolean { + return actsextension.extension === ".py"; + } + testLang(debug: boolean): any { let child = null; if (debug) { diff --git a/src/TypeScript.ts b/src/TypeScript.ts index a8842f2..288dce1 100644 --- a/src/TypeScript.ts +++ b/src/TypeScript.ts @@ -5,12 +5,23 @@ import { actsextension, Lang } from './AcTsExtension'; class TypeScript implements Lang { + // implemente + + // prop + name = "typescript"; + extension = ".ts"; + + // method checkLang(): void { if (!fs.existsSync(actsextension.packagejsonfile) || !fs.existsSync(actsextension.packagelockjsonfile)) { throw `ERROR: missing package.json or package-lock.json, install node.js, run "npm init && npm install --save-dev typescript ts-node @types/node"`; } } + isLang():boolean { + return actsextension.extension === ".ts"; + } + testLang(debug: boolean): any { let child = null; if (debug) { diff --git a/src/Yukicoder.ts b/src/Yukicoder.ts index 95dd9b4..f72d3dc 100644 --- a/src/Yukicoder.ts +++ b/src/Yukicoder.ts @@ -2,6 +2,8 @@ import * as vscode from 'vscode'; import * as fs from "fs"; import superagent from "superagent"; import { actsextension, Coder } from './AcTsExtension'; +import { typescript } from './TypeScript'; +import { python } from './python'; class Yukicoder implements Coder { @@ -17,11 +19,15 @@ class Yukicoder implements Coder { submissionsurl: string; // implements + + // prop + name = "yukicoder"; contestregexp: RegExp; contestmessage: string; taskregexp: RegExp; taskmessage?: string; + // method constructor() { this.contestregexp = /^[0-9]+$/; @@ -30,6 +36,10 @@ class Yukicoder implements Coder { this.taskmessage = "input problemno from url [e.g.: 1680, 1681]"; } + isCoder():boolean { + return actsextension.site === "yukicoder"; + } + async initProp(withtask: boolean) { if (withtask) { @@ -143,8 +153,8 @@ class Yukicoder implements Coder { } getLanguage(): string { - if (actsextension.isTypeScript()) { return "typescript"; } - if (actsextension.isPython()) { return "python3"; } + if (typescript.isLang()) { return "typescript"; } + if (python.isLang()) { return "python3"; } return ""; }