Skip to content

Commit

Permalink
Merge pull request #4 from open-sausages/pulls/1/throwing-a-public
Browse files Browse the repository at this point in the history
API Support `public-files`
  • Loading branch information
Aaron Carlino authored Jan 12, 2018
2 parents e5beb23 + a959a68 commit 3e16a5b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ Recipe types should follow the following rules:
- The `require` must have `silverstripe/recipe-plugin` as a dependency.
- `extra.project-files` must be declared as a list of wildcard patterns, matching the files in the recipe root
as they should be copied to the root project. The relative paths of these resources are equivalent.
- `extra.public-files` must be declared for any files which should be copied to the `public` web folder. If the project
in question doesn't have any public folder, these will be copied to root instead. Note that all public files
must be committed to the recipe `public` folder.

An example recipe:

Expand All @@ -151,9 +154,31 @@ An example recipe:
"project-files": [
"mysite/_config/*.yml",
"mysite/code/MyBlogPage.php"
"client/src/*"
],
"public-files": [
"client/dist/*"
]
},
"prefer-stable": true,
"minimum-stability": "dev"
}
```

The files within this recipe would be organised in the structure:

```
client/
src/
blog.scss
mysite/
_config/
settings.yml
code/
MyBlogPage.php
public/
client/
dist/
blog.css
composer.json
```
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
}
},
"extra": {
"class": "SilverStripe\\RecipePlugin\\RecipePlugin"
"class": "SilverStripe\\RecipePlugin\\RecipePlugin",
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"require": {
"composer-plugin-api": "^1.1"
Expand Down
43 changes: 34 additions & 9 deletions src/RecipeInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ public function __construct(IOInterface $io, Composer $composer) {
* @param string $sourceRoot Base of source files (no trailing slash)
* @param string $destinationRoot Base of destination directory (no trailing slash)
* @param array $filePatterns List of file patterns in wildcard format (e.g. `code/My*.php`)
* @param string $registrationKey Registration key for installed files
* @param string $name Name of project file type being installed
*/
protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $filePatterns)
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'][RecipePlugin::PROJECT_FILES_INSTALLED])
? $composerData['extra'][RecipePlugin::PROJECT_FILES_INSTALLED]
$installedFiles = isset($composerData['extra'][$registrationKey])
? $composerData['extra'][$registrationKey]
: [];

// Load all project files
Expand All @@ -46,7 +48,7 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $

// Write header
if (!$any) {
$this->io->write("Installing project files for recipe <info>{$recipe}</info>:");
$this->io->write("Installing {$name} files for recipe <info>{$recipe}</info>:");
$any = true;
}

Expand Down Expand Up @@ -85,7 +87,7 @@ protected function installProjectFiles($recipe, $sourceRoot, $destinationRoot, $
if (!isset($composerData['extra'])) {
$composerData['extra'] = [];
}
$composerData['extra'][RecipePlugin::PROJECT_FILES_INSTALLED] = $installedFiles;
$composerData['extra'][$registrationKey] = $installedFiles;
$composerFile->write($composerData);
}
}
Expand Down Expand Up @@ -143,18 +145,41 @@ public function installLibrary(PackageInterface $package)
return;
}

// Find recipe base dir
$recipePath = $this->getInstallPath($package);

// Find project path
$destinationPath = dirname(realpath(Factory::getComposerFile()));
$projectPath = dirname(realpath(Factory::getComposerFile()));

// Find public path
$candidatePublicPath = $projectPath . DIRECTORY_SEPARATOR . RecipePlugin::PUBLIC_PATH;
$publicPath = is_dir($candidatePublicPath) ? $candidatePublicPath : $projectPath;

// Copy project files to root
$name = $package->getName();
$extra = $package->getExtra();

// Install project-files
if (isset($extra[RecipePlugin::PROJECT_FILES])) {
$this->installProjectFiles(
$name,
$this->getInstallPath($package),
$destinationPath,
$extra[RecipePlugin::PROJECT_FILES]
$recipePath,
$projectPath,
$extra[RecipePlugin::PROJECT_FILES],
RecipePlugin::PROJECT_FILES_INSTALLED,
'project'
);
}

// Install public-files
if (isset($extra[RecipePlugin::PUBLIC_FILES])) {
$this->installProjectFiles(
$name,
$recipePath . '/' . RecipePlugin::PUBLIC_PATH,
$publicPath,
$extra[RecipePlugin::PUBLIC_FILES],
RecipePlugin::PUBLIC_FILES_INSTALLED,
'public'
);
}
}
Expand Down
22 changes: 20 additions & 2 deletions src/RecipePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@ class RecipePlugin implements PluginInterface, EventSubscriberInterface, Capable
*/
const PROJECT_FILES = 'project-files';

/**
* 'extra' key for public files
*/
const PUBLIC_FILES = 'public-files';

/**
* Hard-coded 'public' web-root folder
*/
const PUBLIC_PATH = 'public';

/**
* 'extra' key for list of project files installed
*/
const PROJECT_FILES_INSTALLED = 'project-files-installed';

/**
* 'extra' key for list of public files installed
*/
const PUBLIC_FILES_INSTALLED = 'public-files-installed';

/**
* 'extra' key for project dependencies installed
*/
Expand Down Expand Up @@ -81,8 +96,11 @@ public function cleanupProject(Event $event)
$file = new JsonFile(Factory::getComposerFile());
$data = $file->read();

// Remove project-files from project, and any empty extra
unset($data['extra']['project-files']);
// 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']);
}
Expand Down

0 comments on commit 3e16a5b

Please sign in to comment.