[config]
skip_core_tasks = true
default_to_workspace = false

[env]
CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true
CARGO_MAKE_WORKSPACE_INCLUDE_MEMBERS = ["contracts/room-contract", "contracts/web-container-contract", "ui"]
CONTRACT_TARGET = "wasm32-unknown-unknown"
CONTRACT_NAME = "room_contract"
BUILD_PROFILE = "release"
# Comma-separated list of features to enable
UI_FEATURES = ""

[tasks.clean]
description = "Clean build artifacts"
command = "cargo"
args = ["clean"]

[tasks.build-room-contract]
description = "Build the room contract WASM"
command = "cargo"
args = ["build", "--profile", "${BUILD_PROFILE}", "--target", "${CONTRACT_TARGET}", "-p", "room-contract", "--target-dir", "target", "--offline"]

[tasks.build-web-container]
description = "Build the web container contract WASM"
command = "cargo"
args = ["build", "--profile", "${BUILD_PROFILE}", "--target", "${CONTRACT_TARGET}", "-p", "web-container-contract", "--target-dir", "target"]

[tasks.build-web-container-tool]
description = "Build the web container tool for native platform"
dependencies = ["build-web-container"]
command = "cargo"
args = ["build", "--profile", "${BUILD_PROFILE}", "--package", "web-container-tool", "--target-dir", "target/native", "--target", "x86_64-unknown-linux-gnu"]

[tasks.build-ui]
description = "Build the Dioxus UI"
dependencies = ["build-room-contract", "uncomment-base-path"]
env = { RIVER_BASE_PATH = "/v1/contract/web/29cBpyCAdKmmqPjriYGqxqNbt6TPVxKbkfRWDD9L7NUG" }
command = "dx"
args = ["build", "--${BUILD_PROFILE}", "--features", "${UI_FEATURES}"]
cwd = "./ui"
condition = { channels = ["stable", "beta", "nightly"] }

[tasks.uncomment-base-path]
private = true
script = '''
sed -i 's/#base_path/base_path/' ui/Dioxus.toml
'''

[tasks.comment-base-path]
private = true
script = '''
sed -i 's/^base_path/#base_path/' ui/Dioxus.toml
'''

[tasks.build-ui-with-cleanup]
description = "Build the Dioxus UI and clean up afterwards"
dependencies = ["build-ui", "comment-base-path"]


[tasks.compress-webapp]
description = "Compress the built webapp into tar.xz"
dependencies = ["build-ui-with-cleanup"]
script = '''
mkdir -p target/webapp
cd target/dx/river-ui/${BUILD_PROFILE}/web/public && \
tar -cJf ../../../../../webapp/webapp.tar.xz *
'''

[tasks.sign-webapp]
description = "Sign the compressed webapp"
dependencies = ["compress-webapp", "build-web-container-tool", "build-web-container"]
script = '''
seconds=$(date +%s)
version=$(( seconds / 60 ))
target/native/x86_64-unknown-linux-gnu/${BUILD_PROFILE}/web-container-tool sign \
  --input target/webapp/webapp.tar.xz \
  --output target/webapp/webapp.metadata \
  --parameters target/webapp/webapp.parameters \
  --version $version
'''

[tasks.build-ui-example]
description = "Build the Dioxus UI with example data"
env = { UI_FEATURES = "example-data" }
dependencies = ["build-contract"]
command = "dx"
args = ["build", "--${BUILD_PROFILE}", "--features", "${UI_FEATURES}"]
cwd = "./ui"

[tasks.build-ui-no-sync]
description = "Build the Dioxus UI without Freenet sync"
env = { UI_FEATURES = "no-sync" }
dependencies = ["build-contract"]
command = "dx"
args = ["build", "--${BUILD_PROFILE}", "--features", "${UI_FEATURES}"]
cwd = "./ui"

[tasks.build-ui-example-no-sync]
description = "Build the Dioxus UI with example data and no Freenet sync"
env = { UI_FEATURES = "example-data,no-sync" }
dependencies = ["build-contract"]
command = "dx"
args = ["build", "--${BUILD_PROFILE}", "--features", "${UI_FEATURES}"]
cwd = "./ui"

[tasks.test-web-container]
description = "Run tests for web-container-contract"
command = "cargo"
args = ["test", "--package", "web-container-contract", "--target-dir", "target/native", "--target", "x86_64-unknown-linux-gnu", "--lib", "--bins"]

[tasks.test-web-container-integration]
description = "Run integration tests for web-container-contract"
command = "cargo"
args = ["test", "--package", "web-container-contract", "--target-dir", "target/native", "--target", "x86_64-unknown-linux-gnu", "--test", "integration_tests"]

[tasks.test-room-contract]
description = "Run tests for room-contract"
command = "cargo"
args = ["test", "--package", "room-contract", "--target-dir", "target/native", "--target", "x86_64-unknown-linux-gnu"]

[tasks.test-scaffold]
description = "Run tests for scaffold crate"
command = "cargo"
args = ["test", "--package", "freenet-scaffold", "--target-dir", "target/native", "--target", "x86_64-unknown-linux-gnu"]

[tasks.test-common]
description = "Run tests for common crate"
command = "cargo"
args = ["test", "--package", "river-common", "--target-dir", "target/native", "--target", "x86_64-unknown-linux-gnu"]

[tasks.test]
description = "Run all tests"
dependencies = ["test-web-container", "test-web-container-integration", "test-room-contract", "test-scaffold", "test-common"]

[tasks.update-published-contract]
description = "Build, publish, and update the published contract files"
dependencies = ["build-web-container"]
script = '''
# Get the contract ID without publishing
contract_id=$(fdev get-contract-id \
  --code target/wasm32-unknown-unknown/release/web_container_contract.wasm \
  --parameters target/webapp/webapp.parameters)

# Update published files
mkdir -p published-contract
cp target/wasm32-unknown-unknown/release/web_container_contract.wasm published-contract/
cp target/webapp/webapp.parameters published-contract/
echo "$contract_id" > published-contract/contract-id.txt
echo "Published contract updated with ID: $contract_id"
echo "Please commit the changes to git"
'''

[tasks.publish-river]
description = "Publish River to Freenet using the committed contract version"
dependencies = ["sign-webapp"]
script = '''
if [ ! -f "published-contract/contract-id.txt" ]; then
    echo "No published contract found"
    exit 1
fi
contract_id=$(cat published-contract/contract-id.txt)
echo "Publishing using committed contract: $contract_id"
fdev publish \
  --code published-contract/web_container_contract.wasm \
  --parameters published-contract/webapp.parameters \
  contract \
  --webapp-archive target/webapp/webapp.tar.xz \
  --webapp-metadata target/webapp/webapp.metadata
'''

[tasks.publish-river-debug]
description = "Publish River to Freenet in debug mode"
env = { BUILD_PROFILE = "debug" }
dependencies = ["sign-webapp"]
script = '''
if [ ! -f "published-contract/contract-id.txt" ]; then
    echo "No published contract found"
    exit 1
fi
contract_id=$(cat published-contract/contract-id.txt)
echo "Publishing using committed contract in debug mode: $contract_id"
fdev publish \
  --code published-contract/web_container_contract.wasm \
  --parameters published-contract/webapp.parameters \
  contract \
  --webapp-archive target/webapp/webapp.tar.xz \
  --webapp-metadata target/webapp/webapp.metadata
'''

[tasks.clippy]
description = "Run clippy on all packages"
command = "cargo"
args = ["clippy", "--manifest-path", "./Cargo.toml", "--workspace", "--exclude", "freenet-stdlib", "--all-targets", "--", "-D", "warnings"]

[tasks.build]
description = "Build everything in release mode (optimized)"
dependencies = ["build-ui-with-cleanup", "build-web-container"]

[tasks.dev-example]
description = "Development build with example data"
env = { UI_FEATURES = "example-data", BUILD_PROFILE = "debug" }
dependencies = ["build-contract"]
command = "dx"
args = ["serve", "--features", "${UI_FEATURES}"]
cwd = "./ui"

[tasks.build-example]
description = "Build everything in release mode with example data"
dependencies = ["build-ui-example"]

[tasks.build-debug]
description = "Build everything in debug mode (faster builds)"
env = { BUILD_PROFILE = "debug" }
dependencies = ["build-ui"]

[tasks.dev]
description = "Development build"
env = { UI_FEATURES = "" }
dependencies = ["build-contract"]
command = "dx"
args = ["serve"]
cwd = "./ui"