Skip to content

Commit

Permalink
Merge pull request #1339 from onflow/update-flix-tests
Browse files Browse the repository at this point in the history
Update flix tests and flixkit-go version used
  • Loading branch information
bthaile authored Jan 18, 2024
2 parents 7591b68 + 3944ccf commit 7d7686e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 82 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/onflow/cadence-tools/languageserver v0.33.3
github.com/onflow/cadence-tools/test v0.14.5
github.com/onflow/fcl-dev-wallet v0.7.4
github.com/onflow/flixkit-go v1.0.2
github.com/onflow/flixkit-go v1.1.0
github.com/onflow/flow-cli/flowkit v1.6.1-0.20231110211255-b41f57a8b8c7
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f
github.com/onflow/flow-emulator v0.59.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ github.com/onflow/cadence-tools/test v0.14.5 h1:u1kYkotKdwKEf9c3h65mI3VMevBkHY+7
github.com/onflow/cadence-tools/test v0.14.5/go.mod h1:ix09Bb3GQ/fZMNpSR8E+vSFItGF54fzP9gFxU7AsOIw=
github.com/onflow/fcl-dev-wallet v0.7.4 h1:vI6t3U0AO88R/Iitn5KsnniSpbN9Lqsqwvi9EJT4C0k=
github.com/onflow/fcl-dev-wallet v0.7.4/go.mod h1:kc42jkiuoPJmxMRFjfbRO9XvnR/3XLheaOerxVMDTiw=
github.com/onflow/flixkit-go v1.0.2 h1:6PVLLb8qQj9L6cs57HUO3wszzbgPD46pc9MAGWHnb1s=
github.com/onflow/flixkit-go v1.0.2/go.mod h1:KGL7oMu4uzt7s0qsNkaqGBYIt+Z38Qbf0JG56qK/Sg0=
github.com/onflow/flixkit-go v1.1.0 h1:yju2lotk2LQBXUwb0rZeOtVbHW2KVSzzRRu/3rPtzok=
github.com/onflow/flixkit-go v1.1.0/go.mod h1:KGL7oMu4uzt7s0qsNkaqGBYIt+Z38Qbf0JG56qK/Sg0=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a00dbf7c061f h1:S8yIZw9LFXfYD1V5H9BiixihHw3GrXVPrmfplSzYaww=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20231016154253-a00dbf7c061f/go.mod h1:jM6GMAL+m0hjusUgiYDNrixPQ6b9s8xjoJQoEu5bHQI=
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.4-0.20231016154253-a00dbf7c061f h1:Ep+Mpo2miWMe4pjPGIaEvEzshRep30dvNgxqk+//FrQ=
Expand Down
120 changes: 49 additions & 71 deletions internal/super/flix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ package super
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"

"github.com/onflow/flixkit-go/flixkit"

Expand Down Expand Up @@ -107,17 +105,28 @@ func init() {

func executeCmd(
args []string,
_ command.GlobalFlags,
flags command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.Config{
flixService := flixkit.NewFlixService(&flixkit.FlixServiceConfig{
FileReader: state,
})
return executeFlixCmd(args, flags, logger, flow, state, flixService)
}

func executeFlixCmd(
args []string,
_ command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
flixService flixkit.FlixService,
) (result command.Result, err error) {
flixQuery := args[0]
ctx := context.Background()
cadenceWithImportsReplaced, err := flixService.GetAndReplaceCadenceImports(ctx, flixQuery, flow.Network().Name)
cadenceWithImportsReplaced, err := flixService.GetTemplateAndReplaceImports(ctx, flixQuery, flow.Network().Name)
if err != nil {
logger.Error("could not replace imports")
return nil, err
Expand Down Expand Up @@ -152,37 +161,25 @@ func packageCmd(
flow flowkit.Services,
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.Config{
flixService := flixkit.NewFlixService(&flixkit.FlixServiceConfig{
FileReader: state,
})

return packageFlixCmd(args, gFlags, logger, flow, state, flixService, flags)
}

func packageFlixCmd(
args []string,
gFlags command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
flixService flixkit.FlixService,
flags flixFlags,
) (result command.Result, err error) {
flixQuery := args[0]
ctx := context.Background()
template, err := flixService.GetTemplate(ctx, flixQuery)
if err != nil {
return nil, err
}
if !isUrl(flixQuery) {
if gFlags.Save != "" {
// resolve template file location to relative path to be used by binding file
flixQuery, err = GetRelativePath(flixQuery, gFlags.Save)
if err != nil {
logger.Error("could not resolve relative path to template")
return nil, err
}
}
}

var out string
var gen flixkit.FclGenerator
switch flags.Lang {
case "js":
gen = *flixkit.NewFclJSGenerator()
case "ts":
gen = *flixkit.NewFclTSGenerator()
default:
return nil, fmt.Errorf("language %s not supported", flags.Lang)
}
out, err = gen.Generate(template, flixQuery)
out, err := flixService.GetTemplateAndCreateBinding(ctx, flixQuery, flags.Lang, gFlags.Save)
if err != nil {
return nil, err
}
Expand All @@ -194,14 +191,31 @@ func packageCmd(
}

func generateCmd(
args []string,
gFlags command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.FlixServiceConfig{
FileReader: state,
Logger: logger,
})

return generateFlixCmd(args, gFlags, logger, flow, state, flixService, flags)
}

func generateFlixCmd(
args []string,
_ command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
flixService flixkit.FlixService,
flags flixFlags,
) (result command.Result, err error) {
cadenceFile := args[0]

depContracts := getDeployedContracts(state)
if cadenceFile == "" {
return nil, fmt.Errorf("no cadence code found")
}
Expand All @@ -211,25 +225,12 @@ func generateCmd(
return nil, fmt.Errorf("could not read cadence file %s: %w", cadenceFile, err)
}

depContracts := GetDeployedContracts(state)
generator, err := flixkit.NewGenerator(depContracts, logger)
if err != nil {
return nil, fmt.Errorf("could not create flix generator %w", err)
}

ctx := context.Background()
var template string
if flags.PreFill != "" {
flixService := flixkit.NewFlixService(&flixkit.Config{
FileReader: state,
})
template, err = flixService.GetTemplate(ctx, flags.PreFill)
if err != nil {
return nil, fmt.Errorf("could not parse template from pre fill %w", err)
}
}

prettyJSON, err := generator.Generate(ctx, string(code), template)
prettyJSON, err := flixService.CreateTemplate(ctx, depContracts, string(code), flags.PreFill)
if err != nil {
return nil, fmt.Errorf("could not generate flix %w", err)
}
Expand All @@ -256,24 +257,7 @@ func (fr *flixResult) Oneliner() string {
return fr.result
}

// GetRelativePath computes the relative path from generated file to flix json file.
// This path is used in the binding file to reference the flix json file.
func GetRelativePath(configFile, bindingFile string) (string, error) {
relPath, err := filepath.Rel(filepath.Dir(bindingFile), configFile)
if err != nil {
return "", err
}

// If the file is in the same directory and doesn't start with "./", prepend it.
if !filepath.IsAbs(relPath) && relPath[0] != '.' {
relPath = "./" + relPath
}

// Currently binding files are js, we need to convert the path to unix style
return filepath.ToSlash(relPath), nil
}

func GetDeployedContracts(state *flowkit.State) flixkit.ContractInfos {
func getDeployedContracts(state *flowkit.State) flixkit.ContractInfos {
allContracts := make(flixkit.ContractInfos)
depNetworks := make([]string, 0)
// get all configured networks in flow.json
Expand All @@ -284,7 +268,6 @@ func GetDeployedContracts(state *flowkit.State) flixkit.ContractInfos {
// get all deployed and alias contracts for configured networks
for _, network := range depNetworks {
contracts, err := state.DeploymentContractsByNetwork(config.Network{Name: network})
fmt.Println("contracts len", network, len(contracts))
if err != nil {
continue
}
Expand All @@ -309,11 +292,6 @@ func GetDeployedContracts(state *flowkit.State) flixkit.ContractInfos {
return allContracts
}

func isUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}

func isPath(path string) bool {
_, err := os.Stat(path)
return err == nil
Expand Down
Loading

0 comments on commit 7d7686e

Please sign in to comment.