Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
run: | ||
deadline: 10m | ||
timeout: 10m | ||
deadline: 20m | ||
timeout: 20m | ||
issues-exit-code: 1 | ||
tests: true | ||
skip-dirs: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "fixture", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"leftpad": "0.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package hook | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/metrue/fx/utils" | ||
) | ||
|
||
// Hooker defines hook interface | ||
type Hooker interface { | ||
Run() error | ||
} | ||
|
||
// Hook to run | ||
type Hook struct { | ||
name string | ||
script string | ||
} | ||
|
||
// New a hook | ||
func New(name string, script string, workdir string) *Hook { | ||
return &Hook{ | ||
name: name, | ||
script: script, | ||
} | ||
} | ||
|
||
// Run execute a hook | ||
func (h *Hook) Run(workdir string) error { | ||
var script string | ||
if !utils.IsRegularFile(h.script) { | ||
hookScript, err := ioutil.TempFile(os.TempDir(), "fx-hook-script-") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(hookScript.Name()) | ||
|
||
content := []byte(h.script) | ||
if _, err = hookScript.Write(content); err != nil { | ||
return err | ||
} | ||
if err := hookScript.Close(); err != nil { | ||
return err | ||
} | ||
script = hookScript.Name() | ||
} else { | ||
absScript, err := filepath.Abs(h.script) | ||
if err != nil { | ||
return err | ||
} | ||
script = absScript | ||
} | ||
|
||
cmd := exec.Command("/bin/sh", script) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
if workdir != "" { | ||
cmd.Dir = workdir | ||
} | ||
|
||
if err := cmd.Run(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Name hook name | ||
func (h *Hook) Name() string { | ||
return h.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package hook | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/metrue/fx/utils" | ||
) | ||
|
||
// HookNameBeforeBuild before build hook | ||
const HookNameBeforeBuild = "before_build" | ||
|
||
// RunBeforeBuildHook trigger before_build hook | ||
func RunBeforeBuildHook(workdir string) error { | ||
hooks, err := descovery("") | ||
if err != nil { | ||
return err | ||
} | ||
for _, h := range hooks { | ||
if h.Name() == HookNameBeforeBuild { | ||
if err := h.Run(workdir); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func descovery(hookdir string) ([]*Hook, error) { | ||
if hookdir == "" { | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
hookdir = filepath.Join(dir, ".hooks") | ||
} | ||
|
||
hooks := []*Hook{} | ||
if !utils.IsDir(hookdir) { | ||
return hooks, nil | ||
} | ||
if err := filepath.Walk(hookdir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.Name() == HookNameBeforeBuild { | ||
hooks = append(hooks, New("before_build", path, "")) | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
return hooks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package hook | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestHookManager(t *testing.T) { | ||
t.Run("descovery in default hookdir .hooks", func(t *testing.T) { | ||
hooks, err := descovery("") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(hooks) != 1 { | ||
t.Fatalf("should have one hook, but got %d", len(hooks)) | ||
} | ||
|
||
if hooks[0].Name() != HookNameBeforeBuild { | ||
t.Fatalf("should be before_build hook, but got %s", hooks[0].Name()) | ||
} | ||
}) | ||
|
||
t.Run("descovery in empty hookdir", func(t *testing.T) { | ||
hooks, err := descovery(filepath.Join(os.TempDir(), ".hooks")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(hooks) != 0 { | ||
t.Fatalf("should get 0 hooks, but got %d", len(hooks)) | ||
} | ||
}) | ||
|
||
t.Run("run before_build hook", func(t *testing.T) { | ||
if err := RunBeforeBuildHook("fixture"); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package hook | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestHook(t *testing.T) { | ||
t.Run("text", func(t *testing.T) { | ||
h := New("before_build", "npm install leftpad", "fixture") | ||
if err := h.Run("fixture"); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
t.Run("script", func(t *testing.T) { | ||
h := New("before_build", ".hooks/before_build", "fixture") | ||
if err := h.Run("fixture"); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters