Skip to content

Commit 787bcf5

Browse files
committed
Merge branch 'release/1.2.0'
2 parents 1035df0 + a476ef9 commit 787bcf5

20 files changed

+351
-69
lines changed

.goreleaser.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ builds:
22
- env:
33
- CGO_ENABLED=0
44
- goos:
5-
- darwin
6-
- linux
75
- windows
86
archive:
7+
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
98
replacements:
10-
darwin: Darwin
11-
linux: Linux
12-
windows: Windows
13-
386: i386
14-
amd64: x86_64
9+
darwin: macOS
10+
386: 32-bit
11+
amd64: 64-bit
1512
checksum:
1613
name_template: 'checksums.txt'
1714
snapshot:
@@ -35,5 +32,5 @@ scoop:
3532
owner: gabrielrobert
3633
name: scoop-bucket
3734
homepage: https://github.com/gabrielrobert/xavtool
38-
description: Xamarin Automating Version Tool
35+
description: Xplat Automating Version Tool
3936
license: MIT

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ os:
44
- linux
55

66
go:
7-
- 1.x
7+
- 1.10.x
88

99
script:
1010
- go test -v

actions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"github.com/urfave/cli"
77
)
88

9-
var handlers = []packageHandler{iOSHandler{}, androidHandler{}, uwpHandler{}}
9+
var handlers = []packageHandler{iOSHandler{}, androidHandler{}, uwpHandler{}, cordovaHandler{}}
1010

1111
// called by executing `xavtool current`
1212
func current(c *cli.Context) error {
13-
allFiles, err := findManifests(getWorkingDir(), handlers)
13+
allFiles, err := findManifests(getGivenPathOrWorkingDir(c), handlers)
1414

1515
// validations
1616
if err != nil {
@@ -29,15 +29,15 @@ func current(c *cli.Context) error {
2929

3030
// show packages
3131
for _, file := range allFiles {
32-
fmt.Println(fmt.Sprintf("%v - %v (%v)", file.Version, file.Name, file.Path))
32+
fmt.Println(fmt.Sprintf("%v [%v] - %v (%v)", file.Version, file.InternalVersion, file.Name, file.Path))
3333
}
3434

3535
return nil
3636
}
3737

3838
// called by executing `xavtool increment`
3939
func increment(c *cli.Context) error {
40-
allFiles, err := findManifests(getWorkingDir(), handlers)
40+
allFiles, err := findManifests(getGivenPathOrWorkingDir(c), handlers)
4141

4242
// validations
4343
if err != nil {
@@ -95,7 +95,7 @@ func set(c *cli.Context) error {
9595
return cli.NewExitError(fmt.Sprintf("Version '%v' is not valid", newVersion), 3)
9696
}
9797

98-
allFiles, err := findManifests(getWorkingDir(), handlers)
98+
allFiles, err := findManifests(getGivenPathOrWorkingDir(c), handlers)
9999

100100
// validations
101101
if err != nil {

android_handler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ func (h androidHandler) getPackageInfo(filePath string) (packageInfo, error) {
3838
}
3939

4040
return packageInfo{
41-
Name: data.Name,
42-
Version: data.VersionName,
43-
Path: filePath,
41+
Name: data.Name,
42+
Version: data.VersionName,
43+
InternalVersion: data.Code,
44+
Path: filePath,
4445
}, nil
4546
}
4647

android_handler_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ func Test_androidHandler_getPackageInfo(t *testing.T) {
3535
handler := new(androidHandler)
3636
currentVersion, err := handler.getPackageInfo(filePath)
3737
require.NoError(t, err)
38-
if currentVersion.Version != "1.0.1" {
39-
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.1")
40-
}
38+
assert.Equal(t, "1.0.1", currentVersion.Version)
39+
assert.Equal(t, "1000100", currentVersion.InternalVersion)
4140
}
4241

4342
func Test_androidHandler_changePackageVersion(t *testing.T) {

appveyor.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ install:
1313
- go version
1414
- go env
1515
- go get github.com/gabrielrobert/xavtool
16-
- go get github.com/goreleaser/goreleaser
16+
- ps: iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
17+
- ps: scoop bucket add goreleaser https://github.com/goreleaser/scoop-bucket.git
18+
- ps: scoop install goreleaser
19+
- goreleaser -v
1720
- go get -t -v ./...
1821

1922
build_script:
2023
- go build
21-
- goreleaser --rm-dist --snapshot
22-
- choco pack
2324

2425
test_script:
2526
- go test -v
2627

28+
after_test:
29+
- goreleaser --rm-dist --snapshot
30+
- choco pack
31+
2732
artifacts:
2833
- path: '**\*.nupkg'
2934

cordova_handler.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/xml"
6+
"errors"
7+
"fmt"
8+
"io"
9+
"strings"
10+
11+
"github.com/clbanning/mxj"
12+
)
13+
14+
type cordovaHandler struct {
15+
}
16+
17+
type cordovaBundlerHeader struct {
18+
XMLName xml.Name `xml:"widget"`
19+
Name string `xml:"id,attr"`
20+
21+
// should be less than 2100000000
22+
Code string `xml:"android-versionCode,attr"`
23+
24+
VersionName string `xml:"version,attr"`
25+
Attrs []xml.Attr `xml:",attr"`
26+
}
27+
28+
func (h cordovaHandler) isPackage(filename string) bool {
29+
return strings.ToLower(getFilename(filename)) == "config.xml"
30+
}
31+
32+
func (h cordovaHandler) getPackageInfo(filePath string) (packageInfo, error) {
33+
byteValue := readFile(filePath)
34+
data, err := h.read(byteValue)
35+
36+
if err != nil {
37+
return packageInfo{Path: filePath, HasError: true}, err
38+
}
39+
40+
return packageInfo{
41+
Name: data.Name,
42+
Version: data.VersionName,
43+
InternalVersion: data.Code,
44+
Path: filePath,
45+
}, nil
46+
}
47+
48+
func (h cordovaHandler) read(data []byte) (*cordovaBundlerHeader, error) {
49+
var header cordovaBundlerHeader
50+
err := xml.Unmarshal(data, &header)
51+
return &header, err
52+
}
53+
54+
func (h cordovaHandler) changePackageVersion(file packageInfo, newVersion string) error {
55+
fileBytes := readFile(file.Path)
56+
processedBytes, err := h.applyVersion(fileBytes, newVersion)
57+
if err != nil {
58+
return fmt.Errorf("Invalid xml file: %v", file.Path)
59+
}
60+
saveFile(file.Path, processedBytes)
61+
return nil
62+
}
63+
64+
func (h cordovaHandler) applyVersion(byteValue []byte, newVersion string) ([]byte, error) {
65+
fileReader := bytes.NewReader(byteValue)
66+
for m, err := mxj.NewMapXmlSeqReader(fileReader); m != nil || err != io.EOF; m, err = mxj.NewMapXmlSeqReader(fileReader) {
67+
if err != nil {
68+
if err == mxj.NO_ROOT {
69+
continue
70+
} else {
71+
return nil, errors.New("Invalid xml")
72+
}
73+
}
74+
vmap := m["widget"].(map[string]interface{})
75+
76+
// edit version attr
77+
acmt, err := mxj.Map(vmap).ValueForPath("#attr.version.#text")
78+
acmt = newVersion
79+
mxj.Map(vmap).SetValueForPath(acmt, "#attr.version.#text")
80+
err = m.SetValueForPath(vmap, "widget")
81+
check(err)
82+
83+
// edit ios-CFBundleVersion attr
84+
acmt, err = mxj.Map(vmap).ValueForPath("#attr.ios-CFBundleVersion.#text")
85+
acmt = newVersion
86+
mxj.Map(vmap).SetValueForPath(acmt, "#attr.ios-CFBundleVersion.#text")
87+
err = m.SetValueForPath(vmap, "widget")
88+
check(err)
89+
90+
// edit android-versionCode attr
91+
acmt, err = mxj.Map(vmap).ValueForPath("#attr.android-versionCode.#text")
92+
acmt = buildAndroidVersionCode(newVersion)
93+
mxj.Map(vmap).SetValueForPath(acmt, "#attr.android-versionCode.#text")
94+
err = m.SetValueForPath(vmap, "widget")
95+
check(err)
96+
97+
b, err := m.XmlSeqIndent("", " ")
98+
check(err)
99+
100+
// Write header
101+
header := `<?xml version="1.0" encoding="utf-8"?>` + "\n"
102+
return []byte(header + string(b)), nil
103+
}
104+
105+
return nil, nil
106+
}

cordova_handler_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func Test_cordovaHandler_isPackage(t *testing.T) {
11+
type args struct {
12+
filename string
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want bool
18+
}{
19+
{"filename", args{"config.xmL"}, true},
20+
{"filename with path and \\", args{"c:/dev/config.xml"}, true},
21+
{"filename with path and /", args{"c:\\dev\\config.xml"}, true},
22+
{"filename with path", args{"c:\\dev/config.xml"}, true},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
handler := new(cordovaHandler)
27+
if got := handler.isPackage(tt.args.filename); got != tt.want {
28+
t.Errorf("isCordovaPackage() = %v, want %v", got, tt.want)
29+
}
30+
})
31+
}
32+
}
33+
34+
func Test_cordovaHandler_getPackageInfo(t *testing.T) {
35+
handler := new(cordovaHandler)
36+
currentVersion, err := handler.getPackageInfo("test/config.xml")
37+
require.NoError(t, err)
38+
assert.Equal(t, "com.example.xavtool", currentVersion.Name)
39+
assert.Equal(t, "1.0.1", currentVersion.Version)
40+
assert.Equal(t, "1000100", currentVersion.InternalVersion)
41+
}
42+
43+
func Test_cordovaHandler_changePackageVersion(t *testing.T) {
44+
handler := new(cordovaHandler)
45+
currentVersion, err := handler.getPackageInfo("test/config.xml")
46+
require.NoError(t, err)
47+
if currentVersion.Version != "1.0.1" {
48+
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.1")
49+
}
50+
51+
handler.changePackageVersion(currentVersion, "1.0.2")
52+
currentVersion, err = handler.getPackageInfo("test/config.xml")
53+
require.NoError(t, err)
54+
if currentVersion.Version != "1.0.2" {
55+
t.Errorf("version mismatch; actual %v, expected %v", currentVersion, "1.0.2")
56+
}
57+
58+
// some kind of rollback
59+
handler.changePackageVersion(currentVersion, "1.0.1")
60+
}
61+
62+
func Test_cordovaHandler_applyVersion(t *testing.T) {
63+
handler := new(cordovaHandler)
64+
processedBytes, err := handler.applyVersion(cordovaSeed, "1.0.2")
65+
require.NoError(t, err)
66+
xml, _ := handler.read(processedBytes)
67+
if xml.VersionName != "1.0.2" {
68+
t.Errorf("VersionName mismatch; expected %v", "1.0.2")
69+
}
70+
if xml.Code != "1000200" {
71+
t.Errorf("code mismatch; actual %v, expected %v", xml.Code, "1000200")
72+
}
73+
}
74+
75+
func Test_cordovaHandler_read(t *testing.T) {
76+
type args struct {
77+
data []byte
78+
}
79+
tests := []struct {
80+
name string
81+
args args
82+
want string
83+
shouldError bool
84+
}{
85+
{"invalid bytes", args{invalidCordovaSeed}, "", true},
86+
{"valid file", args{readFile("test/config.xml")}, "1.0.1", false},
87+
{"valid bytes", args{cordovaSeed}, "1.0.1", false},
88+
}
89+
for _, tt := range tests {
90+
t.Run(tt.name, func(t *testing.T) {
91+
handler := new(cordovaHandler)
92+
got, err := handler.read(tt.args.data)
93+
if tt.shouldError {
94+
require.Error(t, err)
95+
return
96+
}
97+
require.NoError(t, err)
98+
assert.Equal(t, tt.want, got.VersionName)
99+
})
100+
}
101+
}
102+
103+
var cordovaSeed = []byte(`
104+
<?xml version="1.0" encoding="utf-8"?>
105+
<widget android-versionCode="1000100" id="com.example.xavtool" ios-CFBundleVersion="1.0.1" version="1.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
106+
<name>xavtool</name>
107+
<content src="index.html"/>
108+
<access origin="*"/>
109+
</widget>
110+
`)
111+
112+
var invalidCordovaSeed = []byte(`
113+
<?xml version="1.0" encoding="utf-8"?>
114+
<man
115+
`)

finders.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
)
99

1010
type packageInfo struct {
11-
Name string
12-
Version string
13-
Path string
14-
HasError bool
11+
Name string
12+
Version string
13+
InternalVersion string
14+
Path string
15+
HasError bool
1516
}
1617

1718
func findManifests(root string, handlers []packageHandler) ([]packageInfo, error) {
@@ -45,7 +46,8 @@ func findManifests(root string, handlers []packageHandler) ([]packageInfo, error
4546
}
4647

4748
func isIgnored(f os.FileInfo) bool {
48-
if f.IsDir() && stringInSlice(f.Name(), []string{"bin", "obj", ".git"}) {
49+
var ignoredFolders = []string{"bin", "obj", ".git", "CordovaLib", "platforms", "res", "node_modules"}
50+
if f.IsDir() && stringInSlice(f.Name(), ignoredFolders) {
4951
return true
5052
}
5153
return false

0 commit comments

Comments
 (0)