From 1d932995f6fc94deb26e8a7281c92a315c5fd74d Mon Sep 17 00:00:00 2001 From: peterzhu1992 Date: Tue, 24 May 2022 00:08:09 -0400 Subject: [PATCH] Add isDir() checks for measure.go and generate.go in tools Signed-off-by: peterzhu1992 --- examples/embed-directive/embed-directive.go | 6 +- examples/embed-directive/embed-directive.hash | 4 +- examples/embed-directive/embed-directive.sh | 7 +- public/embed-directive | 102 +++++++++--------- public/generics | 2 +- public/index.html | 4 +- public/struct-embedding | 4 +- public/temporary-files-and-directories | 4 +- public/testing-and-benchmarking | 2 +- tools/generate.go | 21 ++-- tools/measure.go | 25 +++-- 11 files changed, 99 insertions(+), 82 deletions(-) diff --git a/examples/embed-directive/embed-directive.go b/examples/embed-directive/embed-directive.go index 2424519..d7b1c13 100644 --- a/examples/embed-directive/embed-directive.go +++ b/examples/embed-directive/embed-directive.go @@ -27,13 +27,13 @@ var folder_FS embed.FS func main() { - // Print out the content of `example_single_file.txt` as `string`. + // Print out content of `example_single_file.txt` as `string`. print(single_file_string) - // Now handle `[]byte`.` + // Now handle `[]byte`. print(string(single_file_byte)) - // Retrieve the file(s) matching `*.hash` pattern by reading from `folder_FS` first, + // Retrieve file(s) matching `*.hash` pattern by reading from variable `folder_FS` first, // then print out. hash_file1 := "example_folder/multi_file1.hash" hash_file2 := "example_folder/multi_file2.hash" diff --git a/examples/embed-directive/embed-directive.hash b/examples/embed-directive/embed-directive.hash index f749cc5..bf62e8b 100644 --- a/examples/embed-directive/embed-directive.hash +++ b/examples/embed-directive/embed-directive.hash @@ -1,2 +1,2 @@ -33356517e565ff2d1f9d7f935012a326e8f5b3ea -yTsQsPe9jth +e996755d889f01c99c353616fb2bdeac6c3be6e6 +-EXJc75EbN- diff --git a/examples/embed-directive/embed-directive.sh b/examples/embed-directive/embed-directive.sh index 5174a03..f6937db 100644 --- a/examples/embed-directive/embed-directive.sh +++ b/examples/embed-directive/embed-directive.sh @@ -1,7 +1,6 @@ -# Use these commands to run the example -# Note: due to limitation on go playground -# This example can only be run on your local machine - +# Use these commands to run the example. +# (Note: due to limitation on go playground, +# this example can only be run on your local machine.) $ mkdir -p example_folder $ echo "hello go" > example_folder/single_file.txt $ echo "123" > example_folder/multi_file1.hash diff --git a/public/embed-directive b/public/embed-directive index 32703e9..35536bb 100644 --- a/public/embed-directive +++ b/public/embed-directive @@ -9,12 +9,12 @@ onkeydown = (e) => { if (e.key == "ArrowLeft") { - window.location.href = 'struct-embedding'; + window.location.href = 'temporary-files-and-directories'; } if (e.key == "ArrowRight") { - window.location.href = 'generics'; + window.location.href = 'testing-and-benchmarking'; } } @@ -27,26 +27,13 @@ -

Starting in Go version 1.16+, Go supports use of //go:embed -which is a directive to embed files and folders into -the application binary by the compiler.

- - - - - - - - - - -

Since Go does not have preprocessor or macros like C type of languages, -pragmas/directives are implemented by the compiler as comments //. -Using //go:embed tells the compiler to include arbitrary files/folders in binary.

+

//go:embed is a compiler directive that allows programs to include arbitrary +files/folders in the binary. Read more about go directive +here.

- +
 package main
 
@@ -55,17 +42,17 @@ Using //go:embed tells the compiler to include arbitrary files/fold -

If you are not using any exported identifiers from embed package directly, -you can add an underscore _ before the package name, ask Go to import embed -without directly using it, like this: _ "embed". In this example, we remove the -underscore as we need to use the embed.FS type from the package.

+

If no exported identifiers is directly used from embed package, +you can add an underscore _ before the package name. In this example, we simply +import it as we need to use the embed.FS type from the package.

 import (
-    "embed"
+    //_ "embed"
+    "embed"
 )
 
@@ -80,7 +67,7 @@ this single file, followed by variable single_file_string to access
-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
 var single_file_string string
 
@@ -88,15 +75,13 @@ this single file, followed by variable single_file_string to access -

Here is a similar example but include single file as []byte. -Note that //go:embed can only be used in package level, -define it in any function will result in error.

+

Here is a similar example but include single file as []byte.

-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
 var single_file_byte []byte
 
@@ -104,17 +89,14 @@ define it in any function will result in error.

-

We can also embed multiple files or even folders with wildcard. -Since Go resolves these files and folders during compile time, we have to use the files in -current working directory as examples. In practice, you can embed files with "//go:embed /path/to/folder" -or with another example "//go:embed /path/to/folder/*". You can add multiple //go:embed before defining a variable.

+

We can also embed multiple files or even folders with wildcard.

-//go:embed *.hash
-//go:embed embed-directive.sh
+//go:embed example_folder/single_file.txt
+//go:embed example_folder/*.hash
 var folder_FS embed.FS
 
@@ -133,42 +115,45 @@ or with another example "//go:embed /path/to/folder/*". Y -

Print out the content of embed-directive.sh as string.

+

Print out content of example_single_file.txt as string.

-    println(single_file_string)
+    print(single_file_string)
 
-

Print out the content of embed-directive.sh as []byte.

+

Now handle []byte.

-    println(string(single_file_byte))
+    print(string(single_file_byte))
 
-

Print out the content of embed-directive.hash by reading the file from folder_FS first, -then print out the content.

+

Retrieve file(s) matching *.hash pattern by reading from variable folder_FS first, +then print out.

-    hash_file := "embed-directive.hash"
-    hash_content, _ := folder_FS.ReadFile(hash_file)
-    println(string(hash_content))
+    hash_file1 := "example_folder/multi_file1.hash"
+    hash_file2 := "example_folder/multi_file2.hash"
+    hash_content1, _ := folder_FS.ReadFile(hash_file1)
+    hash_content2, _ := folder_FS.ReadFile(hash_file2)
+    print(string(hash_content1))
+    print(string(hash_content2))
 
@@ -190,13 +175,32 @@ then print out the content.

-

Use these commands to run the example

+

Use these commands to run the example. +(Note: due to limitation on go playground, +this example can only be run on your local machine.)

+ + + +
+$ mkdir -p example_folder
+$ echo "hello go" > example_folder/single_file.txt
+$ echo "123" > example_folder/multi_file1.hash
+$ echo "456" > example_folder/multi_file2.hash
+ + + + + + -
-$ go run embed-directive.go
+
$ go run embed-directive.go
+hello go
+hello go
+123
+456
@@ -204,7 +208,7 @@ then print out the content.

- Next example: Generics. + Next example: Testing and Benchmarking.

@@ -215,7 +219,7 @@ then print out the content.

diff --git a/public/generics b/public/generics index 2cfe61d..2a21093 100644 --- a/public/generics +++ b/public/generics @@ -9,7 +9,7 @@ onkeydown = (e) => { if (e.key == "ArrowLeft") { - window.location.href = 'embed-directive'; + window.location.href = 'struct-embedding'; } diff --git a/public/index.html b/public/index.html index b26b680..83f47ea 100644 --- a/public/index.html +++ b/public/index.html @@ -71,8 +71,6 @@
  • Struct Embedding
  • -
  • Embed Directive
  • -
  • Generics
  • Errors
  • @@ -163,6 +161,8 @@
  • Temporary Files and Directories
  • +
  • Embed Directive
  • +
  • Testing and Benchmarking
  • Command-Line Arguments
  • diff --git a/public/struct-embedding b/public/struct-embedding index 086bfdf..176a974 100644 --- a/public/struct-embedding +++ b/public/struct-embedding @@ -14,7 +14,7 @@ if (e.key == "ArrowRight") { - window.location.href = 'embed-directive'; + window.location.href = 'generics'; } } @@ -233,7 +233,7 @@ we see that a container now implements the

    - Next example: Embed Directive. + Next example: Generics.

    diff --git a/public/temporary-files-and-directories b/public/temporary-files-and-directories index 91ca3a6..2e8cd85 100644 --- a/public/temporary-files-and-directories +++ b/public/temporary-files-and-directories @@ -14,7 +14,7 @@ if (e.key == "ArrowRight") { - window.location.href = 'testing-and-benchmarking'; + window.location.href = 'embed-directive'; } } @@ -225,7 +225,7 @@ prefixing them with our temporary directory.

    - Next example: Testing and Benchmarking. + Next example: Embed Directive.

    diff --git a/public/testing-and-benchmarking b/public/testing-and-benchmarking index 975dbb2..84918d8 100644 --- a/public/testing-and-benchmarking +++ b/public/testing-and-benchmarking @@ -9,7 +9,7 @@ onkeydown = (e) => { if (e.key == "ArrowLeft") { - window.location.href = 'temporary-files-and-directories'; + window.location.href = 'embed-directive'; } diff --git a/tools/generate.go b/tools/generate.go index 7616c25..fdbe7a0 100644 --- a/tools/generate.go +++ b/tools/generate.go @@ -36,6 +36,11 @@ func check(err error) { } } +func isDir(path string) bool { + fileStat, _ := os.Stat(path) + return fileStat.IsDir() +} + func ensureDir(dir string) { err := os.MkdirAll(dir, 0755) check(err) @@ -275,14 +280,16 @@ func parseExamples() []*Example { example.Segs = make([][]*Seg, 0) sourcePaths := mustGlob("examples/" + exampleID + "/*") for _, sourcePath := range sourcePaths { - if strings.HasSuffix(sourcePath, ".hash") { - example.GoCodeHash, example.URLHash = parseHashFile(sourcePath) - } else { - sourceSegs, filecontents := parseAndRenderSegs(sourcePath) - if filecontents != "" { - example.GoCode = filecontents + if ! isDir(sourcePath) { + if strings.HasSuffix(sourcePath, ".hash") { + example.GoCodeHash, example.URLHash = parseHashFile(sourcePath) + } else { + sourceSegs, filecontents := parseAndRenderSegs(sourcePath) + if filecontents != "" { + example.GoCode = filecontents + } + example.Segs = append(example.Segs, sourceSegs) } - example.Segs = append(example.Segs, sourceSegs) } } newCodeHash := sha1Sum(example.GoCode) diff --git a/tools/measure.go b/tools/measure.go index 7c9d6be..8be80d7 100644 --- a/tools/measure.go +++ b/tools/measure.go @@ -21,6 +21,11 @@ func readLines(path string) []string { return strings.Split(string(srcBytes), "\n") } +func isDir(path string) bool { + fileStat, _ := os.Stat(path) + return fileStat.IsDir() +} + var commentPat = regexp.MustCompile("\\s*\\/\\/") func main() { @@ -29,15 +34,17 @@ func main() { foundLongFile := false for _, sourcePath := range sourcePaths { foundLongLine := false - lines := readLines(sourcePath) - for i, line := range lines { - // Convert tabs to spaces before measuring, so we get an accurate measure - // of how long the output will end up being. - line := strings.Replace(line, "\t", " ", -1) - if !foundLongLine && !commentPat.MatchString(line) && (utf8.RuneCountInString(line) > 58) { - fmt.Printf("measure: %s:%d\n", sourcePath, i+1) - foundLongLine = true - foundLongFile = true + if ! isDir(sourcePath) { + lines := readLines(sourcePath) + for i, line := range lines { + // Convert tabs to spaces before measuring, so we get an accurate measure + // of how long the output will end up being. + line := strings.Replace(line, "\t", " ", -1) + if !foundLongLine && !commentPat.MatchString(line) && (utf8.RuneCountInString(line) > 58) { + fmt.Printf("measure: %s:%d\n", sourcePath, i+1) + foundLongLine = true + foundLongFile = true + } } } }