|
| 1 | +use clap::Parser; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + cli::{ |
| 5 | + self, |
| 6 | + shared::{ |
| 7 | + compile_package, create_migration_diagnostic, print_features_and_migration_steps, |
| 8 | + }, |
| 9 | + }, |
| 10 | + get_migration_steps_or_return, |
| 11 | + migrations::{DryRun, MigrationStepKind}, |
| 12 | +}; |
| 13 | +use anyhow::{Ok, Result}; |
| 14 | +use forc_util::format_diagnostic; |
| 15 | +use itertools::Itertools; |
| 16 | +use sway_core::Engines; |
| 17 | + |
| 18 | +forc_util::cli_examples! { |
| 19 | + crate::cli::Opt { |
| 20 | + [ Check the project in the current path => "forc migrate check"] |
| 21 | + [ Check the project located in another path => "forc migrate check --path {path}" ] |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Check the project for code that needs to be migrated. |
| 26 | +/// |
| 27 | +/// Dry-runs the migration steps and prints places in code that need to be reviewed or changed. |
| 28 | +#[derive(Debug, Parser)] |
| 29 | +pub(crate) struct Command { |
| 30 | + #[clap(flatten)] |
| 31 | + pub check: cli::shared::Compile, |
| 32 | +} |
| 33 | + |
| 34 | +pub(crate) fn exec(command: Command) -> Result<()> { |
| 35 | + let migration_steps = get_migration_steps_or_return!(); |
| 36 | + let engines = Engines::default(); |
| 37 | + let build_instructions = command.check; |
| 38 | + |
| 39 | + let mut program_info = compile_package(&engines, &build_instructions)?; |
| 40 | + |
| 41 | + // Dry-run all the migration steps. |
| 42 | + let mut check_result = vec![]; |
| 43 | + for (feature, migration_steps) in migration_steps.iter() { |
| 44 | + for migration_step in migration_steps.iter() { |
| 45 | + let migration_point_spans = match migration_step.kind { |
| 46 | + MigrationStepKind::Instruction(instruction) => instruction(&program_info)?, |
| 47 | + MigrationStepKind::CodeModification(modification, _) => { |
| 48 | + modification(&mut program_info.as_mut(), DryRun::Yes)? |
| 49 | + } |
| 50 | + MigrationStepKind::Interaction(instruction, _, _) => instruction(&program_info)?, |
| 51 | + }; |
| 52 | + |
| 53 | + check_result.push((feature, migration_step, migration_point_spans)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // For every migration step, display the found occurrences in code that require migration effort, if any. |
| 58 | + for (feature, migration_step, occurrences_spans) in check_result.iter() { |
| 59 | + if let Some(diagnostic) = |
| 60 | + create_migration_diagnostic(engines.se(), feature, migration_step, occurrences_spans) |
| 61 | + { |
| 62 | + format_diagnostic(&diagnostic); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Display the summary of the migration effort. |
| 67 | + let features_and_migration_steps = check_result |
| 68 | + .iter() |
| 69 | + .chunk_by(|(feature, _, _)| feature) |
| 70 | + .into_iter() |
| 71 | + .map(|(key, chunk)| { |
| 72 | + ( |
| 73 | + **key, |
| 74 | + chunk |
| 75 | + .map(|(_, migration_step, migration_point_spans)| { |
| 76 | + (*migration_step, Some(migration_point_spans.len())) |
| 77 | + }) |
| 78 | + .collect::<Vec<_>>(), |
| 79 | + ) |
| 80 | + }) |
| 81 | + .collect::<Vec<_>>(); |
| 82 | + |
| 83 | + println!("Migration effort:"); |
| 84 | + println!(); |
| 85 | + print_features_and_migration_steps(&features_and_migration_steps); |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
0 commit comments