1- import JSZip from 'jszip' ;
1+ import type JSZip from 'jszip' ;
22import type { GenericOptions } from './build-generic' ;
33import type { Contract } from './contract' ;
4- import { printContract , removeCreateLevelAttributes } from './print' ;
5- import { compatibleSorobanVersion , contractsVersionTag } from './utils/version' ;
6- import {
7- addDependenciesWith ,
8- allStellarDependencies ,
9- contractOptionsToContractName ,
10- createRustLibFile ,
11- printContractCargo ,
12- printRustNameTest ,
13- } from './zip-shared' ;
4+ import { createRustZipEnvironment } from './zip-rust' ;
145
15- function getAddressArgs ( c : Contract ) : string [ ] {
16- return c . constructorArgs
17- . filter ( constructorArg => constructorArg . type ?. toLowerCase ( ) === 'address' )
18- . map ( constructorArg => constructorArg . name ) ;
19- }
20-
21- const setupSh = ( c : Contract , opts : GenericOptions , scaffoldContractName : string ) => {
22- const environmentsFileUpdate = ( setUpContract : Contract , setUpScaffoldContractName : string ) => `
23- # Update environments.toml: remove original contracts and insert wizard's contract
24- setup_environment() {
25- local file="environments.toml"
26- local temp
27- temp="$(mktemp)"
28-
29- local in_dev_contracts=0
30- local skip_entry=0
31- local contract_entry_inserted=0
32- insert_contract_entry() {
33- {
34- printf '%s\\n' "[development.contracts.${ setUpScaffoldContractName } _contract]" \\
35- "client = true" "" \\
36- "# If your contract has a \\\`__constructor\\\`, specify your arguments to it here." \\
37- "# These are the same arguments you could pass after the \\\`--\\\` in a call to" \\
38- "# \\\`stellar contract deploy\\\`" \\
39- "# Only available in \\\`development\\\` and \\\`test\\\` environments" \\
40- ${
41- setUpContract . constructorArgs . length
42- ? // Mind the spacing
43- `"# TODO add appropriate values for for the constructors arguments" \\
44- "constructor_args = \\"\\"\\"" \\
45- "${ setUpContract . constructorArgs . map ( constructorArg => `--${ constructorArg . name } \\"ADD_${ constructorArg . name . toLocaleUpperCase ( ) } _${ constructorArg . type ?. toLocaleUpperCase ( ) || 'ARGUMENT' } _HERE\\"` ) . join ( ' ' ) } " \\
46- "\\"\\"\\"" \\
47- ""`
48- : '""'
49- }
50- } >> "$temp"
51- }
52-
53- while IFS= read -r line; do
54- if [[ $contract_entry_inserted -eq 0 && $line == '[staging.network]' ]]; then
55- insert_contract_entry
56- contract_entry_inserted=1
57- fi
58-
59- if [[ $line =~ ^\\[development\\.contracts\\]$ ]]; then
60- printf '%s\\n' "$line" >> "$temp"
61- in_dev_contracts=1
62- skip_entry=0
63- continue
64- fi
65-
66- if [[ $line =~ ^\\[[^]]+\\]$ ]]; then
67- if (( in_dev_contracts )) && [[ $line =~ ^\\[development\\.contracts\\..+\\]$ ]]; then
68- skip_entry=1
69- in_dev_contracts=0
70- continue
71- fi
72- in_dev_contracts=0
73- skip_entry=0
74- printf '%s\\n' "$line" >> "$temp"
75- continue
76- fi
77-
78- if (( skip_entry )); then
79- continue
80- fi
81-
82- if (( in_dev_contracts )); then
83- if [[ $line =~ ^[[:space:]]*# ]]; then
84- printf '%s\\n' "$line" >> "$temp"
85- fi
86- continue
87- fi
88-
89- printf '%s\\n' "$line" >> "$temp"
90- done < "$file"
91-
92- mv "$temp" "$file"
93- }
94- ` ;
95-
96- const updateWorkspaceCargo = `update_cargo() {
97- cp Cargo.toml Cargo.toml.bak
98-
99- cat <<EOF > deps.tmp
100- ${ addDependenciesWith ( `{ git = "https://github.com/OpenZeppelin/stellar-contracts", tag = "${ contractsVersionTag } " }` , allStellarDependencies ) } ${ addDependenciesWith ( `{ version = "${ compatibleSorobanVersion } " }` , [ 'soroban' ] ) }
101- EOF
102-
103- awk '
104- BEGIN {
105- inserted = 0
106- deps = ""
107- while ((getline line < "deps.tmp") > 0) {
108- deps = deps line "\\n"
109- }
110- close("deps.tmp")
111- }
112- /^\\[workspace.dependencies\\]/ {
113- in_deps = 1
114- print
115- if (!inserted) {
116- printf "%s", deps
117- inserted = 1
118- }
119- next
120- }
121- /^\\[/ { in_deps = 0 }
122- in_deps { next }
123- { print }
124- ' Cargo.toml.bak > Cargo.toml
125-
126- rm deps.tmp
127- rm Cargo.toml.bak
128- }` ;
129-
130- return `\
6+ const setupSh = `\
1317#!/usr/bin/env bash
1328#
1339# setup.sh
134- #
135- # This script is meant to set up a Scaffold project and insert the Wizard's contracts in the project
10+ #
11+ # This script is meant to set up a Scaffold project with the Wizard's contracts
13612
13713check_is_installed() {
13814 if ! which "$1" &> /dev/null; then
@@ -143,18 +19,7 @@ check_is_installed() {
14319}
14420
14521scaffold() {
146- tmp_folder="tmp"
147- stellar scaffold init "$tmp_folder"
148-
149- rm -rf "$tmp_folder/contracts"
150-
151- local current_directory
152- current_directory="$(cd "$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
153-
154- shopt -s dotglob
155-
156- cp -a "$current_directory/$tmp_folder"/. "$current_directory"/
157- rm -rf "$current_directory/$tmp_folder"
22+ stellar-scaffold upgrade
15823}
15924
16025init_git(){
@@ -163,10 +28,6 @@ init_git(){
16328 git commit -m "openzeppelin: add wizard output" --quiet
16429}
16530
166- ${ environmentsFileUpdate ( c , scaffoldContractName ) }
167-
168- ${ updateWorkspaceCargo }
169-
17031build_contracts() {
17132 cargo build
17233}
19657 echo "🏗️ Building Scaffold project"
19758
19859 scaffold
199-
200- setup_environment
201-
202- update_cargo
20360
20461 build_contracts
20562
@@ -212,15 +69,11 @@ else
21269 echo "✅ Scaffold project already initialized."
21370fi
21471` ;
215- } ;
216-
217- const readme = ( c : Contract ) => {
218- const hasTodosToResolve = ( c : Contract ) => getAddressArgs ( c ) . length > 0 ;
21972
220- return `\
73+ const wizardReadme = `\
22174# Sample Scaffold Project
22275
223- This project demonstrates a basic Scaffold use case. It comes with a contract generated by [OpenZeppelin Wizard](https://wizard.openzeppelin.com/), a test for that contract, and a script that initiate a Stellar Scaffold project with this contract. [Scaffold Stellar](https://github.com/AhaLabs/scaffold-stellar?tab=readme-ov-file#scaffold-stellar) is a convention-over-configuration toolkit for blockchain and distributed application development on the Stellar network. It provides a seamless development experience through CLI tools, smart contract management, and deployment utilities.
76+ This project demonstrates a basic Scaffold use case. It comes with a contract generated by [OpenZeppelin Wizard](https://wizard.openzeppelin.com/), a test for that contract, and a script that initiates a Stellar Scaffold project with this contract. [Scaffold Stellar](https://github.com/AhaLabs/scaffold-stellar?tab=readme-ov-file#scaffold-stellar) is a convention-over-configuration toolkit for blockchain and distributed application development on the Stellar network. It provides a seamless development experience through CLI tools, smart contract management, and deployment utilities.
22477
22578## Installing dependencies
22679
@@ -235,7 +88,6 @@ This project demonstrates a basic Scaffold use case. It comes with a contract ge
23588\`\`\`
23689bash setup.sh
23790\`\`\`
238- ${ hasTodosToResolve ( c ) ? '\n## Resolve any TODOs \n\nSearch for any TODO comments in the project and resolve them (search for TODO with your code editor).\n' : '' }
23991
24092## Testing the contract
24193
@@ -255,19 +107,13 @@ stellar scaffold watch --build-clients
255107npm run dev
256108\`\`\`
257109` ;
258- } ;
259-
260- export async function zipScaffold ( c : Contract , opts : GenericOptions ) {
261- const zip = new JSZip ( ) ;
262110
263- const scaffoldContractName = contractOptionsToContractName ( opts ?. kind || 'contract' ) ;
264-
265- zip . file ( `contracts/${ scaffoldContractName } /src/contract.rs` , removeCreateLevelAttributes ( printContract ( c ) ) ) ;
266- zip . file ( `contracts/${ scaffoldContractName } /src/test.rs` , printRustNameTest ( c ) ) ;
267- zip . file ( `contracts/${ scaffoldContractName } /src/lib.rs` , createRustLibFile ) ;
268- zip . file ( `contracts/${ scaffoldContractName } /Cargo.toml` , printContractCargo ( scaffoldContractName ) ) ;
269- zip . file ( 'setup.sh' , setupSh ( c , opts , scaffoldContractName ) ) ;
270- zip . file ( 'README-WIZARD.md' , readme ( c ) ) ;
111+ const addScaffoldProjectFiles = ( zip : JSZip ) => {
112+ zip . file ( 'README-WIZARD.md' , wizardReadme ) ;
113+ zip . file ( 'setup.sh' , setupSh ) ;
271114
272115 return zip ;
273- }
116+ } ;
117+
118+ export const zipScaffoldProject = async ( c : Contract , opts : GenericOptions ) =>
119+ addScaffoldProjectFiles ( createRustZipEnvironment ( c , opts ) ) ;
0 commit comments