Exemplo n.º 1
0
// This wraps ego to make sure we use the vendored one.
// TODO vendor ego :D
func main() {
	outfile := flag.String("o", "ego.go", "output file")
	pkgname := flag.String("package", "", "package name")
	flag.Parse()
	log.SetFlags(0)

	// If no paths are provided then use the present working directory.
	roots := flag.Args()
	if len(roots) == 0 {
		roots = []string{"."}
	}

	// If no package name is set then use the directory name of the output file.
	if *pkgname == "" {
		abspath, _ := filepath.Abs(*outfile)
		*pkgname = filepath.Base(filepath.Dir(abspath))
		*pkgname = regexp.MustCompile(`(\w+).*`).ReplaceAllString(*pkgname, "$1")
	}

	// Recursively retrieve all ego templates
	var v visitor
	for _, root := range roots {
		if err := filepath.Walk(root, v.visit); err != nil {
			scanner.PrintError(os.Stderr, err)
			os.Exit(1)
		}
	}

	// Parse every *.ego file.
	var templates []*ego.Template
	for _, path := range v.paths {
		t, err := ego.ParseFile(path)
		if err != nil {
			log.Fatal("parse file: ", err)
		}
		templates = append(templates, t)
	}

	// If we have no templates then exit.
	if len(templates) == 0 {
		os.Exit(0)
	}

	// Write package to output file.
	p := &ego.Package{Templates: templates, Name: *pkgname}
	f, err := os.Create(*outfile)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	buf := &bytes.Buffer{}
	if err := p.Write(buf); err != nil {
		log.Fatalf("write: %s", err)
	}

	err = func() error {
		r := bufio.NewReader(buf)
		for {
			line, err := r.ReadString('\n')
			switch err {
			case io.EOF:
				return nil
			case nil:
				if !strings.HasPrefix(line, "// Generated by") {
					_, err = io.WriteString(f, line)
					if err != nil {
						return err
					}
				}
			default:
				return err
			}
		}
	}()
	if err != nil {
		log.Fatal(err)
	}
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: varding/ego
func main() {
	outfile := flag.String("o", "ego.go", "output file")
	pkgname := flag.String("package", "", "package name")
	versionFlag := flag.Bool("version", false, "print version")
	extFlag := flag.String("ext", ".ego", "all file extensions to scan,use '|' for separation")
	flag.Parse()
	log.SetFlags(0)

	//parse extentions
	exts := strings.Split(*extFlag, "|")
	for _, ext := range exts {
		ext = strings.TrimSpace(ext)
		if len(ext) == 0 {
			continue
		}
		if ext[0] != '.' {
			ext = "." + ext
		}
		extensions = append(extensions, ext)
	}

	// If the version flag is set then print the version.
	if *versionFlag {
		fmt.Printf("ego v%s\n", version)
		return
	}

	// If no paths are provided then use the present working directory.
	roots := flag.Args()
	if len(roots) == 0 {
		roots = []string{"."}
	}

	// If no package name is set then use the directory name of the output file.
	if *pkgname == "" {
		abspath, _ := filepath.Abs(*outfile)
		*pkgname = filepath.Base(filepath.Dir(abspath))
		*pkgname = regexp.MustCompile(`(\w+).*`).ReplaceAllString(*pkgname, "$1")
	}

	// Recursively retrieve all ego templates
	var v visitor
	for _, root := range roots {
		if err := filepath.Walk(root, v.visit); err != nil {
			scanner.PrintError(os.Stderr, err)
			os.Exit(1)
		}
	}

	// Parse every *.ego file.
	var templates []*ego.Template
	for _, path := range v.paths {
		t, err := ego.ParseFile(path)
		if err != nil {
			log.Fatal("parse file: ", err)
		}
		templates = append(templates, t)
	}

	// If we have no templates then exit.
	if len(templates) == 0 {
		os.Exit(0)
	}

	// Write package to output file.
	p := &ego.Package{Templates: templates, Name: *pkgname}
	f, err := os.Create(*outfile)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	// Write template to file.
	if err := p.Write(f); err != nil {
		log.Fatal("write: ", err)
	}
}