github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/cmd/reactGen/react_gen.go (about)

     1  // Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
     2  // Use of this document is governed by a license found in the LICENSE document.
     3  
     4  /*
     5  
     6  reactGen is a go generate generator that helps to automate the process of
     7  writing GopherJS React web applications.
     8  
     9  For more information see https://github.com/myitcv/react/wiki
    10  
    11  */
    12  package main
    13  
    14  import (
    15  	"flag"
    16  	"fmt"
    17  	"log"
    18  	"os"
    19  
    20  	"myitcv.io/gogenerate"
    21  )
    22  
    23  const (
    24  	reactGenCmd = "reactGen"
    25  
    26  	jsPkg = "github.com/gopherjs/gopherjs/js"
    27  )
    28  
    29  func main() {
    30  	log.SetFlags(0)
    31  	log.SetPrefix(reactGenCmd + ": ")
    32  
    33  	flag.Usage = usage
    34  	flag.Parse()
    35  
    36  	wd, err := os.Getwd()
    37  	if err != nil {
    38  		fatalf("unable to get working directory: %v", err)
    39  	}
    40  
    41  	if fInit.val != nil {
    42  		mainInit(wd)
    43  	} else {
    44  		mainGen(wd)
    45  	}
    46  }
    47  
    48  func mainInit(wd string) {
    49  	doinit(wd, *fInit.val)
    50  }
    51  
    52  func mainGen(wd string) {
    53  	gogenerate.DefaultLogLevel(fGoGenLog, gogenerate.LogFatal)
    54  
    55  	envFile, ok := os.LookupEnv(gogenerate.GOFILE)
    56  	if !ok {
    57  		fatalf("env not correct; missing %v", gogenerate.GOFILE)
    58  	}
    59  
    60  	dirFiles, err := gogenerate.FilesContainingCmd(wd, reactGenCmd)
    61  	if err != nil {
    62  		fatalf("could not determine if we are the first file: %v", err)
    63  	}
    64  
    65  	if dirFiles == nil {
    66  		fatalf("cannot find any files containing the %v directive", reactGenCmd)
    67  	}
    68  
    69  	if dirFiles[envFile] != 1 {
    70  		fatalf("expected a single occurrence of %v directive in %v. Got: %v", reactGenCmd, envFile, dirFiles)
    71  	}
    72  
    73  	license, err := gogenerate.CommentLicenseHeader(fLicenseFile)
    74  	if err != nil {
    75  		fatalf("could not comment license file: %v", err)
    76  	}
    77  
    78  	// if we get here, we know we are the first file...
    79  
    80  	dogen(wd, license)
    81  }
    82  
    83  func fatalf(format string, args ...interface{}) {
    84  	panic(fmt.Errorf(format, args...))
    85  }
    86  
    87  func infof(format string, args ...interface{}) {
    88  	if *fGoGenLog == string(gogenerate.LogInfo) {
    89  		log.Printf(format, args...)
    90  	}
    91  }