tacker

a simple web bundler
git clone https://tongong.net/git/tacker.git
Log | Files | Refs | README

commit 13df74dd541c89c3cdc407ff493f1016b22bfd10
parent 9cf4cbe001e215c205b62f4446c0fff35727d756
Author: tongong <tongong@gmx.net>
Date:   Mon, 30 May 2022 17:51:09 +0200

yet another file restructuring & binary module

Diffstat:
MMakefile | 2+-
Abundle_binary.ha | 11+++++++++++
Abundle_css.ha | 6++++++
Abundle_html.ha | 5+++++
Abundle_js.ha | 5+++++
Mmain.ha | 66++++++++++++++++++++----------------------------------------------
6 files changed, 48 insertions(+), 47 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ PREFIX=/usr/local -tacker: +tacker: *.ha hare build -o tacker . clean: diff --git a/bundle_binary.ha b/bundle_binary.ha @@ -0,0 +1,11 @@ +use encoding::base64; +use io; +use os; + +fn tacker_binary(ifile: str, ofile: io::handle) void = { + const enc = base64::newencoder(&base64::url_encoding, ofile); + const ifile = os::open(ifile)!; + defer io::close(ifile)!; + io::copy(&enc, ifile)!; + io::close(&enc)!; +}; diff --git a/bundle_css.ha b/bundle_css.ha @@ -0,0 +1,6 @@ +use fmt; +use io; + +fn tacker_css(ifile: str, ofile: io::handle) void = { + fmt::fprint(ofile, "css is not implemented yet")!; +}; diff --git a/bundle_html.ha b/bundle_html.ha @@ -0,0 +1,5 @@ +use io; + +fn tacker_html(ifile: str, ofile: io::handle) void = { + 0; +}; diff --git a/bundle_js.ha b/bundle_js.ha @@ -0,0 +1,5 @@ +use io; + +fn tacker_js(ifile: str, ofile: io::handle) void = { + 0; +}; diff --git a/main.ha b/main.ha @@ -5,50 +5,6 @@ use io; use os; use strings; -type filetype = enum { - HTML, - JS, - CSS, - BINARY, - UNKNOWN, -}; - -// Bundles all from input linked file and echos the bundle to the output stream. -// ifile: resolved path -fn tacker_write(ifile: str, ofile: io::handle, ft: filetype) void = { - // const data = match (os::open(ifile, fs::flags::RDONLY)) { - // case let data: io::file => - // yield data: io::handle; - // case let data: fs::error => - // fmt::fatalf("file \"{}\" does not exist.", ifile); - // }; - if (ft == filetype::UNKNOWN) { - let slc = strings::runes(ifile); - defer free(slc); - let extstart = lastdotindex(slc); - if (extstart == -1) - fmt::fatalf("file \"{}\" has broken filetype.", ifile); - let ext = runes_to_str(slc[(extstart + 1)..]); - defer free(ext); - static const knownft = [ - ("html", filetype::HTML), - ("js", filetype::JS), - ("css", filetype::CSS), - ]; - for (let i = 0z; i < len(knownft); i += 1) { - if (knownft[i].0 == ext) ft = knownft[i].1; - }; - }; - // TODO - fmt::println(switch (ft) { - case filetype::HTML => yield "html"; - case filetype::JS => yield "js"; - case filetype::CSS => yield "css"; - case filetype::BINARY => yield "bin"; - case filetype::UNKNOWN => yield "unknown"; - })!; -}; - export fn main() void = { const cmd = getopt::parse(os::args, "simple web bundler", @@ -90,6 +46,24 @@ export fn main() void = { defer free(ifile); const defaultfrom = strings::join("", os::getcwd(), "/"); defer free(defaultfrom); - tacker_write(resolve_path(ifile, defaultfrom), ofile, - filetype::UNKNOWN); + const ifile = resolve_path(ifile, defaultfrom); + // has to be uncommented again when the bug is fixed + // https://todo.sr.ht/~sircmpwn/hare/717 + // (because of fmt::fatalf) + // defer free(ifile); + + let slc = strings::runes(ifile); + defer free(slc); + let extstart = lastdotindex(slc); + if (extstart == -1) + fmt::fatalf("file \"{}\" has broken filetype.", ifile); + let ext = runes_to_str(slc[(extstart + 1)..]); + defer free(ext); + + switch (ext) { + case "html" => tacker_html(ifile, ofile); + case "js" => tacker_js(ifile, ofile); + case "css" => tacker_css(ifile, ofile); + case => fmt::fatalf("unknown filetype: \"{}\".", ifile); + }; };