Skip to content

Commit

Permalink
refactor: サイトと言語の抽象化
Browse files Browse the repository at this point in the history
  • Loading branch information
taizod1024 committed Oct 15, 2021
1 parent 39edd67 commit 9f8a3ad
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 39 deletions.
59 changes: 24 additions & 35 deletions src/AcTsExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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 ----`);
}
Expand Down Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/AtCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 = /^(.+)$/;
Expand All @@ -42,6 +48,10 @@ class AtCoder implements Coder {

}

isCoder():boolean {
return actsextension.site === "atcoder";
}

checkLogin() {

if (!this.username || !this.password) {
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +25,10 @@ class Python implements Lang {
}
}

isLang():boolean {
return actsextension.extension === ".py";
}

testLang(debug: boolean): any {
let child = null;
if (debug) {
Expand Down
11 changes: 11 additions & 0 deletions src/TypeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions src/Yukicoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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]+$/;
Expand All @@ -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) {
Expand Down Expand Up @@ -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 "";
}

Expand Down

0 comments on commit 9f8a3ad

Please sign in to comment.