Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dfx new templates #3499

Merged
merged 32 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9634fbc
Base templates
adamspofford-dfinity Jan 11, 2024
5cd718e
frontend tests
adamspofford-dfinity Jan 11, 2024
a81f1e3
Update rust template
adamspofford-dfinity Jan 11, 2024
72f8e7d
Tests
adamspofford-dfinity Jan 12, 2024
ca62ab1
docs
adamspofford-dfinity Jan 12, 2024
6af6ff8
Merge branch 'master' into spofford/new-templates
adamspofford-dfinity Jan 12, 2024
899d891
csp
adamspofford-dfinity Jan 12, 2024
71d31c9
Fix tests
adamspofford-dfinity Jan 12, 2024
a64d1f7
Merge branch 'master' into spofford/new-templates
adamspofford-dfinity Jan 12, 2024
610a97e
.
adamspofford-dfinity Jan 12, 2024
92933e0
.
adamspofford-dfinity Jan 12, 2024
4276b2e
.
adamspofford-dfinity Jan 12, 2024
60a541e
More descriptive changelog
adamspofford-dfinity Jan 12, 2024
0865f40
Merge branch 'master' into spofford/new-templates
adamspofford-dfinity Jan 12, 2024
d138440
Update CHANGELOG.md
adamspofford-dfinity Jan 16, 2024
222fabb
Update dfx.json schema
adamspofford-dfinity Jan 16, 2024
ee12310
Merge branch 'master' into spofford/new-templates
adamspofford-dfinity Jan 16, 2024
42498a7
fix: removes App.css holdover from a different example
krpeacock Jan 18, 2024
65a8fb9
Merge branch 'spofford/new-templates' of github.com:dfinity/sdk into …
krpeacock Jan 18, 2024
817c52e
backwards compatibility & top-level package.json scripts
krpeacock Jan 19, 2024
6fb96cd
fix: dummy actor during svelte tests
krpeacock Jan 19, 2024
3e4c655
test works immediately without dfx generate
krpeacock Jan 20, 2024
f7b611f
Merge branch 'master' into spofford/new-templates
krpeacock Jan 26, 2024
6d541c5
fix ci
adamspofford-dfinity Feb 2, 2024
aa2a7a0
Remove raw redirect setting
adamspofford-dfinity Feb 8, 2024
abd9f09
Merge branch 'master' into spofford/new-templates
adamspofford-dfinity Feb 8, 2024
dbf4941
Rename Svelte to SvelteKit
adamspofford-dfinity Feb 8, 2024
995493b
.
adamspofford-dfinity Feb 8, 2024
3caebcf
revert declarations import path change?
adamspofford-dfinity Feb 8, 2024
8c178f7
Merge branch 'master' into spofford/new-templates
adamspofford-dfinity Feb 8, 2024
510faa4
please work
adamspofford-dfinity Feb 8, 2024
1021406
Merge branch 'master' into spofford/new-templates
lwshang Feb 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

# UNRELEASED

### feat: new starter templates

`dfx new` now has a new set of customizable project templates and an interactive menu for selecting them. Supports the Svelte, Vue, and React frameworks, and Azle and Kybra backends.

### fix: --no-frontend no longer creates a frontend

Previously `dfx new --no-frontend` still created a frontend canister. This behavior is now accessed via `--frontend simple-assets`.

### feat: `dfx cycles redeem-faucet-coupon`

It is now possible to redeem faucet coupons to cycles ledger accounts.
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions docs/cli-reference/dfx-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Use the `dfx new` command to create a new project for the IC. This command creat

You can use the `--dry-run` option to preview the directories and files to be created without adding them to the file system.

If called without any arguments besides the project name, it will interactively prompt you for the values of `--type`, `--frontend`, and `--extras`.

## Basic usage

``` bash
Expand All @@ -14,11 +16,13 @@ dfx new _project_name_ [flag]

You can use the following optional flags with the `dfx new` command:

| Flag | Description |
|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--dry-run` | Generates a preview the directories and files to be created for a new project without adding them to the file system. |
| `--frontend` | Installs the template frontend code for the default project canister. The default value for the flag is `true` if `node.js` is currently installed on your local computer. If `node.js` is not currently installed, you can set this flag to `true` to attempt to install `node.js` and the template file when creating the project or you can set the flag to `false` to skip the installation of template frontend code entirely. | |
| `--no-frontend` | Skips installing the frontend template code and instead creates an asset canister with a dummy `.txt` file. This is the default behavior if `node.js` is currently not installed on your computer. |
| Flag | Description |
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--dry-run` | Generates a preview of the directories and files to be created for a new project without adding them to the file system. |
| `--extras <EXTRAS>` | Comma-separated list of additional features to add to the project template. `bitcoin` and `internet-identity` will insert the appropriate boilerplate into `dfx.json`, and `frontend-tests` adds a `vitest` skeleton to the frontend project. |
| `--frontend <FRONTEND>` | Installs the template frontend code for the default project canister. The default value for the flag is `vanilla` if `node.js` is currently installed on your local computer. If `node.js` is not currently installed, you can set this flag to attempt to install `node.js` and the template file when creating the project or you can set the flag to `none` to skip the installation of template frontend code entirely. Possible values: `svelte`, `react`, `vue`, `vanilla`, `plain-assets`, `none`. |
| `--no-frontend` | Skips installing the frontend template code. This is the default behavior if `node.js` is currently not installed on your computer. Equivalent to `--frontend none`. |
| `--type <TYPE>` | Selects the template backend code for the default project canister. The default value for the flag is `motoko`. Possible values: `motoko`, `rust`, `azle`, `kybra`. |

## Arguments

Expand Down
10 changes: 9 additions & 1 deletion docs/dfx-json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@
"enum": [
"assets"
]
},
"workspace": {
"title": "NPM workspace",
"description": "The workspace in package.json that this canister is in, if it is not in the root workspace.",
"type": [
"string",
"null"
]
}
}
},
Expand Down Expand Up @@ -1068,4 +1076,4 @@
]
}
}
}
}
21 changes: 21 additions & 0 deletions e2e/assets/expect_scripts/rust_svelte_with_tests_and_ii.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/expect -df

match_max 100000
set timeout 30

spawn dfx new e2e_project
expect "Select a backend language:"
# down arrow, Rust should be option 2
send "\033\[B"
send "\r"

expect "Select a frontend framework:"
# no down arrow, Svelte should be option 1
send "\r"
# first and third, should be II and frontend tests respectively
expect "Add extra features"
send " "
send "\033\[B\033\[B"
send " "
send "\r"
expect eof
6 changes: 5 additions & 1 deletion e2e/tests-dfx/assetscanister.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup

dfx_new
dfx_new_assets
}

teardown() {
Expand Down Expand Up @@ -91,6 +91,7 @@ check_permission_failure() {
PREPARE_PRINCIPAL=$(dfx identity get-principal --identity prepare)
COMMIT_PRINCIPAL=$(dfx identity get-principal --identity commit)

rm src/e2e_project_frontend/assets/.ic-assets.json5
install_asset assetscanister
# Prep for a DeleteAsset operation
echo "to-be-deleted" >src/e2e_project_frontend/assets/to-be-deleted.txt
Expand Down Expand Up @@ -196,6 +197,7 @@ check_permission_failure() {
PREPARE_PRINCIPAL=$(dfx identity get-principal --identity prepare)
COMMIT_PRINCIPAL=$(dfx identity get-principal --identity commit)

rm src/e2e_project_frontend/assets/.ic-assets.json5
install_asset assetscanister
dfx_start
mkdir tmp
Expand Down Expand Up @@ -1626,6 +1628,7 @@ WARN: {
}

@test "asset configuration via .ic-assets.json5 - pretty printing when deploying" {
rm src/e2e_project_frontend/assets/.ic-assets.json5
install_asset assetscanister

dfx_start
Expand Down Expand Up @@ -1668,6 +1671,7 @@ WARN: {
}

@test "syncs asset properties when redeploying" {
rm src/e2e_project_frontend/assets/.ic-assets.json5
install_asset assetscanister
dfx_start
assert_command dfx deploy
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/bitcoin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ set_local_network_bitcoin_enabled() {
}

@test "dfx restarts replica when ic-btc-adapter restarts" {
dfx_new hello
dfx_new_assets hello
dfx_start --enable-bitcoin

install_asset greet
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/build_granular.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup

dfx_new
dfx_new_assets
}

teardown() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/canister_http.bash
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ set_shared_local_network_canister_http_empty() {
}

@test "dfx restarts replica when ic-https-outcalls-adapter restarts" {
dfx_new hello
dfx_new_assets hello
dfx_start

install_asset greet
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/create.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load ../utils/cycles-ledger
setup() {
standard_setup

dfx_new
dfx_new_assets
}

teardown() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/deploy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load ../utils/cycles-ledger
setup() {
standard_setup

dfx_new hello
dfx_new_assets hello
}

teardown() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/dotenv.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup

dfx_new
dfx_new_assets
}

teardown() {
Expand Down
14 changes: 0 additions & 14 deletions e2e/tests-dfx/error_diagnosis.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ teardown() {
standard_teardown
}

@test "Duplicate assets in dist/ from src/" {
dfx_new_frontend hello
install_asset greet
dfx_start
assert_command dfx deploy

# simulate previous deploy with CopyPlugin step
cp src/hello_frontend/assets/* dist/hello_frontend/

assert_command_fail dfx deploy
assert_contains "Remove the CopyPlugin step from webpack.config.js"
assert_contains "Delete all files from the dist/ directory"
}

@test "HTTP 403 has a full diagnosis" {
dfx_new hello
install_asset greet
Expand Down
16 changes: 8 additions & 8 deletions e2e/tests-dfx/frontend.bash
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ teardown() {
assert_match "Connection refused"
}

@test "dfx uses .ic-assets.json file provided in src/__project_name__frontend/src" {
echo '[{"match": "*", "headers": {"x-key": "x-value"}}]' > src/e2e_project_frontend/src/.ic-assets.json
@test "dfx uses .ic-assets.json file provided in src/__project_name__frontend/assets" {
echo '[{"match": "*", "headers": {"x-key": "x-value"}}]' > src/e2e_project_frontend/assets/.ic-assets.json5

dfx_start
dfx canister create --all
Expand All @@ -77,14 +77,14 @@ teardown() {
PORT=$(get_webserver_port)
assert_command curl -vv http://localhost:"$PORT"/?canisterId="$ID"
assert_match "< x-key: x-value"
assert_command curl -vv http://localhost:"$PORT"/index.js?canisterId="$ID"
assert_command curl -vv http://localhost:"$PORT"/favicon.ico?canisterId="$ID"
assert_match "< x-key: x-value"
}

@test "dfx uses a custom build command if one is provided" {
jq '.canisters.e2e_project_frontend.source = ["dist/e2e_project_frontend/"]' dfx.json | sponge dfx.json
jq '.canisters.e2e_project_frontend.build = ["npm run custom-build"]' dfx.json | sponge dfx.json
jq '.scripts["custom-build"] = "mkdir -p ./dist/e2e_project_frontend/assets/ && cp -r ./src/e2e_project_frontend/assets/* ./dist/e2e_project_frontend"' package.json | sponge package.json
jq '.canisters.e2e_project_frontend.source = ["src/e2e_project_frontend/dist2"]' dfx.json | sponge dfx.json
jq '.canisters.e2e_project_frontend.build = ["npm run custom-build --workspace e2e_project_frontend"]' dfx.json | sponge dfx.json
jq '.scripts["custom-build"] = "npm run build && mkdir -p ./dist2/ && cp -r ./dist/* ./dist2"' src/e2e_project_frontend/package.json | sponge src/e2e_project_frontend/package.json

dfx_start
dfx canister create --all
Expand All @@ -94,6 +94,6 @@ teardown() {
ID=$(dfx canister id e2e_project_frontend)
PORT=$(get_webserver_port)

assert_command curl -vv http://localhost:"$PORT"/sample-asset.txt?canisterId="$ID"
assert_match "This is a sample asset!"
assert_command curl -vv http://localhost:"$PORT"/index.html?canisterId="$ID"
assert_match "IC Hello Starter"
}
2 changes: 1 addition & 1 deletion e2e/tests-dfx/identity.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup

dfx_new
dfx_new_assets
}

teardown() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup

dfx_new
dfx_new_assets
}

teardown() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/metadata.bash
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ teardown() {
}

@test "asset canister provides candid:service metadata" {
dfx_new hello
dfx_new_assets hello
dfx_start

assert_command dfx deploy
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/mode_reinstall.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup

dfx_new hello
dfx_new_assets hello
}

teardown() {
Expand Down
36 changes: 31 additions & 5 deletions e2e/tests-dfx/new.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ setup() {

teardown() {
standard_teardown
dfx_stop
}

@test "dfx new - good names" {
Expand Down Expand Up @@ -51,9 +52,34 @@ teardown() {
assert_eq "motoko"
}

@test "dfx new always emits sample-asset.txt" {
assert_command dfx new e2e_frontend --frontend
assert_file_exists e2e_frontend/src/e2e_frontend_frontend/assets/sample-asset.txt
assert_command dfx new e2e_no_frontend --no-frontend
assert_file_exists e2e_no_frontend/src/e2e_no_frontend_frontend/assets/sample-asset.txt
@test "frontend templates apply successfully" {
for frontend in sveltekit vue react vanilla simple-assets none; do
Copy link
Member

@ericswanson-dfinity ericswanson-dfinity Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be what is breaking

Suggested change
for frontend in sveltekit vue react vanilla simple-assets none; do
for frontend in svelte-kit vue react vanilla simple-assets none; do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I renamed it in the same commit.

assert_command dfx new e2e_${frontend/-/_} --frontend $frontend
done
assert_file_not_exists e2e_none/src/e2e_none_frontend
}

@test "frontend templates pass the frontend tests" {
dfx_start
for frontend in sveltekit vue react vanilla; do
assert_command dfx new e2e_$frontend --frontend $frontend --extras frontend-tests
pushd e2e_$frontend
assert_command dfx deploy
assert_command npm test --workspaces
popd
done
}

@test "backend templates" {
for backend in motoko rust kybra azle; do
assert_command dfx new e2e_$backend --type $backend --no-frontend
done
}

@test "interactive template selection" {
assert_command "${BATS_TEST_DIRNAME}/../assets/expect_scripts/rust_svelte_with_tests_and_ii.exp"
assert_file_exists e2e_project/Cargo.toml
assert_file_exists e2e_project/src/e2e_project_frontend/src/routes/+page.svelte
assert_file_exists e2e_project/src/e2e_project_frontend/src/setupTests.js
assert_command jq .canisters.internet_identity e2e_project/dfx.json
}
2 changes: 1 addition & 1 deletion e2e/tests-dfx/playground.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load ../utils/_
setup() {
standard_setup
setup_playground
dfx_new hello
dfx_new_assets hello
}

teardown() {
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests-dfx/start.bash
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ teardown() {
}

@test "dfx restarts icx-proxy" {
dfx_new hello
dfx_new_assets hello
dfx_start

install_asset greet
Expand All @@ -155,7 +155,7 @@ teardown() {
}

@test "dfx restarts icx-proxy when the replica restarts" {
dfx_new hello
dfx_new_assets hello
dfx_start

install_asset greet
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests-dfx/usage_env.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ teardown() {
#cache
# create a new project to install dfx cache
assert_command_fail ls "$DFX_CACHE_ROOT/.cache/dfinity/versions"
dfx new hello
dfx new hello --no-frontend
assert_command ls "$DFX_CACHE_ROOT/.cache/dfinity/versions"
assert_command_fail ls "$HOME/.cache/dfinity/versions"
rm -rf hello
Expand All @@ -42,7 +42,7 @@ teardown() {
#cache
# create a new project to install dfx cache
assert_command_fail ls "$HOME/.cache/dfinity/versions"
dfx new hello
dfx new hello --no-frontend
assert_command ls "$HOME/.cache/dfinity/versions"
rm -rf hello
)
Expand Down
Loading
Loading