Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Allow files to have a .tmpl extension which is stripped #5

Closed

Conversation

dhensby
Copy link

@dhensby dhensby commented Feb 22, 2018

Allows a fix for silverstripe/recipe-cms#9

--

Added:

  • Tests, travis integration, test namespaces and autoloading
  • Added APIs to better facilitate mocking (such as file_exists, file_get_contents, copy, etc)
  • Support for recipe files to end in .tmpl so that they don't get picked up by IDEs as first class classes

@tractorcow
Copy link

tractorcow commented Mar 5, 2018

@dhensby good idea but it won't work if you composer create-project silverstripe/recipe-cms. Can you make sure it renames base files as well in the postCreate hook?

Copy link

@tractorcow tractorcow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As commented; Make sure it renames in post-create-project.

@dhensby
Copy link
Author

dhensby commented Mar 5, 2018

@tractorcow this appeared to be the canonical source of mapping from the composer meta data to the filename and I couldn't (and can't now) find anywhere referencing postCreate hooks / logic.

Any pointers where to look for that?

@tractorcow
Copy link

tractorcow commented Mar 5, 2018

Check out this bit of code:

public static function getSubscribedEvents()
    {
        return [
            'post-create-project-cmd' => 'cleanupProject',
            'post-package-update' => 'installPackage',
            'post-package-install' => 'installPackage',
        ];
    }

You would want to add your hook in the cleanupProject hook. Unfortunately you may need to do a bit of refactoring in RecipeInstaller; I suggest a new method designed for renaming root files in the base recipe.

The logic for this method would look VERY similar to RecipeInstaller::installProjectFiles(), except instead of installing files it'll just rename root project files.

Let me know if you need any more help. ;P

@@ -44,6 +44,10 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
$any = false;
foreach($fileIterator as $path => $info) {
$destination = $destinationRoot . substr($path, strlen($sourceRoot));
$extension = pathinfo($destination, PATHINFO_EXTENSION);
if ($extension === 'tmpl') {
$destination = substr($destination, -5);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean substr($destination, 0, -5)? $destination would end up just being .tmpl.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops - fixed

@tractorcow
Copy link

tractorcow commented Mar 5, 2018

This check

// Add file to installed (even if already exists)
            if (!in_array($relativePath, $installedFiles)) {

Should we loosen this check so that it matches path with / without the tml suffix? Just to ensure backwards compatibility. Otherwise files that were deleted will be re-created once and you'll end up with duplicate ["Page.php", "Page.php.tmpl"] in the list.

@dhensby dhensby force-pushed the pulls/allow-tmpl-extension branch 6 times, most recently from 440b867 to 16a9232 Compare March 6, 2018 16:06
@dhensby
Copy link
Author

dhensby commented Mar 6, 2018

@tractorcow cleanupProject doesn't appear to inspect the values of files that are defined in extra meta data. it just unsets the values... I'm not sure how the .tmpl logic would fit in here? sorry if I'm being dense.

/**
* Cleanup the root package on create-project
*
* @param Event $event
*/
public function cleanupProject(Event $event)
{
$file = new JsonFile(Factory::getComposerFile());
$data = $file->read();
// Remove project and public files from project
unset($data['extra'][self::PROJECT_FILES]);
unset($data['extra'][self::PUBLIC_FILES]);
// Remove redundant empty extra
if (empty($data['extra'])) {
unset($data['extra']);
}
// Save back to composer.json
$file->write($data);
}

@dhensby
Copy link
Author

dhensby commented Mar 6, 2018

Should we loosen this check so that it matches path with / without the tml suffix? Just to ensure backwards compatibility

This is currently the case. We normalise the .tmpl path early so that comparisons are done as if it was always named without it. Thus it's written to the composer.json without the .tmpl (if it had one) and compared without it too, I've added a test case to cover this under testInstallProjectFilesWithoutTmplExtension

@dhensby dhensby force-pushed the pulls/allow-tmpl-extension branch from 16a9232 to d8f9f31 Compare March 6, 2018 16:26
@tractorcow
Copy link

tractorcow commented Mar 6, 2018

@tractorcow cleanupProject doesn't appear to inspect the values of files that are defined in extra meta data. it just unsets the values... I'm not sure how the .tmpl logic would fit in here? sorry if I'm being dense.

You do it before clearing them. The logic is:

  • Install project
    • Install each library in project
      • plugin installs files for each library, renaming .tmpl as it goes
  • read composer.json in cleanupProject:
    • For each extra, rename .tmpl (in place)
    • then unset it from extra (since they aren't used anymore after initial install)

We normalise the .tmpl path early

Good call just making sure. :)

if ($extension === 'tmpl') {
$destination = substr($destination, -5);
$destinationExt = pathinfo($destination, PATHINFO_EXTENSION);
if ($destinationExt === 'tmpl') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this condition twice? They both compare the same string that comes from $path? It's hard to follow.
$path -> $destination -> check ext
$path -> $relativePath -> check ext

@dhensby dhensby force-pushed the pulls/allow-tmpl-extension branch 2 times, most recently from 3902b63 to 9ac246a Compare March 7, 2018 13:07
@dhensby dhensby force-pushed the pulls/allow-tmpl-extension branch from 9ac246a to a9ad0bb Compare March 7, 2018 13:53
@maxime-rainville
Copy link

Is this still blocked? Looks like all of @tractorcow feedback has been addressed?

@dhensby
Copy link
Author

dhensby commented Feb 26, 2019

Is this still blocked? Looks like all of @tractorcow feedback has been addressed?

it just doesn't work. I think it's actually really difficult to do :/

@maxime-rainville
Copy link

😞 Shall we close the PR then? Doesn't look like it's going to be addressed in the near future.

@dhensby
Copy link
Author

dhensby commented Feb 27, 2019

Yeah - I think I got as far as I could and wasn't getting anywhere when I last worked on it. It's a big shame, though, as it's a big pain when IDEs pick up these files :(

@dhensby dhensby closed this Feb 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants