Skip to content

Commit

Permalink
Fix #1, add run_args, build_args affects actual building, main_path a…
Browse files Browse the repository at this point in the history
…ccepts module path
  • Loading branch information
zzwx committed Aug 6, 2020
1 parent abe2deb commit 9ba4cfe
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 30 deletions.
1 change: 1 addition & 0 deletions .fresh.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ main_path: ""
tmp_path: ./tmp
build_name: runner-build
build_args: ""
run_args: ""
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
no_rebuild_ext: .tpl, .tmpl, .html
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

.DS_Store
tmp
/**/*/tmp
/.vscode
/.idea
49 changes: 39 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ This fork aims to:
* Allow a trailing comma to be in the settings expecting lists without treating the last entry as an empty string.
* Set a prefix for environment variables that are set using `fresh -e`.
* Generate a `./fresh.yaml` containing all default settings using `fresh -g`.
* Use **module path** as `main_path` instead of file path to let Go build main packages in sub-directories with enabled modules mode which has become a standard.
* Specify `run_args` and `build_args` separately.

Converting to `yaml` configuration allows for multi-line values (with at least one space padding on every line) to be used for long option values.
Also, comments are possible after `#` symbol.
Expand Down Expand Up @@ -66,6 +68,32 @@ In short,

To emulate full ignore similar to the way it worked in original `fresh`, simply comma-separate `a` and `a/**`, which will make both the folder and all the sub and sub-sub folders ignored.

## main_path

For the `main_path` to accept a sub-directory, it should be in a form of a module's exact path rather than a file path.

For example, if your go.mod lists the module as:

```
module something.com/your/path
```

And your sub-directory containing `package main` is under **`cmd`**, then set `main_path` as following:

```yaml
main_path: "something.com/your/path/cmd"
```
If you don't do that, the error will show which is hard to search and get answers to:
```bash
can't load package: package cmd is not in GOROOT (...)
```
### TODO: Attempt to leverage `go list -m` for relative paths

For `main_path` to work as a relative path, `fresh` will have to grab the modules' main path.

## Installation

```bash
Expand Down Expand Up @@ -117,25 +145,26 @@ Here is a sample config file with the default settings:

```yaml
version: 1 #
root: . # The root folder where the project is
main_path: # The folder where main.go is if not in root. example: ./cmd/
root: . # Root folder where the project is
main_path: # Module-style-path where main module is if not in root. example: example.com/name/cmd/
tmp_path: ./tmp # Default temporary folder in which the executable file will be generated and run from
build_name: runner-build # File name that will be built. exe will be automatically appended on Windows
build_args: # build args
build_log: runner-build-errors.log # log file name for build errors
valid_ext: .go, .tpl, .tmpl, .html # the extension for watching for changes
no_rebuild_ext: .tpl, .tmpl, .html # the extensions to ignore rebuilding
ignore: # ignore watching of both folders and individual files. Use * or ** and multiple lines for readability
build_args: # Build args
run_args: # Runtime args
build_log: runner-build-errors.log # Log file name for build errors
valid_ext: .go, .tpl, .tmpl, .html # Extensions list for watching for changes
no_rebuild_ext: .tpl, .tmpl, .html # Extensions list to ignore rebuilding
ignore: # Ignore watching of both folders and individual files. Use * or ** and multiple lines for readability
assets,
tmp
build_delay: 600 # ms to wait after change before attempting to rebuild.
tmp, # Trailing comma will be auto-truncated.
build_delay: 600 # Nanoseconds to wait after change before attempting to rebuild.
colors: true
log_color_main: cyan
log_color_build: yellow
log_color_runner: green
log_color_watcher: magenta
log_color_app:
debug: true # set to false to make fresh less verbose
debug: true # Set to false to make fresh less verbose
```

More examples can be seen [here](https://github.com/zzwx/fresh/tree/master/docs/_examples)
Expand Down
4 changes: 3 additions & 1 deletion docs/_examples/fresh-yaml-not-in-root/config/fresh.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
version: 1
root: .
main_path: /src
main_path: github.com/zzwx/fresh/docs/_examples/fresh-yaml-not-in-root/src
tmp_path: ./tmp
build_name: runner-build
build_args: ""
run_args: ""
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
no_rebuild_ext: .tpl, .tmpl, .html
Expand Down
4 changes: 3 additions & 1 deletion docs/_examples/main-not-in-root/.fresh.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
version: 1
root: .
main_path: /src
main_path: github.com/zzwx/fresh/docs/_examples/main-not-in-root/src
tmp_path: ./tmp
build_name: runner-build
build_args: ""
run_args: ""
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
no_rebuild_ext: .tpl, .tmpl, .html
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ require (
github.com/howeyc/fsnotify v0.9.0
github.com/mattn/go-colorable v0.1.7
github.com/valyala/fasthttp v1.9.0
golang.org/x/sys v0.0.0-20200802091954-4b90ce9b60b3 // indirect
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200802091954-4b90ce9b60b3 h1:qDJKu1y/1SjhWac4BQZjLljqvqiWUhjmDMnonmVGDAU=
golang.org/x/sys v0.0.0-20200802091954-4b90ce9b60b3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 h1:6cBnXxYO+CiRVrChvCosSv7magqTPbyAgz1M8iOv5wM=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
10 changes: 7 additions & 3 deletions runner/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)

func build() (string, bool) {
buildLog("Building...")
args := []string{
"go", "build", "-o", buildPath(), mainPath(),
}
args = append(args, strings.Fields(buildArgs())...)
buildLog("Building... %v", args)

if mustUseDelve() {
return "", true
}

cmd := exec.Command("go", "build", "-o", buildPath(), filepath.Join(root(), mainPath()))
cmd := exec.Command("go", args[1:]...) // [1: skips the "go" in args list

stderr, err := cmd.StderrPipe()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func run() bool {
if mustUseDelve() {
cmd = exec.Command("dlv", strings.Fields(delveArgs())...)
} else {
cmd = exec.Command(buildPath(), strings.Fields(buildArgs())...)
cmd = exec.Command(buildPath(), strings.Fields(runArgs())...)
}

stderr, err := cmd.StderrPipe()
Expand Down
16 changes: 14 additions & 2 deletions runner/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ type Settings struct {
TmpPath string `yaml:"tmp_path"`
BuildName string `yaml:"build_name"`
BuildArgs string `yaml:"build_args"`
RunArgs string `yaml:"run_args"`
BuildLog string `yaml:"build_log"`
ValidExt string `yaml:"valid_ext"`
NoRebuildExt string `yaml:"no_rebuild_ext"`
Ignore string `yaml:"ignore"`
BuildDelay uint `yaml:"build_delay"`
BuildDelay uint `yaml:"build_delay"` // Nanoseconds
Colors bool `yaml:"colors"`
LogColorMain string `yaml:"log_color_main"`
LogColorBuild string `yaml:"log_color_build"`
Expand All @@ -55,6 +56,7 @@ func init() {
settings.TmpPath = "./tmp"
settings.BuildName = "runner-build"
// settings.BuildArgs
// settings.RunArgs
settings.BuildLog = "runner-build-errors.log"
settings.ValidExt = ".go, .tpl, .tmpl, .html"
settings.NoRebuildExt = ".tpl, .tmpl, .html"
Expand Down Expand Up @@ -170,6 +172,10 @@ func tagDetails(field reflect.StructTag) (keyName string, omit bool, noenv bool)
}

func loadEnvSettings() {
envKey := EnvPrefix + "CONFIG_PATH"
if value := os.Getenv(envKey); value != "" {
ConfigPath = value
}
t := reflect.TypeOf(Settings{})
v := reflect.ValueOf(&settings).Elem()
for i := 0; i < t.NumField(); i++ {
Expand Down Expand Up @@ -292,7 +298,9 @@ func root() string {
}

func mainPath() string {
return filepath.Clean(settings.MainPath)
// MainPath is representing a module full path, not a file path
// No need to clean it
return settings.MainPath
}

func tmpPath() string {
Expand All @@ -311,6 +319,10 @@ func buildArgs() string {
return settings.BuildArgs
}

func runArgs() string {
return settings.RunArgs
}

func buildErrorsFileName() string {
return filepath.Clean(settings.BuildLog) // In case a path is included
}
Expand Down
10 changes: 6 additions & 4 deletions runner/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"reflect"
"runtime"
"strconv"
Expand All @@ -30,7 +31,7 @@ func flushEvents() {
select {
case eventName := <-startChannel:
if isDebug() {
mainLog("Receiving event %s", eventName)
mainLog("Event %s", eventName)
}
default:
return
Expand All @@ -55,7 +56,7 @@ func start() {
eventName := <-startChannel

if isDebug() {
mainLog("Receiving first event %s", eventName)
mainLog("First event: %s", eventName)
}
if isDebug() {
mainLog("Sleeping for %d milliseconds...", buildDelay)
Expand All @@ -79,7 +80,7 @@ func start() {

buildFailed := false
if shouldRebuild(eventName) {
mainLog("Rebuilding due to %v...", eventName)
mainLog("Rebuilding due to \"%v\"...", eventName)
errorMessage, ok := build()
if !ok {
buildFailed = true
Expand Down Expand Up @@ -124,6 +125,7 @@ func setEnvVars() {
if err == nil {
os.Setenv(EnvPrefix+"WD", wd) // RUNNER_WD
}
os.Setenv(EnvPrefix+"CONFIG_PATH", ConfigPath)
t := reflect.TypeOf(Settings{})
v := reflect.ValueOf(settings)
for i := 0; i < t.NumField(); i++ {
Expand Down Expand Up @@ -170,7 +172,7 @@ func Start() {
setEnvVars()
watch()
start()
startChannel <- "/"
startChannel <- string(filepath.Separator)

<-done
}
9 changes: 4 additions & 5 deletions runner/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func watchFolder(path string) {
}

func watch() {
root := root()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
r := root()
filepath.Walk(r, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && !isTmpDir(path) {
if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") {
return filepath.SkipDir
Expand All @@ -56,13 +56,12 @@ func watch() {
if isDebug() {
watcherLog("Ignoring %s", path)
}
// Not recursively ignoring anymore
//return filepath.SkipDir
// Not automatically ignoring subdirectories anymore. Ignore has to be explicit.
// return filepath.SkipDir
} else {
watchFolder(path)
}
}

return err
})
}

0 comments on commit 9ba4cfe

Please sign in to comment.