fn perform_cleanup()

in src/lib.rs [133:204]


  fn perform_cleanup(&mut self) {
    // Setup the parser for the specific language
    let piranha_args = &self.piranha_arguments;

    let mut parser = piranha_args.language().parser();

    let mut paths_to_codebase = self.piranha_arguments.paths_to_codebase().clone();

    let temp_dir = if !self.piranha_arguments.code_snippet().is_empty() {
      let td = self.write_code_snippet_to_temp();
      paths_to_codebase = vec![td.path().to_str().unwrap_or_default().to_string()];
      Some(td)
    } else {
      None
    };

    let mut current_global_substitutions = piranha_args.input_substitutions();
    // Keep looping until new `global` rules are added.
    loop {
      let current_rules = self.rule_store.global_rules().clone();

      debug!("\n # Global rules {}", current_rules.len());
      // Iterate over each file containing the usage of the feature flag API

      for (path, content) in self.rule_store.get_relevant_files(
        &paths_to_codebase,
        piranha_args.include(),
        piranha_args.exclude(),
      ) {
        // Get the `SourceCodeUnit` for the file `path` from the cache `relevant_files`.
        // In case of miss, lazily insert a new `SourceCodeUnit`.
        let source_code_unit = self
          .relevant_files
          .entry(path.to_path_buf())
          .or_insert_with(|| {
            SourceCodeUnit::new(
              &mut parser,
              content,
              &current_global_substitutions,
              path.as_path(),
              piranha_args,
            )
          });

        // Apply the rules in this `SourceCodeUnit`
        source_code_unit.apply_rules(&mut self.rule_store, &current_rules, &mut parser, None);

        // Add the substitutions for the global tags to the `current_global_substitutions`
        current_global_substitutions.extend(source_code_unit.global_substitutions());

        // Break when a new `global` rule is added
        if self.rule_store.global_rules().len() > current_rules.len() {
          debug!("Found a new global rule. Will start scanning all the files again.");
          break;
        }
      }
      // If no new `global_rules` were added, break.
      if self.rule_store.global_rules().len() == current_rules.len() {
        break;
      }
    }
    // Delete the temp dir inside which the input code snippet was copied
    if let Some(t) = temp_dir {
      _ = t.close();
    } else {
      let source_code_units = self.get_updated_files();

      for scu in source_code_units.iter() {
        scu.persist();
      }
    }
  }