github.com/naemono/pop@v4.13.1+incompatible/fix/anko.go (about) 1 package fix 2 3 import ( 4 "bytes" 5 "fmt" 6 "regexp" 7 "strings" 8 9 "github.com/gobuffalo/plush" 10 ) 11 12 // Anko converts old anko-form migrations to new plush ones. 13 func Anko(content string) (string, error) { 14 bb := &bytes.Buffer{} 15 16 lines := strings.Split(content, "\n") 17 l := len(lines) 18 fre := regexp.MustCompile(`,\s*func\(t\)\s*{`) 19 20 for i := 0; i < l; i++ { 21 line := lines[i] 22 tl := strings.TrimSpace(line) 23 if strings.HasPrefix(tl, "create_table") { 24 // skip already converted create_table 25 if fre.MatchString(line) { 26 // fix create_table 27 line = fre.ReplaceAllString(line, ") {") 28 ll := i 29 lines[i] = line 30 waitParen := false 31 for { 32 if strings.HasPrefix(tl, "})") { 33 line = "}" + tl[2:] 34 break 35 } else if strings.HasPrefix(tl, "}") { 36 // Now, we have to make sure to match the missing ")" 37 waitParen = true 38 } else if waitParen && strings.HasPrefix(tl, ")") { 39 line = tl[1:] 40 break 41 } 42 i++ 43 if l == i { 44 return "", fmt.Errorf("unclosed create_table statement line %d", ll+1) 45 } 46 line = lines[i] 47 tl = strings.TrimSpace(line) 48 } 49 } 50 } else if strings.HasPrefix(tl, "raw(") { 51 // fix raw 52 line = strings.Replace(line, "raw(", "sql(", -1) 53 } 54 lines[i] = line 55 } 56 57 body := strings.Join(lines, "\n") 58 59 if _, err := plush.Parse(body); err != nil { 60 return "", err 61 } 62 63 bb.WriteString(body) 64 65 return bb.String(), nil 66 }