github.com/holoplot/go-evdev@v0.0.0-20220721205823-d31c64b9d636/build/gen-codes/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "flag" 6 "fmt" 7 "io" 8 "net/http" 9 "os" 10 "regexp" 11 "strings" 12 "time" 13 14 "github.com/holoplot/go-evdev/build/gen-codes/parser" 15 ) 16 17 const ( 18 eventCodes = "include/uapi/linux/input-event-codes.h" 19 inputH = "include/uapi/linux/input.h" 20 ) 21 22 func downloadFile(url string) []byte { 23 client := http.Client{ 24 Timeout: time.Second * 10, 25 } 26 27 resp, err := client.Get(url) 28 if err != nil { 29 panic(fmt.Errorf("downlload \"%s\" file failed: %v", url, err)) 30 } 31 32 data, err := io.ReadAll(resp.Body) 33 if err != nil { 34 panic(fmt.Errorf("reading data of \"%s\" file failed: %v", url, err)) 35 } 36 37 return data 38 } 39 40 type Tag struct { 41 Ref string `json:"ref"` 42 } 43 44 func getTags() []string { 45 var tagRegex = regexp.MustCompile(`^refs/tags/(?P<version>v(\.?\d+)+)$`) 46 var tags []Tag 47 48 url := "https://api.github.com/repos/torvalds/linux/git/refs/tags" 49 err := json.Unmarshal(downloadFile(url), &tags) 50 if err != nil { 51 panic(fmt.Errorf("failed to parse json \"%s\" file: %v", url, err)) 52 } 53 54 var tagsString []string 55 56 for _, t := range tags { 57 match := tagRegex.FindStringSubmatch(t.Ref) 58 if match == nil { 59 continue 60 } 61 62 tagsString = append(tagsString, match[1]) 63 } 64 65 return tagsString 66 } 67 68 func main() { 69 var disableComments, showTags bool 70 var gitTag string 71 72 flag.BoolVar( 73 &disableComments, "disableComments", false, 74 "disable including comments in the output file", 75 ) 76 flag.StringVar(&gitTag, "tag", "", "select precise tag release of Linux") 77 flag.BoolVar(&showTags, "tags", false, "list available Linux tag releases") 78 flag.Parse() 79 80 fmt.Println("Fetching tags...") 81 tags := getTags() 82 83 if len(tags) == 0 { 84 fmt.Printf("no tags found\n") 85 os.Exit(1) 86 } 87 88 newestTag := tags[len(tags)-1] 89 90 fmt.Printf("The newest available tag: %s\n", newestTag) 91 if showTags { 92 for _, tag := range tags { 93 fmt.Printf("- %s\n", tag) 94 } 95 os.Exit(0) 96 } 97 98 var selectedTag = newestTag 99 if gitTag != "" { 100 var found bool 101 for _, tag := range tags { 102 if tag == gitTag { 103 found = true 104 break 105 } 106 } 107 if !found { 108 fmt.Printf("tag \"%s\" does not exist\n", gitTag) 109 os.Exit(1) 110 } 111 selectedTag = gitTag 112 fmt.Printf("found \"%s\" tag\n", gitTag) 113 fmt.Printf("Warning! This codegen was not tested with heavily outdated releases\n") 114 fmt.Printf("nor the code that depends on it.\n") 115 } 116 117 urlBase := "https://raw.githubusercontent.com/torvalds/linux/%s/%s" 118 inputHURL := fmt.Sprintf(urlBase, selectedTag, inputH) 119 eventCodesURL := fmt.Sprintf(urlBase, selectedTag, eventCodes) 120 121 fmt.Println("Downloading files...") 122 fmt.Printf("- %s\n- %s\n", inputHURL, eventCodesURL) 123 inputHContent := string(downloadFile(inputHURL)) 124 eventCodesContent := string(downloadFile(eventCodesURL)) 125 126 c1 := parser.NewCodeProcessor(parser.SelectedPrefixesGroups["input.h"]) 127 c2 := parser.NewCodeProcessor(parser.SelectedPrefixesGroups["input-event-codes.h"]) 128 129 fmt.Println("Processing files...") 130 131 elements1, err := c1.ProcessFile(strings.NewReader(inputHContent)) 132 if err != nil { 133 fmt.Printf("processing input.h file failed: %v\n", err) 134 os.Exit(1) 135 } 136 elements2, err := c2.ProcessFile(strings.NewReader(eventCodesContent)) 137 if err != nil { 138 fmt.Printf("processingi nput-event-codes.h file failed: %v\n", err) 139 os.Exit(1) 140 } 141 142 allElements := append(elements2, elements1...) 143 144 fmt.Println("Generating file...") 145 data := parser.GenerateFile(allElements, disableComments, selectedTag, inputHURL, eventCodesURL) 146 147 fmt.Println("Writing file...") 148 fileName := "codes.go" 149 fd, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) 150 if err != nil { 151 fmt.Printf("Failed to open %s file: %v", fileName, err) 152 os.Exit(1) 153 } 154 155 _, err = fd.WriteString(data) 156 if err != nil { 157 fmt.Printf("Failed to write data to %s file: %v", fileName, err) 158 os.Exit(1) 159 } 160 161 fmt.Println("Done!") 162 }