Skip to content

Commit

Permalink
Support relative roots for monorepos (evilmartians#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmestad committed Dec 6, 2019
1 parent 82db218 commit a745fd4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ pre-commit:
run: bundle exec rubocop --force-exclusion {all_files}
```
* ### **Execute in sub-directory**
If you want to execute the commands in a relative path
```yml
pre-commit:
commands:
backend-linter:
root: "api/" # Careful to have only trailing slash
glob: "*.rb" # glob filter
run: bundle exec rubocop {all_files}
```
* ### **Run scripts**
If oneline commands are not enough, you can execute files. [Example](./docs/full_guide.md#bash-script-example).
Expand Down
26 changes: 26 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
)

const (
rootConfigKey string = "root"
runnerConfigKey string = "runner"
runConfigKey string = "run" // alias for runner
runnerArgsConfigKey string = "runner_args"
Expand Down Expand Up @@ -194,6 +195,9 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
files = FilterInclude(files, getCommandIncludeRegexp(hooksGroup, commandName)) // NOTE: confusing option, suppose delete it
files = FilterExclude(files, getCommandExcludeRegexp(hooksGroup, commandName))

cmdRoot := getRoot(hooksGroup, commandName)
files = FilterRelative(files, cmdRoot)

VerbosePrint("Files after filters: \n", files)

files_esc := []string{}
Expand All @@ -211,6 +215,9 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

command := exec.Command("sh", "-c", runner)
if cmdRoot != "" {
command.Dir = cmdRoot
}
command.Stdin = os.Stdin

ptyOut, err := pty.Start(command)
Expand Down Expand Up @@ -410,6 +417,11 @@ func getCommands(hooksGroup string) []string {
return keys
}

func getRoot(hooksGroup string, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, rootConfigKey}, ".")
return viper.GetString(key)
}

func getCommandIncludeRegexp(hooksGroup, executableName string) string {
key := strings.Join([]string{hooksGroup, commandsConfigKey, executableName, includeConfigKey}, ".")
return viper.GetString(key)
Expand Down Expand Up @@ -500,6 +512,20 @@ func FilterGlob(vs []string, matcher string) []string {
return vsf
}

func FilterRelative(vs []string, matcher string) []string {
if matcher == "" {
return vs
}

vsf := make([]string, 0)
for _, v := range vs {
if strings.HasPrefix(v, matcher) {
vsf = append(vsf, strings.Replace(v, matcher, "./", 1))
}
}
return vsf
}

func FilterInclude(vs []string, matcher string) []string {
if matcher == "" {
return vs
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
version string = "0.6.5"
version string = "0.7.0"
)

// versionCmd represents the version command
Expand Down
1 change: 1 addition & 0 deletions spec/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

it 'contain expected output' do
expect(@stdout).to include(expected_output)

end
end

Expand Down

0 comments on commit a745fd4

Please sign in to comment.