|
1 | 1 | package commands
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + json2 "encoding/json" |
4 | 7 | "fmt"
|
| 8 | + . "github.com/chermehdi/egor/config" |
| 9 | + "github.com/chermehdi/egor/templates" |
| 10 | + "github.com/fatih/color" |
5 | 11 | "github.com/urfave/cli/v2"
|
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "path" |
| 16 | + "strings" |
| 17 | + template2 "text/template" |
| 18 | + "time" |
6 | 19 | )
|
7 | 20 |
|
8 |
| -var ParseCommand cli.Command = cli.Command{ |
| 21 | +const listenAddr = ":4243" |
| 22 | + |
| 23 | +// Serialize task into a JSON string. |
| 24 | +func SerializeTask(meta EgorMeta) (string, error) { |
| 25 | + var buffer bytes.Buffer |
| 26 | + encoder := json2.NewEncoder(&buffer) |
| 27 | + if err := encoder.Encode(meta); err != nil { |
| 28 | + return "", err |
| 29 | + } |
| 30 | + return buffer.String(), nil |
| 31 | +} |
| 32 | + |
| 33 | +func CreateFile(filePath string) (*os.File, error) { |
| 34 | + return os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0777) |
| 35 | +} |
| 36 | + |
| 37 | +func CreateDirectoryStructure(task Task, config Config) error { |
| 38 | + dir, err := os.Getwd() |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + taskDir := path.Join(dir, task.Name) |
| 43 | + if err = os.Mkdir(taskDir, 0777); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + if err = os.Chdir(taskDir); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + egorMeta := NewEgorMeta(task, config) |
| 50 | + |
| 51 | + file, err := CreateFile(config.ConfigFileName) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + if err = egorMeta.Save(file); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + if err = os.Mkdir("inputs", 0777); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + if err = os.Mkdir("outputs", 0777); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + inputs := egorMeta.Inputs |
| 66 | + for i := 0; i < len(inputs); i++ { |
| 67 | + file, err := CreateFile(inputs[i].Path) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + _, err = file.WriteString(task.Tests[i].Input) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + outputs := egorMeta.Outputs |
| 78 | + for i := 0; i < len(outputs); i++ { |
| 79 | + file, err := CreateFile(outputs[i].Path) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + _, err = file.WriteString(task.Tests[i].Output) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + } |
| 88 | + templateContent, err := templates.ResolveTemplateByLanguage(config.Lang.Default) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + template := template2.New("Solution template") |
| 93 | + compiledTemplate, err := template.Parse(templateContent) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + taskFile, err := CreateFile(egorMeta.TaskFile) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + if err = compiledTemplate.Execute(taskFile, config); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// Given the json string returned by competitive companion |
| 108 | +// it will parse it as a json Task and return it. |
| 109 | +func extractTaskFromJson(json string) (*Task, error) { |
| 110 | + var task Task |
| 111 | + err := json2.Unmarshal([]byte(json), &task) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + return &task, nil |
| 116 | +} |
| 117 | + |
| 118 | +func createWebServer(quit chan<- string) *http.Server { |
| 119 | + router := http.NewServeMux() // here you could also go with third party packages to create a router |
| 120 | + // Register your routes |
| 121 | + router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 122 | + w.WriteHeader(http.StatusOK) |
| 123 | + content, err := ioutil.ReadAll(r.Body) |
| 124 | + if err != nil { |
| 125 | + panic(err) |
| 126 | + } |
| 127 | + quit <- string(content) |
| 128 | + }) |
| 129 | + return &http.Server{ |
| 130 | + Addr: listenAddr, |
| 131 | + Handler: router, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func waitForShutDown(server *http.Server, done chan<- string, quit <-chan string) { |
| 136 | + content := <-quit |
| 137 | + color.Magenta("Shutting down the server") |
| 138 | + |
| 139 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 140 | + defer cancel() |
| 141 | + |
| 142 | + server.SetKeepAlivesEnabled(false) |
| 143 | + |
| 144 | + if err := server.Shutdown(ctx); err != nil { |
| 145 | + color.Red("Could not shutdown the server") |
| 146 | + } |
| 147 | + done <- content |
| 148 | +} |
| 149 | + |
| 150 | +func ParseAction(context *cli.Context) error { |
| 151 | + msgReceiveChannel := make(chan string, 1) |
| 152 | + msgReadChannel := make(chan string, 1) |
| 153 | + |
| 154 | + server := createWebServer(msgReadChannel) |
| 155 | + |
| 156 | + go waitForShutDown(server, msgReceiveChannel, msgReadChannel) |
| 157 | + |
| 158 | + color.Green("Starting the server on %s\n", listenAddr) |
| 159 | + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 160 | + color.Red("Could not listen on %s, %v\n", listenAddr, err) |
| 161 | + } |
| 162 | + json := <-msgReceiveChannel |
| 163 | + color.Green("Server stopped") |
| 164 | + fmt.Printf("Received data %v\n", json) |
| 165 | + // first line contains a json string |
| 166 | + lines := strings.Split(json, "\n") |
| 167 | + task, err := extractTaskFromJson(lines[1]) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + fmt.Printf("task %v\n", task) |
| 172 | + config, err := LoadDefaultConfiguration() |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + err = CreateDirectoryStructure(*task, *config) |
| 177 | + if err != nil { |
| 178 | + color.Red("Error happened %v", err) |
| 179 | + } |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +// Command to parse tasks from the `Competitive Companion` Chrome extension. |
| 184 | +// Running this command, will start a server on the default port that the extension |
| 185 | +// uses, and will create a directory structure containing input files, expected output files, |
| 186 | +// and an additional `egor-meta.json` file, and finally your task file, which is usually a `main.cpp` or `Main.java` |
| 187 | +// file depending on the default configured language. |
| 188 | +var ParseCommand = cli.Command{ |
9 | 189 | Name: "parse",
|
10 | 190 | Aliases: []string{"p"},
|
11 |
| - Usage: "parse task from navigator", |
| 191 | + Usage: "Parse a task using 'Competitive Companion'", |
12 | 192 | UsageText: "parse task from navigator",
|
13 |
| - Action: func(context *cli.Context) error { |
14 |
| - fmt.Println("Hello from parse") |
15 |
| - return nil |
16 |
| - }, |
| 193 | + Action: ParseAction, |
17 | 194 | }
|
0 commit comments