-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl "gocode" subcommand for outputting sample Go code using "gtree" … (
#175) * Impl "gocode" subcommand for outputting sample Go code using "gtree" package * nits * Goパッケージの依存関係リストからツリー生成するGoプログラムを出力 * update README * update README
- Loading branch information
Showing
4 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
tmp.md | ||
go_dependences.txt | ||
tmp/ | ||
coverage.* | ||
gtreetest/ | ||
root/ | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type gocode string | ||
|
||
func (g gocode) print() error { | ||
_, err := fmt.Print(strings.Trim(string(g), "\n")) | ||
return err | ||
} | ||
|
||
func (g gocode) println() error { | ||
if err := g.print(); err != nil { | ||
return err | ||
} | ||
_, err := fmt.Println() | ||
return err | ||
} | ||
|
||
const findToTree gocode = ` | ||
package main | ||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"github.com/ddddddO/gtree" | ||
) | ||
// $ gtree gocode > find_to_tree.go && go mod init xxx 2>/dev/null && go mod tidy 2>/dev/null && find . -type d -o -type f -print | go run find_to_tree.go | ||
func main() { | ||
var ( | ||
root *gtree.Node | ||
node *gtree.Node | ||
) | ||
scanner := bufio.NewScanner(os.Stdin) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
splited := strings.Split(line, "/") | ||
for i, s := range splited { | ||
if root == nil { | ||
root = gtree.NewRoot(s) | ||
node = root | ||
continue | ||
} | ||
if i == 0 { | ||
continue | ||
} | ||
tmp := node.Add(s) | ||
node = tmp | ||
} | ||
node = root | ||
} | ||
if err := gtree.OutputProgrammably(os.Stdout, root); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
` | ||
|
||
const goDependencesToTree gocode = ` | ||
package main | ||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"github.com/ddddddO/gtree" | ||
) | ||
// $ ls | grep go.mod && go list -deps ./path/to/go_dir > go_dependences.txt | ||
// $ mkdir tmp && cd tmp && gtree gocode --godeps-to-tree > godeps_to_tree.go && go mod init xxx 2>/dev/null && go mod tidy 2>/dev/null && cat ../go_dependences.txt | go run godeps_to_tree.go | ||
func main() { | ||
var ( | ||
root = gtree.NewRoot("[All Dependencies]") | ||
node *gtree.Node | ||
) | ||
scanner := bufio.NewScanner(os.Stdin) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
splited := strings.Split(line, "/") | ||
for i, s := range splited { | ||
if i == 0 { | ||
node = root.Add(s) | ||
continue | ||
} | ||
node = node.Add(s) | ||
} | ||
} | ||
if err := gtree.OutputProgrammably(os.Stdout, root); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
` |
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