Commit f43e8fe1 authored by Ivica Puljic's avatar Ivica Puljic Committed by Ivica Puljic
Browse files

Issue #3257070 by pivica: Generator moving (refactoring) partials detection needs improvements

parent 52b654b5
Loading
Loading
Loading
Loading
+46 −7
Original line number Diff line number Diff line
@@ -1022,7 +1022,7 @@ function _bs_base_flatten_sass_file_imports($target_sass_file, $target_machine_n
    $check_parent = TRUE;
  }
  else {
    // For the depth greater then 0 we will check parent theme only if it has
    // For the depth greater than 0 we will check parent theme only if it has
    // direct import.
    $parent_imports = [];
    foreach (array_keys($parent_themes_sass_files) as $parent_theme) {
@@ -1064,8 +1064,8 @@ function _bs_base_flatten_sass_file_imports($target_sass_file, $target_machine_n
      }
    }
    // If there are new additional lines from parent themes we need to check
    // them also. This will produce duplicate lines so we need to take that into
    // consideration.
    // them also. This will produce duplicate lines, so we need to take that
    // into consideration.
    if (!empty($additional_lines)) {
      // If first line is init the lets add parent imports after it.
      if (strpos($lines[0], '@import "init"') === 0) {
@@ -1120,7 +1120,7 @@ function _bs_base_flatten_sass_file_imports($target_sass_file, $target_machine_n
          }
        }
        else {
          // If file does not exist check that it is not partial maybe.
          // If file does not exist check that it is not maybe a partial.
          $sass_partial_file_path_part = explode('/', $sass_file_path_part);
          $sass_partial_file_path_part[count($sass_partial_file_path_part) - 1] = '_' . $sass_partial_file_path_part[count($sass_partial_file_path_part) - 1];
          $sass_partial_file_path_part = implode('/', $sass_partial_file_path_part);
@@ -1139,7 +1139,7 @@ function _bs_base_flatten_sass_file_imports($target_sass_file, $target_machine_n
          if ($sass_file_context === 'init' && empty($sass_file_path)) {
            continue;
          }
          // Expand the import for all partials from this themes.
          // Expand the import for all partials from these themes.
          elseif ($sass_file_context === 'partials') {
            // Get the sub-folder sass part of the target file.
            $subfolder = NULL;
@@ -1163,7 +1163,7 @@ function _bs_base_flatten_sass_file_imports($target_sass_file, $target_machine_n
      }
    }
    else {
      // If we land here and we are target theme ($depth == 0) then something
      // If we land here, and we are target theme ($depth == 0) then something
      // is probably wrong lets report it.
      // If we are in some parent theme ($depth > 0) then we ignore this line.
      if ($depth === 0) {
@@ -1646,7 +1646,7 @@ function _bs_base_update_sass_files($theme_machine_name) {
    $target_theme_sass_files = _bs_base_get_sass_files($target_path);
  }

  // Iterate over all *.scss files and for all non partial files flatten SASS
  // Iterate over all *.scss files and for all non-partial files flatten SASS
  // imports.
  foreach ($target_theme_sass_files as $sass_file) {
    // Do not process partials.
@@ -1660,6 +1660,13 @@ function _bs_base_update_sass_files($theme_machine_name) {
    // in existing imports. If yes remove them. This is a case when some
    // partials from parent themes are moved to new SASS file due to
    // refactoring.
    // Before removing duplicated imports check the content of target partials -
    // if the partials holds only variables, mixins or functions (no CSS rules)
    // then having multiple imports is fine and we should not remove it.
    // @TODO - this is a very complex logic which does not need to be valid
    // always. If this part of code is making more problems in future then
    // consider to remove it and use update functions to handle refactor cases
    // on per case base?
    foreach ($new_sass_files as $new_file) {
      if (empty($new_file)) {
        continue;
@@ -1679,6 +1686,38 @@ function _bs_base_update_sass_files($theme_machine_name) {

      $remove_lines = array_intersect($flattened_sass, $new_sass_file_flattened);
      if (!empty($remove_lines)) {
        foreach ($remove_lines as $line_no => $remove_line) {
          if (preg_match("#^(//|/\*)?\s*(@import\s+['\"])([a-zA-Z0-9_@]+)(.*?)['\"]#", $remove_line, $matches)) {
            $remove_line_theme = $matches[3];
            $remove_line_filepath_part = $matches[4];

            if (isset($all_themes[$remove_line_theme]) && !empty($remove_line_filepath_part)) {
              $parts = explode('/', $remove_line_filepath_part);
              $last_element = array_key_last($parts);
              if (strpos($parts[$last_element], '_') !== 0) {
                $parts[$last_element] = '_' . $parts[$last_element];
              }

              // Make partial file name.
              $partial_filename = DRUPAL_ROOT . '/' . $all_themes[$remove_line_theme]->subpath . join('/', $parts) . '.scss';

              if (isset($parent_themes_sass_files[$remove_line_theme][$partial_filename])) {
                $file_contents = file_get_contents($partial_filename);
                if ($file_contents === FALSE) {
                  drush_log("Can not open file $partial_filename for a check.", LogLevel::WARNING);
                  return FALSE;
                }

                // If there are no CSS rules in this partial we will consider it
                // as a variable/mixin/function partial that CAN be included in
                // multiple files and therefor we will not remove it.
                if (preg_match_all("#^[\.\#\[a-zA-Z0-9\*].+?\s+\{#m", $file_contents, $matches) === 0) {
                  unset($remove_lines[$line_no]);
                }
              }
            }
          }
        }
        $flattened_sass = array_diff($flattened_sass, $remove_lines);
      }
    }