forklaunch 1.20.1

Launch faster with forklaunch
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::{collections::HashSet, fs::read_to_string, path::Path};

use anyhow::{Result, bail};
use convert_case::{Case, Casing};
use indexmap::IndexMap;
use serde_yml::{from_value, to_value};
use termcolor::StandardStream;
use walkdir::WalkDir;

use super::clean_application::clean_application;
use crate::{
    constants::Runtime,
    core::{
        client_sdk::{change_project_in_client_sdk, regenerate_client_sdk_compliance},
        docker::{
            Command, DependsOn, DockerBuild, DockerCompose, DockerService, HealthTest,
            Healthcheck,
        },
        manifest::{MutableManifestData, ProjectEntry, ProjectType},
        move_template::MoveTemplate,
        package_json::{
            application_package_json::ApplicationPackageJson,
            project_package_json::ProjectPackageJson, replace_project_in_workspace_definition,
        },
        removal_template::RemovalTemplate,
        rendered_template::{RenderedTemplate, RenderedTemplatesCache},
        string::short_circuit_replacement,
        tsconfig::update_project_in_modules_tsconfig,
    },
};

pub(crate) fn change_name_in_files(
    base_path: &Path,
    existing_name: &str,
    name: &str,
    runtime: &Runtime,
    confirm: bool,
    project_entry: &mut ProjectEntry,
    rendered_templates_cache: &mut RenderedTemplatesCache,
    stdout: &mut StandardStream,
) -> Result<Vec<RemovalTemplate>> {
    clean_application(base_path.parent().unwrap(), runtime, confirm, stdout)?;

    let mut removal_templates = Vec::new();

    if let Some(routers) = &mut project_entry.routers {
        routers.iter_mut().for_each(|router| {
            if router == existing_name {
                *router = name.to_string();
            }
        });
    }

    let existing_camel_case_name = existing_name.to_case(Case::Camel);
    let existing_kebab_case_name = existing_name.to_case(Case::Kebab);
    let existing_pascal_case_name = existing_name.to_case(Case::Pascal);

    let camel_case_name = name.to_case(Case::Camel);
    let kebab_case_name = name.to_case(Case::Kebab);
    let pascal_case_name = name.to_case(Case::Pascal);

    let mut seen_existing_names = HashSet::new();
    let mut replacements = vec![(existing_pascal_case_name.clone(), pascal_case_name.clone())];
    seen_existing_names.insert(existing_pascal_case_name.clone());

    for (existing_replacement, new_replacement) in [
        (existing_camel_case_name.clone(), camel_case_name.clone()),
        (existing_kebab_case_name.clone(), kebab_case_name.clone()),
        (existing_name.to_string(), name.to_string()),
    ] {
        if !seen_existing_names.contains(&existing_replacement) {
            replacements.push((existing_replacement.clone(), new_replacement.clone()));
            seen_existing_names.insert(existing_replacement.clone());
        }
    }

    for entry in WalkDir::new(base_path) {
        let entry = entry.unwrap();
        if entry.file_type().is_file() {
            if entry.path().to_string_lossy().contains("node_modules")
                || entry.path().to_string_lossy().contains("dist")
                || entry.path().to_string_lossy().contains("lib")
            {
                continue;
            }
            let relative_path = entry.path().strip_prefix(base_path)?;

            let content =
                if let Some(template) = rendered_templates_cache.get(entry.path()).ok().unwrap() {
                    template.content.clone()
                } else {
                    read_to_string(entry.path())?
                };

            let new_content = short_circuit_replacement(&content, &replacements);
            let new_file_name = short_circuit_replacement(
                &relative_path.to_string_lossy().to_string(),
                &replacements,
            );

            let new_path = base_path.join(new_file_name.clone());
            rendered_templates_cache.insert(
                entry.path().to_string_lossy(),
                RenderedTemplate {
                    path: new_path.clone().into(),
                    content: new_content,
                    context: None,
                },
            );

            if relative_path.to_string_lossy().to_string() != new_file_name.clone() {
                removal_templates.push(RemovalTemplate {
                    path: entry.path().to_path_buf(),
                })
            }
        }
    }

    Ok(removal_templates)
}

pub(crate) fn change_name(
    base_path: &Path,
    name: &str,
    confirm: bool,
    manifest_data: MutableManifestData,
    application_package_json: &mut ApplicationPackageJson,
    project_package_json: &mut ProjectPackageJson,
    docker_compose: Option<&mut DockerCompose>,
    rendered_templates_cache: &mut RenderedTemplatesCache,
    removal_templates: &mut Vec<RemovalTemplate>,
    stdout: &mut StandardStream,
) -> Result<MoveTemplate> {
    let existing_name = base_path.file_name().unwrap().to_string_lossy().to_string();

    // TODO: change the name of the package in client-sdk if service or worker (could just be simple find/replace)
    let (runtime, app_name, projects, project_peer_topology) = match manifest_data {
        MutableManifestData::Service(manifest_data) => {
            manifest_data.service_name = name.to_string();
            (
                manifest_data.runtime.as_mut(),
                manifest_data.app_name.as_mut(),
                &mut manifest_data.projects,
                manifest_data.project_peer_topology.values_mut(),
            )
        }
        MutableManifestData::Library(manifest_data) => {
            manifest_data.library_name = name.to_string();
            (
                manifest_data.runtime.as_mut(),
                manifest_data.app_name.as_mut(),
                &mut manifest_data.projects,
                manifest_data.project_peer_topology.values_mut(),
            )
        }
        MutableManifestData::Router(manifest_data) => {
            manifest_data.router_name = name.to_string();
            (
                manifest_data.runtime.as_mut(),
                manifest_data.app_name.as_mut(),
                &mut manifest_data.projects,
                manifest_data.project_peer_topology.values_mut(),
            )
        }
        MutableManifestData::Worker(manifest_data) => {
            manifest_data.worker_name = name.to_string();
            (
                manifest_data.runtime.as_mut(),
                manifest_data.app_name.as_mut(),
                &mut manifest_data.projects,
                manifest_data.project_peer_topology.values_mut(),
            )
        }
        _ => bail!("Invalid manifest data"),
    };

    let _ = replace_project_in_workspace_definition(
        base_path.parent().unwrap(),
        application_package_json,
        &runtime.parse()?,
        &existing_name,
        name,
        rendered_templates_cache,
    )?;

    let project_entry = projects
        .iter_mut()
        .find(|project| project.name == existing_name)
        .unwrap();

    project_entry.name = name.to_string();

    project_peer_topology.for_each(|group| {
        group.iter_mut().for_each(|project| {
            if project == &existing_name {
                *project = name.to_string();
            }
        });
    });

    project_package_json.name = Some(format!("@{}/{}", app_name, name.to_string()));

    if let Some(docker_compose) = docker_compose {
        let new_services: IndexMap<String, DockerService> = docker_compose
            .services
            .iter_mut()
            .map(|(key, value)| {
                if key.contains(&existing_name) {
                    (
                        key.replace(&existing_name, &name),
                        DockerService {
                            hostname: value
                                .hostname
                                .as_ref()
                                .map(|hostname| hostname.replace(&existing_name, &name)),
                            container_name: value.container_name.as_ref().map(|container_name| {
                                container_name.replace(&existing_name, &name)
                            }),
                            image: value
                                .image
                                .as_ref()
                                .map(|image| image.replace(&existing_name, &name)),
                            restart: value.restart.clone(),
                            build: value.build.as_ref().map(|build| DockerBuild {
                                context: build.context.replace(&existing_name, &name),
                                dockerfile: build.dockerfile.replace(&existing_name, &name),
                            }),
                            environment: value.environment.as_ref().map(|environment| {
                                environment
                                    .iter()
                                    .map(|(key, value)| {
                                        (
                                            key.replace(&existing_name, &name),
                                            value.replace(&existing_name, &name),
                                        )
                                    })
                                    .collect()
                            }),
                            depends_on: value.depends_on.as_ref().map(|depends_on| {
                                depends_on
                                    .iter()
                                    .map(|(key, value)| {
                                        (
                                            key.replace(&existing_name, &name),
                                            DependsOn {
                                                condition: value.condition.clone(),
                                            },
                                        )
                                    })
                                    .collect()
                            }),
                            ports: value.ports.as_ref().map(|ports| {
                                ports
                                    .iter()
                                    .map(|port| port.replace(&existing_name, &name))
                                    .collect()
                            }),
                            volumes: value.volumes.as_ref().map(|volumes| {
                                volumes
                                    .iter()
                                    .map(|volume| volume.replace(&existing_name, &name))
                                    .collect()
                            }),
                            networks: value.networks.as_ref().map(|networks| {
                                networks
                                    .iter()
                                    .map(|network| network.replace(&existing_name, &name))
                                    .collect()
                            }),
                            working_dir: value
                                .working_dir
                                .as_ref()
                                .map(|working_dir| working_dir.replace(&existing_name, &name)),
                            command: value.command.as_ref().map(|command| match command {
                                Command::Simple(command) => {
                                    Command::Simple(command.replace(&existing_name, &name))
                                }
                                Command::Multiple(commands) => Command::Multiple(
                                    commands
                                        .iter()
                                        .map(|command| command.replace(&existing_name, &name))
                                        .collect(),
                                ),
                            }),
                            entrypoint: value.entrypoint.as_ref().map(|entrypoint| match entrypoint {
                                Command::Simple(s) => {
                                    Command::Simple(s.replace(&existing_name, &name))
                                }
                                Command::Multiple(args) => Command::Multiple(
                                    args
                                        .iter()
                                        .map(|arg| arg.replace(&existing_name, &name))
                                        .collect(),
                                ),
                            }),
                            healthcheck: value.healthcheck.as_ref().map(|healthcheck| {
                                Healthcheck {
                                    test: match &healthcheck.test {
                                        HealthTest::String(s) => {
                                            HealthTest::String(
                                                s.replace(&existing_name, &name),
                                            )
                                        }
                                        HealthTest::List(list) => {
                                            HealthTest::List(
                                                list.iter()
                                                    .map(|item| item.replace(&existing_name, &name))
                                                    .collect(),
                                            )
                                        }
                                    },
                                    interval: healthcheck.interval.replace(&existing_name, &name),
                                    timeout: healthcheck.timeout.replace(&existing_name, &name),
                                    retries: healthcheck.retries,
                                    start_period: healthcheck
                                        .start_period
                                        .replace(&existing_name, &name),
                                    additional_properties: healthcheck
                                        .additional_properties
                                        .iter()
                                        .map(|(key, value)| {
                                            (
                                                key.replace(&existing_name, &name),
                                                to_value(
                                                    from_value::<String>(value.clone())
                                                        .unwrap()
                                                        .replace(&existing_name, &name),
                                                )
                                                .unwrap(),
                                            )
                                        })
                                        .collect(),
                                }
                            }),
                            additional_properties: value
                                .additional_properties
                                .iter()
                                .map(|(key, value)| {
                                    (
                                        key.replace(&existing_name, &name),
                                        to_value(
                                            from_value::<String>(value.clone())
                                                .unwrap()
                                                .replace(&existing_name, &name),
                                        )
                                        .unwrap(),
                                    )
                                })
                                .collect(),
                        },
                    )
                } else {
                    (key.to_string(), value.clone())
                }
            })
            .collect();

        docker_compose.services = new_services;
    }

    removal_templates.extend(change_name_in_files(
        base_path,
        &existing_name,
        name,
        &runtime.parse()?,
        confirm,
        project_entry,
        rendered_templates_cache,
        stdout,
    )?);

    let project_changed_type = project_entry.r#type.clone();
    if project_changed_type == ProjectType::Service
        || project_changed_type == ProjectType::Worker
    {
        change_project_in_client_sdk(
            rendered_templates_cache,
            base_path.parent().unwrap(),
            &app_name,
            &existing_name,
            &name,
        )?;

        // project_entry borrow ends above; safe to re-borrow `projects` as &[_]
        regenerate_client_sdk_compliance(
            rendered_templates_cache,
            base_path.parent().unwrap(),
            projects,
        )?;
    }

    let tsconfig_update =
        update_project_in_modules_tsconfig(base_path.parent().unwrap(), &existing_name, &name)?;
    rendered_templates_cache.insert(
        tsconfig_update.path.to_string_lossy().to_string(),
        tsconfig_update,
    );

    Ok(MoveTemplate {
        path: base_path.to_path_buf(),
        target: base_path.parent().unwrap().join(name),
    })
}