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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php:
- 7.1
- 7.2
- nightly

matrix:
fast_finish: true
allow_failures:
- php: nightly

before_script:
- composer validate
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

script:
- vendor/bin/phpunit tests/
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"SilverStripe\\RecipePlugin\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SilverStripe\\Test\\RecipePlugin\\": "src/"
}
},
"extra": {
"class": "SilverStripe\\RecipePlugin\\RecipePlugin",
"branch-alias": {
Expand All @@ -24,6 +29,7 @@
"composer-plugin-api": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^7",
"composer/composer": "^1.2"
},
"minimum-stability": "dev"
Expand Down
62 changes: 51 additions & 11 deletions src/RecipeInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use FilesystemIterator;
use Iterator;
use RecursiveDirectoryIterator;
Expand All @@ -16,8 +17,16 @@

class RecipeInstaller extends LibraryInstaller {

public function __construct(IOInterface $io, Composer $composer) {
parent::__construct($io, $composer, null);
/**
* RecipeInstaller constructor.
*
* @param IOInterface $io
* @param Composer $composer
* @param string $type
* @param Filesystem $filesystem
*/
public function __construct(IOInterface $io, Composer $composer, $type = null, Filesystem $filesystem = null) {
parent::__construct($io, $composer, $type, $filesystem);
}

/**
Expand All @@ -32,19 +41,23 @@ public function __construct(IOInterface $io, Composer $composer) {
*/
protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $filePatterns, $registrationKey, $name = 'project')
{
// load composer json data
$composerFile = new JsonFile(Factory::getComposerFile(), null, $this->io);
$composerData = $composerFile->read();
$installedFiles = isset($composerData['extra'][$registrationKey])
? $composerData['extra'][$registrationKey]
: [];
// fetch the installed files from the json data
$installedFiles = $this->getInstalledFiles($registrationKey);

// Load all project files
$fileIterator = $this->getFileIterator($sourceRoot, $filePatterns);
$any = false;
foreach($fileIterator as $path => $info) {
$destination = $destinationRoot . substr($path, strlen($sourceRoot));
$destinationExt = pathinfo($destination, PATHINFO_EXTENSION);
if ($destinationExt === 'tmpl') {
$destination = substr($destination, 0, -5);
}
$relativePath = substr($path, strlen($sourceRoot) + 1); // Name path without leading '/'
$relativePathExt = pathinfo($relativePath, PATHINFO_EXTENSION);
if ($relativePathExt === 'tmpl') {
$relativePath = substr($relativePath, 0, -5);
}

// Write header
if (!$any) {
Expand All @@ -53,8 +66,8 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
}

// Check if file exists
if (file_exists($destination)) {
if (file_get_contents($destination) === file_get_contents($path)) {
if ($this->fileExists($destination)) {
if ($this->fileGetContents($destination) === $this->fileGetContents($path)) {
$this->io->write(
" - Skipping <info>$relativePath</info> (<comment>existing, but unchanged</comment>)"
);
Expand All @@ -72,7 +85,7 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
$any++;
$this->io->write(" - Copying <info>$relativePath</info>");
$this->filesystem->ensureDirectoryExists(dirname($destination));
copy($path, $destination);
$this->filesystem->copy($path, $destination);
}

// Add file to installed (even if already exists)
Expand All @@ -84,6 +97,8 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
// If any files are written, modify composer.json with newly installed files
if ($installedFiles) {
sort($installedFiles);
$composerFile = $this->getComposerFile();
$composerData = $composerFile->read();
if (!isset($composerData['extra'])) {
$composerData['extra'] = [];
}
Expand All @@ -92,6 +107,31 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
}
}

public function fileExists($filename)
{
return file_exists($filename);
}

public function fileGetContents($filename, $use_include_path = false, $context = null, $offset = 0, $maxlen = null)
{
return file_get_contents($filename, $use_include_path, $context, $offset, $maxlen);
}

protected function getComposerFile()
{
return new JsonFile(Factory::getComposerFile(), null, $this->io);
}

protected function getInstalledFiles($registrationKey)
{
// load composer json data
$composerFile = $this->getComposerFile();
$composerData = $composerFile->read();
return isset($composerData['extra'][$registrationKey])
? $composerData['extra'][$registrationKey]
: [];
}

/**
* Get iterator of matching source files to copy
*
Expand Down
Loading