fourkitchens / emulsify

DEPRECATED (see README for new version)
GNU General Public License v2.0
163 stars 70 forks source link

Twig copy script incorrect #254

Open andydempster opened 6 years ago

andydempster commented 6 years ago

https://github.com/fourkitchens/emulsify/blob/develop/scripts/twig_functions.sh does not really work due to the way in which the file is found. Assuming the parent directory tree is one of a number of options is inelegant and not working for my use case.

For example: my theme is located in a multi-site install under sites/[sitename]/themes/custom/[themename] which even the long path in Line 5 will not find. I could edit the script to accommodate the extra directory layers but I just have a feeling there's a better way to find the file.

Unfortunately I am not a Bash script expert so would need some help here!

Some combination of find -f -name bem.functions.php and dirname I think will work but there are obviously issues around the OS and where the website is hosted (although I suspect /var/www would be most common)

andydempster commented 6 years ago

Just tested this little beauty and it worked. I will find some time to clone and do a PR...

#!/bin/bash
# Link contrib twig functions into the components directory

function search_up() {
  local LOOK=${PWD%/}
    while [[ -n $LOOK ]]; do
        [[ -e $LOOK/$1 ]] && {
            echo "$LOOK"
            return
        }
        LOOK=${LOOK%/*}
    done
  [[ -e /$1 ]] && echo /
}

# Find the drupal root so we can find the vendor
DRUPALROOT=$(search_up /vendor)

if [ $DRUPALROOT ]
then
  if [ -f $DRUPALROOT/vendor/drupal-pattern-lab/bem-twig-extension/bem.function.php ]
  then
    # Set vendor directory var
    VENDORDIR=$DRUPALROOT/vendor
  else
    # No vendor directory found
    echo "Vendor directory or bem function file not found. Please run composer install."
  fi
else
  # Bueller? Bueller?
  echo "Do you even Drupal bro?"
fi

if [ $VENDORDIR ]
then
  # If we found a vendor directory, copy the twig functions into place
  # Array of twig functions to copy, starting from the vendor directory
  twig_functions=(
    "drupal-pattern-lab/add-attributes-twig-extension/add_attributes.function.php"
    "drupal-pattern-lab/bem-twig-extension/bem.function.php"
  )

  # Create symlinks for all contrib twig functions
  for i in "${twig_functions[@]}"
  do
    cp $VENDORDIR/$i ../components/_twig-components/functions/.
  done
fi
andydempster commented 6 years ago

Thanks @evanmwillhite - an alternative to my script has been suggested to setup autoloading for the library (https://github.com/drupal-pattern-lab/bem-twig-extension/blob/master/composer.json)

We can add

"autoload": {
        "psr-4": {
            "drupal-pattern-lab\\bem-twig-extension\\": "lib/" 
        }
    }

Need to check the lib/ bit

ccjjmartin commented 6 years ago

I am liking the second idea (autoload with composer). I wonder if this would require us to do some refactoring of the bem function repository since out bem file isn't located underneath a /src (preferred) or /lib directory: https://github.com/drupal-pattern-lab/bem-twig-extension

I don't know how many other projects are dependent upon it but they would require the composer.json file update too.

evanmwillhite commented 5 years ago

@ccjjmartin any more research here? Would we also need to do this for the other twig extensions as well?

https://github.com/drupal-pattern-lab/bem-twig-extension https://github.com/drupal-pattern-lab/add-attributes-twig-extension https://github.com/drupal-pattern-lab/attach-library-twig-extension