Skip to content

Commit

Permalink
completed basic toml build
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodegress committed Jan 25, 2023
1 parent fd14f5b commit 12cc15b
Show file tree
Hide file tree
Showing 10 changed files with 748 additions and 396 deletions.
41 changes: 23 additions & 18 deletions lib/builder/classic.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Optional } from "../optional";
import { Builder } from ".";
import { Result } from "../result";
import { Ini, Toml } from "../config";
import { StandardIni, CommonToml } from "../config";
export interface FileLike {
filename: string;
dirname: string;
Expand All @@ -14,21 +14,26 @@ export declare enum ClassicSourceFileType {
TOML = 0,
TXT = 1,
PNG = 2,
OGG = 3,
WAV = 4,
MP3 = 5,
UNKNOWN_ASSET = 6
JPEG = 3,
OGG = 4,
WAV = 5,
MP3 = 6,
UNKNOWN_ASSET = 7
}
export declare class ClassicSourceFile {
private type;
private content;
private constructor();
static toml(content: Toml): ClassicSourceFile;
static toml(content: CommonToml): ClassicSourceFile;
static txt(content: string): ClassicSourceFile;
static asset(content: PathLike): ClassicSourceFile;
isToml(): boolean;
isCommonToml(): boolean;
isTxt(): boolean;
toml(callback: (content: Toml) => void): void;
isImage(): boolean;
isSoundOrMusic(): boolean;
isUnknownAsset(): boolean;
toml(callback: (content: CommonToml) => void): ClassicSourceFile;
txt(callback: (content: string) => void): ClassicSourceFile;
}
export declare class ClassicSource implements FileLike {
filename: string;
Expand All @@ -37,26 +42,26 @@ export declare class ClassicSource implements FileLike {
sourceFile: ClassicSourceFile;
target: Optional<ClassicTarget>;
private constructor();
static toml(filename: string, dirname: string, path: string, content: Toml): ClassicSource;
static toml(filename: string, dirname: string, path: string, content: CommonToml): ClassicSource;
static txt(filename: string, dirname: string, path: string, content: string): ClassicSource;
static pathLike(filename: string, dirname: string, path: string, content: PathLike): ClassicSource;
isToml(): boolean;
isCommonToml(): boolean;
isBuilt(): boolean;
built(callback: (target: ClassicTarget) => void): ClassicSource;
unbuilt(callback: () => void): ClassicSource;
}
export declare enum ClassicTargetFileType {
INI = 0,
TXT = 1,
ASSET = 2
INI = "INI",
TXT = "TXT",
ASSET = "ASSET"
}
export declare class CopyFromSource {
}
export declare class ClassicTargetFile {
type: ClassicTargetFileType;
content: Ini | string | CopyFromSource;
content: StandardIni | string | CopyFromSource;
private constructor();
static ini(content: Ini): ClassicTargetFile;
static ini(content: StandardIni): ClassicTargetFile;
static txt(content: string): ClassicTargetFile;
static asset(): ClassicTargetFile;
}
Expand All @@ -66,7 +71,7 @@ export declare class ClassicTarget implements FileLike {
source: ClassicSource;
targetFile: ClassicTargetFile;
constructor(filename: string, dirname: string, source: ClassicSource, targetFile: ClassicTargetFile);
isIni(): boolean;
isStandardIni(): boolean;
}
export declare class ClassicBuilderSync implements Builder<ClassicSource, ClassicTarget> {
context: {
Expand All @@ -83,10 +88,10 @@ export declare class ClassicBuilderSync implements Builder<ClassicSource, Classi
}>;
build(path: string): Promise<ClassicTarget>;
buildAll(): Promise<ClassicTarget[]>;
requireSync(path: string): Result<{
requireSync(path: string, starterPath?: string): Result<{
source: ClassicSource;
target: ClassicTarget;
}, Error>;
buildSync(path: string): Result<ClassicTarget, Error>;
buildSync(path: string, starterPath?: string): Result<ClassicTarget, Error>;
buildAllSync(): Result<ClassicTarget[], Error>;
}
173 changes: 132 additions & 41 deletions lib/builder/classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ var ClassicSourceFileType;
ClassicSourceFileType[ClassicSourceFileType["TOML"] = 0] = "TOML";
ClassicSourceFileType[ClassicSourceFileType["TXT"] = 1] = "TXT";
ClassicSourceFileType[ClassicSourceFileType["PNG"] = 2] = "PNG";
ClassicSourceFileType[ClassicSourceFileType["OGG"] = 3] = "OGG";
ClassicSourceFileType[ClassicSourceFileType["WAV"] = 4] = "WAV";
ClassicSourceFileType[ClassicSourceFileType["MP3"] = 5] = "MP3";
ClassicSourceFileType[ClassicSourceFileType["UNKNOWN_ASSET"] = 6] = "UNKNOWN_ASSET";
ClassicSourceFileType[ClassicSourceFileType["JPEG"] = 3] = "JPEG";
ClassicSourceFileType[ClassicSourceFileType["OGG"] = 4] = "OGG";
ClassicSourceFileType[ClassicSourceFileType["WAV"] = 5] = "WAV";
ClassicSourceFileType[ClassicSourceFileType["MP3"] = 6] = "MP3";
ClassicSourceFileType[ClassicSourceFileType["UNKNOWN_ASSET"] = 7] = "UNKNOWN_ASSET";
})(ClassicSourceFileType = exports.ClassicSourceFileType || (exports.ClassicSourceFileType = {}));
class ClassicSourceFile {
constructor(type, content) {
Expand All @@ -40,6 +41,9 @@ class ClassicSourceFile {
if (path_browserify_1.default.extname(content.path) == '.png') {
return ClassicSourceFileType.PNG;
}
else if (path_browserify_1.default.extname(content.path) == '.jpg' || path_browserify_1.default.extname(content.path) == '.jpeg') {
return ClassicSourceFileType.JPEG;
}
else if (path_browserify_1.default.extname(content.path) == '.ogg') {
return ClassicSourceFileType.OGG;
}
Expand All @@ -54,16 +58,42 @@ class ClassicSourceFile {
}
})(), content);
}
isToml() {
isCommonToml() {
return this.type == ClassicSourceFileType.TOML;
}
isTxt() {
return this.type == ClassicSourceFileType.TXT;
}
isImage() {
return this.type == ClassicSourceFileType.PNG;
}
isSoundOrMusic() {
return this.type == ClassicSourceFileType.OGG || this.type == ClassicSourceFileType.MP3 || this.type == ClassicSourceFileType.WAV;
}
isUnknownAsset() {
return this.type == ClassicSourceFileType.UNKNOWN_ASSET;
}
toml(callback) {
if (this.isToml() && this.content instanceof config_1.Toml) {
callback(this.content);
if (this.isCommonToml()) {
if ((0, config_1.implCommonToml)(this.content)) {
callback(this.content);
}
else {
throw new Error('internal error');
}
}
return this;
}
txt(callback) {
if (this.isTxt()) {
if (typeof this.content == 'string') {
callback(this.content);
}
else {
throw new Error('internal error');
}
}
return this;
}
}
exports.ClassicSourceFile = ClassicSourceFile;
Expand Down Expand Up @@ -92,8 +122,8 @@ class ClassicSource {
static pathLike(filename, dirname, path, content) {
return new ClassicSource(filename, dirname, path, content);
}
isToml() {
return this.sourceFile.isToml();
isCommonToml() {
return this.sourceFile.isCommonToml();
}
isBuilt() {
return this.target != undefined;
Expand All @@ -110,9 +140,9 @@ class ClassicSource {
exports.ClassicSource = ClassicSource;
var ClassicTargetFileType;
(function (ClassicTargetFileType) {
ClassicTargetFileType[ClassicTargetFileType["INI"] = 0] = "INI";
ClassicTargetFileType[ClassicTargetFileType["TXT"] = 1] = "TXT";
ClassicTargetFileType[ClassicTargetFileType["ASSET"] = 2] = "ASSET";
ClassicTargetFileType["INI"] = "INI";
ClassicTargetFileType["TXT"] = "TXT";
ClassicTargetFileType["ASSET"] = "ASSET";
})(ClassicTargetFileType = exports.ClassicTargetFileType || (exports.ClassicTargetFileType = {}));
class CopyFromSource {
}
Expand Down Expand Up @@ -140,7 +170,7 @@ class ClassicTarget {
this.source = source;
this.targetFile = targetFile;
}
isIni() {
isStandardIni() {
return path_browserify_1.default.extname(this.filename) == '.ini';
}
}
Expand Down Expand Up @@ -170,45 +200,106 @@ class ClassicBuilderSync {
this.buildAllSync().ok((value) => resolve(value)).err((value) => reject(value));
});
}
requireSync(path) {
let { source, target } = { source: (0, optional_1.none)(), target: (0, optional_1.none)() };
let error = (0, optional_1.none)();
(0, optional_1.optional)(this.context.sources.find((value) => value.path == path)).some((value) => {
source = (0, optional_1.some)(value);
value.built((value) => target = (0, optional_1.some)(value)).unbuilt(() => {
this.buildSync(path).ok((result) => target = (0, optional_1.some)(result)).err((result) => error = (0, optional_1.some)(result));
requireSync(path, starterPath) {
try {
(0, optional_1.optional)(starterPath).some((starterPath) => {
if (starterPath == path) {
throw (0, result_1.err)(new Error(`circle require ${starterPath} by ${path}`));
}
});
}).none(() => error = (0, optional_1.some)(new Error('requirement does not exists')));
if (error.isSome()) {
return (0, result_1.err)(error.unwrap());
(0, optional_1.optional)(this.context.sources.find((value) => value.path == path)).some((source) => {
source.built((target) => { throw (0, result_1.ok)({ source, target }); }).unbuilt(() => {
this.buildSync(path).ok((target) => { throw (0, result_1.ok)({ source, target }); }).err((error) => { throw (0, result_1.err)(error); });
});
}).none(() => { throw (0, result_1.err)(new Error('requirement does not exists')); });
throw new Error('unreachable!');
}
else {
return (0, result_1.ok)({ source: source.unwrap(), target: target.unwrap() });
catch (result) {
if (result instanceof (result_1.Result)) {
return result;
}
else {
throw result;
}
}
}
buildSync(path) {
let target = (0, optional_1.none)();
let error = (0, optional_1.none)();
(0, optional_1.optional)(this.context.sources.find((value) => value.path == path)).some((source) => {
source.built((value) => target = (0, optional_1.some)(value)).unbuilt(() => {
});
}).none(() => error = (0, optional_1.some)(new Error('source does not exists')));
if (error.isSome()) {
return (0, result_1.err)(error.unwrap());
buildSync(path, starterPath) {
try {
(0, optional_1.optional)(this.context.sources.find((value) => value.path == path)).some((source) => {
source.sourceFile.toml((toml) => {
let ini = {};
const gen = (0, config_1.commonTomlEveryKey)(toml);
while (true) {
const { value, done } = gen.next();
if (!done) {
value.some(({ secMain, secSub, key, value }) => {
secSub.some((secSub) => {
(0, optional_1.optional)(ini[`${secMain}_${secSub}`]).none(() => {
ini[`${secMain}_${secSub}`] = {};
});
(0, optional_1.optional)(ini[`${secMain}_${secSub}`]).some((section) => {
section[key] = (() => {
if (Array.isArray(value)) {
return value.join();
}
else {
return value.toString();
}
})();
});
}).none(() => {
(0, optional_1.optional)(ini[secMain]).none(() => {
ini[secMain] = {};
});
(0, optional_1.optional)(ini[secMain]).some((section) => {
section[key] = (() => {
if (Array.isArray(value)) {
return value.join();
}
else {
return value.toString();
}
})();
});
});
});
}
else {
break;
}
}
const target = new ClassicTarget(source.filename.replace('.toml', '.ini'), source.dirname, source, ClassicTargetFile.ini(ini));
source.target = (0, optional_1.some)(target);
throw (0, result_1.ok)(target);
});
}).none(() => { throw (0, result_1.err)(new Error('source does not exists')); });
throw new Error('unreachable!');
}
else {
return (0, result_1.ok)(target.unwrap());
catch (result) {
if (result instanceof (result_1.Result)) {
return result;
}
else {
throw result;
}
}
}
buildAllSync() {
let error = (0, optional_1.none)();
for (const source of this.context.sources) {
const result = this.buildSync(source.path).err((result) => error = (0, optional_1.some)(result));
if (result.isErr()) {
return (0, result_1.err)(error.unwrap());
try {
for (const source of this.context.sources) {
const result = this.buildSync(source.path).err((error) => { throw (0, result_1.err)(error); });
}
throw (0, result_1.ok)(this.context.targets);
}
catch (result) {
if (result instanceof (result_1.Result)) {
return result;
}
else {
throw result;
}
}
return (0, result_1.ok)(this.context.targets);
}
}
exports.ClassicBuilderSync = ClassicBuilderSync;
30 changes: 23 additions & 7 deletions lib/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import { Result } from "./result";
export declare class Ini {
import { Optional } from "./optional";
export interface StandardIni {
[key: string]: {
[key: string]: string;
};
constructor(obj: Object);
static from(obj: Object): Result<Ini>;
}
export declare class Toml {
export declare function implStandardIni(obj: any): obj is StandardIni;
export interface ClassicToml {
[key: string]: {
[key: string]: string;
};
}
export declare function implClassicToml(obj: any): obj is ClassicToml;
export interface CommonToml {
[key: string]: {
[key: string]: number | string | boolean | string[] | number[] | boolean[];
} | {
[key: string]: {
[key: string]: number | string | boolean | string[] | number[] | boolean[];
};
};
constructor(obj: Object);
static from(obj: Object): Result<Toml>;
}
export declare function arrayType(arr: Array<any>): Optional<'string' | 'number' | 'boolean'>;
export declare function implCommonToml(obj: any): obj is CommonToml;
export declare function commonTomlEveryKey(toml: CommonToml): Generator<Optional<{
secMain: string;
secSub: Optional<string>;
key: string;
value: string | number | boolean | string[] | number[] | boolean[];
}>, Optional<{
secMain: string;
secSub: Optional<string>;
key: string;
value: string | number | boolean | string[] | number[] | boolean[];
}>, unknown>;
Loading

0 comments on commit 12cc15b

Please sign in to comment.