Skip to content

Commit

Permalink
UWP handler refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielrobert committed Apr 6, 2018
1 parent 473b4ca commit a8c28ab
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
6 changes: 1 addition & 5 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/urfave/cli"
)

var handlers = []packageHandler{iOSHandler{}, androidHandler{}}
var handlers = []packageHandler{iOSHandler{}, androidHandler{}, uwpHandler{}}

// called by executing `xavtool current`
func current(c *cli.Context) error {
Expand Down Expand Up @@ -132,8 +132,4 @@ func setVersion(file packageInfo, version string) {
handler.changePackageVersion(file, version)
}
}

if isUWPPackage(file.Path) {
changeUWPPackageVersion(file, version)
}
}
4 changes: 0 additions & 4 deletions finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func findManifests(root string, handlers []packageHandler) ([]packageInfo, error
}
}

if isUWPPackage(f.Name()) {
fileList = append(fileList, getUWPPackageInfo(path))
}

return nil
})

Expand Down
2 changes: 1 addition & 1 deletion finders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Test_FindManifests(t *testing.T) {
dir, err := os.Getwd()
check(err)

foundFiles, err := findManifests(dir, []packageHandler{iOSHandler{}, androidHandler{}})
foundFiles, err := findManifests(dir, []packageHandler{iOSHandler{}, androidHandler{}, uwpHandler{}})
if foundFiles == nil || len(foundFiles) == 0 {
t.Errorf("at least one file should be found")
}
Expand Down
31 changes: 19 additions & 12 deletions uwp_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"strings"

"github.com/clbanning/mxj"
)

type uwpHandler struct {
}

type uwpIdentity struct {
Version string `xml:"Version,attr"`
}
Expand All @@ -24,40 +28,43 @@ type uwpBundlerHeader struct {
Attrs []xml.Attr `xml:",attr"`
}

func isUWPPackage(filename string) bool {
func (h uwpHandler) isPackage(filename string) bool {
return strings.ToLower(getFilename(filename)) == "package.appxmanifest"
}

func getUWPPackageInfo(filePath string) packageInfo {
func (h uwpHandler) getPackageInfo(filePath string) (packageInfo, error) {
byteValue := readFile(filePath)
data, err := readUWPData(byteValue)
data, err := h.read(byteValue)

if err != nil {
return packageInfo{Path: filePath, HasError: true}
return packageInfo{Path: filePath, HasError: true}, err
}

return packageInfo{
Name: data.Properties.Name,
Version: data.Identity.Version,
Path: filePath,
}
}, nil
}

func readUWPData(data []byte) (*uwpBundlerHeader, error) {
func (h uwpHandler) read(data []byte) (*uwpBundlerHeader, error) {
var header uwpBundlerHeader
err := xml.Unmarshal(data, &header)
return &header, err
}

func changeUWPPackageVersion(file packageInfo, newVersion string) error {
func (h uwpHandler) changePackageVersion(file packageInfo, newVersion string) error {
fileBytes := readFile(file.Path)
processedBytes := applyVersionToUWPXML(fileBytes, newVersion)
processedBytes, err := h.applyVersion(fileBytes, newVersion)
if err != nil {
return fmt.Errorf("Invalid xml file: %v", file.Path)
}
saveFile(file.Path, processedBytes)
return nil
}

func applyVersionToUWPXML(data []byte, newVersion string) []byte {
fileReader := bytes.NewReader(data)
func (h uwpHandler) applyVersion(byteValue []byte, newVersion string) ([]byte, error) {
fileReader := bytes.NewReader(byteValue)
for m, err := mxj.NewMapXmlSeqReader(fileReader); m != nil || err != io.EOF; m, err = mxj.NewMapXmlSeqReader(fileReader) {
if err != nil {
if err == mxj.NO_ROOT {
Expand All @@ -81,8 +88,8 @@ func applyVersionToUWPXML(data []byte, newVersion string) []byte {

// Write header
header := `<?xml version="1.0" encoding="utf-8"?>` + "\n"
return []byte(header + string(b))
return []byte(header + string(b)), nil
}

return nil
return nil, nil
}
39 changes: 24 additions & 15 deletions uwp_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var uwpFilePath = "test/Package.appxmanifest"

func Test_isUWPPackage(t *testing.T) {
func Test_uwpHandler_isPackage(t *testing.T) {
type args struct {
filename string
}
Expand All @@ -25,15 +25,18 @@ func Test_isUWPPackage(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isUWPPackage(tt.args.filename); got != tt.want {
t.Errorf("isUWPPackage() = %v, want %v", got, tt.want)
handler := new(uwpHandler)
if got := handler.isPackage(tt.args.filename); got != tt.want {
t.Errorf("isPackage() = %v, want %v", got, tt.want)
}
})
}
}

func Test_getUWPPackageInfo(t *testing.T) {
currentVersion := getUWPPackageInfo(uwpFilePath)
func Test_uwpHandler_getPackageInfo(t *testing.T) {
handler := new(uwpHandler)
currentVersion, err := handler.getPackageInfo(uwpFilePath)
require.NoError(t, err)
if currentVersion.Version != "1.0.1.0" {
t.Errorf("version mismatch; actual %v, expected %v", currentVersion.Version, "1.0.1.0")
}
Expand All @@ -42,31 +45,36 @@ func Test_getUWPPackageInfo(t *testing.T) {
}
}

func Test_changeUWPPackageVersion(t *testing.T) {
currentVersion := getUWPPackageInfo(uwpFilePath)
func Test_uwpHandler_changePackageVersion(t *testing.T) {
handler := new(uwpHandler)
currentVersion, err := handler.getPackageInfo(uwpFilePath)
require.NoError(t, err)
if currentVersion.Version != "1.0.1.0" {
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.1.0")
}

changeUWPPackageVersion(currentVersion, "1.0.2")
currentVersion = getUWPPackageInfo(uwpFilePath)
handler.changePackageVersion(currentVersion, "1.0.2")
currentVersion, err = handler.getPackageInfo(uwpFilePath)
require.NoError(t, err)
if currentVersion.Version != "1.0.2.0" {
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.2.0")
}

// some kind of rollback
changeUWPPackageVersion(currentVersion, "1.0.1")
handler.changePackageVersion(currentVersion, "1.0.1")
}

func Test_applyVersionToUWPXML(t *testing.T) {
processedBytes := applyVersionToUWPXML(uwpSeed, "1.0.2")
xml, _ := readUWPData(processedBytes)
func Test_uwpHandler_applyVersion(t *testing.T) {
handler := new(uwpHandler)
processedBytes, err := handler.applyVersion(uwpSeed, "1.0.2")
require.NoError(t, err)
xml, _ := handler.read(processedBytes)
if xml.Identity.Version != "1.0.2.0" {
t.Errorf("VersionName mismatch; expected %v, got %v", "1.0.2.0", xml.Identity.Version)
}
}

func Test_readUWPData(t *testing.T) {
func Test_uwpHandler_read(t *testing.T) {
type args struct {
data []byte
}
Expand All @@ -82,7 +90,8 @@ func Test_readUWPData(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readUWPData(tt.args.data)
handler := new(uwpHandler)
got, err := handler.read(tt.args.data)
if tt.shouldError {
require.Error(t, err)
return
Expand Down

0 comments on commit a8c28ab

Please sign in to comment.