Skip to content

Commit

Permalink
pliz git:plug-hook now also insert the .phpcs.xml.dist file into the …
Browse files Browse the repository at this point in the history
…project if it does not exists
  • Loading branch information
vorban committed Jul 26, 2021
1 parent 1717edf commit 6e57e86
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 54 deletions.
1 change: 0 additions & 1 deletion domain/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func DefaultTaskNames() []TaskID {
"composer",
"gulp",
"db:update",
"git:hook",
}
}

Expand Down
120 changes: 81 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"embed"
"fmt"
"io/ioutil"
"os"
Expand All @@ -17,6 +18,9 @@ import (
cli "github.com/jawher/mow.cli"
)

//go:embed resources/*
var resources embed.FS

func main() {

app := cli.App("pliz", "Manage projects building")
Expand Down Expand Up @@ -198,8 +202,7 @@ func main() {
* 5. Plug Git precommit Hook
*/

fmt.Printf("%s %s\n", color.MagentaString("Executing:"), "git:plug-hook")
cmd = domain.NewCommand([]string{"pliz", "git:plug-hook"})
cmd = domain.NewCommand([]string{"pliz", "git:plug-hook", "-i"})
cmd.Execute()
/*
* 6. The end
Expand Down Expand Up @@ -329,50 +332,89 @@ func main() {
})

app.Command("git:plug-hook", "Register hooks in the Git repository", func(cmd *cli.Cmd) {
include := cmd.BoolOpt("i include-phpcs-config", false, "Includes the default phpcs config, if not already setup in the repo")
force := cmd.BoolOpt("f force", false, "Forces the include of the default phpcs config, even if already setup in the repo")

cmd.Action = func() {
fmt.Printf("%s %s\n", color.MagentaString("Executing:"), "git:hook")
code := `#!/bin/sh
# don't validate if phpcs is not found
PHPCS="$(command -v phpcs)"
if [ -z "$PHPCS" ]; then
echo "phpcs unavailable; skipping PHP Standards validation..."
exit
fi
# get all modified and staged PHP files (.blade included)
# and run phpcs over them
FILES="$(git diff --name-only --staged | grep .php | tr "\n" " ")"
if [ ! -z "$FILES" ]; then
OUTPUT=$(phpcs --report=summary $FILES)
if [ ! -z "$OUTPUT" ]; then
echo "$OUTPUT"
exit 1
fi
fi
exit 0`
// try to write the specific project hook
// if it cannot be read (i.e. does not exist),
// then write the default code above
script, err := ioutil.ReadFile("ops/git/hooks/pre-commit")
if err != nil {
err := ioutil.WriteFile(".git/hooks/pre-commit", script, 0755)
if err != nil {
fmt.Printf("Unable to write file: %v", err)
}
} else {
fmt.Printf("./ops/git/hooks/pre-commit not found, fallback to default script")
err := ioutil.WriteFile(".git/hooks/pre-commit", []byte(code), 0755)
if err != nil {
fmt.Printf("Unable to write file: %v", err)
}
}
fmt.Printf("%s git:plug-hook\n\n", color.MagentaString("Executing:"))

plugPhpcsConfig(*include, *force)
plugHook()
}
})

app.Run(os.Args)
}

func plugPhpcsConfig(include bool, force bool) {
if !include {
return
}

_, err := ioutil.ReadFile(".phpcs.xml.dist")

// if file already exists and there is no force flag: return
if err == nil && !force {
fmt.Printf("%s .phpcs.xml.dist already exists.\n", color.YellowString("Skipping include:"))
return
}

config, err := resources.ReadFile("resources/.phpcs.xml.dist")
if err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to read default config:"), err)
return
}

// write +wr for everybody
if err := ioutil.WriteFile(".phpcs.xml.dist", config, 0555); err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to write file:"), err)
return
}

fmt.Printf("%s ./.phpcs.xml.dist\n", color.GreenString("Successfully plugged:"))
}

func plugHook() {
// open the hook code from the project config
script, err := ioutil.ReadFile("ops/git/hooks/pre-commit")

// if it cannot be read, plug the default hook instead
if err != nil {
plugDefaultHook()
return
}
plugProjectHook(script)
}

func plugProjectHook(script []byte) {
// write +wr for everybody, +x for user
err := ioutil.WriteFile(".git/hooks/pre-commit", script, 0755)

if err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to write file:"), err)
return
}
fmt.Printf("%s .git/hooks/pre-commit\n", color.GreenString("Successfully plugged:"))
}

func plugDefaultHook() {
fmt.Printf("./ops/git/hooks/pre-commit not found, fallback to default hook")

script, err := resources.ReadFile("git-hooks/pre-commit")

if err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to read default hook:"), err)
return
}

if err := ioutil.WriteFile(".git/hooks/pre-commit", script, 0755); err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to write file:"), err)
return
}

fmt.Printf("%s .git/hooks/pre-commit\n", color.GreenString("Successfully plugged:"))
}

func parseAndCheckConfig() {
err := config.Check()
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions resources/.phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>The coding standard for Webup.</description>

<exclude-pattern>resources/views/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>node_modules/*</exclude-pattern>
<!-- for whatever reason, phpcs actually lints js files -->
<exclude-pattern>*.js</exclude-pattern>
<exclude-pattern>_ide_helper_models.php</exclude-pattern>
<exclude-pattern>_ide_helper.php</exclude-pattern>

<rule ref="PSR12"/>
<rule ref="PSR1.Classes.ClassDeclaration">
<exclude-pattern>database/migrations/*</exclude-pattern>
</rule>
</ruleset>
20 changes: 20 additions & 0 deletions resources/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# don't validate if phpcs is not found
PHPCS="$(command -v ./vendor/squizlabs/php_codesniffer/bin/phpcs)"
if [ -z "$PHPCS" ]; then
echo "phpcs unavailable; skipping PHP Standards validation..."
exit
fi

# get all modified and staged PHP files (.blade included)
# and run phpcs over them
FILES="$(git diff --name-only --staged --diff-filter=ACM | grep .php | tr "\n" " ")"
if [ ! -z "$FILES" ]; then
OUTPUT=$(./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary $FILES)
if [ ! -z "$OUTPUT" ]; then
echo "$OUTPUT"
exit 1
fi
fi
exit 0
2 changes: 0 additions & 2 deletions tasks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func CreateTaskWithName(name domain.TaskID, config domain.Config) (domain.Task,
return GulpTask(config.Containers.Builder), nil
case "db:update":
return DbUpdateTask(config.Containers.App), nil
case "git:plug-hook":
return GitPlugHookTask(), nil
}

return domain.Task{}, fmt.Errorf("Unable to find the task '%s'\n", name)
Expand Down
12 changes: 0 additions & 12 deletions tasks/git-hook.go

This file was deleted.

0 comments on commit 6e57e86

Please sign in to comment.