-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathinit.rs
More file actions
270 lines (239 loc) · 10.4 KB
/
Copy pathinit.rs
File metadata and controls
270 lines (239 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::{
cli_args::{
clap_definitions::{InitArgs, ProjectPaths},
init_config::{self, Ecosystem},
interactive_init::prompt_missing_init_args,
},
commands,
config_parsing::{
entity_parsing::Schema,
graph_migration::generate_config_from_subgraph_id,
human_config::HumanConfig,
system_config::{get_envio_version, SystemConfig},
},
hbs_templating::{
contract_import_templates, hbs_dir_generator::HandleBarsDirGenerator,
init_templates::InitTemplates,
},
project_paths::ParsedProjectPaths,
template_dirs::TemplateDirs,
utils::file_system,
};
use anyhow::{Context, Result};
use std::path::PathBuf;
pub async fn run_init_args(init_args: InitArgs, project_paths: &ProjectPaths) -> Result<()> {
let template_dirs = TemplateDirs::new();
//get_init_args_interactive opens an interactive cli for required args to be selected
//if they haven't already been
let init_config = prompt_missing_init_args(init_args, project_paths)
.await
.context("Failed during interactive input")?;
let parsed_project_paths = ParsedProjectPaths::try_from(init_config.clone())
.context("Failed parsing paths from interactive input")?;
// The cli errors if the folder exists, the user must provide a new folder to proceed which we create below
std::fs::create_dir_all(&parsed_project_paths.project_root)?;
match &init_config.ecosystem {
Ecosystem::Fuel {
init_flow: init_config::fuel::InitFlow::Template(template),
} => {
template_dirs
.get_and_extract_template(
template,
&init_config.language,
&parsed_project_paths.project_root,
)
.context(format!(
"Failed initializing Fuel template {} with language {} at path {:?}",
&template, &init_config.language, &parsed_project_paths.project_root,
))?;
}
Ecosystem::Evm {
init_flow: init_config::evm::InitFlow::Template(template),
} => {
template_dirs
.get_and_extract_template(
template,
&init_config.language,
&parsed_project_paths.project_root,
)
.context(format!(
"Failed initializing Evm template {} with language {} at path {:?}",
&template, &init_config.language, &parsed_project_paths.project_root,
))?;
}
Ecosystem::Evm {
init_flow: init_config::evm::InitFlow::SubgraphID(cid),
} => {
template_dirs
.get_and_extract_blank_template(
&init_config.language,
&parsed_project_paths.project_root,
)
.context(format!(
"Failed initializing blank template for Subgraph Migration with language {} \
at path {:?}",
&init_config.language, &parsed_project_paths.project_root,
))?;
let evm_config = generate_config_from_subgraph_id(
&parsed_project_paths.project_root,
cid,
&init_config.language,
)
.await
.context("Failed generating config from subgraph")?;
let system_config = SystemConfig::from_human_config(
HumanConfig::Evm(evm_config),
Schema::empty(),
&parsed_project_paths,
)
.context("Failed parsing config")?;
let auto_schema_handler_template =
contract_import_templates::AutoSchemaHandlerTemplate::try_from(
system_config,
&init_config.language,
init_config.api_token.clone(),
)
.context("Failed converting config to auto auto_schema_handler_template")?;
auto_schema_handler_template
.generate_subgraph_migration_templates(
&init_config.language,
&parsed_project_paths.project_root,
)
.context("Failed generating subgraph migration templates for event handlers.")?;
}
Ecosystem::Fuel {
init_flow: init_config::fuel::InitFlow::ContractImport(contract_import_selection),
} => {
let fuel_config = contract_import_selection.to_human_config(&init_config);
// TODO: Allow parsed paths to not depend on a written config.yaml file in file system
file_system::write_file_string_to_system(
fuel_config.to_string(),
parsed_project_paths.project_root.join("config.yaml"),
)
.await
.context("Failed writing imported config.yaml")?;
for selected_contract in &contract_import_selection.contracts {
file_system::write_file_string_to_system(
selected_contract.abi.raw.clone(),
parsed_project_paths
.project_root
.join(selected_contract.get_vendored_abi_file_path()),
)
.await
.context(format!(
"Failed vendoring ABI file for {} contract",
selected_contract.name
))?;
}
//Use an empty schema config to generate auto_schema_handler_template
//After it's been generated, the schema exists and codegen can parse it/use it
let system_config = SystemConfig::from_human_config(
HumanConfig::Fuel(fuel_config),
Schema::empty(),
&parsed_project_paths,
)
.context("Failed parsing config")?;
let auto_schema_handler_template =
contract_import_templates::AutoSchemaHandlerTemplate::try_from(
system_config,
&init_config.language,
init_config.api_token.clone(),
)
.context("Failed converting config to auto auto_schema_handler_template")?;
template_dirs
.get_and_extract_blank_template(
&init_config.language,
&parsed_project_paths.project_root,
)
.context(format!(
"Failed initializing blank template for Contract Import with language {} at \
path {:?}",
&init_config.language, &parsed_project_paths.project_root,
))?;
auto_schema_handler_template
.generate_contract_import_templates(
&init_config.language,
&parsed_project_paths.project_root,
)
.context(
"Failed generating contract import templates for schema and event handlers.",
)?;
}
Ecosystem::Evm {
init_flow: init_config::evm::InitFlow::ContractImport(auto_config_selection),
} => {
let evm_config = auto_config_selection
.to_human_config(&init_config)
.context("Failed to converting auto config selection into config.yaml")?;
// TODO: Allow parsed paths to not depend on a written config.yaml file in file system
file_system::write_file_string_to_system(
evm_config.to_string(),
parsed_project_paths.project_root.join("config.yaml"),
)
.await
.context("failed writing imported config.yaml")?;
//Use an empty schema config to generate auto_schema_handler_template
//After it's been generated, the schema exists and codegen can parse it/use it
let system_config = SystemConfig::from_human_config(
HumanConfig::Evm(evm_config),
Schema::empty(),
&parsed_project_paths,
)
.context("Failed parsing config")?;
let auto_schema_handler_template =
contract_import_templates::AutoSchemaHandlerTemplate::try_from(
system_config,
&init_config.language,
init_config.api_token.clone(),
)
.context("Failed converting config to auto auto_schema_handler_template")?;
template_dirs
.get_and_extract_blank_template(
&init_config.language,
&parsed_project_paths.project_root,
)
.context(format!(
"Failed initializing blank template for Contract Import with language {} at \
path {:?}",
&init_config.language, &parsed_project_paths.project_root,
))?;
auto_schema_handler_template
.generate_contract_import_templates(
&init_config.language,
&parsed_project_paths.project_root,
)
.context(
"Failed generating contract import templates for schema and event handlers.",
)?;
}
}
let envio_version = get_envio_version()?;
let hbs_template = InitTemplates::new(
init_config.name.clone(),
&init_config.language,
&parsed_project_paths,
envio_version.clone(),
init_config.api_token,
)
.context("Failed creating init templates")?;
let init_shared_template_dir = template_dirs.get_init_template_dynamic_shared()?;
let hbs_generator = HandleBarsDirGenerator::new(
&init_shared_template_dir,
&hbs_template,
&parsed_project_paths.project_root,
);
hbs_generator.generate_hbs_templates()?;
println!("Project template ready");
println!("Running codegen");
let config = SystemConfig::parse_from_project_files(&parsed_project_paths)
.context("Failed parsing config")?;
commands::codegen::run_codegen(&config).await?;
// If the project directory is not the current directory, print a message for user to cd into it
if parsed_project_paths.project_root != PathBuf::from(".") {
println!(
"Please run `cd {}` to run the rest of the envio commands",
parsed_project_paths.project_root.to_str().unwrap_or("")
);
}
Ok(())
}