Skip to content

Instantly share code, notes, and snippets.

@jasonvarga
Last active July 31, 2022 18:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonvarga/ce4f0ad8e3b6d934f8d9da489f2ef64b to your computer and use it in GitHub Desktop.
Save jasonvarga/ce4f0ad8e3b6d934f8d9da489f2ef64b to your computer and use it in GitHub Desktop.
Statamic Beta 38 Blueprints Migration Helper

Upgrading Blueprints for changes in Beta 38

Instructions:

  • Upgrade to Statamic 3.0.0-beta.38
  • Copy the contents of this gist's console.php into routes/console.php
  • Run php artisan migrate:beta38
  • Delete the command you just copied. It was only temporary.

To confirm it worked:

  • You should see your blueprints organized into subdirectories.
  • The blueprint or blueprints keys inside the yaml files for collections, taxonomies, asset containers, globals, and forms should be removed.
  • If you had a collection/taxonomy with multiple blueprints, they should have appropriate order keys in them.
<?php // copy everything after this line
use Illuminate\Support\Facades\Artisan;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Collection;
use Statamic\Facades\File;
use Statamic\Facades\Form;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\YAML;
use Statamic\Support\Arr;
use Statamic\Support\Str;
class MigrateBetaThirtyEight
{
protected $console;
protected $usedBlueprints = [];
public function __construct($console)
{
$this->console = $console;
}
public function migrate()
{
$this->console->comment('Collections:');
Collection::all()->each(function ($collection) {
$this->console->line(" {$collection->title()}");
$this->pluckBlueprintsAndCopy($collection, 'collections/'.$collection->handle(), false);
$collection->queryEntries()->get('blueprint')->map(function ($entry) {
return $entry->get('blueprint');
})->filter()->unique()->each(function ($blueprint) use ($collection) {
$this->copyBlueprint($blueprint, $blueprint, 'collections/'.$collection->handle());
});
});
$this->console->comment('Taxonomies:');
Taxonomy::all()->each(function ($taxonomy) {
$this->console->line(" {$taxonomy->title()}");
$this->pluckBlueprintsAndCopy($taxonomy, 'taxonomies/'.$taxonomy->handle(), false);
$taxonomy->queryTerms()->get('blueprint')->map(function ($entry) {
return $entry->get('blueprint');
})->filter()->unique()->each(function ($blueprint) use ($taxonomy) {
$this->copyBlueprint($blueprint, $blueprint, 'taxonomies/'.$taxonomy->handle());
});
});
$this->console->comment('Global Sets:');
GlobalSet::all()->each(function ($set) {
$this->console->line(" {$set->title()}");
$this->pluckBlueprintsAndCopy($set, 'globals', true);
});
$this->console->comment('Asset Containers:');
AssetContainer::all()->each(function ($container) {
$this->console->line(" {$container->title()}");
[$yaml] = $this->pluckBlueprintsAndCopy($container, 'assets', true, false);
$defaultAssetBlueprint = Blueprint::directory().'/asset.yaml';
if (! isset($yaml['blueprint']) && File::exists($defaultAssetBlueprint)) {
$this->copyBlueprint('asset', $container->handle(), 'assets');
}
});
$this->console->comment('Forms:');
Form::all()->each(function ($form) {
$this->console->line(" {$form->title()}");
$this->pluckBlueprintsAndCopy($form, 'forms', true);
});
foreach ($this->usedBlueprints as $blueprint) {
if (File::exists($blueprint)) {
File::delete($blueprint);
}
}
$this->console->line('');
$this->console->info('[✓] Blueprint migration complete');
}
protected function pluckBlueprintsAndCopy($item, $namespace, $useItemHandle, $save = true)
{
$yaml = YAML::file($item->path())->parse();
$blueprints = Arr::wrap(
Arr::getFirst($yaml, ['blueprint', 'blueprints'], [])
);
foreach ($blueprints as $i => $blueprint) {
$this->copyBlueprint(
$blueprint,
$useItemHandle ? $item->handle() : $blueprint,
$namespace,
count($blueprints) === 1 ? null : $i + 1
);
}
$newYaml = $yaml;
unset($newYaml['blueprint'], $newYaml['blueprints']);
if ($save) {
File::put($item->path(), YAML::dump($newYaml));
}
return [$yaml, $newYaml];
}
protected function copyBlueprint($oldHandle, $newHandle, $namespace, $order = null)
{
$original = resource_path('blueprints/'.$oldHandle.'.yaml');
$this->usedBlueprints[] = $original;
if (! File::exists($original)) {
return;
}
File::makeDirectory($dest = resource_path('blueprints/'.$namespace));
$yaml = YAML::file($original)->parse();
if ($order) {
$yaml['order'] = $order;
}
File::put($destPath = $dest.'/'.$newHandle.'.yaml', YAML::dump($yaml));
$this->console->line(" <info>[✓]</info> {$this->relativePath($destPath)}");
}
protected function relativePath($path)
{
return Str::after($path, Blueprint::directory().'/');
}
}
Artisan::command('migrate:beta38', function () {
(new MigrateBetaThirtyEight($this))->migrate();
});
@admench
Copy link

admench commented Jul 22, 2020

Awesome work with this! Thanks a lot! 🙌🏼

@studio1902
Copy link

Flawless, thanks!

@jonassiewertsen
Copy link

Good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment