|
| 1 | +// Copyright 2018 Nicholas Ng |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package golang |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + "go/format" |
| 21 | + "io" |
| 22 | + |
| 23 | + "go.nickng.io/asyncpi" |
| 24 | + "golang.org/x/tools/imports" |
| 25 | +) |
| 26 | + |
| 27 | +// FormatOptions defines options for changing |
| 28 | +// the format of generated code. |
| 29 | +type FormatOptions struct { |
| 30 | + Main bool |
| 31 | + Debug bool |
| 32 | + Format bool |
| 33 | + FmtStyle FormatStyle |
| 34 | +} |
| 35 | + |
| 36 | +// FormatStyle defines the tools to use for formatting code |
| 37 | +type FormatStyle int |
| 38 | + |
| 39 | +const ( |
| 40 | + // Gofmt is the option to use go/format style |
| 41 | + Gofmt FormatStyle = iota |
| 42 | + // GoImports is the option to use goimports style |
| 43 | + GoImports |
| 44 | +) |
| 45 | + |
| 46 | +// GenerateOpts writes Go code of the Process p to w using options opt. |
| 47 | +func GenerateOpts(p asyncpi.Process, opt FormatOptions, w io.Writer) error { |
| 48 | + var program bytes.Buffer |
| 49 | + const progHeader = `package main |
| 50 | + func main() { |
| 51 | +` |
| 52 | + const progFooter = ` |
| 53 | +}` |
| 54 | + |
| 55 | + if opt.Main { |
| 56 | + fmt.Fprintf(&program, progHeader) |
| 57 | + } |
| 58 | + if opt.Debug { |
| 59 | + fmt.Fprintf(&program, "// Process %s\n", p.Calculi()) |
| 60 | + fmt.Fprint(&program, `fmt.Fprintln(os.Stderr, "--- start ---");`) |
| 61 | + } |
| 62 | + if err := Generate(p, &program); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + if opt.Debug { |
| 66 | + fmt.Fprint(&program, `fmt.Fprintln(os.Stderr, "--- end ---");`) |
| 67 | + } |
| 68 | + if opt.Main { |
| 69 | + fmt.Fprint(&program, progFooter) |
| 70 | + } |
| 71 | + if opt.Format { |
| 72 | + switch opt.FmtStyle { |
| 73 | + case Gofmt: |
| 74 | + b, err := format.Source(program.Bytes()) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + _, err = w.Write(b) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + case GoImports: |
| 83 | + b, err := imports.Process("/tmp/tmp.go", program.Bytes(), &imports.Options{ |
| 84 | + Comments: true, |
| 85 | + Fragment: !opt.Main, |
| 86 | + TabIndent: true, |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + _, err = w.Write(b) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | + } |
| 98 | + _, err := io.Copy(w, &program) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
0 commit comments