Created
July 13, 2015 07:25
-
-
Save bradrydzewski/04f6b71c97e492c540a0 to your computer and use it in GitHub Desktop.
make.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build ignore | |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"strings" | |
"github.com/kr/text" | |
) | |
const dest = "dist" | |
func main() { | |
err := os.MkdirAll(dest, 0777) | |
err = dist() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
err = embedStyles() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
err = embedScripts() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
err = embedViews() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
// copy file to dist directory | |
func dist() error { | |
data, err := ioutil.ReadFile("index.html") | |
if err != nil { | |
return err | |
} | |
return ioutil.WriteFile("dist/index.html", data, 0777) | |
} | |
// embedViews is a helper function that embeds all angular views | |
// in the main index.html file. | |
func embedViews() error { | |
var buf_ bytes.Buffer | |
buf := text.NewIndentWriter(&buf_, []byte("\t")) | |
page, err := ioutil.ReadFile("dist/index.html") | |
if err != nil { | |
return err | |
} | |
pagestr := string(page) | |
views, err := filepath.Glob("scripts/views/*.html") | |
if err != nil { | |
return err | |
} | |
for _, view := range views { | |
v, err := ioutil.ReadFile(view) | |
if err != nil { | |
return err | |
} | |
o := fmt.Sprintf("<script type='text/ng-template' id='/%s'>", view) | |
c := "</script>" | |
buf.Write([]byte(o)) | |
buf.Write([]byte(v)) | |
buf.Write([]byte(c)) | |
} | |
pagestr = strings.Replace(pagestr, "<!--INJECT:VIEWS-->", buf_.String(), -1) | |
return ioutil.WriteFile("dist/index.html", []byte(pagestr), 0777) | |
} | |
// embedScripts is a helper function that takes all JavaScript | |
// files and combines into a single file, and writes to /dist/scripts.js | |
func embedScripts() error { | |
var buf bytes.Buffer | |
page, err := ioutil.ReadFile("dist/index.html") | |
if err != nil { | |
return err | |
} | |
pagestr := string(page) | |
paths, err := filepath.Glob("scripts/*/*.js") | |
if err != nil { | |
return err | |
} | |
paths = append([]string{"scripts/app.js"}, paths...) | |
for _, path := range paths { | |
s, err := ioutil.ReadFile(path) | |
if err != nil { | |
return err | |
} | |
o := "<script type='text/javascript'>" | |
c := "</script>" | |
buf.WriteString(o) | |
buf.Write(s) | |
buf.WriteString(c) | |
} | |
pagestr = strings.Replace(pagestr, "<!--INJECT:SCRIPTS-->", buf.String(), -1) | |
return ioutil.WriteFile("dist/index.html", []byte(pagestr), 0777) | |
} | |
// embedScripts is a helper function that takes all JavaScript | |
// files and combines into a single file, and writes to /dist/scripts.js | |
func embedStyles() error { | |
var buf bytes.Buffer | |
page, err := ioutil.ReadFile("dist/index.html") | |
if err != nil { | |
return err | |
} | |
pagestr := string(page) | |
paths, err := filepath.Glob("styles/*.css") | |
if err != nil { | |
return err | |
} | |
for _, path := range paths { | |
s, err := ioutil.ReadFile(path) | |
if err != nil { | |
return err | |
} | |
o := "<style>" | |
c := "</style>" | |
buf.WriteString(o) | |
buf.Write(s) | |
buf.WriteString(c) | |
} | |
pagestr = strings.Replace(pagestr, "<!--INJECT:STYLES-->", buf.String(), -1) | |
return ioutil.WriteFile("dist/index.html", []byte(pagestr), 0777) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment