diff --git a/cf.go b/cf.go index 1c0e4f75..83d60507 100644 --- a/cf.go +++ b/cf.go @@ -12,7 +12,7 @@ import ( docopt "github.com/docopt/docopt-go" ) -const version = "v0.7.2" +const version = "v0.8.0" func main() { usage := `Codeforces Tool $%version%$ (cf). https://github.com/xalanq/cf-tool diff --git a/client/parse.go b/client/parse.go index 6440fa41..1ff49fce 100644 --- a/client/parse.go +++ b/client/parse.go @@ -97,8 +97,8 @@ func (c *Client) ParseContestProblem(contestID, problemID, path string) (samples } // ParseContest parse for contest -func (c *Client) ParseContest(contestID, rootPath string, race bool) (err error) { - problems, err := c.StatisContest(contestID) +func (c *Client) ParseContest(contestID, rootPath string, race bool) (problems []StatisInfo, err error) { + problems, err = c.StatisContest(contestID) if err != nil { return } diff --git a/cmd/config.go b/cmd/config.go index 7f900038..e632c3ca 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -11,11 +11,12 @@ import ( func Config(args map[string]interface{}) error { color.Cyan("Configure the tool") cfg := config.New(config.ConfigPath) - ansi.Println("0) username and password") - ansi.Println("1) add a template") - ansi.Println("2) delete a template") - ansi.Println("3) set default template") - index := util.ChooseIndex(4) + ansi.Println(`0) username and password`) + ansi.Println(`1) add a template`) + ansi.Println(`2) delete a template`) + ansi.Println(`3) set default template`) + ansi.Println(`4) run "cf gen" after "cf parse"`) + index := util.ChooseIndex(5) if index == 0 { return cfg.Login(config.SessionPath) } else if index == 1 { @@ -24,6 +25,8 @@ func Config(args map[string]interface{}) error { return cfg.RemoveTemplate() } else if index == 3 { return cfg.SetDefaultTemplate() + } else if index == 4 { + return cfg.SetGenAfterParse() } return nil } diff --git a/cmd/gen.go b/cmd/gen.go index 8af14541..bec6aa5b 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -26,6 +26,34 @@ func parseTemplate(source string, cfg *config.Config) string { return source } +func readTemplateSource(path string, cfg *config.Config) (source string, err error) { + b, err := ioutil.ReadFile(path) + if err != nil { + return + } + source = parseTemplate(string(b), cfg) + return +} + +func gen(source, currentPath, ext string) error { + path := filepath.Join(currentPath, filepath.Base(currentPath)) + + savePath := path + ext + i := 1 + for _, err := os.Stat(savePath); err == nil; _, err = os.Stat(savePath) { + tmpPath := fmt.Sprintf("%v%v%v", path, i, ext) + fmt.Printf("%v exists. Rename to %v\n", filepath.Base(savePath), filepath.Base(tmpPath)) + savePath = tmpPath + i++ + } + + err := ioutil.WriteFile(savePath, []byte(source), 0644) + if err == nil { + color.Green("Generated! See %v", filepath.Base(savePath)) + } + return err +} + // Gen command func Gen(args map[string]interface{}) error { cfg := config.New(config.ConfigPath) @@ -54,30 +82,17 @@ func Gen(args map[string]interface{}) error { path = cfg.Template[cfg.Default].Path } - b, err := ioutil.ReadFile(path) + source, err := readTemplateSource(path, cfg) if err != nil { return err } - source := parseTemplate(string(b), cfg) + currentPath, err := os.Getwd() if err != nil { return err } - ext := filepath.Ext(path) - path = filepath.Join(currentPath, filepath.Base(currentPath)) - - savePath := path + ext - i := 1 - for _, err := os.Stat(savePath); err == nil; _, err = os.Stat(savePath) { - tmpPath := fmt.Sprintf("%v%v%v", path, i, ext) - fmt.Printf("%v exists. Rename to %v\n", filepath.Base(savePath), filepath.Base(tmpPath)) - savePath = tmpPath - i++ - } - err = ioutil.WriteFile(savePath, []byte(source), 0644) - if err == nil { - color.Green("Generated! See %v", filepath.Base(savePath)) - } + ext := filepath.Ext(path) + gen(source, currentPath, ext) return err } diff --git a/cmd/parse.go b/cmd/parse.go index 65b2bb93..288e7fac 100644 --- a/cmd/parse.go +++ b/cmd/parse.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "os" "path/filepath" "strings" @@ -17,7 +18,30 @@ func Parse(args map[string]interface{}) error { return err } cfg := config.New(config.ConfigPath) + source := "" + ext := "" + if cfg.GenAfterParse { + if len(cfg.Template) == 0 { + return errors.New("You have to add at least one code template by `cf config add`") + } + path := cfg.Template[cfg.Default].Path + ext = filepath.Ext(path) + if source, err = readTemplateSource(path, cfg); err != nil { + return err + } + } cln := client.New(config.SessionPath) + parseContest := func(contestID, rootPath string, race bool) error { + problems, err := cln.ParseContest(contestID, rootPath, race) + if err == nil && cfg.GenAfterParse { + for _, problem := range problems { + problemID := strings.ToLower(problem.ID) + path := filepath.Join(rootPath, problemID) + gen(source, path, ext) + } + } + return err + } work := func() error { contestID := "" problemID := "" @@ -25,7 +49,7 @@ func Parse(args map[string]interface{}) error { var ok bool if contestID, ok = args[""].(string); ok { if problemID, ok = args[""].(string); !ok { - return cln.ParseContest(contestID, filepath.Join(currentPath, contestID), args["race"].(bool)) + return parseContest(contestID, filepath.Join(currentPath, contestID), args["race"].(bool)) } problemID = strings.ToLower(problemID) path = filepath.Join(currentPath, contestID, problemID) @@ -39,7 +63,7 @@ func Parse(args map[string]interface{}) error { return err } if problemID == contestID { - return cln.ParseContest(contestID, currentPath, args["race"].(bool)) + return parseContest(contestID, currentPath, args["race"].(bool)) } } samples, err := cln.ParseContestProblem(contestID, problemID, path) @@ -48,6 +72,9 @@ func Parse(args map[string]interface{}) error { return err } color.Green("Parsed %v %v with %v samples", contestID, problemID, samples) + if cfg.GenAfterParse { + gen(source, path, ext) + } return nil } if err = work(); err != nil { diff --git a/config/config.go b/config/config.go index 97762d95..4c500945 100644 --- a/config/config.go +++ b/config/config.go @@ -21,11 +21,12 @@ type CodeTemplate struct { // Config load and save configuration type Config struct { - Username string `json:"username"` - Password string `json:"password"` - Template []CodeTemplate `json:"template"` - Default int `json:"default"` - path string + Username string `json:"username"` + Password string `json:"password"` + Template []CodeTemplate `json:"template"` + Default int `json:"default"` + GenAfterParse bool `json:"gen_after_parse"` + path string } // New an empty config diff --git a/config/misc.go b/config/misc.go new file mode 100644 index 00000000..19dec010 --- /dev/null +++ b/config/misc.go @@ -0,0 +1,11 @@ +package config + +import ( + "github.com/xalanq/cf-tool/util" +) + +// SetGenAfterParse set it yes or no +func (c *Config) SetGenAfterParse() (err error) { + c.GenAfterParse = util.YesOrNo(`Run "cf gen" after "cf test" (y/n)? `) + return c.save() +}