Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Twig copy script incorrect #254

Open
andydempster opened this issue Jun 19, 2018 · 4 comments
Open

Twig copy script incorrect #254

andydempster opened this issue Jun 19, 2018 · 4 comments

Comments

@andydempster
Copy link

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
Copy link
Author

andydempster commented Jun 19, 2018

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants