Skip to content

Commit

Permalink
feat: adding ability to execute with extra args (backwards compatible) (
Browse files Browse the repository at this point in the history
#55)

* feat: adding ability to execute with extra args (backwards compatible)

* cI: reverting algokit version
  • Loading branch information
aorumbayev authored Aug 23, 2024
1 parent 0232bb6 commit 50ce441
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Run the following commands within the project folder:
#### Terminal
Directly manage and interact with your project using AlgoKit commands:

1. **Build Contracts**: `algokit project run build` compiles all smart contracts.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network.
1. **Build Contracts**: `algokit project run build` compiles all smart contracts. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project run build -- hello_world` will only build the `hello_world` contract.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project deploy localnet -- hello_world` will only deploy the `hello_world` contract.

#### VS Code
For a seamless experience with breakpoint debugging and other features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = "Algorand smart contracts"
authors = ["None <None>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@
root_path = Path(__file__).parent


def main(action: str) -> None:
def main(action: str, contract_name: str | None = None) -> None:
artifact_path = root_path / "artifacts"

# Filter contracts if a specific contract name is provided
filtered_contracts = [
c for c in contracts if contract_name is None or c.name == contract_name
]

match action:
case "build":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
build(artifact_path / contract.name, contract.path)
case "deploy":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Deploying app {contract.name}")
output_dir = artifact_path / contract.name
app_spec_file_name = next(
Expand All @@ -49,7 +55,7 @@ def main(action: str) -> None:
if contract.deploy:
deploy(app_spec_path, contract.deploy)
case "all":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
app_spec_path = build(artifact_path / contract.name, contract.path)
if contract.deploy:
Expand All @@ -58,7 +64,9 @@ def main(action: str) -> None:


if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) > 2:
main(sys.argv[1], sys.argv[2])
elif len(sys.argv) > 1:
main(sys.argv[1])
else:
main("all")
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Run the following commands within the project folder:
#### Terminal
Directly manage and interact with your project using AlgoKit commands:

1. **Build Contracts**: `algokit project run build` compiles all smart contracts.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network.
1. **Build Contracts**: `algokit project run build` compiles all smart contracts. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project run build -- hello_world` will only build the `hello_world` contract.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project deploy localnet -- hello_world` will only deploy the `hello_world` contract.

#### VS Code
For a seamless experience with breakpoint debugging and other features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = "Algorand smart contracts"
authors = ["None <None>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@
root_path = Path(__file__).parent


def main(action: str) -> None:
def main(action: str, contract_name: str | None = None) -> None:
artifact_path = root_path / "artifacts"

# Filter contracts if a specific contract name is provided
filtered_contracts = [
c for c in contracts if contract_name is None or c.name == contract_name
]

match action:
case "build":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
build(artifact_path / contract.name, contract.path)


if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) > 2:
main(sys.argv[1], sys.argv[2])
elif len(sys.argv) > 1:
main(sys.argv[1])
else:
main("build")
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,41 @@ async function importDeployerIfExists(dir: string) {
const deployerPath = path.resolve(dir, 'deploy-config')
if (fs.existsSync(deployerPath + '.ts') || fs.existsSync(deployerPath + '.js')) {
const deployer = await import(deployerPath)
return deployer.deploy
return { ...deployer, name: path.basename(dir) }
}
return null
}

// get a list of all deployers from the subdirectories
async function getDeployers() {
const directories = fs.readdirSync(baseDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => path.resolve(baseDir, dirent.name))
const directories = fs
.readdirSync(baseDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => path.resolve(baseDir, dirent.name))

return Promise.all(directories.map(importDeployerIfExists))
const deployers = await Promise.all(directories.map(importDeployerIfExists))
return deployers.filter((deployer) => deployer !== null) // Filter out null values
}

// execute all the deployers
(async () => {
const contractDeployers = (await getDeployers()).filter(Boolean)
const contractName = process.argv.length > 2 ? process.argv[2] : undefined
const contractDeployers = await getDeployers()

const filteredDeployers = contractName
? contractDeployers.filter(deployer => deployer.name === contractName)
: contractDeployers

if (contractName && filteredDeployers.length === 0) {
console.warn(`No deployer found for contract name: ${contractName}`)
return
}

for (const deployer of contractDeployers) {
for (const deployer of filteredDeployers) {
try {
await deployer()
await deployer.deploy()
} catch (e) {
console.error(e)
console.error(`Error deploying ${deployer.name}:`, e)
}
}
})()
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Run the following commands within the project folder:
#### Terminal
Directly manage and interact with your project using AlgoKit commands:

1. **Build Contracts**: `algokit project run build` compiles all smart contracts.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network.
1. **Build Contracts**: `algokit project run build` compiles all smart contracts. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project run build -- hello_world` will only build the `hello_world` contract.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project deploy localnet -- hello_world` will only deploy the `hello_world` contract.

#### VS Code
For a seamless experience with breakpoint debugging and other features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = "Algorand smart contracts"
authors = ["None <None>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@
root_path = Path(__file__).parent


def main(action: str) -> None:
def main(action: str, contract_name: str | None = None) -> None:
artifact_path = root_path / "artifacts"

# Filter contracts if a specific contract name is provided
filtered_contracts = [
c for c in contracts if contract_name is None or c.name == contract_name
]

match action:
case "build":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
build(artifact_path / contract.name, contract.path)
case "deploy":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Deploying app {contract.name}")
output_dir = artifact_path / contract.name
app_spec_file_name = next(
Expand All @@ -49,7 +55,7 @@ def main(action: str) -> None:
if contract.deploy:
deploy(app_spec_path, contract.deploy)
case "all":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
app_spec_path = build(artifact_path / contract.name, contract.path)
if contract.deploy:
Expand All @@ -58,7 +64,9 @@ def main(action: str) -> None:


if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) > 2:
main(sys.argv[1], sys.argv[2])
elif len(sys.argv) > 1:
main(sys.argv[1])
else:
main("all")
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Run the following commands within the project folder:
#### Terminal
Directly manage and interact with your project using AlgoKit commands:

1. **Build Contracts**: `algokit project run build` compiles all smart contracts.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network.
1. **Build Contracts**: `algokit project run build` compiles all smart contracts. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project run build -- hello_world` will only build the `hello_world` contract.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project deploy localnet -- hello_world` will only deploy the `hello_world` contract.

#### VS Code
For a seamless experience with breakpoint debugging and other features:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = "Algorand smart contracts"
authors = ["None <None>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@
root_path = Path(__file__).parent


def main(action: str) -> None:
def main(action: str, contract_name: str | None = None) -> None:
artifact_path = root_path / "artifacts"

# Filter contracts if a specific contract name is provided
filtered_contracts = [
c for c in contracts if contract_name is None or c.name == contract_name
]

match action:
case "build":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
build(artifact_path / contract.name, contract.path)


if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) > 2:
main(sys.argv[1], sys.argv[2])
elif len(sys.argv) > 1:
main(sys.argv[1])
else:
main("build")
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,41 @@ async function importDeployerIfExists(dir: string) {
const deployerPath = path.resolve(dir, 'deploy-config')
if (fs.existsSync(deployerPath + '.ts') || fs.existsSync(deployerPath + '.js')) {
const deployer = await import(deployerPath)
return deployer.deploy
return { ...deployer, name: path.basename(dir) }
}
return null
}

// get a list of all deployers from the subdirectories
async function getDeployers() {
const directories = fs.readdirSync(baseDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => path.resolve(baseDir, dirent.name))
const directories = fs
.readdirSync(baseDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => path.resolve(baseDir, dirent.name))

return Promise.all(directories.map(importDeployerIfExists))
const deployers = await Promise.all(directories.map(importDeployerIfExists))
return deployers.filter((deployer) => deployer !== null) // Filter out null values
}

// execute all the deployers
(async () => {
const contractDeployers = (await getDeployers()).filter(Boolean)
const contractName = process.argv.length > 2 ? process.argv[2] : undefined
const contractDeployers = await getDeployers()

const filteredDeployers = contractName
? contractDeployers.filter(deployer => deployer.name === contractName)
: contractDeployers

if (contractName && filteredDeployers.length === 0) {
console.warn(`No deployer found for contract name: ${contractName}`)
return
}

for (const deployer of contractDeployers) {
for (const deployer of filteredDeployers) {
try {
await deployer()
await deployer.deploy()
} catch (e) {
console.error(e)
console.error(`Error deploying ${deployer.name}:`, e)
}
}
})()
6 changes: 4 additions & 2 deletions examples/production_python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Run the following commands within the project folder:
#### Terminal
Directly manage and interact with your project using AlgoKit commands:

1. **Build Contracts**: `algokit project run build` compiles all smart contracts.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network.
1. **Build Contracts**: `algokit project run build` compiles all smart contracts. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project run build -- hello_world` will only build the `hello_world` contract.
2. **Deploy**: Use `algokit project deploy localnet` to deploy contracts to the local network. You can also specify a specific contract by passing the name of the contract folder as an extra argument.
For example: `algokit project deploy localnet -- hello_world` will only deploy the `hello_world` contract.

#### VS Code
For a seamless experience with breakpoint debugging and other features:
Expand Down
1 change: 1 addition & 0 deletions examples/production_python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = "Algorand smart contracts"
authors = ["None <None>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
Expand Down
18 changes: 13 additions & 5 deletions examples/production_python/smart_contracts/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@
root_path = Path(__file__).parent


def main(action: str) -> None:
def main(action: str, contract_name: str | None = None) -> None:
artifact_path = root_path / "artifacts"

# Filter contracts if a specific contract name is provided
filtered_contracts = [
c for c in contracts if contract_name is None or c.name == contract_name
]

match action:
case "build":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
build(artifact_path / contract.name, contract.path)
case "deploy":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Deploying app {contract.name}")
output_dir = artifact_path / contract.name
app_spec_file_name = next(
Expand All @@ -49,7 +55,7 @@ def main(action: str) -> None:
if contract.deploy:
deploy(app_spec_path, contract.deploy)
case "all":
for contract in contracts:
for contract in filtered_contracts:
logger.info(f"Building app at {contract.path}")
app_spec_path = build(artifact_path / contract.name, contract.path)
if contract.deploy:
Expand All @@ -58,7 +64,9 @@ def main(action: str) -> None:


if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) > 2:
main(sys.argv[1], sys.argv[2])
elif len(sys.argv) > 1:
main(sys.argv[1])
else:
main("all")
Loading

0 comments on commit 50ce441

Please sign in to comment.