Skip to content

Commit

Permalink
Merge fix-15-depends
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Casabianca committed Sep 25, 2024
1 parent 1822523 commit aad0296
Show file tree
Hide file tree
Showing 53 changed files with 383 additions and 172 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NeON

[![Build Status](https://travis-ci.org/c4s4/neon.svg?branch=master)](https://travis-ci.org/c4s4/neon)
<!--[![Build Status](https://travis-ci.org/c4s4/neon.svg?branch=master)](https://travis-ci.org/c4s4/neon)-->
[![Code Quality](https://goreportcard.com/badge/github.com/c4s4/neon)](https://goreportcard.com/report/github.com/c4s4/neon)
[![Codecov](https://codecov.io/gh/c4s4/neon/branch/master/graph/badge.svg)](https://codecov.io/gh/c4s4/neon)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
8 changes: 3 additions & 5 deletions build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ targets:
depends: [version-test, build]
steps:
- for: file
in: 'find(joinpath("test", "bugs"), "*.yml")'
in: 'find(".", "test/bugs/*.yml")'
do:
- 'path = joinpath("test", "bugs", file)'
- print: "Running build file '={path}'"
- try:
- $: ['bin/neon', '-file', =path]
- print: "Running build file '={file}'"
- $: ['neon', '-file', =file]

refs:
doc: Generate reference documentation
Expand Down
40 changes: 25 additions & 15 deletions neon/build/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package build

import (
"fmt"
"github.com/mattn/anko/core"
"github.com/mattn/anko/packages"
"github.com/mattn/anko/parser"
"github.com/mattn/anko/vm"
"os"
"reflect"
"regexp"
"runtime"
"sort"
"strings"

"github.com/mattn/anko/core"
"github.com/mattn/anko/packages"
"github.com/mattn/anko/parser"
"github.com/mattn/anko/vm"
)

const (
Expand All @@ -38,9 +39,10 @@ var (
// - Index: tracks steps index while running build
// - Stack: tracks targets calls
type Context struct {
VM *vm.Env
Build *Build
Stack *Stack
VM *vm.Env
Build *Build
Stack *Stack
History *History
}

// NewContext make a new build context
Expand All @@ -51,9 +53,10 @@ func NewContext(build *Build) *Context {
packages.DefineImport(v)
LoadBuiltins(v)
context := &Context{
VM: v,
Build: build,
Stack: NewStack(),
VM: v,
Build: build,
Stack: NewStack(),
History: NewHistory(),
}
return context
}
Expand All @@ -62,9 +65,10 @@ func NewContext(build *Build) *Context {
// Return: a pointer to the context copy
func (context *Context) Copy() *Context {
another := &Context{
VM: context.VM.DeepCopy(),
Build: context.Build,
Stack: context.Stack.Copy(),
VM: context.VM.DeepCopy(),
Build: context.Build,
Stack: context.Stack.Copy(),
History: context.History.Copy(),
}
return another
}
Expand Down Expand Up @@ -372,11 +376,17 @@ func (context *Context) EvaluateEnvironment() ([]string, error) {
return lines, nil
}

// Message print a message on the console
// - text: the text to print on console
func (context *Context) Message(text string) {
Message(text)
}

// Message print a message on the console
// - text: the text to print on console
// - args: a slice of string arguments (as for fmt.Printf())
func (context *Context) Message(text string, args ...interface{}) {
Message(text, args...)
func (context *Context) MessageArgs(text string, args ...interface{}) {
MessageArgs(text, args...)
}

// FormatScriptError adds line and column numbers on parser or vm errors.
Expand Down
56 changes: 56 additions & 0 deletions neon/build/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package build

import (
"strings"
)

// History is structure for an history
type History struct {
Targets []string
}

// NewHistory makes a new history
// Returns: a pointer to the history
func NewHistory() *History {
history := History{
Targets: make([]string, 0),
}
return &history
}

// Contains tells if the history contains given target
// - target: target to test
// Returns: a boolean telling if target is in the history
func (history *History) Contains(name string) bool {
for _, target := range history.Targets {
if name == target {
return true
}
}
return false
}

// Push a target on the history
// - target: target to push on the history
// Return: an error if we are in an infinite loop
func (history *History) Push(target *Target) error {
history.Targets = append(history.Targets, target.Name)
return nil
}

// ToString returns string representation of the history, such as:
// "foo, bar, spam"
// Return: the history as a string
func (history *History) String() string {
names := make([]string, len(history.Targets))
copy(names, history.Targets)
return strings.Join(names, ", ")
}

// Copy returns a copy of the history
// Return: pointer to a copy of the history
func (history *History) Copy() *History {
another := make([]string, len(history.Targets))
copy(another, history.Targets)
return &History{another}
}
85 changes: 53 additions & 32 deletions neon/build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package build

import (
"fmt"
"github.com/c4s4/neon/neon/util"
"github.com/fatih/color"
"strings"
"unicode/utf8"

"github.com/c4s4/neon/neon/util"
"github.com/fatih/color"
)

type colorizer func(a ...interface{}) string

// Grey is a flag that tells if we print on console without color
var Grey = false
// Gray is a flag that tells if we print on console without color
var Gray = false

// Color definitions
var colorTitle colorizer
Expand All @@ -20,19 +21,35 @@ var colorError colorizer

// Message prints a message on console:
// - text: text to print (that might embed fields to print, such as "%s")
func Message(text string) {
printGray(text)
}

// MessageArgs prints a message on console:
// - text: text to print (that might embed fields to print, such as "%s")
// - args: arguments for the text to print
func Message(text string, args ...interface{}) {
printGrey(text, args...)
func MessageArgs(text string, args ...interface{}) {
printGrayArgs(text, args...)
}

// Info prints an information message on console:
// - text: text to print (that might embed fields to print, such as "%s")
func Info(text string) {
if Gray {
printGray(text)
} else {
printColor(colorTitle(text))
}
}

// InfoArgs prints an information message on console:
// - text: text to print (that might embed fields to print, such as "%s")
// - args: arguments for the text to print
func Info(text string, args ...interface{}) {
if Grey {
printGrey(text, args...)
func InfoArgs(text string, args ...interface{}) {
if Gray {
printGrayArgs(text, args...)
} else {
printColor(colorTitle(text), args...)
printColorArgs(colorTitle(text), args...)
}
}

Expand All @@ -44,17 +61,17 @@ func Title(text string) {
length = 2
}
message := fmt.Sprintf("%s %s --", strings.Repeat("-", length), text)
if Grey {
printGrey(message)
if Gray {
printGray(message)
} else {
printColor(colorTitle(message))
}
}

// PrintOk prints a green OK on the console
func PrintOk() {
if Grey {
printGrey("OK")
if Gray {
printGray("OK")
} else {
printColor(colorOk("OK"))
}
Expand All @@ -64,33 +81,37 @@ func PrintOk() {
// text
// - text: the explanatory text to print
func PrintError(text string) {
if Grey {
printGrey("ERROR %s", text)
if Gray {
printGrayArgs("ERROR %s", text)
} else {
printColor("%s %s", colorError("ERROR"), text)
printColorArgs("%s %s", colorError("ERROR"), text)
}
}

// PrintColor prints a string in given color
// - text: the text to print
func printColor(text string) {
fmt.Println(text)
}

// PrintColor prints a string with arguments in given color
// - text: the text to print
// - args: the arguments for the text to print
func printColor(text string, args ...interface{}) {
if len(args) > 0 {
fmt.Fprintf(color.Output, text, args...)
fmt.Println()
} else {
fmt.Println(text)
}
func printColorArgs(text string, args ...interface{}) {
fmt.Fprintf(color.Output, text, args...)
fmt.Println()
}

// PrintGrey prints a string with arguments in grey
// PrintGrey prints a string in gray
// - text: the text to print
func printGray(text string) {
fmt.Println(text)
}

// PrintGreyArgs prints a string with arguments in gray
// - text: the text to print
// - args: the arguments for the text to print
func printGrey(text string, fields ...interface{}) {
if len(fields) > 0 {
fmt.Printf(text, fields...)
fmt.Println()
} else {
fmt.Println(text)
}
func printGrayArgs(text string, fields ...interface{}) {
fmt.Printf(text, fields...)
fmt.Println()
}
4 changes: 2 additions & 2 deletions neon/build/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func TestInfoNotGrey(t *testing.T) {
}

func TestInfoGrey(t *testing.T) {
Grey = true
Gray = true
stdout := os.Stdout
read, write, _ := os.Pipe()
os.Stdout = write
Info("This is a test!")
os.Stdout = stdout
Grey = false
Gray = false
write.Close()
out, _ := io.ReadAll(read)
if string(out) != "This is a test!\n" {
Expand Down
6 changes: 3 additions & 3 deletions neon/build/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func InstallPlugin(plugin, repository string) error {
}
pluginPath := filepath.Join(repository, plugin)
if util.DirExists(pluginPath) {
Message("Plugin '%s' already installed in '%s'", plugin, pluginPath)
MessageArgs("Plugin '%s' already installed in '%s'", plugin, pluginPath)
return nil
}
gitRepository := "https://" + PluginSite + "/" + plugin + ".git"
command := exec.Command("git", "clone", gitRepository, pluginPath)
Message("Running command '%s'...", strings.Join(command.Args, " "))
MessageArgs("Running command '%s'...", strings.Join(command.Args, " "))
output, err := command.CombinedOutput()
if err != nil {
re = regexp.MustCompile("\n\n")
Expand All @@ -119,7 +119,7 @@ func InstallPlugin(plugin, repository string) error {
Message(message)
return fmt.Errorf("installing plugin '%s'", plugin)
}
Message("Plugin '%s' installed in '%s'", plugin, pluginPath)
MessageArgs("Plugin '%s' installed in '%s'", plugin, pluginPath)
return nil
}

Expand Down
9 changes: 7 additions & 2 deletions neon/build/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ func (target *Target) Run(context *Context) error {
if err := context.Stack.Push(target); err != nil {
return err
}
if err := context.History.Push(target); err != nil {
return err
}
for _, name := range target.Depends {
if err := target.Build.Root.RunTarget(context, name); err != nil {
return err
if !context.History.Contains(name) {
if err := target.Build.Root.RunTarget(context, name); err != nil {
return err
}
}
}
Title(target.Name)
Expand Down
Loading

0 comments on commit aad0296

Please sign in to comment.