github.com/tiagovtristao/plz@v13.4.0+incompatible/src/utils/wrapper.go (about)

     1  // +build !bootstrap
     2  
     3  package utils
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/Songmu/prompter"
    14  
    15  	"github.com/thought-machine/please/src/core"
    16  	"github.com/thought-machine/please/src/fs"
    17  )
    18  
    19  const configTemplate = `; Please config file
    20  ; Leaving this file as is is enough to use plz to build your project.
    21  ; Please will stay on whatever version you currently have until you run
    22  ; 'plz update', when it will download the latest available version.
    23  ;
    24  ; Or you can uncomment the following to pin everyone to a particular version;
    25  ; when you change it all users will automatically get updated.
    26  ; [please]
    27  ; version = %s
    28  `
    29  const bazelCompatibilityConfig = `
    30  [bazel]
    31  compatibility = true
    32  `
    33  const wrapperScriptName = "pleasew"
    34  
    35  // InitConfig initialises a .plzconfig template in the given directory.
    36  func InitConfig(dir string, bazelCompatibility bool) {
    37  	if dir == "." {
    38  		if core.FindRepoRoot() {
    39  			config := path.Join(core.RepoRoot, core.ConfigFileName)
    40  			if !prompter.YN(fmt.Sprintf("You already seem to be in a plz repo (found %s). Continue?", config), false) {
    41  				os.Exit(1)
    42  			}
    43  		}
    44  	}
    45  	dir, err := filepath.Abs(dir)
    46  	if err != nil {
    47  		log.Warning("Can't determine absolute directory: %s", err)
    48  	}
    49  	config := path.Join(dir, core.ConfigFileName)
    50  	contents := fmt.Sprintf(configTemplate, core.PleaseVersion)
    51  	if bazelCompatibility {
    52  		contents += bazelCompatibilityConfig
    53  	}
    54  	if err := ioutil.WriteFile(config, []byte(contents), 0644); err != nil {
    55  		log.Fatalf("Failed to write file: %s", err)
    56  	}
    57  	fmt.Printf("Wrote config template to %s, you're now ready to go!\n", config)
    58  	// Now write the wrapper script
    59  	data := MustAsset(wrapperScriptName)
    60  	if err := ioutil.WriteFile(wrapperScriptName, data, 0755); err != nil {
    61  		log.Fatalf("Failed to write file: %s", err)
    62  	}
    63  	fmt.Printf("\nAlso wrote wrapper script to %s; users can invoke that directly to run Please, even without it installed.\n", wrapperScriptName)
    64  }
    65  
    66  // InitConfigFile sets a bunch of values in a config file.
    67  func InitConfigFile(filename string, options map[string]string) {
    68  	b := readConfig(filename)
    69  	for k, v := range options {
    70  		parts := strings.Split(k, ".")
    71  		if len(parts) != 2 {
    72  			log.Fatalf("unknown key format: %s", k)
    73  		}
    74  		b = append(b, []byte(fmt.Sprintf("[%s]\n%s = %s\n", parts[0], parts[1], v))...)
    75  	}
    76  	if err := fs.EnsureDir(filename); err != nil {
    77  		log.Fatalf("Cannot create directory for new file: %s", err)
    78  	} else if err := ioutil.WriteFile(filename, b, 0644); err != nil {
    79  		log.Fatalf("Failed to write updated config file: %s", err)
    80  	}
    81  }
    82  
    83  func readConfig(filename string) []byte {
    84  	if !fs.PathExists(filename) {
    85  		return nil
    86  	}
    87  	b, err := ioutil.ReadFile(filename)
    88  	if err != nil {
    89  		log.Fatalf("Failed to read config file: %s", err)
    90  	}
    91  	return b
    92  }