/
wrapper.go
62 lines (56 loc) · 1.82 KB
/
wrapper.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
// +build !bootstrap
package utils
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/Songmu/prompter"
"core"
)
const configTemplate = `; Please config file
; Leaving this file as is is enough to use plz to build your project.
; Please will stay on whatever version you currently have until you run
; 'plz update', when it will download the latest available version.
;
; Or you can uncomment the following to pin everyone to a particular version;
; when you change it all users will automatically get updated.
; [please]
; version = %s
`
const bazelCompatibilityConfig = `
[bazel]
compatibility = true
`
const wrapperScriptName = "pleasew"
// InitConfig initialises a .plzconfig template in the given directory.
func InitConfig(dir string, bazelCompatibility bool) {
if dir == "." {
if core.FindRepoRoot() {
config := path.Join(core.RepoRoot, core.ConfigFileName)
if !prompter.YN(fmt.Sprintf("You already seem to be in a plz repo (found %s). Continue?", config), false) {
os.Exit(1)
}
}
}
dir, err := filepath.Abs(dir)
if err != nil {
log.Warning("Can't determine absolute directory: %s", err)
}
config := path.Join(dir, core.ConfigFileName)
contents := fmt.Sprintf(configTemplate, core.PleaseVersion)
if bazelCompatibility {
contents += bazelCompatibilityConfig
}
if err := ioutil.WriteFile(config, []byte(contents), 0644); err != nil {
log.Fatalf("Failed to write file: %s", err)
}
fmt.Printf("Wrote config template to %s, you're now ready to go!\n", config)
// Now write the wrapper script
data := MustAsset(wrapperScriptName)
if err := ioutil.WriteFile(wrapperScriptName, data, 0755); err != nil {
log.Fatalf("Failed to write file: %s", err)
}
fmt.Printf("\nAlso wrote wrapper script to %s; users can invoke that directly to run Please, even without it installed.\n", wrapperScriptName)
}