1
1
package main
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"io/ioutil"
6
7
"net/url"
7
8
"os"
8
9
"path/filepath"
9
10
"strings"
10
11
12
+ androidCache "github.com/bitrise-io/go-android/cache"
11
13
"github.com/bitrise-io/go-steputils/cache"
12
14
"github.com/bitrise-io/go-utils/log"
13
15
"github.com/bitrise-io/go-utils/pathutil"
14
16
"github.com/bitrise-io/go-utils/sliceutil"
15
- androidCache "github.com/bitrise-io/go-android/cache"
16
17
)
17
18
18
19
func cacheCocoapodsDeps (projectLocation string ) error {
@@ -65,14 +66,12 @@ func cacheAndroidDeps(projectDir string) error {
65
66
return androidCache .Collect (androidDir , cache .LevelDeps )
66
67
}
67
68
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 )
73
72
}
74
73
75
- contents , err := ioutil .ReadFile (resolutionFilePath )
74
+ contents , err := ioutil .ReadFile (filepath )
76
75
if err != nil {
77
76
return "" , fmt .Errorf ("failed to read package resolution file, error: %s" , err )
78
77
}
@@ -212,14 +211,12 @@ func cacheableFlutterDepPaths(packageToLocation map[string]url.URL) ([]string, e
212
211
}
213
212
214
213
func cacheFlutterDeps (projectDir string ) error {
215
- contents , err := openPackageResolutionFile (projectDir )
214
+ packageToLocation , err := readOldPackageFormat (projectDir )
216
215
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
+ }
223
220
}
224
221
225
222
cachePaths , err := cacheableFlutterDepPaths (packageToLocation )
@@ -234,3 +231,63 @@ func cacheFlutterDeps(projectDir string) error {
234
231
}
235
232
return pubCache .Commit ()
236
233
}
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
+ }
0 commit comments