Repository: onflow/flow-cli
Pull request #1654 (2024-06-13 16:49:39)

Update Cadence 1.0 branch

## Description

Merge `master` into `feature/stable-cadence`

______

For contributor use:

- [ ] Targeted PR against `master` branch
- [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work
- [x] Code follows the [standards mentioned here](https://github.com/onflow/flow-cli/blob/master/CONTRIBUTING.md#styleguides)
- [x] Updated relevant documentation
- [x] Re-reviewed `Files changed` in the Github PR explorer
- [x] Added appropriate labels



Patch:
diff --git a/internal/super/generate.go b/internal/super/generate.go
index e0d10ca3..11b738cb 100644
--- a/internal/super/generate.go
+++ b/internal/super/generate.go
@@ -109,8 +109,8 @@ func generateContract(
 	_ flowkit.Services,
 	state *flowkit.State,
 ) (result command.Result, err error) {
-	generator := NewGenerator(DefaultCadenceDirectory, state, logger, false, true)
-	contract := Contract{Name: args[0], Account: args[1]}
+	generator := NewGenerator("", state, logger, false, true)
+	contract := Contract{Name: args[0], Account: ""}
 	err = generator.Create(TemplateMap{ContractType: []TemplateItem{contract}})
 	return nil, err
 }
@@ -122,7 +122,7 @@ func generateTransaction(
 	_ flowkit.Services,
 	state *flowkit.State,
 ) (result command.Result, err error) {
-	generator := NewGenerator(DefaultCadenceDirectory, state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	transaction := ScriptTemplate{Name: args[0]}
 	err = generator.Create(TemplateMap{TransactionType: []TemplateItem{transaction}})
 	return nil, err
@@ -135,7 +135,7 @@ func generateScript(
 	_ flowkit.Services,
 	state *flowkit.State,
 ) (result command.Result, err error) {
-	generator := NewGenerator(DefaultCadenceDirectory, state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	script := ScriptTemplate{Name: args[0]}
 	err = generator.Create(TemplateMap{ScriptType: []TemplateItem{script}})
 	return nil, err
@@ -283,9 +283,9 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
 
 	var fileToWrite string
 	var testFileToWrite string
-	var rootDir = DefaultCadenceDirectory
+	var rootDir string
 	var basePath string
-	var testsBasePath = "tests"
+	var testsBasePath = filepath.Join(DefaultCadenceDirectory, "tests")
 	var err error
 
 	if g.directory != "" {
@@ -296,7 +296,7 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
 
 	switch templateType {
 	case ContractType:
-		basePath = "contracts"
+		basePath = filepath.Join(DefaultCadenceDirectory, "contracts")
 		fileData := map[string]interface{}{"Name": name}
 		for k, v := range data {
 			fileData[k] = v
@@ -311,7 +311,7 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
 			return fmt.Errorf("error generating contract test template: %w", err)
 		}
 	case ScriptType:
-		basePath = "scripts"
+		basePath = filepath.Join(DefaultCadenceDirectory, "scripts")
 		fileData := map[string]interface{}{}
 		for k, v := range data {
 			fileData[k] = v
@@ -321,7 +321,7 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
 			return fmt.Errorf("error generating script template: %w", err)
 		}
 	case TransactionType:
-		basePath = "transactions"
+		basePath = filepath.Join(DefaultCadenceDirectory, "transactions")
 		fileData := map[string]interface{}{}
 		for k, v := range data {
 			fileData[k] = v
@@ -334,6 +334,8 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
 		return fmt.Errorf("invalid template type: %s", templateType)
 	}
 
+	fmt.Println("account: ", account)
+
 	directoryWithBasePath := filepath.Join(rootDir, basePath, account)
 	filenameWithBasePath := filepath.Join(rootDir, basePath, account, filename)
 	relativeFilenameWithBasePath := filepath.Join(basePath, account, filename)
@@ -381,6 +383,9 @@ func (g *Generator) generate(templateType, templateName, name, account string, d
 	}
 
 	if templateType == ContractType {
+		fmt.Println("directoryWithBasePath: ", directoryWithBasePath)
+		fmt.Println("filenameWithBasePath: ", filenameWithBasePath)
+		fmt.Println("relativeFilenameWithBasePath: ", relativeFilenameWithBasePath)
 		err := g.updateContractsState(name, relativeFilenameWithBasePath)
 		if err != nil {
 			return err
diff --git a/internal/super/setup.go b/internal/super/setup.go
index ea691036..d9652b76 100644
--- a/internal/super/setup.go
+++ b/internal/super/setup.go
@@ -112,6 +112,22 @@ func create(
 	return &setupResult{targetDir: targetDir}, nil
 }
 
+func updateGitignore(targetDir string) error {
+	gitignorePath := filepath.Join(targetDir, ".gitignore")
+	f, err := os.OpenFile(gitignorePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+	if err != nil {
+		return err
+	}
+	defer f.Close()
+
+	_, err = f.WriteString("\n# flow\nemulator-account.pkey\nimports\n")
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
 func createConfigOnly(targetDir string, readerWriter flowkit.ReaderWriter) error {
 	params := config.InitConfigParameters{
 		ServiceKeySigAlgo:  "ECDSA_P256",
@@ -130,6 +146,11 @@ func createConfigOnly(targetDir string, readerWriter flowkit.ReaderWriter) error
 		return err
 	}
 
+	err = updateGitignore(targetDir)
+	if err != nil {
+		return err
+	}
+
 	return nil
 }
 
@@ -192,8 +213,6 @@ func startInteractiveSetup(
 	// cadence/transactions/DefaultTransaction.cdc
 	// cadence/tests/DefaultContract_test.cdc
 
-	directoryPath := filepath.Join(tempDir, "cadence")
-
 	templates := TemplateMap{
 		"contract": []TemplateItem{
 			Contract{
@@ -218,7 +237,7 @@ func startInteractiveSetup(
 		},
 	}
 
-	generator := NewGenerator(directoryPath, state, logger, true, false)
+	generator := NewGenerator(tempDir, state, logger, true, false)
 	err = generator.Create(templates)
 	if err != nil {
 		return "", err
@@ -237,6 +256,11 @@ func startInteractiveSetup(
 		return "", err
 	}
 
+	err = updateGitignore(tempDir)
+	if err != nil {
+		return "", err
+	}
+
 	// Move the temp directory to the target directory
 	err = os.Rename(tempDir, targetDir)
 	if err != nil {


Test patch:
diff --git a/internal/super/generate_test.go b/internal/super/generate_test.go
index e07c7cbe..9417192e 100644
--- a/internal/super/generate_test.go
+++ b/internal/super/generate_test.go
@@ -34,17 +34,17 @@ func TestGenerateNewContract(t *testing.T) {
 	logger := output.NewStdoutLogger(output.NoneLog)
 	_, state, _ := util.TestMocks(t)
 
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 
 	// Test contract generation
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
+	fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/TestContract.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, fileContent)
 
-	testContent, err := state.ReaderWriter().ReadFile("cadence/tests/TestContract_test.cdc")
+	testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/TestContract_test.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, testContent)
 
@@ -72,7 +72,7 @@ access(all) fun testContract() {
 	assert.Equal(t, expectedTestContent, util.NormalizeLineEndings(string(testContent)))
 
 	// Test file already exists scenario
-	generatorTwo := NewGenerator("cadence", state, logger, false, true)
+	generatorTwo := NewGenerator("", state, logger, false, true)
 	err = generatorTwo.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
 	assert.Error(t, err)
 	expectedError := fmt.Sprintf("file already exists: %s", filepath.FromSlash("cadence/contracts/TestContract.cdc"))
@@ -83,17 +83,17 @@ func TestGenerateContractWithAccount(t *testing.T) {
 	logger := output.NewStdoutLogger(output.NoneLog)
 	_, state, _ := util.TestMocks(t)
 
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 
 	// Test contract generation
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: "example-account"}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/example-account/TestContract.cdc")
+	fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/example-account/TestContract.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, fileContent)
 
-	testContent, err := state.ReaderWriter().ReadFile("cadence/tests/TestContract_test.cdc")
+	testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/TestContract_test.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, testContent)
 }
@@ -104,7 +104,7 @@ func TestGenerateNewContractSkipTests(t *testing.T) {
 
 	generateFlags.SkipTests = true
 
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	t.Cleanup(func() {
 		generateFlags.SkipTests = false
 	})
@@ -113,11 +113,11 @@ func TestGenerateNewContractSkipTests(t *testing.T) {
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
+	fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/TestContract.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, fileContent)
 
-	testContent, err := state.ReaderWriter().ReadFile("cadence/tests/TestContract_test.cdc")
+	testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/TestContract_test.cdc"))
 	assert.Error(t, err, "Failed to read generated file")
 	assert.Nil(t, testContent)
 }
@@ -127,15 +127,15 @@ func TestGenerateNewContractWithCDCExtension(t *testing.T) {
 	_, state, _ := util.TestMocks(t)
 
 	// Test contract generation
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "Tester.cdc", Account: ""}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	fileContent, err := state.ReaderWriter().ReadFile("cadence/contracts/Tester.cdc")
+	fileContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/Tester.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, fileContent)
 
-	testContent, err := state.ReaderWriter().ReadFile("cadence/tests/Tester_test.cdc")
+	testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/tests/Tester_test.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, testContent)
 }
@@ -145,17 +145,17 @@ func TestGenerateNewContractFileAlreadyExists(t *testing.T) {
 	_, state, _ := util.TestMocks(t)
 
 	// Test contract generation
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
 	//// Check if the file exists in the correct directory
-	content, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
+	content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/TestContract.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, content)
 
 	// Test file already exists scenario
-	generatorTwo := NewGenerator("cadence", state, logger, false, true)
+	generatorTwo := NewGenerator("", state, logger, false, true)
 	err = generatorTwo.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
 	assert.Error(t, err)
 	expectedError := fmt.Sprintf("file already exists: %s", filepath.FromSlash("cadence/contracts/TestContract.cdc"))
@@ -166,12 +166,12 @@ func TestGenerateNewContractWithFileExtension(t *testing.T) {
 	logger := output.NewStdoutLogger(output.NoneLog)
 	_, state, _ := util.TestMocks(t)
 
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract.cdc", Account: ""}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
 	// Check file exists
-	content, err := state.ReaderWriter().ReadFile("cadence/contracts/TestContract.cdc")
+	content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/contracts/TestContract.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, content)
 }
@@ -180,11 +180,11 @@ func TestGenerateNewScript(t *testing.T) {
 	logger := output.NewStdoutLogger(output.NoneLog)
 	_, state, _ := util.TestMocks(t)
 
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	err := generator.Create(TemplateMap{"script": []TemplateItem{ScriptTemplate{Name: "TestScript"}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	content, err := state.ReaderWriter().ReadFile("cadence/scripts/TestScript.cdc")
+	content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/scripts/TestScript.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, content)
 
@@ -199,11 +199,11 @@ func TestGenerateNewTransaction(t *testing.T) {
 	logger := output.NewStdoutLogger(output.NoneLog)
 	_, state, _ := util.TestMocks(t)
 
-	generator := NewGenerator("cadence", state, logger, false, true)
+	generator := NewGenerator("", state, logger, false, true)
 	err := generator.Create(TemplateMap{"transaction": []TemplateItem{TransactionTemplate{Name: "TestTransaction"}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	content, err := state.ReaderWriter().ReadFile("cadence/transactions/TestTransaction.cdc")
+	content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("cadence/transactions/TestTransaction.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, content)
 
@@ -223,11 +223,11 @@ func TestGenerateNewWithDirFlag(t *testing.T) {
 	err := generator.Create(TemplateMap{"contract": []TemplateItem{Contract{Name: "TestContract", Account: ""}}})
 	assert.NoError(t, err, "Failed to generate contract")
 
-	content, err := state.ReaderWriter().ReadFile("customDir/contracts/TestContract.cdc")
+	content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("customDir/cadence/contracts/TestContract.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, content)
 
-	testContent, err := state.ReaderWriter().ReadFile("customDir/tests/TestContract_test.cdc")
+	testContent, err := state.ReaderWriter().ReadFile(filepath.FromSlash("customDir/cadence/tests/TestContract_test.cdc"))
 	assert.NoError(t, err, "Failed to read generated file")
 	assert.NotNil(t, testContent)