Giter Club home page Giter Club logo

core-composer-scaffold's Introduction

Mautic Composer Scaffold

This project provides a composer plugin for placing scaffold files (like index.php, update.php, โ€ฆ) from the mautic/core-lib project into their desired location inside the web root. Only individual files may be scaffolded with this plugin.

The purpose of scaffolding files is to allow Mautic sites to be fully managed by Composer, and still allow individual asset files to be placed in arbitrary locations. The goal of doing this is to enable a properly configured composer template to produce a file layout that exactly matches the file layout of a Mautic 3.x and earlier tarball distribution. Other file layouts will also be possible; for example, a project layout very similar to the current nickveenhof/mautic-project template will also be provided. When one of these projects is used, the user should be able to use composer require and composer update on a Mautic site immediately after untarring the downloaded archive.

Note that the dependencies of a Mautic site are only able to scaffold files if explicitly granted that right in the top-level composer.json file. See allowed packages, below.

Usage

Mautic Composer Scaffold is used by requiring mautic/core-composer-scaffold in your project, and providing configuration settings in the extra section of your project's composer.json file. Additional configuration from the composer.json file of your project's dependencies is also consulted in order to scaffold the files a project needs. Additional information may be added to the beginning or end of scaffold files, as is commonly done to .htaccess and robots.txt files. See altering scaffold files for more information.

Typically, the scaffold operations run automatically as needed, e.g. after composer install, so it is usually not necessary to do anything different to scaffold a project once the configuration is set up in the project composer.json file, as described below. To scaffold files directly, run:

composer mautic:scaffold

Allowed Packages

Scaffold files are stored inside of projects that are required from the main project's composer.json file as usual. The scaffolding operation happens after composer install, and involves copying or symlinking the desired assets to their destination location. In order to prevent arbitrary dependencies from copying files via the scaffold mechanism, only those projects that are specifically permitted by the top-level project will be used to scaffold files.

Example: Permit scaffolding from the project mautic/core-lib

  "name": "my/project",
  ...
  "extra": {
    "mautic-scaffold": {
      "allowed-packages": [
        "mautic/core-lib"
      ],
      ...
    }
  }

Allowing a package to scaffold files also permits it to delegate permission to scaffold to any project that it requires itself. This allows a package to organize its scaffold assets as it sees fit. For example, the project mautic/core-lib may choose to store its assets in a subproject mautic/assets.

It is possible for a project to obtain scaffold files from multiple projects. For example, a Mautic project using a distribution, and installing on a specific web hosting service provider might take its scaffold files from:

  • Mautic core
  • Its distribution
  • A project provided by the hosting provider
  • The project itself

Each project allowed to scaffold by the top-level project will be used in turn, with projects declared later in the allowed-packages list taking precedence over the projects named before. The top-level composer.json itself is always implicitly allowed to scaffold files, and its scaffold files have highest priority.

Defining Project Locations

The top-level project in turn must define where the web root is located. It does so via the locations mapping, as shown below:

  "name": "my/project",
  ...
  "extra": {
    "mautic-scaffold": {
      "locations": {
        "web-root": "./docroot"
      },
      ...
    }
  }

This makes it possible to configure a project with different file layouts; for example, either the mautic/mautic file layout or the nickveenhof/mautic-project file layout could be used to set up a project.

If a web-root is not explicitly defined, then it will default to ./.

Altering Scaffold Files

Sometimes, a project might wish to use a scaffold file provided by a dependency, but alter it in some way. Two forms of alteration are supported: appending and patching.

The example below shows a project that appends additional entries onto the end of the robots.txt file provided by mautic/core-lib:

  "name": "my/project",
  ...
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/robots.txt": {
          "append": "assets/my-robots-additions.txt",
        }
      }
    }
  }

It is also possible to prepend to a scaffold file instead of, or in addition to appending by including a "prepend" entry that provides the relative path to the file to prepend to the scaffold file.

The example below demonstrates the use of the post-mautic-scaffold-cmd hook to patch the .htaccess file using a patch.

  "name": "my/project",
  ...
  "scripts": {
    "post-mautic-scaffold-cmd": [
      "cd docroot && patch -p1 <../patches/htaccess-ssl.patch"
    ]
  }

Defining Scaffold Files

The placement of scaffold assets is under the control of the project that provides them, but the location is always relative to some directory defined by the root project -- usually the web root. For example, the scaffold file robots.txt is copied from its source location, assets/robots.txt into the web root in the snippet below.

{
  "name": "mautic/assets",
  ...
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/robots.txt": "assets/robots.txt",
        ...
      }
    }
  }
}

Excluding Scaffold Files

Sometimes, a project might prefer to entirely replace a scaffold file provided by a dependency, and receive no further updates for it. This can be done by setting the value for the scaffold file to exclude to false:

  "name": "my/project",
  ...
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/robots.txt": false
      }
    }
  }

If possible, use the append and prepend directives as explained in altering scaffold files, above. Excluding a file means that your project will not get any bug fixes or other updates to files that are modified locally.

Overwrite

By default, scaffold files overwrite whatever content exists at the target location. Sometimes a project may wish to provide the initial contents for a file that will not be changed in subsequent updates. This can be done by setting the overwrite flag to false, as shown in the example below:

{
  "name": "service-provider/mautic-scaffold-files",
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/sites/default/settings.php": {
          "mode": "replace",
          "path": "assets/sites/default/settings.php",
          "overwrite": false
        }
      }
    }
  }
}

Note that the overwrite directive is intended to be used by starter kits, service providers, and so on. Individual Mautic sites should exclude the file by setting its value to false instead.

Autoload File

The scaffold tool automatically creates the required autoload.php file at the Mautic root as part of the scaffolding operation. This file should not be modified or customized in any way. If it is committed to the repository, though, then the scaffold tool will stop managing it. If the location of the vendor directory is changed for any reason, and the autoload.php file has been committed to the repository, manually delete it and then run composer install to update it.

Specifications

Reference section for the configuration directives for the "mautic-scaffold" section of the "extra" section of a composer.json file appear below.

allowed-packages

The allowed-packages configuration setting contains an ordered list of package names that will be used during the scaffolding phase.

"allowed-packages": [
  "mautic/core-lib",
],

file-mapping

The file-mapping configuration setting consists of a map from the destination path of the file to scaffold to a set of properties that control how the file should be scaffolded.

The available properties are as follows:

  • mode: One of "replace", "append" or "skip".
  • path: The path to the source file to write over the destination file.
  • prepend: The path to the source file to prepend to the destination file, which must always be a scaffold file provided by some other project.
  • append: Like prepend, but appends content rather than prepends.
  • overwrite: If false, prevents a replace from happening if the destination already exists.

The mode may be inferred from the other properties. If the mode is not specified, then the following defaults will be supplied:

  • replace: Selected if a path property is present, or if the entry's value is a string rather than a property set.
  • append: Selected if a prepend or append property is present.
  • skip: Selected if the entry's value is a boolean false.

Examples:

"file-mapping": {
  "[web-root]/sites/default/default.settings.php": {
    "mode": "replace",
    "path": "assets/sites/default/default.settings.php",
    "overwrite": true
  },
  "[web-root]/sites/default/settings.php": {
    "mode": "replace",
    "path": "assets/sites/default/settings.php",
    "overwrite": false
  },
  "[web-root]/robots.txt": {
    "mode": "append",
    "prepend": "assets/robots-prequel.txt",
    "append": "assets/robots-append.txt"
  },
  "[web-root]/.htaccess": {
    "mode": "skip",
  }
}

The short-form of the above example would be:

"file-mapping": {
  "[web-root]/sites/default/default.settings.php": "assets/sites/default/default.settings.php",
  "[web-root]/sites/default/settings.php": {
    "path": "assets/sites/default/settings.php",
    "overwrite": false
  },
  "[web-root]/robots.txt": {
    "prepend": "assets/robots-prequel.txt",
    "append": "assets/robots-append.txt"
  },
  "[web-root]/.htaccess": false
}

Note that there is no distinct "prepend" mode; "append" mode is used to both append and prepend to scaffold files. The reason for this is that scaffold file entries are identified in the file-mapping section keyed by their destination path, and it is not possible for multiple entries to have the same key. If "prepend" were a separate mode, then it would not be possible to both prepend and append to the same file.

By default, append operations may only be applied to files that were scaffolded by a previously evaluated project. If the force-append attribute is added to an append operation, though, then the append will be made to non-scaffolded files if and only if the append text does not already appear in the file. When using this mode, it is also possible to provide default contents to use in the event that the destination file is entirely missing.

The example below demonstrates scaffolding a settings-custom.php file, and including it from the existing settings.php file.

"file-mapping": {
  "[web-root]/sites/default/settings-custom.php": "assets/settings-custom.php",
  "[web-root]/sites/default/settings.php": {
    "append": "assets/include-settings-custom.txt",
    "force-append": true,
    "default": "assets/initial-default-settings.txt"
  }
}

Note that the example above still works if used with a project that scaffolds the settings.php file.

gitignore

The gitignore configuration setting controls whether or not this plugin will manage .gitignore files for files written during the scaffold operation.

  • true: .gitignore files will be updated when scaffold files are written.
  • false: .gitignore files will never be modified.
  • Not set: .gitignore files will be updated if the target directory is a local working copy of a git repository, and the vendor directory is ignored in that repository.

locations

The locations configuration setting contains a list of named locations that may be used in placing scaffold files. The only required location is web-root. Other locations may also be defined if desired.

"locations": {
  "web-root": "./docroot"
},

symlink

The symlink property causes replace operations to make a symlink to the source file rather than copying it. This is useful when doing core development, as the symlink files themselves should not be edited. Note that append operations override the symlink option, to prevent the original scaffold assets from being altered.

"symlink": true,

Managing Scaffold Files

Scaffold files should be treated the same way that the vendor directory is handled. If you need to commit vendor (e.g. in order to deploy your site), then you should also commit your scaffold files. You should not commit your vendor directory or scaffold files unless it is necessary.

If a dependency provides a scaffold file with overwrite set to false, that file should be committed to your repository.

By default, .gitignore files will be automatically updated if needed when scaffold files are written. See the gitignore setting in the Specifications section above.

Examples

Some full-length examples appear below.

Sample composer.json for a project that relies on packages that use composer-scaffold:

{
  "name": "my/project",
  "require": {
    "nickveenhof/mautic-core-composer-scaffold": "*",
    "composer/installers": "^1.2",
    "cweagans/composer-patches": "^1.6.5",
    "mautic/core-lib": "3.x-dev",
    "service-provider/mautic-3x-scaffold-files": "^1"
  },
  "config": {
    "optimize-autoloader": true,
    "sort-packages": true
  },
  "extra": {
    "mautic-scaffold": {
      "allowed-packages": [
        "mautic/core-lib"
      ],
      "locations": {
        "web-root": "./docroot"
      },
      "symlink": true,
      "overwrite": true,
      "file-mapping": {
        "[web-root]/.htaccess": false,
        "[web-root]/robots.txt": "assets/robots-default.txt"
      }
    }
  }
}

Sample composer.json for mautic/core-lib, with assets placed in a different project:

{
  "name": "mautic/core-lib",
  "extra": {
    "mautic-scaffold": {
      "allowed-packages": [
        "mautic/assets",
      ]
    }
  }
}

Sample composer.json for composer-scaffold files in mautic/assets:

{
  "name": "mautic/assets",
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/.csslintrc": "assets/.csslintrc",
        "[web-root]/.editorconfig": "assets/.editorconfig",
        "[web-root]/.eslintignore": "assets/.eslintignore",
        "[web-root]/.eslintrc.json": "assets/.eslintrc.json",
        "[web-root]/.gitattributes": "assets/.gitattributes",
        "[web-root]/.ht.router.php": "assets/.ht.router.php",
        "[web-root]/.htaccess": "assets/.htaccess",
        "[web-root]/index.php": "assets/index.php",
        "[web-root]/index.php": "assets/index_dev.php",
        "[web-root]/robots.txt": "assets/robots.txt",
      }
    }
  }
}

Sample composer.json for a library that implements composer-scaffold:

{
  "name": "service-provider/mautic-3x-scaffold-files",
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/app/config/config_prod.php": "assets/app/config/config_prod.php"
      }
    }
  }
}

Append to robots.txt:

{
  "name": "service-provider/mautic-3x-scaffold-files",
  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[web-root]/robots.txt": {
          "append": "assets/my-robots-additions.txt",
        }
      }
    }
  }
}

Patch a file after it's copied:

"post-mautic-scaffold-cmd": [
  "cd docroot && patch -p1 <../patches/htaccess-ssl.patch"
]

Related Plugins

composer/installers

The composer/installers plugin is similar to this plugin in that it allows dependencies to be installed in locations other than the vendor directory. However, Composer and the composer/installers plugin have a limitation that one project cannot be moved inside of another project. Therefore, if you use composer/installers to place Mautic modules inside the directory web/modules/contrib, then you cannot also use composer/installers to place files such as index.php and robots.txt into the web directory. The mautic-scaffold plugin was created to work around this limitation.

core-composer-scaffold's People

Contributors

escopecz avatar nickveenhof avatar rcheesley avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

mollux

core-composer-scaffold's Issues

Scaffold file permissions not preserved

Example case is bin/console e.g.

  "extra": {
    "mautic-scaffold": {
      "file-mapping": {
        "[project-root]/bin/console": "assets/scaffold/files/console"
      }
    }
  }

proof of concept:

$ chmod +x docroot/app/assets/scaffold/files/console 

$ echo "#I was here" >> docroot/app/assets/scaffold/files/console 

$ ls -al docroot/app/assets/scaffold/files/console 
# -rwxr-xr-x 1 rjocoleman rjocoleman 1540 Nov 10 12:31 docroot/app/assets/scaffold/files/console

$ composer mautic:scaffold
# Scaffolding files for mautic/core-lib:
#  - Copy [project-root]/bin/console from assets/scaffold/files/console

$ ls -al bin/console 
# -rw-r--r-- 1 rjocoleman rjocoleman 1540 Nov 10 12:31 bin/console

$ tail -n 1 bin/console 
# #I was here

$ ls -al docroot/app/assets/scaffold/files/console 
# -rwxr-xr-x 1 rjocoleman rjocoleman 1540 Nov 10 12:31 docroot/app/assets/scaffold/files/console

As it stands users need to run an extra chmod +x bin/console for that to work as expected (or prefix with php and run it via php).

A potential mitigation is in the copy command to grab the scaffold file permissions and set the same permissions when the new file is created, here-ish:

/**
* {@inheritdoc}
*/
public function process(ScaffoldFilePath $destination, IOInterface $io, ScaffoldOptions $options) {
$fs = new Filesystem();
$destination_path = $destination->fullPath();
// Do nothing if overwrite is 'false' and a file already exists at the
// destination.
if ($this->overwrite === FALSE && file_exists($destination_path)) {
$interpolator = $destination->getInterpolator();
$io->write($interpolator->interpolate(" - Skip <info>[dest-rel-path]</info> because it already exists and overwrite is <comment>false</comment>."));
return new ScaffoldResult($destination, FALSE);
}
// Get rid of the destination if it exists, and make sure that
// the directory where it's going to be placed exists.
$fs->remove($destination_path);
$fs->ensureDirectoryExists(dirname($destination_path));
if ($options->symlink()) {
return $this->symlinkScaffold($destination, $io);
}
return $this->copyScaffold($destination, $io);
}
/**
* Copies the scaffold file.
*
* @param \Mautic\Composer\Plugin\Scaffold\ScaffoldFilePath $destination
* Scaffold file to process.
* @param \Composer\IO\IOInterface $io
* IOInterface to writing to.
*
* @return \Mautic\Composer\Plugin\Scaffold\Operations\ScaffoldResult
* The scaffold result.
*/
protected function copyScaffold(ScaffoldFilePath $destination, IOInterface $io) {
$interpolator = $destination->getInterpolator();
$this->source->addInterpolationData($interpolator);
$success = file_put_contents($destination->fullPath(), $this->contents());
if (!$success) {
throw new \RuntimeException($interpolator->interpolate("Could not copy source file <info>[src-rel-path]</info> to <info>[dest-rel-path]</info>!"));
}
$io->write($interpolator->interpolate(" - Copy <info>[dest-rel-path]</info> from <info>[src-rel-path]</info>"));
return new ScaffoldResult($destination, $this->overwrite);
}

I'd be happy to make a PR for this if it's deemed as important.

`app/config/local.php` gets removed by `composer update`

Consider a Mautic installation created as follows:

composer create-project mautic/recommended-project:^4 some-dir --no-interaction

In the root directory, I have a .gitignore which is the default but with the following lines added:

/docroot/app/*
/docroot/plugins/*
/docroot/themes/*

This is based on this recommendation to not check those files into Git (as I haven't modified them):

The Mautic Composer Scaffold plugin can download the scaffold files (like index.php, .htaccess, โ€ฆ) to the docroot/ directory of your project. If you have not customized those files you could choose to not check them into your version control system (e.g. git)

This is problematic when updating Mautic to the latest version (composer update). It removes /docroot/app/config/local.php.

Before composer update, the local.php file in the folder tree below:

After running composer update to update Mautic, the file is gone:

I fixed it by storing local.php in config/local.php (in the root of my Mautic installation) and pointing Mautic to that file through paths_local.php as follows:

 "extra": {
    "mautic-scaffold": {
      "locations": {
        "web-root": "docroot/",
      },
      "file-mapping": {
        "[web-root]/app/config/paths_local.php": "scaffold-files/paths_local.php"
      }
    },

The contents of scaffold-files/paths_local.php are as follows:

<?php

$paths['local_config'] = '%kernel.root_dir%/../../config/local.php';

This basically means that I can just store local.php in the config folder in the root directory of my Mautic instance.

Just wanted to report this here in case someone is running into the same issue. Curious to hear from others whether they have a better approach to this ๐Ÿ‘๐Ÿผ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.