template.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. use heck::CamelCase;
  2. use heck::SnakeCase;
  3. pub fn virtual_manifest() -> &'static str {
  4. r#"[workspace]
  5. members = [
  6. "programs/*"
  7. ]
  8. "#
  9. }
  10. pub fn cargo_toml(name: &str) -> String {
  11. format!(
  12. r#"[package]
  13. name = "{0}"
  14. version = "0.1.0"
  15. description = "Created with Anchor"
  16. edition = "2018"
  17. [lib]
  18. crate-type = ["cdylib", "lib"]
  19. name = "{1}"
  20. [features]
  21. no-entrypoint = []
  22. no-idl = []
  23. cpi = ["no-entrypoint"]
  24. default = []
  25. [dependencies]
  26. anchor-lang = "0.4.0"
  27. "#,
  28. name,
  29. name.to_snake_case(),
  30. )
  31. }
  32. pub fn deploy_script_host(cluster_url: &str, script_path: &str) -> String {
  33. format!(
  34. r#"
  35. const anchor = require('@project-serum/anchor');
  36. // Deploy script defined by the user.
  37. const userScript = require("{0}");
  38. async function main() {{
  39. const url = "{1}";
  40. const preflightCommitment = 'recent';
  41. const connection = new anchor.web3.Connection(url, preflightCommitment);
  42. const wallet = anchor.Wallet.local();
  43. const provider = new anchor.Provider(connection, wallet, {{
  44. preflightCommitment,
  45. commitment: 'recent',
  46. }});
  47. // Run the user's deploy script.
  48. userScript(provider);
  49. }}
  50. main();
  51. "#,
  52. script_path, cluster_url,
  53. )
  54. }
  55. pub fn deploy_script() -> &'static str {
  56. r#"
  57. // Migrations are an early feature. Currently, they're nothing more than this
  58. // single deploy script that's invoked from the CLI, injecting a provider
  59. // configured from the workspace's Anchor.toml.
  60. const anchor = require("@project-serum/anchor");
  61. module.exports = async function (provider) {
  62. // Configure client to use the provider.
  63. anchor.setProvider(provider);
  64. // Add your deploy script here.
  65. }
  66. "#
  67. }
  68. pub fn ts_deploy_script() -> &'static str {
  69. r#"
  70. // Migrations are an early feature. Currently, they're nothing more than this
  71. // single deploy script that's invoked from the CLI, injecting a provider
  72. // configured from the workspace's Anchor.toml.
  73. const anchor = require("@project-serum/anchor");
  74. module.exports = async function (provider) {
  75. // Configure client to use the provider.
  76. anchor.setProvider(provider);
  77. // Add your deploy script here.
  78. }
  79. "#
  80. }
  81. pub fn xargo_toml() -> &'static str {
  82. r#"[target.bpfel-unknown-unknown.dependencies.std]
  83. features = []"#
  84. }
  85. pub fn lib_rs(name: &str) -> String {
  86. format!(
  87. r#"use anchor_lang::prelude::*;
  88. #[program]
  89. pub mod {} {{
  90. use super::*;
  91. pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {{
  92. Ok(())
  93. }}
  94. }}
  95. #[derive(Accounts)]
  96. pub struct Initialize {{}}"#,
  97. name.to_snake_case(),
  98. )
  99. }
  100. pub fn mocha(name: &str) -> String {
  101. format!(
  102. r#"const anchor = require('@project-serum/anchor');
  103. describe('{}', () => {{
  104. // Configure the client to use the local cluster.
  105. anchor.setProvider(anchor.Provider.env());
  106. it('Is initialized!', async () => {{
  107. // Add your test here.
  108. const program = anchor.workspace.{};
  109. const tx = await program.rpc.initialize();
  110. console.log("Your transaction signature", tx);
  111. }});
  112. }});
  113. "#,
  114. name,
  115. name.to_camel_case(),
  116. )
  117. }
  118. pub fn ts_mocha(name: &str) -> String {
  119. format!(
  120. r#"import * as anchor from '@project-serum/anchor';
  121. describe('{}', () => {{
  122. // Configure the client to use the local cluster.
  123. anchor.setProvider(anchor.Provider.env());
  124. it('Is initialized!', async () => {{
  125. // Add your test here.
  126. const program = anchor.workspace.{};
  127. const tx = await program.rpc.initialize();
  128. console.log("Your transaction signature", tx);
  129. }});
  130. }});
  131. "#,
  132. name,
  133. name.to_camel_case(),
  134. )
  135. }
  136. pub fn ts_config() -> &'static str {
  137. r#"{
  138. "compilerOptions": {
  139. "types": ["mocha", "chai"],
  140. "typeRoots": ["./node_modules/@types"],
  141. "lib": ["es2015"],
  142. "module": "commonjs",
  143. "target": "es6",
  144. "esModuleInterop": true
  145. }
  146. }
  147. "#
  148. }
  149. pub fn git_ignore() -> &'static str {
  150. r#"# ignore Mac OS noise
  151. .DS_Store
  152. # ignore the build directory for Rust/Anchor
  153. target
  154. # Ignore backup files creates by cargo fmt.
  155. **/*.rs.bk
  156. "#
  157. }