|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path" |
| 6 | + template2 "text/template" |
| 7 | + |
| 8 | + "github.com/chermehdi/egor/config" |
| 9 | + "github.com/chermehdi/egor/templates" |
| 10 | + "github.com/fatih/color" |
| 11 | + "io/ioutil" |
| 12 | + |
| 13 | + "github.com/urfave/cli/v2" |
| 14 | +) |
| 15 | + |
| 16 | +type Batcher struct { |
| 17 | + judge Judge |
| 18 | +} |
| 19 | + |
| 20 | +func (b *Batcher) Setup() error { |
| 21 | + b.judge.Setup() |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +func (b *Batcher) Run() error { |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +func (b *Batcher) Cleanup() error { |
| 30 | + b.judge.Cleanup() |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +func CreateBatch(context *cli.Context) error { |
| 35 | + configuration, err := config.LoadDefaultConfiguration() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + cwd, err := os.Getwd() |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + egorMetaFile := path.Join(cwd, configuration.ConfigFileName) |
| 44 | + egorMeta, err := config.LoadMetaFromPath(egorMetaFile) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + if egorMeta.HasBatch() { |
| 50 | + color.Red("The task already has a batch file, Aborting...") |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + genPath := path.Join(cwd, "gen.cpp") |
| 55 | + egorMeta.BatchFile = genPath |
| 56 | + // Create the generator file |
| 57 | + |
| 58 | + genTemp := template2.New("Solution template") |
| 59 | + compiledTemplate, err := genTemp.Parse(templates.GeneratorTemplate) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + genFile, err := config.CreateFile(genPath) |
| 64 | + if err != nil { |
| 65 | + return nil |
| 66 | + } |
| 67 | + // TODO(chermehdi): Move this outside, it smells of code repetition |
| 68 | + if err = compiledTemplate.Execute(genFile, configuration); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + randH := path.Join(cwd, "rand.h") |
| 72 | + // Create the rand.h file |
| 73 | + if err = ioutil.WriteFile(randH, []byte(templates.RandH), 0755); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + // Update the metadata |
| 77 | + fileName := path.Join(cwd, configuration.ConfigFileName) |
| 78 | + if err = egorMeta.SaveToFile(fileName); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + color.Green("Batch creation completed successfully") |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func RunBatch(context *cli.Context) error { |
| 86 | + configuration, err := config.LoadDefaultConfiguration() |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + cwd, err := os.Getwd() |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + egorMetaFile := path.Join(cwd, configuration.ConfigFileName) |
| 95 | + egorMeta, err := config.LoadMetaFromPath(egorMetaFile) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + judge, err := NewJudgeFor(egorMeta, configuration, getChecker(context.String("checker"))) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + if err := judge.Setup(); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + defer judge.Cleanup() |
| 108 | + report := newJudgeReport() |
| 109 | + |
| 110 | + for i, input := range egorMeta.Inputs { |
| 111 | + output := egorMeta.Outputs[i] |
| 112 | + caseDescription := NewCaseDescription(input, output, egorMeta.TimeLimit) |
| 113 | + status := judge.RunTestCase(*caseDescription) |
| 114 | + report.Add(status, *caseDescription) |
| 115 | + } |
| 116 | + report.Display() |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +var BatchCommand = cli.Command{ |
| 121 | + Name: "batch", |
| 122 | + Aliases: []string{"b"}, |
| 123 | + Usage: "Create and Run bach tests", |
| 124 | + UsageText: "egor batch (create | run)", |
| 125 | + Subcommands: []*cli.Command{ |
| 126 | + { |
| 127 | + Name: "create", |
| 128 | + Aliases: []string{"c"}, |
| 129 | + Usage: "Create the template for the batch test", |
| 130 | + Action: CreateBatch, |
| 131 | + }, |
| 132 | + { |
| 133 | + Name: "run", |
| 134 | + Aliases: []string{"r"}, |
| 135 | + Usage: "Run the batch test", |
| 136 | + Action: RunBatch, |
| 137 | + }, |
| 138 | + }, |
| 139 | +} |
0 commit comments