Skip to content

Commit b50b5a1

Browse files
committed
Handle new package json file
1 parent a373d1c commit b50b5a1

File tree

2 files changed

+103
-14
lines changed

2 files changed

+103
-14
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+
}

0 commit comments

Comments
 (0)