-
Notifications
You must be signed in to change notification settings - Fork 1
/
postGenerateScript.go
80 lines (63 loc) · 1.52 KB
/
postGenerateScript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
/*
Checks for the existence of a post-generation script. If it exists (and is executable)
this will execute it, with the working directory set to the generated project.
*/
func executePostGenerate(sourcePath string, generatedPath string) error {
var command *exec.Cmd
var output []byte
var scriptPath string
var workingDirectory string
var err error
scriptPath = fmt.Sprintf("%s%s_postGenerate.sh", sourcePath, string(os.PathSeparator))
scriptPath, err = filepath.Abs(scriptPath)
if err != nil {
return err
}
generatedPath, err = filepath.Abs(generatedPath)
if err != nil {
return err
}
// file doesn't exist, exit quietly.
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
return nil
}
workingDirectory, err = os.Getwd()
if err != nil {
return err
}
err = os.Chdir(generatedPath)
if err != nil {
return err
}
defer os.Chdir(workingDirectory)
command = exec.Command(scriptPath, "")
output, err = command.CombinedOutput()
if err != nil {
return err
}
fmt.Printf(string(output))
// remove postgenerate script from generated directory.
err = removePostGenerate(generatedPath)
if err != nil {
return err
}
return nil
}
func removePostGenerate(generatedPath string) error {
var scriptPath string
var err error
scriptPath = fmt.Sprintf("%s%s_postGenerate.sh", generatedPath, string(os.PathSeparator))
scriptPath, err = filepath.Abs(scriptPath)
if err != nil {
return err
}
err = os.Remove(scriptPath)
return err
}