Skip to content

Commit

Permalink
more fixes from second round of comments. Almost there. I'm now
Browse files Browse the repository at this point in the history
getting the entire dep tree, but something still not working, I
think due to picking up lower version when a component is multiply
depended on.
  • Loading branch information
mco-gh committed Feb 15, 2018
1 parent 1110932 commit 35deefa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
14 changes: 9 additions & 5 deletions claat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
prefix = flag.String("prefix", "../../", "URL prefix for html format")
globalGA = flag.String("ga", "UA-49880327-14", "global Google Analytics account")
extra = flag.String("extra", "", "Additional arguments to pass to format templates. JSON object of string,string key values.")
addr = flag.String("addr", "localhost:9090", "hostname and port to bind web server to")

version string // set by linker -X
)
Expand Down Expand Up @@ -137,7 +138,7 @@ func usage() {
flag.PrintDefaults()
}

const usageText = `Usage: claat <cmd> [export flags] src [src ...]
const usageText = `Usage: claat <cmd> [options] src [src ...]
Available commands are: export, serve, update, version.
Expand Down Expand Up @@ -173,10 +174,13 @@ The program exits with non-zero code if at least one src could not be exported.
## Serve command
Serve provides a simple web server for viewing exported codelabs.
It takes no arguments and, with no path specified, it presents the
current directory contents. Clicking on a directory representing
an exported codelab will load all the required dependencies and render
the generated codelab as it would appear in production.
It takes no arguments and presents the current directory contents.
Clicking on a directory representing an exported codelab will load
all the required dependencies and render the generated codelab as
it would appear in production.
The serve command takes a -addr host:port option, to specify the
desired hostname or IP address and port number to bind to.
## Update command
Expand Down
78 changes: 45 additions & 33 deletions claat/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func cmdServe() {
return
}
// Go get the dependencies.
err = fetchRepo(depsDir, "googlecodelabs/codelab-components#^1.0.0")
err = fetchRepo(depsDir, "googlecodelabs/codelab-components#2.0.2")
if err != nil {
fmt.Println(err)
return
}
//os.Rename(depsDir+"/codelab-components", depsDir+"/google-codelab-elements")
//os.Rename(depsDir+"/code-prettify", depsDir+"/google-prettify")
os.Rename(depsDir+"/codelab-components", depsDir+"/google-codelab-elements")
os.Rename(depsDir+"/code-prettify", depsDir+"/google-prettify")
err = os.MkdirAll(elemDir, 0755)
if err != nil {
fmt.Println(err)
Expand All @@ -61,15 +61,13 @@ func cmdServe() {
log.Fatal("Cannot create file", err)
}
defer f.Close()
fmt.Fprintf(f, codelabElem)
f.WriteString(codelabElem)
}
fmt.Println("Dependencies installed.")

port := "9090"
http.Handle("/", http.FileServer(http.Dir(".")))
fmt.Println("Serving on localhost:" + port + ", opening browser tab now...")
openBrowser("http://127.0.0.1:" + port)
err = http.ListenAndServe(":"+port, nil) // set listen port
fmt.Println("Serving on localhost:" + *addr + ", opening browser tab now...")
openBrowser(*addr)
err = http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
Expand All @@ -78,6 +76,7 @@ func cmdServe() {
// downloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func downloadFile(filepath string, url string) error {
fmt.Println("Downloading " + url)
out, err := os.Create(filepath)
if err != nil {
return err
Expand All @@ -95,28 +94,51 @@ func downloadFile(filepath string, url string) error {
return nil
}

func fetchRepo(depsDir string, repoVers string) error {
// parse url into repoName and version
s := strings.Split(repoVers, "#^")
vers := ""
func fetchRepo(depsDir string, spec string) error {
if spec == "^0.7.2" {
spec = "webcomponents/webcomponentsjs#^0.7.2"
}
var user, repo, path, vers string
s := strings.Split(spec, "#")
path = s[0]
if len(s) > 1 {
vers = s[1]
if s[1][0] == '^' {
vers = s[1][1:]
}
} else {
vers = "master"
}
s = strings.Split(path, "/")
user = s[0]
if len(s) > 1 {
repo = s[1]
}
s = strings.Split(s[0], "/")
user := s[0]
repo := s[1]

// if repo already exists locally, return immediately, we're done.
if _, err := os.Stat(depsDir + "/" + repo); os.IsExist(err) {
if _, err := os.Stat(depsDir + "/" + repo); err == nil {
return nil
}
url := "https://github.com/" + user + "/" + repo + "/archive/" + vers + ".zip"
zipFile := depsDir + "/" + repo + ".zip"
fmt.Println("Downloading " + url)
url := "https://github.com/" + user + "/" + repo + "/archive/v" + vers + ".zip"
err := downloadFile(zipFile, url)
if err != nil {
return err
}
// If get fails, it will download a file containing only "404: Not Found".
// We check for that case by looking for an unusally small file.
var st os.FileInfo
if st, err = os.Stat(zipFile); err != nil {
return err
}
if st.Size() < 20 {
os.Remove(zipFile)
url = "https://github.com/" + user + "/" + repo + "/archive/" + vers + ".zip"
err = downloadFile(zipFile, url)
if err != nil {
return err
}
}
err = unzip(zipFile, depsDir)
if err != nil {
return err
Expand All @@ -134,21 +156,14 @@ func fetchRepo(depsDir string, repoVers string) error {
if err != nil {
return err
}
type Dep struct {
Name string
RepoVers string
var b struct {
Dependencies map[string]string
}
var objmap map[string]*json.RawMessage
err = json.Unmarshal(raw, &objmap)
err = json.Unmarshal(raw, &b)
if err != nil {
return err
}
var deps map[string]string
err = json.Unmarshal(*objmap["dependencies"], &deps)
if err != nil {
return err
}
for _, v := range deps {
for _, v := range b.Dependencies {
err = fetchRepo(depsDir, v)
if err != nil {
return err
Expand All @@ -172,9 +187,7 @@ func unzip(src, dest string) error {
return err
}
defer rc.Close()

path := filepath.Join(dest, f.Name)

if f.FileInfo().IsDir() {
return os.MkdirAll(path, f.Mode())
}
Expand All @@ -183,7 +196,6 @@ func unzip(src, dest string) error {
if err != nil {
return err
}

_, err = io.Copy(f2, rc)
// Catch both io.Copy and file.Close errors but prefer returning the former.
if err1 := f2.Close(); err1 != nil && err == nil {
Expand Down

0 comments on commit 35deefa

Please sign in to comment.