Skip to content

Commit

Permalink
completed basic build functional
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodegress committed Jan 26, 2023
1 parent 12cc15b commit 796ad92
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 24 deletions.
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/normalize-path": "^3.0.0",
"@types/path-browserify": "^1.0.0",
"@typescript-eslint/parser": "^5.48.2",
"chai": "^4.3.7",
Expand All @@ -23,6 +24,7 @@
"typescript": "^4.9.4"
},
"dependencies": {
"normalize-path": "^3.0.0",
"path-browserify": "^1.0.1",
"toml": "^3.0.0"
},
Expand Down
120 changes: 97 additions & 23 deletions src/builder/classic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { none, optional, Optional, some } from "../optional";

import path from "path-browserify";
import pathb from "path-browserify";
import normalizePath from "normalize-path";
import { Builder } from ".";
import { err, ok, Result } from "../result";
import { StandardIni, CommonToml, implCommonToml, commonTomlEveryKey } from "../config";

export function normalize(pat: string): string {
return normalizePath(pathb.normalize(pat));
}

export interface FileLike {
filename: string;
dirname: string;
Expand All @@ -19,14 +24,14 @@ export class PathLike {
}

export enum ClassicSourceFileType {
TOML,
TXT,
PNG,
JPEG,
OGG,
WAV,
MP3,
UNKNOWN_ASSET
TOML = 'TOML',
TXT = 'TXT',
PNG = 'PNG',
JPEG = 'JPEG',
OGG = 'OGG',
WAV = 'WAV',
MP3 = 'MP3',
UNKNOWN_ASSET = 'UNKNOWN_ASSET'
}

export class ClassicSourceFile {
Expand All @@ -35,6 +40,9 @@ export class ClassicSourceFile {

private constructor(type: ClassicSourceFileType, content: CommonToml | string | PathLike) {
this.type = type;
if(content instanceof PathLike) {
content.path = normalize(content.path);
}
this.content = content;
}

Expand All @@ -48,15 +56,15 @@ export class ClassicSourceFile {

static asset(content: PathLike): ClassicSourceFile {
return new ClassicSourceFile((() => {
if(path.extname(content.path) == '.png') {
if(pathb.extname(content.path) == '.png') {
return ClassicSourceFileType.PNG;
} else if(path.extname(content.path) == '.jpg' || path.extname(content.path) == '.jpeg') {
} else if(pathb.extname(content.path) == '.jpg' || pathb.extname(content.path) == '.jpeg') {
return ClassicSourceFileType.JPEG;
} else if(path.extname(content.path) == '.ogg') {
} else if(pathb.extname(content.path) == '.ogg') {
return ClassicSourceFileType.OGG;
} else if(path.extname(content.path) == '.mp3') {
} else if(pathb.extname(content.path) == '.mp3') {
return ClassicSourceFileType.MP3;
} else if(path.extname(content.path) == '.wav') {
} else if(pathb.extname(content.path) == '.wav') {
return ClassicSourceFileType.WAV;
} else {
return ClassicSourceFileType.UNKNOWN_ASSET;
Expand All @@ -73,7 +81,7 @@ export class ClassicSourceFile {
}

isImage(): boolean {
return this.type == ClassicSourceFileType.PNG;
return this.type == ClassicSourceFileType.PNG || this.type == ClassicSourceFileType.JPEG;
}

isSoundOrMusic(): boolean {
Expand Down Expand Up @@ -105,6 +113,39 @@ export class ClassicSourceFile {
}
return this;
}

image(callback: (content: PathLike) => void): ClassicSourceFile {
if(this.isImage()) {
if(this.content instanceof PathLike) {
callback(this.content);
} else {
throw new Error('internal error');
}
}
return this;
}

soundOrMusic(callback: (content: PathLike) => void): ClassicSourceFile {
if(this.isSoundOrMusic()) {
if(this.content instanceof PathLike) {
callback(this.content);
} else {
throw new Error('internal error');
}
}
return this;
}

unknownAsset(callback: (content: PathLike) => void): ClassicSourceFile {
if(this.isUnknownAsset()) {
if(this.content instanceof PathLike) {
callback(this.content);
} else {
throw new Error('internal error');
}
}
return this;
}
}

export class ClassicSource implements FileLike {
Expand All @@ -116,10 +157,11 @@ export class ClassicSource implements FileLike {

private constructor(filename: string, dirname: string, path: string, content: CommonToml | string | PathLike) {
this.filename = filename;
this.dirname = dirname;
this.path = path;
this.dirname = normalize(dirname);
this.path = normalize(path);
this.target = none();
if(content instanceof PathLike) {
content.path = normalize(content.path);
this.sourceFile = ClassicSourceFile.asset(content);
} else if(typeof content == 'object') {
this.sourceFile = ClassicSourceFile.toml(content);
Expand Down Expand Up @@ -197,13 +239,15 @@ export class ClassicTarget implements FileLike {

constructor(filename: string, dirname: string, source: ClassicSource, targetFile: ClassicTargetFile) {
this.filename = filename;
this.dirname = dirname;
this.dirname = normalize(dirname);
source.dirname = normalize(source.dirname);
source.path = normalize(source.path);
this.source = source;
this.targetFile = targetFile;
}

isStandardIni(): boolean {
return path.extname(this.filename) == '.ini';
return pathb.extname(this.filename) == '.ini';
}
}

Expand Down Expand Up @@ -251,9 +295,9 @@ export class ClassicBuilderSync implements Builder<ClassicSource, ClassicTarget>
});
optional(this.context.sources.find((value) => value.path == path)).some((source) => {
source.built((target) => {throw ok({source, target})}).unbuilt(() => {
this.buildSync(path).ok((target) => {throw ok({source, target})}).err((error) => {throw err<{source: ClassicSource;target: ClassicTarget;}, Error>(error)});
this.buildSync(path, starterPath).ok((target) => {throw ok({source, target})}).err((error) => {throw err<{source: ClassicSource;target: ClassicTarget;}, Error>(error)});
});
}).none(() => {throw err(new Error('requirement does not exists'))});
}).none(() => {throw err(new Error(`requirement "${path}" does not exists`))});
throw new Error('unreachable!');
} catch(result) {
if(result instanceof Result<{source: ClassicSource;target: ClassicTarget;}, Error>) {
Expand Down Expand Up @@ -295,7 +339,19 @@ export class ClassicBuilderSync implements Builder<ClassicSource, ClassicTarget>
if(Array.isArray(value)) {
return value.join();
} else {
return value.toString();
let outputValue = value.toString();
if(secMain == 'core' && key == 'copyFrom') {
let requirePath = normalize(value.toString().trim());
if(requirePath.startsWith('/')) {
requirePath = requirePath.replace(/^\//, '');
outputValue = 'ROOT:' + requirePath;
} else {
requirePath = normalize(pathb.join(source.dirname, requirePath));
outputValue = requirePath;
}
this.requireSync(requirePath, starterPath != undefined ? starterPath : source.path).err((error) => {throw err(error)});
}
return outputValue.replace(/\.toml$/, '.ini');
}
})();
});
Expand All @@ -305,8 +361,26 @@ export class ClassicBuilderSync implements Builder<ClassicSource, ClassicTarget>
break;
}
}
const target = new ClassicTarget(source.filename.replace('.toml', '.ini'), source.dirname, source, ClassicTargetFile.ini(ini));
const target = new ClassicTarget(source.filename.replace(/\.toml$/, '.ini'), source.dirname, source, ClassicTargetFile.ini(ini));
source.target = some(target);
this.context.targets.push(target);
throw ok(target);
}).txt((content) => {
const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.txt(content));
source.target = some(target);
this.context.targets.push(target);
throw ok(target);
}).image((content) => {
const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.asset());
this.context.targets.push(target);
throw ok(target);
}).soundOrMusic((content) => {
const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.asset());
this.context.targets.push(target);
throw ok(target);
}).unknownAsset((content) => {
const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.asset());
this.context.targets.push(target);
throw ok(target);
});
}).none(() => {throw err<ClassicTarget, Error>(new Error('source does not exists'))});
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,20 @@ describe('ClassicBuilderSync', () => {
}
})()).to.equal('0');
});
it('requires and puts the target', () => {
const builder = new ClassicBuilderSync('.', 'src', 'dist', [ClassicSource.toml('abc.toml', 'abc', 'abc/abc.toml', { 'core': { 'name': 'az', 'defineUnitMemory': ['string az', 'string ok'] }, 'projectile': {'1': {'x': 0}}})]);
expect(builder.requireSync('abc/abc.toml').isOk()).to.equal(true);
expect(builder.context.targets.find((target) => target.source.path == 'abc/abc.toml') != undefined).to.equal(true);

});
it('requires the correct file path', () => {
const builder = new ClassicBuilderSync('.', 'src', 'dist', [ClassicSource.toml('abc.toml', 'abc', 'abc/abc.toml', { 'core': { 'name': 'az', 'defineUnitMemory': ['string az', 'string ok'] }, 'projectile': {'1': {'x': 0}}})]);
expect(builder.requireSync('abc/abc.toml').isOk()).to.equal(true);
expect(builder.requireSync('abc/abc.toml').unwrap().source.path).to.equal('abc/abc.toml');
});
it('requires when need', () => {
const builder = new ClassicBuilderSync('.', 'src', 'dist', [ClassicSource.toml('abc.toml', 'abc', 'abc/abc.toml', { 'core': { 'name': 'az', 'copyFrom': '/requires/require.toml', 'defineUnitMemory': ['string az', 'string ok'] }, 'projectile': {'1': {'x': 0}}}), ClassicSource.toml('require.toml', 'requires', 'requires/require.toml', { 'core': { 'displayName': 'require' }})]);
expect(builder.buildSync('abc/abc.toml').isOk()).to.equal(true);
expect(builder.context.targets.find((target) => target.source.path == 'requires/require.toml') != undefined).to.equal(true);
});
});

0 comments on commit 796ad92

Please sign in to comment.