Initial code add for macros
This commit is contained in:
parent
db3f4056d5
commit
461f56df0a
23
command.go
23
command.go
@ -32,23 +32,20 @@ import (
|
|||||||
|
|
||||||
type command []string
|
type command []string
|
||||||
|
|
||||||
func (c command) expandEnv() []string {
|
|
||||||
var args []string
|
|
||||||
for _, value := range c {
|
|
||||||
args = append(args, os.ExpandEnv(value))
|
|
||||||
}
|
|
||||||
|
|
||||||
return args
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c command) process(dir string, flags int) error {
|
func (c command) process(dir string, flags int) error {
|
||||||
if len(c) < 1 {
|
var args []string
|
||||||
|
args = appendExpEnv(args, c)
|
||||||
|
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
switch {
|
||||||
|
case len(args) == 0:
|
||||||
return fmt.Errorf("command element is invalid")
|
return fmt.Errorf("command element is invalid")
|
||||||
|
case len(args) == 1:
|
||||||
|
cmd = exec.Command(args[0])
|
||||||
|
default:
|
||||||
|
cmd = exec.Command(args[0], args[1:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
args := c.expandEnv()
|
|
||||||
|
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
|
55
macro.go
Normal file
55
macro.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
||||||
|
all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type macro struct {
|
||||||
|
Prefix []string
|
||||||
|
Suffix []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m macro) process(dir string, params []string, flags int) error {
|
||||||
|
var args []string
|
||||||
|
args = appendExpEnv(args, m.Prefix)
|
||||||
|
args = appendExpEnv(args, params)
|
||||||
|
args = appendExpEnv(args, m.Suffix)
|
||||||
|
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
switch {
|
||||||
|
case len(args) == 0:
|
||||||
|
return fmt.Errorf("macro element is invalid")
|
||||||
|
case len(args) == 1:
|
||||||
|
cmd = exec.Command(args[0])
|
||||||
|
default:
|
||||||
|
cmd = exec.Command(args[0], args[1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Dir = dir
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
|
||||||
|
if flags&flagVerbose == flagVerbose {
|
||||||
|
log.Printf("executing macro %s", strings.Join(args, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
23
task.go
23
task.go
@ -24,13 +24,14 @@ package main
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type task struct {
|
type taskDef struct {
|
||||||
Deps []string
|
Deps []string
|
||||||
Links []link
|
Links []link
|
||||||
Cmds []command
|
Cmds []command
|
||||||
|
Macros []macro
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *task) process(taskName, srcDir, dstDir string, conf *config, flags int) error {
|
func (t *taskDef) process(taskName, srcDir, dstDir string, conf *config, flags int) error {
|
||||||
handled, ok := conf.tasksHandled[taskName]
|
handled, ok := conf.tasksHandled[taskName]
|
||||||
if ok && handled {
|
if ok && handled {
|
||||||
return nil
|
return nil
|
||||||
@ -49,6 +50,24 @@ func (t *task) process(taskName, srcDir, dstDir string, conf *config, flags int)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, macro := range t.Macros {
|
||||||
|
if len(macro) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
macroName := macro[0]
|
||||||
|
macroParams := macro[1:]
|
||||||
|
|
||||||
|
depMacro, ok := conf.Macros[macroName]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("macro dependency not found %s", macroName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := depMacro.process(dstDir, macroParams, flags); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if flags&flagNoLink == 0 {
|
if flags&flagNoLink == 0 {
|
||||||
for _, currLink := range t.Links {
|
for _, currLink := range t.Links {
|
||||||
if err := currLink.process(srcDir, dstDir, flags); err != nil {
|
if err := currLink.process(srcDir, dstDir, flags); err != nil {
|
||||||
|
24
util.go
Normal file
24
util.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
||||||
|
all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
func appendExpEnv(dst, src []string) []string {
|
||||||
|
for _, value := range src {
|
||||||
|
dst = append(dst, os.ExpandEnv(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
return dst
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user