Merge pull request #60 from vssio/fix#39
refactor: change the structure of commands
This commit is contained in:
commit
c0150762fb
3 changed files with 102 additions and 77 deletions
|
@ -1,15 +1,12 @@
|
||||||
module commands
|
module commands
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import cli
|
|
||||||
import log
|
import log
|
||||||
import time
|
import time
|
||||||
import markdown
|
import markdown
|
||||||
import internal.template
|
import internal.template
|
||||||
import internal.config
|
import internal.config
|
||||||
|
|
||||||
const default_config = 'config.toml'
|
|
||||||
|
|
||||||
const default_template = 'layouts/index.html'
|
const default_template = 'layouts/index.html'
|
||||||
|
|
||||||
const defautl_static = 'static'
|
const defautl_static = 'static'
|
||||||
|
@ -18,7 +15,7 @@ const default_index = 'index.md'
|
||||||
|
|
||||||
const default_dist = 'dist'
|
const default_dist = 'dist'
|
||||||
|
|
||||||
struct Builder {
|
struct BuildCommand {
|
||||||
mut:
|
mut:
|
||||||
config config.Config
|
config config.Config
|
||||||
logger log.Log
|
logger log.Log
|
||||||
|
@ -28,29 +25,14 @@ mut:
|
||||||
config_map map[string]string
|
config_map map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_builder(logger log.Log) Builder {
|
// new_build_cmd create new build command instance.
|
||||||
return Builder{
|
pub fn new_build_cmd(conf config.Config, logger log.Log) BuildCommand {
|
||||||
|
return BuildCommand{
|
||||||
|
config: conf
|
||||||
logger: logger
|
logger: logger
|
||||||
dist: commands.default_dist
|
dist: commands.default_dist
|
||||||
static_dir: commands.defautl_static
|
static_dir: commands.defautl_static
|
||||||
}
|
config_map: conf.as_map()
|
||||||
}
|
|
||||||
|
|
||||||
// new_build_cmd returns a cli.Command for build command
|
|
||||||
pub fn new_build_cmd() cli.Command {
|
|
||||||
return cli.Command{
|
|
||||||
name: 'build'
|
|
||||||
description: 'build your site'
|
|
||||||
usage: 'vss build'
|
|
||||||
execute: fn (cmd cli.Command) ! {
|
|
||||||
mut logger := log.Log{}
|
|
||||||
logger.set_level(log.Level.info)
|
|
||||||
conf := load_config(commands.default_config)!
|
|
||||||
build(conf, mut logger) or {
|
|
||||||
logger.error(err.msg())
|
|
||||||
println('Build failed')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +71,7 @@ fn check_layout(path string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut b Builder) md2html(md_path string) ! {
|
fn (mut b BuildCommand) md2html(md_path string) ! {
|
||||||
// get html body content from md
|
// get html body content from md
|
||||||
b.logger.info('start md to html: ${md_path}')
|
b.logger.info('start md to html: ${md_path}')
|
||||||
content := get_content(md_path)!
|
content := get_content(md_path)!
|
||||||
|
@ -119,21 +101,15 @@ fn (mut b Builder) md2html(md_path string) ! {
|
||||||
os.write_file(dist_path, html)!
|
os.write_file(dist_path, html)!
|
||||||
}
|
}
|
||||||
|
|
||||||
// load_config loads a toml config file
|
|
||||||
fn load_config(toml_file string) !config.Config {
|
|
||||||
toml_text := os.read_file(toml_file)!
|
|
||||||
return config.load(toml_text)
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy_static copy static files to dist
|
// copy_static copy static files to dist
|
||||||
fn (b Builder) copy_static() ! {
|
fn (b BuildCommand) copy_static() ! {
|
||||||
if os.exists(b.static_dir) {
|
if os.exists(b.static_dir) {
|
||||||
os.cp_all(b.static_dir, b.dist, false)!
|
os.cp_all(b.static_dir, b.dist, false)!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create_dist_dir create build output destination
|
// create_dist_dir create build output destination
|
||||||
fn (mut b Builder) create_dist_dir() ! {
|
fn (mut b BuildCommand) create_dist_dir() ! {
|
||||||
if os.exists(b.dist) {
|
if os.exists(b.dist) {
|
||||||
b.logger.info('re-create dist dir')
|
b.logger.info('re-create dist dir')
|
||||||
os.rmdir_all(b.dist)!
|
os.rmdir_all(b.dist)!
|
||||||
|
@ -144,7 +120,7 @@ fn (mut b Builder) create_dist_dir() ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut b Builder) is_ignore(path string) bool {
|
fn (mut b BuildCommand) is_ignore(path string) bool {
|
||||||
// e.g. README.md
|
// e.g. README.md
|
||||||
file_name := os.file_name(path)
|
file_name := os.file_name(path)
|
||||||
// notify user that build was skipped
|
// notify user that build was skipped
|
||||||
|
@ -154,30 +130,33 @@ fn (mut b Builder) is_ignore(path string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(conf config.Config, mut logger log.Log) ! {
|
fn (mut b BuildCommand) set_base_url(url string) {
|
||||||
|
b.config.base_url = url
|
||||||
|
b.config_map['base_url'] = url
|
||||||
|
}
|
||||||
|
|
||||||
|
// run build command main process
|
||||||
|
pub fn (mut b BuildCommand) run() ! {
|
||||||
println('Start building')
|
println('Start building')
|
||||||
mut sw := time.new_stopwatch()
|
mut sw := time.new_stopwatch()
|
||||||
mut b := new_builder(logger)
|
|
||||||
template_content := os.read_file(commands.default_template)!
|
template_content := os.read_file(commands.default_template)!
|
||||||
b.template_content = template_content
|
b.template_content = template_content
|
||||||
b.config = conf
|
|
||||||
b.config_map = conf.as_map()
|
|
||||||
|
|
||||||
b.create_dist_dir()!
|
b.create_dist_dir()!
|
||||||
// copy static dir files
|
// copy static dir files
|
||||||
logger.info('copy static files')
|
b.logger.info('copy static files')
|
||||||
b.copy_static()!
|
b.copy_static()!
|
||||||
|
|
||||||
mds := normalise_paths(os.walk_ext('.', '.md'))
|
mds := normalise_paths(os.walk_ext('.', '.md'))
|
||||||
logger.info('start md to html')
|
b.logger.info('start md to html')
|
||||||
for path in mds {
|
for path in mds {
|
||||||
if b.is_ignore(path) {
|
if b.is_ignore(path) {
|
||||||
logger.info('${path} is included in ignore_files, skip build')
|
b.logger.info('${path} is included in ignore_files, skip build')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
b.md2html(path)!
|
b.md2html(path)!
|
||||||
}
|
}
|
||||||
logger.info('end md to html')
|
b.logger.info('end md to html')
|
||||||
|
|
||||||
sw.stop()
|
sw.stop()
|
||||||
println('Total in ' + sw.elapsed().milliseconds().str() + ' ms')
|
println('Total in ' + sw.elapsed().milliseconds().str() + ' ms')
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
module commands
|
module commands
|
||||||
|
|
||||||
import cli
|
|
||||||
import log
|
import log
|
||||||
import net.http
|
import net.http
|
||||||
import os
|
import os
|
||||||
|
@ -8,20 +7,19 @@ import internal.config
|
||||||
|
|
||||||
const cport = 8080
|
const cport = 8080
|
||||||
|
|
||||||
// new_serve_cmd returns a cli.Command for serve command
|
struct ServeCommand {
|
||||||
pub fn new_serve_cmd() cli.Command {
|
mut:
|
||||||
return cli.Command{
|
logger log.Log
|
||||||
name: 'serve'
|
port int
|
||||||
description: 'serve dist'
|
buildcmd BuildCommand
|
||||||
usage: 'vss serve'
|
}
|
||||||
execute: fn (cmd cli.Command) ! {
|
|
||||||
mut logger := log.Log{}
|
// new_serve_cmd creates a new ServeCommand instance.
|
||||||
logger.set_level(log.Level.info)
|
pub fn new_serve_cmd(conf config.Config, logger log.Log) ServeCommand {
|
||||||
serve(mut logger) or {
|
return ServeCommand{
|
||||||
logger.error(err.msg())
|
logger: logger
|
||||||
println('serve failed')
|
port: commands.cport
|
||||||
}
|
buildcmd: new_build_cmd(conf, logger)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +72,7 @@ mut:
|
||||||
time_stamp i64
|
time_stamp i64
|
||||||
}
|
}
|
||||||
|
|
||||||
fn watch(path string, conf config.Config, mut logger log.Log) {
|
fn (mut s ServeCommand) watch(path string) {
|
||||||
mut res := []string{}
|
mut res := []string{}
|
||||||
os.walk_with_context(path, &res, fn (mut res []string, fpath string) {
|
os.walk_with_context(path, &res, fn (mut res []string, fpath string) {
|
||||||
res << fpath
|
res << fpath
|
||||||
|
@ -100,8 +98,8 @@ fn watch(path string, conf config.Config, mut logger log.Log) {
|
||||||
println('modified file: ${w.path}')
|
println('modified file: ${w.path}')
|
||||||
w.time_stamp = now
|
w.time_stamp = now
|
||||||
|
|
||||||
build(conf, mut logger) or {
|
s.build() or {
|
||||||
logger.error(err.msg())
|
s.logger.error(err.msg())
|
||||||
println('Build failed')
|
println('Build failed')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,7 +107,12 @@ fn watch(path string, conf config.Config, mut logger log.Log) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serve(mut logger log.Log) ! {
|
fn (mut s ServeCommand) build() ! {
|
||||||
|
s.buildcmd.run()!
|
||||||
|
}
|
||||||
|
|
||||||
|
// run serve command main process.
|
||||||
|
pub fn (mut s ServeCommand) run() ! {
|
||||||
mut handler := MyHttpHandler{
|
mut handler := MyHttpHandler{
|
||||||
root: 'dist'
|
root: 'dist'
|
||||||
}
|
}
|
||||||
|
@ -118,18 +121,18 @@ fn serve(mut logger log.Log) ! {
|
||||||
port: commands.cport
|
port: commands.cport
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// base_url を localhost にする
|
||||||
local_base_url := 'http://localhost:${commands.cport}/'
|
local_base_url := 'http://localhost:${commands.cport}/'
|
||||||
mut conf := load_config(default_config)!
|
s.buildcmd.set_base_url(local_base_url)
|
||||||
conf.base_url = local_base_url
|
|
||||||
println(local_base_url)
|
|
||||||
|
|
||||||
// build before server startup
|
// build
|
||||||
build(conf, mut logger) or {
|
s.build() or {
|
||||||
logger.error(err.msg())
|
s.logger.error(err.msg())
|
||||||
println('Build failed')
|
println('build failed')
|
||||||
}
|
}
|
||||||
|
|
||||||
w := spawn watch('.', conf, mut logger)
|
println(local_base_url)
|
||||||
|
w := spawn s.watch('.')
|
||||||
server.listen_and_serve()
|
server.listen_and_serve()
|
||||||
|
|
||||||
w.wait()
|
w.wait()
|
||||||
|
|
59
main.v
59
main.v
|
@ -2,26 +2,69 @@ module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import cli
|
import cli
|
||||||
|
import log
|
||||||
import commands
|
import commands
|
||||||
|
import internal.config
|
||||||
|
|
||||||
const version = '0.3.0'
|
const version = '0.3.0'
|
||||||
|
|
||||||
fn main() {
|
const default_config = 'config.toml'
|
||||||
mut app := cli.Command{
|
|
||||||
|
// load_config loads a toml config file
|
||||||
|
fn load_config(toml_file string) !config.Config {
|
||||||
|
toml_text := os.read_file(toml_file)!
|
||||||
|
return config.load(toml_text)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_logger() log.Log {
|
||||||
|
return log.Log{
|
||||||
|
level: log.Level.info
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_commands() cli.Command {
|
||||||
|
return cli.Command{
|
||||||
name: 'vss'
|
name: 'vss'
|
||||||
version: version
|
version: version
|
||||||
description: 'static site generator'
|
description: 'static site generator'
|
||||||
execute: fn (cmd cli.Command) ! {
|
execute: fn (cmd cli.Command) ! {
|
||||||
println(cmd.help_message())
|
println(cmd.help_message())
|
||||||
}
|
}
|
||||||
|
commands: [
|
||||||
|
cli.Command{
|
||||||
|
name: 'build'
|
||||||
|
description: 'build your site'
|
||||||
|
usage: 'vss build'
|
||||||
|
execute: fn (cmd cli.Command) ! {
|
||||||
|
mut logger := init_logger()
|
||||||
|
conf := load_config(default_config)!
|
||||||
|
mut c := commands.new_build_cmd(conf, logger)
|
||||||
|
c.run() or {
|
||||||
|
logger.error(err.msg())
|
||||||
|
println('build failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
cli.Command{
|
||||||
|
name: 'serve'
|
||||||
|
description: 'serve dist'
|
||||||
|
usage: 'vss serve'
|
||||||
|
execute: fn (cmd cli.Command) ! {
|
||||||
|
mut logger := init_logger()
|
||||||
|
conf := load_config(default_config)!
|
||||||
|
mut c := commands.new_serve_cmd(conf, logger)
|
||||||
|
c.run() or {
|
||||||
|
logger.error(err.msg())
|
||||||
|
println('serve failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add commands
|
fn main() {
|
||||||
app.add_command(commands.new_build_cmd())
|
mut app := init_commands()
|
||||||
app.add_command(commands.new_serve_cmd())
|
|
||||||
|
|
||||||
app.setup()
|
app.setup()
|
||||||
|
|
||||||
// run the app
|
|
||||||
app.parse(os.args)
|
app.parse(os.args)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue