Skip to content

Commit

Permalink
Refactor buildmetadata to not require svn export
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 14, 2023
1 parent 08d3fe1 commit 5bae150
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ geocoding
buildmetadata
functions/*
dist/
_build
130 changes: 72 additions & 58 deletions cmd/buildmetadata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,77 @@ import (
"github.com/nyaruka/phonenumbers"
)

func main() {
if err := buildMetadata(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func buildMetadata() error {
fmt.Print("Cloning upstream repo... ")

if err := cloneUpstreamRepo("git@github.com:google/libphonenumber.git"); err != nil {
return err
}

fmt.Print("OK\nBuilding number metadata...")

metadata, err := buildNumberMetadata("resources/PhoneNumberMetadata.xml", "metadataData", "metadata_bin.go", false)
if err != nil {
return err
}

fmt.Print("OK\nBuilding short number metadata...")

_, err = buildNumberMetadata("resources/ShortNumberMetadata.xml", "shortNumberMetadataData", "shortnumber_metadata_bin.go", true)
if err != nil {
return err
}

fmt.Print("OK\nBuilding region metadata...")

buildRegions(metadata)
buildTimezones()
buildPrefixData(&carrier)
buildPrefixData(&geocoding)
return nil
}

func cloneUpstreamRepo(url string) error {
os.RemoveAll("_build")

cmd := exec.Command("git", "clone", "--depth=1", url, "_build")
if err := cmd.Run(); err != nil {
return fmt.Errorf("error cloning upstream repo: %w", err)
}

return nil
}

func buildNumberMetadata(src, varName, dst string, short bool) (*phonenumbers.PhoneMetadataCollection, error) {
body, err := os.ReadFile("_build/" + src)
if err != nil {
return nil, fmt.Errorf("error reading %s: %w", src, err)
}

collection, err := phonenumbers.BuildPhoneMetadataCollection(body, false, false, short)
if err != nil {
return nil, fmt.Errorf("error parsing %s: %w", src, err)
}

data, err := proto.Marshal(collection)
if err != nil {
return nil, fmt.Errorf("error marshaling metadata as protobuf: %w", err)
}

if err := os.WriteFile(dst, generateBinFile(varName, data), os.FileMode(0664)); err != nil {
return nil, fmt.Errorf("error writing metadata output: %w", err)
}

return collection, nil
}

type prefixBuild struct {
url string
dir string
Expand All @@ -32,12 +103,6 @@ type prefixBuild struct {
}

const (
metadataURL = "https://raw.githubusercontent.com/googlei18n/libphonenumber/master/resources/PhoneNumberMetadata.xml"
metadataPath = "metadata_bin.go"

shortNumberMetadataURL = "https://raw.githubusercontent.com/googlei18n/libphonenumber/master/resources/ShortNumberMetadata.xml"
shortNumberMetadataPath = "shortnumber_metadata_bin.go"

tzURL = "https://raw.githubusercontent.com/googlei18n/libphonenumber/master/resources/timezones/map_data.txt"
tzPath = "prefix_to_timezone_bin.go"
tzVar = "timezoneMapData"
Expand Down Expand Up @@ -122,7 +187,7 @@ func writeFile(filePath string, data []byte) {
}

fmt.Printf("Writing new %s\n", filePath)
err := ioutil.WriteFile(filePath, data, os.FileMode(0664))
err := os.WriteFile(filePath, data, os.FileMode(0664))
if err != nil {
log.Fatalf("Error writing '%s': %s", filePath, err)
}
Expand Down Expand Up @@ -246,48 +311,6 @@ func writeIntStringArrayMap(path string, varName string, prefixMap map[int][]str
writeFile(path, generateBinFile(varName, data.Bytes()))
}

func buildMetadata() *phonenumbers.PhoneMetadataCollection {
log.Println("Fetching PhoneNumberMetadata.xml from Github")
body := fetchURL(metadataURL)

log.Println("Building new metadata collection")
collection, err := phonenumbers.BuildPhoneMetadataCollection(body, false, false, false)
if err != nil {
log.Fatalf("Error converting XML: %s", err)
}

// write it out as a protobuf
data, err := proto.Marshal(collection)
if err != nil {
log.Fatalf("Error marshalling metadata: %v", err)
}

log.Println("Writing new metadata_bin.go")
writeFile(metadataPath, generateBinFile("metadataData", data))
return collection
}

func buildShortNumberMetadata() *phonenumbers.PhoneMetadataCollection {
log.Println("Fetching ShortNumberMetadata.xml from Github")
body := fetchURL(shortNumberMetadataURL)

log.Println("Building new short number metadata collection")
collection, err := phonenumbers.BuildPhoneMetadataCollection(body, false, false, true)
if err != nil {
log.Fatalf("Error converting XML: %s", err)
}

// write it out as a protobuf
data, err := proto.Marshal(collection)
if err != nil {
log.Fatalf("Error marshalling metadata: %v", err)
}

log.Println("Writing new metadata_bin.go")
writeFile(shortNumberMetadataPath, generateBinFile("shortNumberMetadataData", data))
return collection
}

// generates the file contents for a data file
func generateBinFile(variableName string, data []byte) []byte {
var compressed bytes.Buffer
Expand Down Expand Up @@ -476,12 +499,3 @@ func readMappingsForDir(dir string) map[int]string {
log.Printf("Read %d mappings in %s\n", len(mappings), dir)
return mappings
}

func main() {
metadata := buildMetadata()
buildShortNumberMetadata()
buildRegions(metadata)
buildTimezones()
buildPrefixData(&carrier)
buildPrefixData(&geocoding)
}
10 changes: 5 additions & 5 deletions prefix_to_carriers_bin.go

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions prefix_to_geocodings_bin.go

Large diffs are not rendered by default.

0 comments on commit 5bae150

Please sign in to comment.