From 606ca7a87e006624078f984703a306c0ebf42fa5 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink <17958158+jribbink@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:43:37 -0700 Subject: [PATCH] Generate Readme.md (#1762) --- internal/super/generator/file_template.go | 66 +++++++ .../generator/fixtures/README_no_deps.md | 163 +++++++++++++++++ .../generator/fixtures/README_with_deps.md | 168 +++++++++++++++++ internal/super/generator/generator_test.go | 75 ++++++++ .../super/generator/templates/README.md.tmpl | 167 +++++++++++++++++ internal/super/setup.go | 54 ++++-- testing/better/README.md | 172 ++++++++++++++++++ 7 files changed, 852 insertions(+), 13 deletions(-) create mode 100644 internal/super/generator/file_template.go create mode 100644 internal/super/generator/fixtures/README_no_deps.md create mode 100644 internal/super/generator/fixtures/README_with_deps.md create mode 100644 internal/super/generator/templates/README.md.tmpl create mode 100644 testing/better/README.md diff --git a/internal/super/generator/file_template.go b/internal/super/generator/file_template.go new file mode 100644 index 000000000..a6f5f03ad --- /dev/null +++ b/internal/super/generator/file_template.go @@ -0,0 +1,66 @@ +/* + * Flow CLI + * + * Copyright Flow Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package generator + +import ( + "github.com/onflow/flowkit/v2" +) + +// FileTemplate is a template for raw +type FileTemplate struct { + TemplatePath string + TargetPath string + Data map[string]interface{} +} + +func NewFileTemplate( + templatePath string, + targetPath string, + data map[string]interface{}, +) FileTemplate { + return FileTemplate{ + TemplatePath: templatePath, + TargetPath: targetPath, + Data: data, + } +} + +var _ TemplateItem = FileTemplate{} + +// GetType returns the type of the contract +func (c FileTemplate) GetType() string { + return "file" +} + +func (c FileTemplate) GetTemplatePath() string { + return c.TemplatePath +} + +// GetData returns the data of the contract +func (c FileTemplate) GetData() map[string]interface{} { + return c.Data +} + +func (c FileTemplate) GetTargetPath() string { + return c.TargetPath +} + +func (c FileTemplate) UpdateState(state *flowkit.State) error { + return nil +} diff --git a/internal/super/generator/fixtures/README_no_deps.md b/internal/super/generator/fixtures/README_no_deps.md new file mode 100644 index 000000000..d44869cca --- /dev/null +++ b/internal/super/generator/fixtures/README_no_deps.md @@ -0,0 +1,163 @@ +## ๐Ÿ‘‹ Welcome Flow Developer! + +Welcome to your new Flow project. This project is a starting point for you to develop smart contracts on the Flow Blockchain. It comes with example contracts, scripts, transactions, and tests to help you get started. + +## ๐Ÿ”จ Getting Started + +Here are some essential resources to help you hit the ground running: + +- **[Flow Documentation](https://developers.flow.com/)** - The official Flow Documentation is a great starting point to start learning about about [building](https://developers.flow.com/build/flow) on Flow. +- **[Cadence Documentation](https://cadence-lang.org/docs/language)** - Cadence is the native language for the Flow Blockchain. It is a resource-oriented programming language that is designed for developing smart contracts. The documentation is a great place to start learning about the language. +- **[Visual Studio Code](https://code.visualstudio.com/)** and the **[Cadence Extension](https://marketplace.visualstudio.com/items?itemName=onflow.cadence)** - It is recommended to use the Visual Studio Code IDE with the Cadence extension installed. This will provide syntax highlighting, code completion, and other features to support Cadence development. +- **[Flow Clients](https://developers.flow.com/tools/clients)** - There are clients available in multiple languages to interact with the Flow Blockchain. You can use these clients to interact with your smart contracts, run transactions, and query data from the network. +- **[Block Explorers](https://developers.flow.com/ecosystem/block-explorers)** - Block explorers are tools that allow you to explore on-chain data. You can use them to view transactions, accounts, events, and other information. [Flowser](https://flowser.dev/) is a powerful block explorer for local development on the Flow Emulator. + +## ๐Ÿ“ฆ Project Structure + +Your project has been set up with the following structure: + +- `flow.json` - This is the configuration file for your project (analogous to a `package.json` file for NPM). It has been initialized with a basic configuration to get started. +- `/cadence` - This is where your Cadence smart contracts code lives + +Inside the `cadence` folder you will find: +- `/contracts` - This folder contains your Cadence contracts (these are deployed to the network and contain the business logic for your application) + - `ExampleContract.cdc` +- `/scripts` - This folder contains your Cadence scripts (read-only operations) + - `ExampleScript.cdc` +- `/transactions` - This folder contains your Cadence transactions (state-changing operations) + - `ExampleTransaction.cdc` +- `/tests` - This folder contains your Cadence tests (integration tests for your contracts, scripts, and transactions to verify they behave as expected) + - `ExampleTest.cdc` + +## ๐Ÿ‘จโ€๐Ÿ’ป Start Developing + +### Creating a New Contract + +To add a new contract to your project, run the following command: + +```shell +flow generate contract +``` + +This command will create a new contract file and add it to the `flow.json` configuration file. + +### Creating a New Script + +To add a new script to your project, run the following command: + +```shell +flow generate script +``` + +This command will create a new script file. Scripts are used to read data from the blockchain and do not modify state (i.e. get the current balance of an account, get a user's NFTs, etc). + +You can import any of your own contracts or installed dependencies in your script file using the `import` keyword. For example: + +```cadence +import "Counter" +``` + +### Creating a New Transaction + +To add a new transaction to your project you can use the following command: + +```shell +flow generate transaction +``` + +This command will create a new transaction file. Transactions are used to modify the state of the blockchain (i.e purchase an NFT, transfer tokens, etc). + +You can import any dependencies as you would in a script file. + +### Creating a New Test + +To add a new test to your project you can use the following command: + +```shell +flow generate test +``` + +This command will create a new test file. Tests are used to verify that your contracts, scripts, and transactions are working as expected. + +### Installing External Dependencies + +If you want to use external contract dependencies (such as NonFungibleToken, FlowToken, FungibleToken, etc.) you can install them using [Flow CLI Dependency Manager](https://developers.flow.com/tools/flow-cli/dependency-manager). + +For example, to install the NonFungibleToken contract you can use the following command: + +```shell +flow deps add mainnet://1d7e57aa55817448.NonFungibleToken +``` + +Contracts can be found using [ContractBrowser](https://contractbrowser.com/), but be sure to verify the authenticity before using third-party contracts in your project. + +## ๐Ÿงช Testing + +To verify that your project is working as expected you can run the tests using the following command: + +```shell +flow test +``` + +This command will run all tests with the `_test.cdc` suffix (these can be found in the `cadence/tests` folder). You can add more tests here using the `flow generate test` command (or by creating them manually). + +To learn more about testing in Cadence, check out the [Cadence Test Framework Documentation](https://cadence-lang.org/docs/testing-framework). + +## ๐Ÿš€ Deploying Your Project + +To deploy your project to the Flow network, you must first have a Flow account and have configured your deployment targets in the `flow.json` configuration file. + +You can create a new Flow account using the following command: + +```shell +flow accounts create +``` + +Learn more about setting up deployment targets in the [Flow CLI documentation](https://developers.flow.com/tools/flow-cli/deployment/project-contracts). + +### Deploying to the Flow Emulator + +To deploy your project to the Flow Emulator, start the emulator using the following command: + +```shell +flow emulator --start +``` + +To deploy your project, run the following command: + +```shell +flow project deploy --network=emulator +``` + +This command will start the Flow Emulator and deploy your project to it. You can now interact with your project using the Flow CLI or alternate [client](https://developers.flow.com/tools/clients). + +### Deploying to Flow Testnet + +To deploy your project to Flow Testnet you can use the following command: + +```shell +flow project deploy --network=testnet +``` + +This command will deploy your project to Flow Testnet. You can now interact with your project on this network using the Flow CLI or any other Flow client. + +### Deploying to Flow Mainnet + +To deploy your project to Flow Mainnet you can use the following command: + +```shell +flow project deploy --network=mainnet +``` + +This command will deploy your project to Flow Mainnet. You can now interact with your project using the Flow CLI or alternate [client](https://developers.flow.com/tools/clients). + +## ๐Ÿ“š Other Resources + +- [Cadence Design Patterns](https://cadence-lang.org/docs/design-patterns) +- [Cadence Anti-Patterns](https://cadence-lang.org/docs/anti-patterns) +- [Flow Core Contracts](https://developers.flow.com/build/core-contracts) + +## ๐Ÿค Community +- [Flow Community Forum](https://forum.onflow.org/) +- [Flow Discord](https://discord.gg/flow) +- [Flow Twitter](https://x.com/flow_blockchain) diff --git a/internal/super/generator/fixtures/README_with_deps.md b/internal/super/generator/fixtures/README_with_deps.md new file mode 100644 index 000000000..c1c2bfc9f --- /dev/null +++ b/internal/super/generator/fixtures/README_with_deps.md @@ -0,0 +1,168 @@ +## ๐Ÿ‘‹ Welcome Flow Developer! + +Welcome to your new Flow project. This project is a starting point for you to develop smart contracts on the Flow Blockchain. It comes with example contracts, scripts, transactions, and tests to help you get started. + +## ๐Ÿ”จ Getting Started + +Here are some essential resources to help you hit the ground running: + +- **[Flow Documentation](https://developers.flow.com/)** - The official Flow Documentation is a great starting point to start learning about about [building](https://developers.flow.com/build/flow) on Flow. +- **[Cadence Documentation](https://cadence-lang.org/docs/language)** - Cadence is the native language for the Flow Blockchain. It is a resource-oriented programming language that is designed for developing smart contracts. The documentation is a great place to start learning about the language. +- **[Visual Studio Code](https://code.visualstudio.com/)** and the **[Cadence Extension](https://marketplace.visualstudio.com/items?itemName=onflow.cadence)** - It is recommended to use the Visual Studio Code IDE with the Cadence extension installed. This will provide syntax highlighting, code completion, and other features to support Cadence development. +- **[Flow Clients](https://developers.flow.com/tools/clients)** - There are clients available in multiple languages to interact with the Flow Blockchain. You can use these clients to interact with your smart contracts, run transactions, and query data from the network. +- **[Block Explorers](https://developers.flow.com/ecosystem/block-explorers)** - Block explorers are tools that allow you to explore on-chain data. You can use them to view transactions, accounts, events, and other information. [Flowser](https://flowser.dev/) is a powerful block explorer for local development on the Flow Emulator. + +## ๐Ÿ“ฆ Project Structure + +Your project has been set up with the following structure: + +- `flow.json` - This is the configuration file for your project (analogous to a `package.json` file for NPM). It has been initialized with a basic configuration and your selected Core Contract dependencies to get started. + + Your project has also been configured with the following dependencies. You can add more dependencies using the `flow deps add` command: + - `FlowToken` + - `FungibleToken` + +- `/cadence` - This is where your Cadence smart contracts code lives + +Inside the `cadence` folder you will find: +- `/contracts` - This folder contains your Cadence contracts (these are deployed to the network and contain the business logic for your application) + - `ExampleContract.cdc` +- `/scripts` - This folder contains your Cadence scripts (read-only operations) + - `ExampleScript.cdc` +- `/transactions` - This folder contains your Cadence transactions (state-changing operations) + - `ExampleTransaction.cdc` +- `/tests` - This folder contains your Cadence tests (integration tests for your contracts, scripts, and transactions to verify they behave as expected) + - `ExampleTest.cdc` + +## ๐Ÿ‘จโ€๐Ÿ’ป Start Developing + +### Creating a New Contract + +To add a new contract to your project, run the following command: + +```shell +flow generate contract +``` + +This command will create a new contract file and add it to the `flow.json` configuration file. + +### Creating a New Script + +To add a new script to your project, run the following command: + +```shell +flow generate script +``` + +This command will create a new script file. Scripts are used to read data from the blockchain and do not modify state (i.e. get the current balance of an account, get a user's NFTs, etc). + +You can import any of your own contracts or installed dependencies in your script file using the `import` keyword. For example: + +```cadence +import "Counter" +``` + +### Creating a New Transaction + +To add a new transaction to your project you can use the following command: + +```shell +flow generate transaction +``` + +This command will create a new transaction file. Transactions are used to modify the state of the blockchain (i.e purchase an NFT, transfer tokens, etc). + +You can import any dependencies as you would in a script file. + +### Creating a New Test + +To add a new test to your project you can use the following command: + +```shell +flow generate test +``` + +This command will create a new test file. Tests are used to verify that your contracts, scripts, and transactions are working as expected. + +### Installing External Dependencies + +If you want to use external contract dependencies (such as NonFungibleToken, FlowToken, FungibleToken, etc.) you can install them using [Flow CLI Dependency Manager](https://developers.flow.com/tools/flow-cli/dependency-manager). + +For example, to install the NonFungibleToken contract you can use the following command: + +```shell +flow deps add mainnet://1d7e57aa55817448.NonFungibleToken +``` + +Contracts can be found using [ContractBrowser](https://contractbrowser.com/), but be sure to verify the authenticity before using third-party contracts in your project. + +## ๐Ÿงช Testing + +To verify that your project is working as expected you can run the tests using the following command: + +```shell +flow test +``` + +This command will run all tests with the `_test.cdc` suffix (these can be found in the `cadence/tests` folder). You can add more tests here using the `flow generate test` command (or by creating them manually). + +To learn more about testing in Cadence, check out the [Cadence Test Framework Documentation](https://cadence-lang.org/docs/testing-framework). + +## ๐Ÿš€ Deploying Your Project + +To deploy your project to the Flow network, you must first have a Flow account and have configured your deployment targets in the `flow.json` configuration file. + +You can create a new Flow account using the following command: + +```shell +flow accounts create +``` + +Learn more about setting up deployment targets in the [Flow CLI documentation](https://developers.flow.com/tools/flow-cli/deployment/project-contracts). + +### Deploying to the Flow Emulator + +To deploy your project to the Flow Emulator, start the emulator using the following command: + +```shell +flow emulator --start +``` + +To deploy your project, run the following command: + +```shell +flow project deploy --network=emulator +``` + +This command will start the Flow Emulator and deploy your project to it. You can now interact with your project using the Flow CLI or alternate [client](https://developers.flow.com/tools/clients). + +### Deploying to Flow Testnet + +To deploy your project to Flow Testnet you can use the following command: + +```shell +flow project deploy --network=testnet +``` + +This command will deploy your project to Flow Testnet. You can now interact with your project on this network using the Flow CLI or any other Flow client. + +### Deploying to Flow Mainnet + +To deploy your project to Flow Mainnet you can use the following command: + +```shell +flow project deploy --network=mainnet +``` + +This command will deploy your project to Flow Mainnet. You can now interact with your project using the Flow CLI or alternate [client](https://developers.flow.com/tools/clients). + +## ๐Ÿ“š Other Resources + +- [Cadence Design Patterns](https://cadence-lang.org/docs/design-patterns) +- [Cadence Anti-Patterns](https://cadence-lang.org/docs/anti-patterns) +- [Flow Core Contracts](https://developers.flow.com/build/core-contracts) + +## ๐Ÿค Community +- [Flow Community Forum](https://forum.onflow.org/) +- [Flow Discord](https://discord.gg/flow) +- [Flow Twitter](https://x.com/flow_blockchain) diff --git a/internal/super/generator/generator_test.go b/internal/super/generator/generator_test.go index 230df5955..a2c0a54ef 100644 --- a/internal/super/generator/generator_test.go +++ b/internal/super/generator/generator_test.go @@ -19,6 +19,7 @@ package generator import ( + "embed" "fmt" "path/filepath" "testing" @@ -30,6 +31,9 @@ import ( "github.com/onflow/flow-cli/internal/util" ) +//go:embed fixtures/*.* +var fixturesFS embed.FS + func TestGenerateNewContract(t *testing.T) { logger := output.NewStdoutLogger(output.NoneLog) _, state, _ := util.TestMocks(t) @@ -221,3 +225,74 @@ access(all) fun testContract() { }` assert.Equal(t, expectedContent, util.NormalizeLineEndings(string(content))) } + +func TestGenerateReadmeNoDeps(t *testing.T) { + logger := output.NewStdoutLogger(output.NoneLog) + _, state, _ := util.TestMocks(t) + + g := NewGenerator("", state, logger, false, true) + err := g.Create(FileTemplate{ + TemplatePath: "README.md.tmpl", + TargetPath: "README.md", + Data: map[string]interface{}{ + "Dependencies": []map[string]interface{}{}, + "Contracts": []map[string]interface{}{ + {"Name": "ExampleContract"}, + }, + "Transactions": []map[string]interface{}{ + {"Name": "ExampleTransaction"}, + }, + "Scripts": []map[string]interface{}{ + {"Name": "ExampleScript"}, + }, + "Tests": []map[string]interface{}{ + {"Name": "ExampleTest"}, + }, + }, + }) + assert.NoError(t, err, "Failed to generate file") + + content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("README.md")) + assert.NoError(t, err, "Failed to read generated file") + assert.NotNil(t, content) + + readmeNoDepsFixture, _ := fixturesFS.ReadFile("fixtures/README_no_deps.md") + assert.Equal(t, string(readmeNoDepsFixture), string(content)) +} + +func TestGenerateReadmeWithDeps(t *testing.T) { + logger := output.NewStdoutLogger(output.NoneLog) + _, state, _ := util.TestMocks(t) + + g := NewGenerator("", state, logger, false, true) + err := g.Create(FileTemplate{ + TemplatePath: "README.md.tmpl", + TargetPath: "README.md", + Data: map[string]interface{}{ + "Dependencies": []map[string]interface{}{ + {"Name": "FlowToken"}, + {"Name": "FungibleToken"}, + }, + "Contracts": []map[string]interface{}{ + {"Name": "ExampleContract"}, + }, + "Transactions": []map[string]interface{}{ + {"Name": "ExampleTransaction"}, + }, + "Scripts": []map[string]interface{}{ + {"Name": "ExampleScript"}, + }, + "Tests": []map[string]interface{}{ + {"Name": "ExampleTest"}, + }, + }, + }) + assert.NoError(t, err, "Failed to generate file") + + content, err := state.ReaderWriter().ReadFile(filepath.FromSlash("README.md")) + assert.NoError(t, err, "Failed to read generated file") + assert.NotNil(t, content) + + readmeWithDepsFixture, _ := fixturesFS.ReadFile("fixtures/README_with_deps.md") + assert.Equal(t, string(readmeWithDepsFixture), string(content)) +} diff --git a/internal/super/generator/templates/README.md.tmpl b/internal/super/generator/templates/README.md.tmpl new file mode 100644 index 000000000..1e3d61cfb --- /dev/null +++ b/internal/super/generator/templates/README.md.tmpl @@ -0,0 +1,167 @@ +## ๐Ÿ‘‹ Welcome Flow Developer! + +Welcome to your new Flow project. This project is a starting point for you to develop smart contracts on the Flow Blockchain. It comes with example contracts, scripts, transactions, and tests to help you get started. + +## ๐Ÿ”จ Getting Started + +Here are some essential resources to help you hit the ground running: + +- **[Flow Documentation](https://developers.flow.com/)** - The official Flow Documentation is a great starting point to start learning about about [building](https://developers.flow.com/build/flow) on Flow. +- **[Cadence Documentation](https://cadence-lang.org/docs/language)** - Cadence is the native language for the Flow Blockchain. It is a resource-oriented programming language that is designed for developing smart contracts. The documentation is a great place to start learning about the language. +- **[Visual Studio Code](https://code.visualstudio.com/)** and the **[Cadence Extension](https://marketplace.visualstudio.com/items?itemName=onflow.cadence)** - It is recommended to use the Visual Studio Code IDE with the Cadence extension installed. This will provide syntax highlighting, code completion, and other features to support Cadence development. +- **[Flow Clients](https://developers.flow.com/tools/clients)** - There are clients available in multiple languages to interact with the Flow Blockchain. You can use these clients to interact with your smart contracts, run transactions, and query data from the network. +- **[Block Explorers](https://developers.flow.com/ecosystem/block-explorers)** - Block explorers are tools that allow you to explore on-chain data. You can use them to view transactions, accounts, events, and other information. [Flowser](https://flowser.dev/) is a powerful block explorer for local development on the Flow Emulator. + +## ๐Ÿ“ฆ Project Structure + +Your project has been set up with the following structure: + +- `flow.json` - This is the configuration file for your project (analogous to a `package.json` file for NPM). It has been initialized with a basic configuration{{ if len .Dependencies }} and your selected Core Contract dependencies{{ end }} to get started.{{ if len .Dependencies }} + + Your project has also been configured with the following dependencies. You can add more dependencies using the `flow deps add` command:{{ range .Dependencies }} + - `{{ .Name }}`{{ end }} +{{ end }} +- `/cadence` - This is where your Cadence smart contracts code lives + +Inside the `cadence` folder you will find: +- `/contracts` - This folder contains your Cadence contracts (these are deployed to the network and contain the business logic for your application){{ range .Contracts }} + - `{{ .Name }}.cdc`{{ end }} +- `/scripts` - This folder contains your Cadence scripts (read-only operations){{ range .Scripts }} + - `{{ .Name }}.cdc`{{ end }} +- `/transactions` - This folder contains your Cadence transactions (state-changing operations){{ range .Transactions }} + - `{{ .Name }}.cdc`{{ end }} +- `/tests` - This folder contains your Cadence tests (integration tests for your contracts, scripts, and transactions to verify they behave as expected){{ range .Tests }} + - `{{ .Name }}.cdc`{{ end }} + +## ๐Ÿ‘จโ€๐Ÿ’ป Start Developing + +### Creating a New Contract + +To add a new contract to your project, run the following command: + +```shell +flow generate contract +``` + +This command will create a new contract file and add it to the `flow.json` configuration file. + +### Creating a New Script + +To add a new script to your project, run the following command: + +```shell +flow generate script +``` + +This command will create a new script file. Scripts are used to read data from the blockchain and do not modify state (i.e. get the current balance of an account, get a user's NFTs, etc). + +You can import any of your own contracts or installed dependencies in your script file using the `import` keyword. For example: + +```cadence +import "Counter" +``` + +### Creating a New Transaction + +To add a new transaction to your project you can use the following command: + +```shell +flow generate transaction +``` + +This command will create a new transaction file. Transactions are used to modify the state of the blockchain (i.e purchase an NFT, transfer tokens, etc). + +You can import any dependencies as you would in a script file. + +### Creating a New Test + +To add a new test to your project you can use the following command: + +```shell +flow generate test +``` + +This command will create a new test file. Tests are used to verify that your contracts, scripts, and transactions are working as expected. + +### Installing External Dependencies + +If you want to use external contract dependencies (such as NonFungibleToken, FlowToken, FungibleToken, etc.) you can install them using [Flow CLI Dependency Manager](https://developers.flow.com/tools/flow-cli/dependency-manager). + +For example, to install the NonFungibleToken contract you can use the following command: + +```shell +flow deps add mainnet://1d7e57aa55817448.NonFungibleToken +``` + +Contracts can be found using [ContractBrowser](https://contractbrowser.com/), but be sure to verify the authenticity before using third-party contracts in your project. + +## ๐Ÿงช Testing + +To verify that your project is working as expected you can run the tests using the following command: + +```shell +flow test +``` + +This command will run all tests with the `_test.cdc` suffix (these can be found in the `cadence/tests` folder). You can add more tests here using the `flow generate test` command (or by creating them manually). + +To learn more about testing in Cadence, check out the [Cadence Test Framework Documentation](https://cadence-lang.org/docs/testing-framework). + +## ๐Ÿš€ Deploying Your Project + +To deploy your project to the Flow network, you must first have a Flow account and have configured your deployment targets in the `flow.json` configuration file. + +You can create a new Flow account using the following command: + +```shell +flow accounts create +``` + +Learn more about setting up deployment targets in the [Flow CLI documentation](https://developers.flow.com/tools/flow-cli/deployment/project-contracts). + +### Deploying to the Flow Emulator + +To deploy your project to the Flow Emulator, start the emulator using the following command: + +```shell +flow emulator --start +``` + +To deploy your project, run the following command: + +```shell +flow project deploy --network=emulator +``` + +This command will start the Flow Emulator and deploy your project to it. You can now interact with your project using the Flow CLI or alternate [client](https://developers.flow.com/tools/clients). + +### Deploying to Flow Testnet + +To deploy your project to Flow Testnet you can use the following command: + +```shell +flow project deploy --network=testnet +``` + +This command will deploy your project to Flow Testnet. You can now interact with your project on this network using the Flow CLI or any other Flow client. + +### Deploying to Flow Mainnet + +To deploy your project to Flow Mainnet you can use the following command: + +```shell +flow project deploy --network=mainnet +``` + +This command will deploy your project to Flow Mainnet. You can now interact with your project using the Flow CLI or alternate [client](https://developers.flow.com/tools/clients). + +## ๐Ÿ“š Other Resources + +- [Cadence Design Patterns](https://cadence-lang.org/docs/design-patterns) +- [Cadence Anti-Patterns](https://cadence-lang.org/docs/anti-patterns) +- [Flow Core Contracts](https://developers.flow.com/build/core-contracts) + +## ๐Ÿค Community +- [Flow Community Forum](https://forum.onflow.org/) +- [Flow Discord](https://discord.gg/flow) +- [Flow Twitter](https://x.com/flow_blockchain) diff --git a/internal/super/setup.go b/internal/super/setup.go index 4e3530075..696eef133 100644 --- a/internal/super/setup.go +++ b/internal/super/setup.go @@ -201,11 +201,20 @@ func startInteractiveSetup( return "", fmt.Errorf("failed to initialize configuration: %w", err) } - // Generate standard cadence files - // cadence/contracts/Counter.cdc - // cadence/scripts/GetCounter.cdc - // cadence/transactions/IncrementCounter.cdc - // cadence/tests/Counter_test.cdc + msg := "Would you like to install any core contracts and their dependencies?" + if prompt.GenericBoolPrompt(msg) { + err := dependencymanager.PromptInstallCoreContracts(logger, state, tempDir, nil) + if err != nil { + return "", err + } + } + + // Generate standard cadence files & README.md + // cadence/contracts/DefaultContract.cdc + // cadence/scripts/DefaultScript.cdc + // cadence/transactions/DefaultTransaction.cdc + // cadence/tests/DefaultContract_test.cdc + // README.md templates := []generator.TemplateItem{ generator.ContractTemplate{ @@ -222,6 +231,33 @@ func startInteractiveSetup( TemplatePath: "transaction_counter.cdc.tmpl", Data: map[string]interface{}{"ContractName": "Counter"}, }, + generator.FileTemplate{ + TemplatePath: "README.md.tmpl", + TargetPath: "README.md", + Data: map[string]interface{}{ + "Dependencies": (func() []map[string]interface{} { + contracts := []map[string]interface{}{} + for _, dep := range *state.Dependencies() { + contracts = append(contracts, map[string]interface{}{ + "Name": dep.Name, + }) + } + return contracts + })(), + "Contracts": []map[string]interface{}{ + {"Name": "Counter"}, + }, + "Scripts": []map[string]interface{}{ + {"Name": "GetCounter"}, + }, + "Transactions": []map[string]interface{}{ + {"Name": "IncrementCounter"}, + }, + "Tests": []map[string]interface{}{ + {"Name": "Counter_test"}, + }, + }, + }, } g := generator.NewGenerator(tempDir, state, logger, true, false) @@ -230,14 +266,6 @@ func startInteractiveSetup( return "", err } - msg := "Would you like to install any core contracts and their dependencies?" - if prompt.GenericBoolPrompt(msg) { - err := dependencymanager.PromptInstallCoreContracts(logger, state, tempDir, nil) - if err != nil { - return "", err - } - } - err = state.Save(filepath.Join(tempDir, "flow.json")) if err != nil { return "", err diff --git a/testing/better/README.md b/testing/better/README.md new file mode 100644 index 000000000..2502ef6de --- /dev/null +++ b/testing/better/README.md @@ -0,0 +1,172 @@ +## ๐Ÿ‘‹ Welcome Flow Developer! + +Welcome to your new Flow project. This project is a starting point for you to develop smart contracts on the Flow Blockchain. It comes with example contracts, scripts, transactions, and tests to help you get started. + +## ๐Ÿ”จ Getting started + +Here are some essential resources to help you hit the ground running: + +- **[Flow Documentation](https://docs.onflow.org/)** - The official Flow Documentation is a great starting point to begin learn about [building](https://developers.flow.com/build/flow) on Flow. +- **[Cadence Documentation](https://cadence-lang.org/docs/language)** - Cadence is the native language for the Flow Blockchain, it is a resource-oriented programming language that is designed for developing smart contracts. The documentation is a great place to start learning about the language. +- **[Visual Studio Code](https://code.visualstudio.com/)** and the **[Cadence Extension](https://marketplace.visualstudio.com/items?itemName=onflow.cadence)** - It is recommended to use the Visual Studio Code IDE with the Cadence extension installed. This will include syntax highlighting, code completion, and other features to support Cadence development. +- **[Flow Clients](https://developers.flow.com/tools/clients)** - There are clients available in multiple languages to interact with the Flow Blockchain. You can use these clients to interact with your smart contracts, run transactions, and query data from the network. +- **[Block Explorers](https://developers.flow.com/ecosystem/block-explorers)** - Block explorers are tools that allow you to explore on-chain data. You can use them to view transactions, accounts, events, and other information. [Flowser](https://flowser.dev/) is a powerful block explorer for local development on the Flow Emulator. + +## ๐Ÿ“ฆ Project Structure +Your project has been set up with the following structure: + +- `flow.json` - This is the configuration file for your project (analogous to a `package.json` file for NPM). It has been initialized with a basic configuration and your selected Core Contract dependencies to get started. + + Your project has also been configured with the following dependencies. You can add more dependencies using the `flow deps add` command: + - `Burner` + +- `/cadence` - This is where your Cadence smart contracts code lives + +Inside `cadence` folder you will find: +- `/contracts` - This folder contains your Cadence contracts (these are deployed to the network and contain the business logic for your application) + + - `Counter.cdc` + +- `/scripts` - This folder contains your Cadence scripts (read-only operations) + + - `GetCounter.cdc` + +- `/transactions` - This folder contains your Cadence transactions (state-changing operations) + + - `IncrementCounter.cdc` + +- `/tests` - This folder contains your Cadence tests (integration tests for your contracts, scripts, and transactions to verify they behave as expected) + - `Counter_test.cdc` + +## ๐Ÿ‘จโ€๐Ÿ’ป Start Developing + +### Creating a New Contract + +To add a new contract to your project, run the following command: + +```shell +flow generate contract +``` + +This command will create a new contract file and add it to the `flow.json` configuration file. + +### Creating a New Script + +To add a new script to your project, run the following command: + +```shell +flow generate script +``` + +This command will create a new script file. Scripts are used to read data from the blockchain and do not modify state (i.e. get the current balance of an account, get a user's NFTs, etc). + +You can import any of your own contracts or installed dependencies in your script file using the `import` keyword. For example: + +```cadence +import "Counter" +``` + +### Creating a New Transaction + +To add a new transaction to your project you can use the following command: + +```shell +flow generate transaction +``` + +This command will create a new transaction file. Transactions are used to modify the state of the blockchain (i.e purchase an NFT, transfer tokens, etc). + +You can import any dependencies as you would in a script file. + +### Creating a New test + +To add a new test to your project you can use the following command: + +```shell +flow generate test +``` + +This command will create a new test file. Tests are used to verify that your contracts, scripts, and transactions are working as expected. + +### Installing external dependencies + +If you want to use external contract dependencies (such as NonFungibleToken, FlowToken, FungibleToken, etc.) you can install them using [Flow CLI Dependency Manager](https://developers.flow.com/tools/flow-cli/dependency-manager). + +For example, to install the NonFungibleToken contract you can use the following command: + +```shell +flow deps add mainnet://1d7e57aa55817448.NonFungibleToken +``` + +Contracts can be found using [ContractBrowser](https://contractbrowser.com/), but be sure to verify the authenticity before using third-party contracts in your project. + +## ๐Ÿงช Testing + +To verify that your project is working as expected you can run the tests using the following command: + +```shell +flow test +``` + +This command will run all tests with the `_test.cdc` suffix (these can be found in the `cadence/tests` folder). You can add more tests here using the `flow generate test` command (or by creating them manually). + +To learn more about testing in Cadence, check out the [Cadence Test Framework Documentation](https://cadence-lang.org/docs/testing-framework). + +## ๐Ÿš€ Deploying your project + +To deploy your project to the Flow network, you must first have a Flow account and have configured your deployment targets in the `flow.json` configuration file. + +You can create a new Flow account using the following command: + +```shell +flow accounts create +``` + +Learn more about setting up deployment targets in the [Flow CLI documentation](https://developers.flow.com/tools/flow-cli/deployment/project-contracts). + +### Deploying to the Flow Emulator + +To deploy your project to the Flow Emulator, start the emulator using the following command: + +```shell +flow emulator --start +``` + +To deploy your project, run the following command: + +```shell +flow project deploy --network=emulator +``` + +This command will start the Flow Emulator and deploy your project to it. You can now interact with your smart contracts using the Flow CLI or any other Flow client. + +### Deploying to Flow Testnet + +To deploy your project to Flow Testnet you can use the following command: + +```shell +flow project deploy --network=testnet +``` + +This command will deploy your project to Flow Testnet. You can now interact with your project using the Flow Testnet. + +### Deploying to Flow Mainnet + +To deploy your project to Flow Mainnet you can use the following command: + +```shell +flow project deploy --network=mainnet +``` + +This command will deploy your project to Flow Mainnet. You now then interact with your project using the Flow Mainnet. + +## ๐Ÿ“š Other Resources + +- [Cadence Design Patterns](https://cadence-lang.org/docs/design-patterns) +- [Cadence Anti-Patterns](https://cadence-lang.org/docs/anti-patterns) +- [Flow Core Contracts](https://developers.flow.com/build/core-contracts) + +## ๐Ÿค Community +- [Flow Community Forum](https://forum.onflow.org/) +- [Flow Discord](https://discord.gg/flow) +- [Flow Twitter](https://x.com/flow_blockchain)