Skip to content

Commit

Permalink
feat: add contract init command (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets authored Jun 20, 2023
1 parent 869220e commit 40a30f7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
58 changes: 58 additions & 0 deletions packages/core/sample-projects/contract/Cargo.lock

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

9 changes: 9 additions & 0 deletions packages/core/sample-projects/contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]
members = []

[profile.release]
overflow-checks = true
opt-level = 's'
lto = false
codegen-units = 1
panic = 'abort'
11 changes: 11 additions & 0 deletions packages/core/sample-projects/contract/capsule.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# [rust]
# # path of rust contracts workspace directory,
# # a `Cargo.toml` file is expected under the directory.
# workspace_dir = "."
# toolchain = "nightly-2022-08-01"
# docker_image = "thewawar/ckb-capsule:2022-08-01"

# capsule version
version = "0.9.0 81e9876"
# path of deployment config file
deployment = "deployment.toml"
Empty file.
31 changes: 30 additions & 1 deletion packages/core/src/builtin-tasks/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync } from 'node:child_process'
import read from 'read'
import { existsSync, writeFileSync } from 'node:fs'
import { existsSync, writeFileSync, cpSync, rmSync } from 'node:fs'
import path from 'node:path'
import { KuaiError } from '@ckb-js/kuai-common'
import { ERRORS } from '../errors-list'
Expand Down Expand Up @@ -290,3 +290,32 @@ subtask('contract:new')
const workspace = await run('contract:get-workspace')
execSync(`cd ${workspace} && capsule new-contract ${name} --template ${template}`, { stdio: 'inherit' })
})

interface InitArgs {
name: string
template: string
}

subtask('contract:init')
.addParam('name', 'The name of new contract project')
.addParam(
'template',
'language template [default: rust] [possible values: rust, c, c-sharedlib]',
'rust',
paramTypes.string,
true,
)
.setAction(async ({ name, template }: InitArgs, { run }) => {
const workspace = (await run('contract:get-workspace')) as string
const newProjectPath = path.join(workspace, '..', name)
execSync(`capsule new ${name} --template ${template}`, { stdio: 'inherit' })

// remove .git file
rmSync(path.join(newProjectPath, '.git'), { recursive: true })

// copy project to workspace directory
cpSync(newProjectPath, workspace, { recursive: true })

// remove temp files
rmSync(newProjectPath, { recursive: true })
})

0 comments on commit 40a30f7

Please sign in to comment.