Skip to content

Instantly share code, notes, and snippets.

@twfahey1
Created September 26, 2019 14:49
Show Gist options
  • Save twfahey1/fce8c918713cebcb98ccd057e26276cb to your computer and use it in GitHub Desktop.
Save twfahey1/fce8c918713cebcb98ccd057e26276cb to your computer and use it in GitHub Desktop.
Convert Drupal pre-composer project to Composer based
#!/bin/bash
### Convert a pre-Composer Drupal project to use Composer for contrib modules. ###
# This script can automate the process of taking a codebase previously managed via
# drush or otherwise non-composer, and add all the modules to the composer.json
# in the docroot, with the current version number. The script will get
# all modules and version numbers, and pass to composer for an update.
# This statement will be appended on as each module is found. We can add as
# many modules as are found to the statement, so that composer can fire
# one time and add all the modules found.
composer_update_statement="composer require "
# Any folders that contain contributed moduels can be added to this array.
# So, if you need more than the web/modules/contrib, just add the string
# to this array.
folders_to_scan=("web/modules/contrib")
for module_folder in ${folders_to_scan[@]}; do
# For each folder, we want to find all modules, and inspect the info.yml file to
# determine the version number. This is then ultimately added to the composer.json file
# in the docroot.
for module_name in $(ls ${module_folder}); do
module_path=${module_folder}/${module_name}
path_to_info_file_for_module=${module_path}/${module_name}.info.yml
version_data=$(cat ${path_to_info_file_for_module} | grep version:)
## The version data string contains extra characters, we just want to to get the version number for Composer.
# E.g. version: '8.x-1.23'
search="8.x"
initial_version_string=${version_data##*$search}
# remove the beginning text.
partial_version_string=${initial_version_string%% *}
# Remove the ending ' symbol
partial_version_string_without_quotation=${partial_version_string//\'/}
# Remove the first found dash and get the version number suitable for composer.
version_number=${partial_version_string_without_quotation/-/}
if [ -z "$version_number" ]
then
echo "Skipping $module_name, couldn't determine a version number from info.yml..."
else
# Append this module to the existing composer update statement.
composer_update_statement=$composer_update_statement" drupal/$module_name:$version_number"
fi
done
done
# At this point, we've built up a composer update statement with all the modules that were found.
# By executing the statement, composer will add all the current modules and versions to composer.
${composer_update_statement}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment