Skip to content

Commit

Permalink
refactor(core): split functions from main to core
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 19, 2023
1 parent 0c62d3c commit 824879a
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 105 deletions.
13 changes: 11 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ func ParseCli() Arguments {
}
}

// prints the help with all options available
// prints a help page with only the usage
func PrintShortHelp() {
fmt.Println(`Usage:
fleck [Options] file
Run 'fleck --help' for an in depth help page
`)
}

// prints the help with all options available
func PrintLongHelp() {
fmt.Println(`Usage:
fleck [Options] file
Options:`)
for _, v := range OPTIONS {
fmt.Printf("\t--%-20s\t\t%s\n", v.Name, v.Description)
}
fmt.Println("")
fmt.Println("\nOnline documentation: https://github.com/xnacly/fleck")
}

func PrintVersion(version, buildAt, buildBy string) {
Expand Down
106 changes: 106 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package core

import (
"fmt"
"os"
"time"

"github.com/xnacly/fleck/cli"
"github.com/xnacly/fleck/generator"
"github.com/xnacly/fleck/logger"
"github.com/xnacly/fleck/parser"
"github.com/xnacly/fleck/preprocessor"
"github.com/xnacly/fleck/scanner"
)

// alerts the user if a flag depends on a different flag to have an effect
func FlagCombinationSensible() {
for _, f := range cli.OPTIONS {
if len(f.Requires) == 0 {
continue
}
if cli.GetFlag(cli.ARGUMENTS, f.Name) && !cli.GetFlag(cli.ARGUMENTS, f.Requires) {
logger.LWarn(fmt.Sprintf("flag '--%s' requires flag '--%s' to be set, otherwise it has no effect.", f.Name, f.Requires))
}
}
}

// watches for changes in a file, recompiles the file if a change occurs, can be exited via <C-c>
func WatchForChanges(fileName string, executor func(string)) {
executor(fileName)
logger.LInfo("watching for changes...")

initialStat, err := os.Stat(fileName)
if err != nil {
logger.LError("failed to watch for changes: " + err.Error())
}

for {
stat, err := os.Stat(fileName)
if err != nil {
logger.L("test")
logger.LError("failed to watch for changes: " + err.Error())
}

if stat.Size() != initialStat.Size() || stat.ModTime() != stat.ModTime() {
initialStat = stat
logger.LInfo("detected change, recompiling...")
executor(fileName)
}

time.Sleep(1 * time.Second)
}
}

func Run(fileName string) {
start := time.Now()

if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") {
logger.LInfo("preprocessor enabled, starting...")
preprocessor.Process(cli.ARGUMENTS, fileName)
fileName = fileName + ".fleck"
}

logger.LDebug("starting scanner")
lexerStart := time.Now()
s := scanner.New(fileName)
tokens := s.Lex()
logger.LDebug("lexed " + fmt.Sprint(len(tokens)) + " token, took " + time.Since(lexerStart).String())
if logger.DEBUG {
s.PrintTokens()
}

logger.LDebug("starting parser")
parserStart := time.Now()
p := parser.New(tokens)
result := p.Parse()
logger.LDebug("parsed " + fmt.Sprint(len(result)) + " items, took " + time.Since(parserStart).String())
logger.LDebug("parsed tags:", result)

var toc string
if cli.GetFlag(cli.ARGUMENTS, "toc") {
logger.LDebug("generating toc...")
toc = p.GenerateToc()
}

if cli.GetFlag(cli.ARGUMENTS, "no-template") {
generator.WritePlain(fileName, result, toc)
} else {
generator.WriteTemplate(fileName, result, toc)
}

logger.LInfo("compiled '" + fileName + "', took: " + time.Since(start).String())

defer func() {
if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") {
if cli.GetFlag(cli.ARGUMENTS, "keep-temp") {
return
}
logger.LDebug("cleanup, removing: '" + fileName + "'")
err := os.Remove(fileName)
if err != nil {
logger.LWarn(err.Error())
}
}
}()
}
1 change: 1 addition & 0 deletions doc/Builds.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go build -tags=bare .
The `bare` build does not support:

- cli options and flags
- watch mode and live preview
- colored output
- extensive logs and time stamps for compilation steps
- templates
Expand Down
1 change: 1 addition & 0 deletions doc/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Fleck implements the basic markdown features everyone needs, while extending mar
- Fleck escapes html characters and does therefore not support inline html.
- Fleck minifies the output html by default
- Fleck provides a default template for the compiled html to be written to
- Fleck allows the user to view their markdown via a live preview, which recompiles after changes and reloads the page
11 changes: 10 additions & 1 deletion doc/Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ The `cli` modules main objective is to handle parsing of command line arguments

It contains the `Arguments` structure, which in itself contains all the flags and arguments fleck can receive and the `ParseCli` function which returns the structure.

`cli` also contains the function which is used to print the help message (`PrintShortHelp`).
`cli` also contains the functions used to print the help message (`PrintShortHelp` & `PrintLongHelp`).

### core

The `core` module contains functions necessary for flecks execution, such as the check if the provided flags are sensible and no flag is set which requires an other flag to have an effect on the output.
It also contains the `Run` function and the `Watch` function, which are both used to wrap the calls to the preprocessor, lexer, parser and generator.

### doc

The doc directory contains all written documentation for fleck, such as [architecture](./Architecture.md) and [structure](#)

### generator

The generator module includes the default template fleck will write its output to and is responsible for setting the outputs document title, styling and content.

### logger

The `logger` module contains logging helpers with colors.
Expand Down
110 changes: 8 additions & 102 deletions fleck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,131 +3,37 @@
package main

import (
"fmt"
"os"
"time"

"github.com/xnacly/fleck/cli"
"github.com/xnacly/fleck/generator"
"github.com/xnacly/fleck/core"
"github.com/xnacly/fleck/logger"
"github.com/xnacly/fleck/parser"
"github.com/xnacly/fleck/preprocessor"
"github.com/xnacly/fleck/scanner"
)

// supplied by the build process
var VERSION = ""
var BUILD_AT = ""
var BUILD_BY = ""

// alerts the user if a flag depends on a different flag to have an effect
func flagCombinationSensible() {
for _, f := range cli.OPTIONS {
if len(f.Requires) == 0 {
continue
}
if cli.GetFlag(cli.ARGUMENTS, f.Name) && !cli.GetFlag(cli.ARGUMENTS, f.Requires) {
logger.LWarn(fmt.Sprintf("flag '--%s' requires flag '--%s' to be set, otherwise it has no effect.", f.Name, f.Requires))
}
}
}

// watches for changes in a file, recompiles the file if a change occurs, can be exited via <C-c>
func watchForChanges(fileName string) {
run(fileName)
logger.LInfo("watching for changes...")

initialStat, err := os.Stat(fileName)
if err != nil {
logger.LError("failed to watch for changes: " + err.Error())
}

for {
stat, err := os.Stat(fileName)
if err != nil {
logger.L("test")
logger.LError("failed to watch for changes: " + err.Error())
}

if stat.Size() != initialStat.Size() || stat.ModTime() != stat.ModTime() {
initialStat = stat
logger.LInfo("detected change, recompiling...")
run(fileName)
}

time.Sleep(1 * time.Second)
}
}

func run(fileName string) {
start := time.Now()

if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") {
logger.LInfo("preprocessor enabled, starting...")
preprocessor.Process(cli.ARGUMENTS, fileName)
fileName = fileName + ".fleck"
}

logger.LDebug("starting scanner")
lexerStart := time.Now()
s := scanner.New(fileName)
tokens := s.Lex()
logger.LDebug("lexed " + fmt.Sprint(len(tokens)) + " token, took " + time.Since(lexerStart).String())
if logger.DEBUG {
s.PrintTokens()
}

logger.LDebug("starting parser")
parserStart := time.Now()
p := parser.New(tokens)
result := p.Parse()
logger.LDebug("parsed " + fmt.Sprint(len(result)) + " items, took " + time.Since(parserStart).String())
logger.LDebug("parsed tags:", result)

var toc string
if cli.GetFlag(cli.ARGUMENTS, "toc") {
logger.LDebug("generating toc...")
toc = p.GenerateToc()
}

if cli.GetFlag(cli.ARGUMENTS, "no-template") {
generator.WritePlain(fileName, result, toc)
} else {
generator.WriteTemplate(fileName, result, toc)
}

logger.LInfo("compiled '" + fileName + "', took: " + time.Since(start).String())

defer func() {
if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") {
if cli.GetFlag(cli.ARGUMENTS, "keep-temp") {
return
}
logger.LDebug("cleanup, removing: '" + fileName + "'")
err := os.Remove(fileName)
if err != nil {
logger.LWarn(err.Error())
}
}
}()
}

// TODO: only rebuild if the file changed, md5 hash?
func main() {
cli.ARGUMENTS = cli.ParseCli()

if cli.GetFlag(cli.ARGUMENTS, "version") {
cli.PrintVersion(VERSION, BUILD_AT, BUILD_BY)
}

if cli.GetFlag(cli.ARGUMENTS, "help") {
cli.PrintShortHelp()
cli.PrintLongHelp()
os.Exit(0)
}

if len(cli.ARGUMENTS.InputFile) == 0 {
cli.PrintShortHelp()
logger.LError("not enough arguments, specify an input file")
}

flagCombinationSensible()
core.FlagCombinationSensible()

logger.DEBUG = cli.GetFlag(cli.ARGUMENTS, "debug")
logger.SILENT = cli.GetFlag(cli.ARGUMENTS, "silent")
Expand All @@ -141,8 +47,8 @@ func main() {
}

if cli.GetFlag(cli.ARGUMENTS, "watch") {
watchForChanges(fileName)
core.WatchForChanges(fileName, core.Run)
} else {
run(fileName)
core.Run(fileName)
}
}
2 changes: 2 additions & 0 deletions test.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
```js
// `npm run this-shit`
console.log("🤬");

console.log(`${name} is dumb`);
```

<h1 class="heading" id="this-is-the-first-heading">
Expand Down

0 comments on commit 824879a

Please sign in to comment.