Skip to content

Commit

Permalink
Merge pull request #1 from suntong/reconstruct
Browse files Browse the repository at this point in the history
Reconstruct
  • Loading branch information
suntong committed Feb 24, 2016
2 parents 9763302 + 7fb3cac commit b387613
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 286 deletions.
94 changes: 94 additions & 0 deletions cmd/easygen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
////////////////////////////////////////////////////////////////////////////
// Porgram: EasyGen
// Purpose: Easy to use universal code/text generator
// Authors: Tong Sun (c) 2015, All rights reserved
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
// Program start

package main

import (
"flag"
"fmt"
"os"
)

import (
"github.com/suntong/easygen"
)

////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions

////////////////////////////////////////////////////////////////////////////
// Global variables definitions

////////////////////////////////////////////////////////////////////////////
// Commandline definitions

func init() {

// set default values for command line parameters
flag.BoolVar(&easygen.Opts.HTML, "html", false,
"treat the template file as html instead of text")
flag.StringVar(&easygen.Opts.TemplateStr, "ts", "",
"template string (in text)")
flag.StringVar(&easygen.Opts.TemplateFile, "tf", "",
".tmpl template file `name` (default: same as .yaml file)")
flag.StringVar(&easygen.Opts.ExtYaml, "ey", ".yaml",
"`extension` of yaml file")
flag.StringVar(&easygen.Opts.ExtTmpl, "et", ".tmpl",
"`extension` of template file")
flag.StringVar(&easygen.Opts.StrFrom, "rf", "",
"replace from, the from string used for the replace function")
flag.StringVar(&easygen.Opts.StrTo, "rt", "",
"replace to, the to string used for the replace function")
flag.IntVar(&easygen.Opts.Debug, "debug", 0,
"debugging `level`")

// Now override those default values from environment variables
if len(easygen.Opts.TemplateStr) == 0 ||
len(os.Getenv("EASYGEN_TS")) != 0 {
easygen.Opts.TemplateStr = os.Getenv("EASYGEN_TS")
}
if len(easygen.Opts.TemplateFile) == 0 ||
len(os.Getenv("EASYGEN_TF")) != 0 {
easygen.Opts.TemplateFile = os.Getenv("EASYGEN_TF")
}
if len(easygen.Opts.ExtYaml) == 0 ||
len(os.Getenv("EASYGEN_EY")) != 0 {
easygen.Opts.ExtYaml = os.Getenv("EASYGEN_EY")
}
if len(easygen.Opts.ExtTmpl) == 0 ||
len(os.Getenv("EASYGEN_ET")) != 0 {
easygen.Opts.ExtTmpl = os.Getenv("EASYGEN_ET")
}
if len(easygen.Opts.StrFrom) == 0 ||
len(os.Getenv("EASYGEN_RF")) != 0 {
easygen.Opts.StrFrom = os.Getenv("EASYGEN_RF")
}
if len(easygen.Opts.StrTo) == 0 ||
len(os.Getenv("EASYGEN_RT")) != 0 {
easygen.Opts.StrTo = os.Getenv("EASYGEN_RT")
}

}

////////////////////////////////////////////////////////////////////////////
// Main

func main() {
flag.Usage = easygen.Usage
flag.Parse()

// One mandatory non-flag arguments
if len(flag.Args()) < 1 {
easygen.Usage()
}
fileName := flag.Args()[0]
easygen.TFStringsInit()

fmt.Print(easygen.Generate(easygen.Opts.HTML, fileName))
}
48 changes: 48 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// !!! !!!
// WARNING: Code automatically generated. Editing discouraged.
// !!! !!!

package easygen

import (
"flag"
"fmt"
"os"
)

////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions

const progname = "easygen" // os.Args[0]

// The Options struct defines the structure to hold the commandline values
type Options struct {
HTML bool // treat the template file as html instead of text
TemplateStr string // template string (in text)
TemplateFile string // .tmpl template file `name` (default: same as .yaml file)
ExtYaml string // `extension` of yaml file
ExtTmpl string // `extension` of template file
StrFrom string // replace from, the from string used for the replace function
StrTo string // replace to, the to string used for the replace function
Debug int // debugging `level`
}

////////////////////////////////////////////////////////////////////////////
// Global variables definitions

// Opts holds the actual values from the command line parameters
var Opts = Options{ExtYaml: ".yaml", ExtTmpl: ".tmpl"}

////////////////////////////////////////////////////////////////////////////
// Commandline definitions

// Usage function shows help on commandline usage
func Usage() {
fmt.Fprintf(os.Stderr,
"\nUsage:\n %s [flags] YamlFileName\n\nFlags:\n\n",
progname)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr,
"\nYamlFileName: The name for the .yaml data and .tmpl template file\n\tOnly the name part, without extension. Can include the path as well.\n")
os.Exit(0)
}
131 changes: 116 additions & 15 deletions easygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,138 @@
////////////////////////////////////////////////////////////////////////////
// Program start

package main
package easygen

import (
"flag"
"bytes"
"fmt"
ht "html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
tt "text/template"

"github.com/suntong/easygen/easygenapi"
"gopkg.in/yaml.v2"
)

////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions

// common type for a *(text|html).Template value
type template interface {
Execute(wr io.Writer, data interface{}) error
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
Name() string
}

////////////////////////////////////////////////////////////////////////////
// Global variables definitions

////////////////////////////////////////////////////////////////////////////
// Commandline definitions
// Function definitions

////////////////////////////////////////////////////////////////////////////
// Main
// Generate2 will produce output according to the given template and driving data files
func Generate2(HTML bool, fileNameTempl string, fileName string) string {
Opts.TemplateFile = fileNameTempl
ret := Generate(HTML, fileName)
Opts.TemplateFile = ""
return ret
}

// Generate0 will produce output according from driving data without a template file
func Generate0(HTML bool, strTempl string, fileName string) string {
Opts.TemplateStr = strTempl
ret := Generate(HTML, fileName)
Opts.TemplateStr = ""
return ret
}

// Generate will produce output from the template according to driving data
func Generate(HTML bool, fileName string) string {
source, err := ioutil.ReadFile(fileName + Opts.ExtYaml)
checkError(err)

func main() {
flag.Usage = easygenapi.Usage
flag.Parse()
m := make(map[interface{}]interface{})

// One mandatory non-flag arguments
if len(flag.Args()) < 1 {
easygenapi.Usage()
err = yaml.Unmarshal(source, &m)
checkError(err)

// template file name
fileNameT := fileName
if len(Opts.TemplateFile) > 0 {
fileNameT = Opts.TemplateFile
}
fileName := flag.Args()[0]
easygenapi.TFStringsInit()

fmt.Print(easygenapi.Generate(easygenapi.Opts.HTML, fileName))
t, err := parseFiles(HTML, fileNameT+Opts.ExtTmpl)
checkError(err)

buf := new(bytes.Buffer)
err = t.Execute(buf, m)
checkError(err)

return buf.String()
}

// parseFiles, initialization. By Matt Harden @gmail.com
func parseFiles(HTML bool, filenames ...string) (template, error) {
tname := filepath.Base(filenames[0])

// use text template
funcMapHT := ht.FuncMap{
"minus1": minus1,
"cls2lc": cls2lc.String,
"cls2uc": cls2uc.String,
"cls2ss": cls2ss.String,
"ck2lc": ck2lc.String,
"ck2uc": ck2uc.String,
"ck2ls": ck2ls.String,
"ck2ss": ck2ss.String,
"clc2ss": clc2ss.String,
"cuc2ss": cuc2ss.String,
}

_ = funcMapHT

if HTML {
// use html template
t, err := ht.ParseFiles(filenames...)
//t, err := ht.New("HT").Funcs(funcMapHT).ParseFiles(filenames...)
return t, err
}

// use text template
funcMap := tt.FuncMap{
"eqf": strings.EqualFold,
"split": strings.Fields,
"minus1": minus1,
"replace": replace,
"replacec": replacec,
"cls2lc": cls2lc.String,
"cls2uc": cls2uc.String,
"cls2ss": cls2ss.String,
"ck2lc": ck2lc.String,
"ck2uc": ck2uc.String,
"ck2ls": ck2ls.String,
"ck2ss": ck2ss.String,
"clc2ss": clc2ss.String,
"cuc2ss": cuc2ss.String,
}

if len(Opts.TemplateStr) > 0 {
t, err := tt.New("TT").Funcs(funcMap).Parse(Opts.TemplateStr)
return t, err
}

t, err := tt.New(tname).Funcs(funcMap).ParseFiles(filenames...)
return t, err
}

// Exit if error occurs
func checkError(err error) {
if err != nil {
fmt.Printf("[%s] Fatal error - %v", progname, err.Error())
os.Exit(1)
}
}
Loading

0 comments on commit b387613

Please sign in to comment.