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

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"strings"
    11  	"text/template"
    12  )
    13  
    14  const (
    15  	templateHeader = "Template generated by " + reactGenCmd
    16  
    17  	templateMinimal   = "minimal"
    18  	templateBootstrap = "bootstrap"
    19  )
    20  
    21  // TODO this needs a significant overhaul to support multiple templates
    22  // extensibility etc but works for now
    23  func doinit(wd string, tmplName string) {
    24  	tmpl := struct {
    25  		DirName   string
    26  		Bootstrap bool
    27  	}{
    28  		DirName: filepath.Base(wd),
    29  	}
    30  
    31  	switch tmplName {
    32  	case templateMinimal:
    33  	case templateBootstrap:
    34  		tmpl.Bootstrap = true
    35  	default:
    36  		panic(fmt.Errorf("unknown template %q", tmplName))
    37  	}
    38  
    39  	for fn, c := range minimal {
    40  		b := bytes.NewBuffer(nil)
    41  
    42  		t := template.New(fn)
    43  		_, err := t.Parse(c)
    44  		if err != nil {
    45  			panic(fmt.Errorf("failed to parse template %v: %v", fn, err))
    46  		}
    47  
    48  		err = t.Execute(b, tmpl)
    49  		if err != nil {
    50  			panic(fmt.Errorf("failed to execute template %v: %v", fn, err))
    51  		}
    52  
    53  		toWrite := b.Bytes()
    54  
    55  		fp := filepath.Join(wd, fn)
    56  
    57  		if strings.HasSuffix(fn, ".go") {
    58  
    59  			out, err := goFmtBuf(b)
    60  			if err == nil {
    61  				toWrite = out.Bytes()
    62  			}
    63  		}
    64  
    65  		err = ioutil.WriteFile(fp, toWrite, 0644)
    66  		if err != nil {
    67  			panic(fmt.Errorf("failed to write file %v: %v", fp, err))
    68  		}
    69  	}
    70  
    71  	gg := exec.Command("go", "generate")
    72  	gg.Dir = wd
    73  	gg.Stderr = os.Stderr
    74  	gg.Stdout = os.Stdout
    75  
    76  	err := gg.Run()
    77  	if err != nil {
    78  		fatalf("failed to run go generate: %v", err)
    79  	}
    80  }
    81  
    82  var minimal = map[string]string{
    83  	// app.go
    84  	"app.go": `// ` + templateHeader + `
    85  
    86  package main
    87  
    88  import (
    89  	"myitcv.io/react"
    90  )
    91  
    92  type AppDef struct {
    93  	react.ComponentDef
    94  }
    95  
    96  func App() *AppElem {
    97  	return buildAppElem()
    98  }
    99  
   100  func (a AppDef) Render() react.Element {
   101  	return react.Div(nil,
   102  		react.H1(nil,
   103  			react.S("Hello World"),
   104  		),
   105  		react.P(nil,
   106  			react.S("This is my first GopherJS React App."),
   107  		),
   108  	)
   109  }
   110  `,
   111  
   112  	// main.go
   113  	"main.go": `// ` + templateHeader + `
   114  
   115  package main
   116  
   117  import (
   118  	"myitcv.io/react"
   119  
   120  	"honnef.co/go/js/dom"
   121  )
   122  
   123    //go:generate reactGen
   124  
   125  var document = dom.GetWindow().Document()
   126  
   127  func main() {
   128  	domTarget := document.GetElementByID("app")
   129  
   130  	react.Render(App(), domTarget)
   131  }
   132  `,
   133  
   134  	// index.html
   135  	"index.html": `<!--` + templateHeader + `-->
   136  
   137  <!doctype html>
   138  <html lang="en">
   139    <head>
   140  	 <meta charset="utf-8">
   141  	 <meta http-equiv="X-UA-Compatible" content="IE=edge">
   142  	 <meta name="viewport" content="width=device-width, initial-scale=1">
   143  
   144  	 {{if .Bootstrap}}
   145      <title>Hello World Bootstrap</title>
   146  
   147  	 <!-- START BOOTSTRAP -->
   148  
   149  	 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
   150  	 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   151  	 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
   152  
   153  
   154  	 <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
   155  	 <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
   156  	 <!--[if lt IE 9]>
   157  		 <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
   158  		 <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
   159  	 <![endif]-->
   160  
   161  	 <!-- END BOOTSTRAP -->
   162  	 {{else}}
   163      <title>Hello World</title>
   164  	 {{end}}
   165  
   166    </head>
   167    <body>
   168  	 {{if .Bootstrap}}
   169  	 <div class="container-fluid">
   170  	 {{end}}
   171      <div id="app"></div>
   172  	 {{if .Bootstrap}}
   173  	 </div>
   174  	 {{end}}
   175      <script src="{{.DirName}}.js"></script>
   176    </body>
   177  </html>
   178  `,
   179  }