Skip to content

Handle new package file #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 71 additions & 14 deletions cache.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"

androidCache "github.com/bitrise-io/go-android/cache"
"github.com/bitrise-io/go-steputils/cache"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/sliceutil"
androidCache "github.com/bitrise-io/go-android/cache"
)

func cacheCocoapodsDeps(projectLocation string) error {
Expand Down Expand Up @@ -65,14 +66,12 @@ func cacheAndroidDeps(projectDir string) error {
return androidCache.Collect(androidDir, cache.LevelDeps)
}

func openPackageResolutionFile(projectDir string) (string, error) {
resolutionFilePath := filepath.Join(projectDir, ".packages")

if _, err := os.Stat(resolutionFilePath); os.IsNotExist(err) {
return "", fmt.Errorf("package resolution file (%s) not found, error: %s", resolutionFilePath, err)
func openFile(filepath string) (string, error) {
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return "", fmt.Errorf("file (%s) not found, error: %s", filepath, err)
}

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

func cacheFlutterDeps(projectDir string) error {
contents, err := openPackageResolutionFile(projectDir)
packageToLocation, err := readOldPackageFormat(projectDir)
if err != nil {
return err
}

packageToLocation, err := parsePackageResolutionFile(contents)
if err != nil {
return fmt.Errorf("failed to parse Flutter package resolution file, error: %s", err)
packageToLocation, err = readNewJSONFormat(projectDir)
if err != nil {
return err
}
}

cachePaths, err := cacheableFlutterDepPaths(packageToLocation)
Expand All @@ -234,3 +231,63 @@ func cacheFlutterDeps(projectDir string) error {
}
return pubCache.Commit()
}

func readOldPackageFormat(projectDir string) (map[string]url.URL, error) {
packagePath := filepath.Join(projectDir, ".packages")
contents, err := openFile(packagePath)
if err != nil {
return map[string]url.URL{}, err
}

packageToLocation, err := parsePackageResolutionFile(contents)
if err != nil {
return map[string]url.URL{}, fmt.Errorf("failed to parse Flutter package resolution file, error: %s", err)
}

return packageToLocation, nil
}

func readNewJSONFormat(projectDir string) (map[string]url.URL, error) {
packagePath := filepath.Join(projectDir, ".dart_tool/package_config.json")
contents, err := openFile(packagePath)
if err != nil {
return map[string]url.URL{}, err
}

packages, err := parseJSON(contents)
if err != nil {
return map[string]url.URL{}, err
}

return packages, nil
}

func parseJSON(contents string) (map[string]url.URL, error) {
type packageConfig struct {
Packages []struct {
Name string `json:"name"`
RootUri string `json:"rootUri"`
PackageUri string `json:"packageUri"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://dart.googlesource.com/sdk/+/5ae183856945440597e0674436dd89b9e08632bc/.dart_tool/package_config.json as an example, at least packageUri can be missing. Should this field rather be string? and omitempty?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

Copy link
Contributor Author

@tothszabi tothszabi Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go does not have optional fields and the way it works is that it initialises every field to the fields default value. For strings this is the "" empty string. Which is great for us and this way we do not need to care if the packageUri exists or not.

The omitempty tag is only useful for encoding. It is not used during decoding.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know what is exactly the rule here.
But in my old team we usually approved the code pro-actively allowing the author to fix comments and tests and merge without blockers.
Approved =)

} `json:"packages"`
}

var config packageConfig
err := json.Unmarshal([]byte(contents), &config)
if err != nil {
return map[string]url.URL{}, err
}

packages := map[string]url.URL{}

for _, item := range config.Packages {
path := filepath.Join(item.RootUri, item.PackageUri)
location, err := url.Parse(path)
if err != nil {
return map[string]url.URL{}, fmt.Errorf("could not parse location URI: %s", path)
}

packages[item.Name] = *location
}

return packages, nil
}
32 changes: 32 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"github.com/stretchr/testify/assert"
"net/url"
"path/filepath"
"reflect"
"testing"

"github.com/bitrise-io/go-utils/log"
"github.com/stretchr/testify/require"
)

// Example file:
Expand Down Expand Up @@ -173,3 +176,32 @@ func Test_cacheableFlutterDepPaths(t *testing.T) {
})
}
}

func TestJSONFormatParsing(t *testing.T) {
testData := `
{
"configVersion": 2,
"packages": [
{
"name": "insert-cool-name-here",
"rootUri": "file:///path/to/file",
"packageUri": "lib/",
"languageVersion": "2.12"
}
],
"generated": "2023-04-26T10:11:25.639598Z",
"generator": "pub",
"generatorVersion": "2.15.1"
}
`
result, err := parseJSON(testData)
require.NoError(t, err)

fileURL, err := url.Parse(filepath.Join("file:///path/to/file", "lib/"))
require.NoError(t, err)

expected := map[string]url.URL{
"insert-cool-name-here": *fileURL,
}
assert.Equal(t, expected, result)
}
2 changes: 1 addition & 1 deletion e2e/bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
app:
envs:
- SAMPLE_APP_URL: https://github.com/bitrise-io/Bitrise-Flutter-Sample.git
- SAMPLE_APP_BRANCH: main
- SAMPLE_APP_BRANCH: e2e-test
- ORIGIN_SOURCE_DIR: $BITRISE_SOURCE_DIR

# secrets
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require (
github.com/bitrise-io/go-xcode v0.0.0-20210714144817-465637d28b74
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/ryanuber/go-glob v1.0.0
github.com/stretchr/testify v1.8.2
)
13 changes: 11 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -39,11 +41,16 @@ github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkB
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
Expand All @@ -69,6 +76,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand All @@ -77,6 +85,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
howett.net/plist v0.0.0-20201203080718-1454fab16a06/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading