Skip to content

Commit f2d76a7

Browse files
authored
Handle new package file (#46)
* Add dependency * Handle new package json file * wip
1 parent eac2962 commit f2d76a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+23062
-17
lines changed

cache.go

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io/ioutil"
67
"net/url"
78
"os"
89
"path/filepath"
910
"strings"
1011

12+
androidCache "github.com/bitrise-io/go-android/cache"
1113
"github.com/bitrise-io/go-steputils/cache"
1214
"github.com/bitrise-io/go-utils/log"
1315
"github.com/bitrise-io/go-utils/pathutil"
1416
"github.com/bitrise-io/go-utils/sliceutil"
15-
androidCache "github.com/bitrise-io/go-android/cache"
1617
)
1718

1819
func cacheCocoapodsDeps(projectLocation string) error {
@@ -65,14 +66,12 @@ func cacheAndroidDeps(projectDir string) error {
6566
return androidCache.Collect(androidDir, cache.LevelDeps)
6667
}
6768

68-
func openPackageResolutionFile(projectDir string) (string, error) {
69-
resolutionFilePath := filepath.Join(projectDir, ".packages")
70-
71-
if _, err := os.Stat(resolutionFilePath); os.IsNotExist(err) {
72-
return "", fmt.Errorf("package resolution file (%s) not found, error: %s", resolutionFilePath, err)
69+
func openFile(filepath string) (string, error) {
70+
if _, err := os.Stat(filepath); os.IsNotExist(err) {
71+
return "", fmt.Errorf("file (%s) not found, error: %s", filepath, err)
7372
}
7473

75-
contents, err := ioutil.ReadFile(resolutionFilePath)
74+
contents, err := ioutil.ReadFile(filepath)
7675
if err != nil {
7776
return "", fmt.Errorf("failed to read package resolution file, error: %s", err)
7877
}
@@ -212,14 +211,12 @@ func cacheableFlutterDepPaths(packageToLocation map[string]url.URL) ([]string, e
212211
}
213212

214213
func cacheFlutterDeps(projectDir string) error {
215-
contents, err := openPackageResolutionFile(projectDir)
214+
packageToLocation, err := readOldPackageFormat(projectDir)
216215
if err != nil {
217-
return err
218-
}
219-
220-
packageToLocation, err := parsePackageResolutionFile(contents)
221-
if err != nil {
222-
return fmt.Errorf("failed to parse Flutter package resolution file, error: %s", err)
216+
packageToLocation, err = readNewJSONFormat(projectDir)
217+
if err != nil {
218+
return err
219+
}
223220
}
224221

225222
cachePaths, err := cacheableFlutterDepPaths(packageToLocation)
@@ -234,3 +231,63 @@ func cacheFlutterDeps(projectDir string) error {
234231
}
235232
return pubCache.Commit()
236233
}
234+
235+
func readOldPackageFormat(projectDir string) (map[string]url.URL, error) {
236+
packagePath := filepath.Join(projectDir, ".packages")
237+
contents, err := openFile(packagePath)
238+
if err != nil {
239+
return map[string]url.URL{}, err
240+
}
241+
242+
packageToLocation, err := parsePackageResolutionFile(contents)
243+
if err != nil {
244+
return map[string]url.URL{}, fmt.Errorf("failed to parse Flutter package resolution file, error: %s", err)
245+
}
246+
247+
return packageToLocation, nil
248+
}
249+
250+
func readNewJSONFormat(projectDir string) (map[string]url.URL, error) {
251+
packagePath := filepath.Join(projectDir, ".dart_tool/package_config.json")
252+
contents, err := openFile(packagePath)
253+
if err != nil {
254+
return map[string]url.URL{}, err
255+
}
256+
257+
packages, err := parseJSON(contents)
258+
if err != nil {
259+
return map[string]url.URL{}, err
260+
}
261+
262+
return packages, nil
263+
}
264+
265+
func parseJSON(contents string) (map[string]url.URL, error) {
266+
type packageConfig struct {
267+
Packages []struct {
268+
Name string `json:"name"`
269+
RootUri string `json:"rootUri"`
270+
PackageUri string `json:"packageUri"`
271+
} `json:"packages"`
272+
}
273+
274+
var config packageConfig
275+
err := json.Unmarshal([]byte(contents), &config)
276+
if err != nil {
277+
return map[string]url.URL{}, err
278+
}
279+
280+
packages := map[string]url.URL{}
281+
282+
for _, item := range config.Packages {
283+
path := filepath.Join(item.RootUri, item.PackageUri)
284+
location, err := url.Parse(path)
285+
if err != nil {
286+
return map[string]url.URL{}, fmt.Errorf("could not parse location URI: %s", path)
287+
}
288+
289+
packages[item.Name] = *location
290+
}
291+
292+
return packages, nil
293+
}

cache_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
22

33
import (
4+
"github.com/stretchr/testify/assert"
45
"net/url"
6+
"path/filepath"
57
"reflect"
68
"testing"
79

810
"github.com/bitrise-io/go-utils/log"
11+
"github.com/stretchr/testify/require"
912
)
1013

1114
// Example file:
@@ -173,3 +176,32 @@ func Test_cacheableFlutterDepPaths(t *testing.T) {
173176
})
174177
}
175178
}
179+
180+
func TestJSONFormatParsing(t *testing.T) {
181+
testData := `
182+
{
183+
"configVersion": 2,
184+
"packages": [
185+
{
186+
"name": "insert-cool-name-here",
187+
"rootUri": "file:///path/to/file",
188+
"packageUri": "lib/",
189+
"languageVersion": "2.12"
190+
}
191+
],
192+
"generated": "2023-04-26T10:11:25.639598Z",
193+
"generator": "pub",
194+
"generatorVersion": "2.15.1"
195+
}
196+
`
197+
result, err := parseJSON(testData)
198+
require.NoError(t, err)
199+
200+
fileURL, err := url.Parse(filepath.Join("file:///path/to/file", "lib/"))
201+
require.NoError(t, err)
202+
203+
expected := map[string]url.URL{
204+
"insert-cool-name-here": *fileURL,
205+
}
206+
assert.Equal(t, expected, result)
207+
}

e2e/bitrise.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
44
app:
55
envs:
66
- SAMPLE_APP_URL: https://github.com/bitrise-io/Bitrise-Flutter-Sample.git
7-
- SAMPLE_APP_BRANCH: main
7+
- SAMPLE_APP_BRANCH: e2e-test
88
- ORIGIN_SOURCE_DIR: $BITRISE_SOURCE_DIR
99

1010
# secrets

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ require (
99
github.com/bitrise-io/go-xcode v0.0.0-20210714144817-465637d28b74
1010
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
1111
github.com/ryanuber/go-glob v1.0.0
12+
github.com/stretchr/testify v1.8.2
1213
)

go.sum

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
2828
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
2929
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
3030
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
31+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
3132
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
3233
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
34+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3335
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
3436
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3537
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -39,11 +41,16 @@ github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkB
3941
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
4042
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4143
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
44+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
45+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
4246
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4347
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4448
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
45-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
4649
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
50+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
51+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
52+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
53+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
4754
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4855
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
4956
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
@@ -69,6 +76,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
6976
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7077
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7178
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
79+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
7280
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
7381
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
7482
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -77,6 +85,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7785
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
7886
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7987
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
80-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
8188
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
89+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
90+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8291
howett.net/plist v0.0.0-20201203080718-1454fab16a06/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=

vendor/github.com/davecgh/go-spew/LICENSE

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypass.go

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)