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

Change publication tags to use Yaml instead of Json #837

Merged
merged 13 commits into from
Jan 16, 2023
4 changes: 2 additions & 2 deletions packages/publications/src/Commands/MakePublicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ protected function captureTagFieldInput(PublicationFieldDefinition $field): ?Pub

$options = PublicationService::getValuesForTagName($tagGroup);
if ($options->isEmpty()) {
return $this->handleEmptyOptionsCollection($field, 'tag', 'No tags for this publication type found in tags.json');
return $this->handleEmptyOptionsCollection($field, 'tag', 'No tags for this publication type found in tags.yml');
}

$this->tip('You can enter multiple tags separated by commas');

$choice = $this->reloadableChoice($this->getReloadableTagValuesArrayClosure($tagGroup),
'Which tag would you like to use?',
'Reload tags.json',
'Reload tags.yml',
true
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function printSelectionInformation(): void
protected function saveTagsToDisk(): void
{
$this->infoComment('Saving tag data to',
DiscoveryService::createClickableFilepath(Hyde::path('tags.json'))
DiscoveryService::createClickableFilepath(Hyde::path('tags.yml'))
);

app(PublicationTags::class)->addTagGroups($this->tags)->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ protected function getFieldType(): PublicationFieldTypes
protected function getTagGroup(): string
{
if (empty(PublicationTags::getTagGroups())) {
$this->error('No tag groups have been added to tags.json');
$this->error('No tag groups have been added to tags.yml');
if ($this->confirm('Would you like to add some tags now?')) {
$this->call('make:publicationTag');

$this->newLine();
$this->comment("Okay, we're back on track!");
} else {
throw new InvalidArgumentException('Can not create a tag field without any tag groups defined in tags.json');
throw new InvalidArgumentException('Can not create a tag field without any tag groups defined in tags.yml');
}
}

Expand Down
26 changes: 13 additions & 13 deletions packages/publications/src/Models/PublicationTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
namespace Hyde\Publications\Models;

use function file_exists;
use function file_get_contents;
use Hyde\Facades\Filesystem;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Hyde;
use Illuminate\Support\Collection;
use function json_decode;
use function json_encode;
use JsonException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

/**
* Object representation for the tags.json file.
* Object representation for the tags.yml file.
*
* @see \Hyde\Publications\Testing\Feature\PublicationTagsTest
*/
Expand Down Expand Up @@ -83,7 +81,7 @@ public function addTagsToGroup(string $name, array|string $values): self
*/
public function save(): self
{
Filesystem::putContents('tags.json', json_encode($this->tags, JSON_PRETTY_PRINT));
Filesystem::putContents('tags.yml', Yaml::dump($this->tags->toArray()));

return $this;
}
Expand Down Expand Up @@ -119,20 +117,22 @@ public static function getTagGroups(): array
}

/**
* Validate the tags.json file is valid.
* Validate the tags.yml file is valid.
*
* @internal This method is experimental and may be removed without notice
*
* @todo Support Yaml
*/
public static function validateTagsFile(): void
{
if (! file_exists(Hyde::path('tags.json'))) {
throw new FileNotFoundException('tags.json');
if (! file_exists(Hyde::path('tags.yml'))) {
throw new FileNotFoundException('tags.yml');
}

$tags = json_decode(file_get_contents(Hyde::path('tags.json')), true);
$tags = Yaml::parseFile(Hyde::path('tags.yml'));

if (! is_array($tags) || empty($tags)) {
throw new JsonException('Could not decode tags.json');
throw new ParseException('Could not decode tags.yml');
}

foreach ($tags as $name => $values) {
Expand All @@ -148,8 +148,8 @@ public static function validateTagsFile(): void
/** @return array<string, array<string>> */
protected function parseTagsFile(): array
{
if (file_exists(Hyde::path('tags.json'))) {
return json_decode(file_get_contents(Hyde::path('tags.json')), true);
if (file_exists(Hyde::path('tags.yml'))) {
return Yaml::parseFile(Hyde::path('tags.yml'));
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function test_media_input_selects_the_right_file()

public function test_command_with_single_tag_input()
{
$this->file('tags.json', json_encode([
$this->file('tags.yml', json_encode([
'test-publication' => ['foo', 'bar', 'baz'],
]));
$this->makeSchemaFile([
Expand All @@ -350,7 +350,7 @@ public function test_command_with_single_tag_input()

public function test_command_with_multiple_tag_inputs()
{
$this->file('tags.json', json_encode([
$this->file('tags.yml', json_encode([
'test-publication' => ['foo', 'bar', 'baz'],
]));
$this->makeSchemaFile([
Expand Down Expand Up @@ -439,7 +439,7 @@ public function test_tag_input_with_no_tags()
]);

$this->artisan('make:publication test-publication')
->expectsOutput('Warning: No tags for this publication type found in tags.json')
->expectsOutput('Warning: No tags for this publication type found in tags.yml')
->expectsConfirmation('Would you like to skip this field?')
->expectsOutput('Error: Unable to locate any tags for this publication type')
->assertExitCode(1);
Expand All @@ -460,7 +460,7 @@ public function test_tag_input_with_no_tags_but_skips()
]);

$this->artisan('make:publication test-publication')
->expectsOutput('Warning: No tags for this publication type found in tags.json')
->expectsOutput('Warning: No tags for this publication type found in tags.yml')
->expectsConfirmation('Would you like to skip this field?', 'yes')
->doesntExpectOutput('Error: Unable to locate any tags for this publication type')
->assertExitCode(0);
Expand Down Expand Up @@ -512,8 +512,8 @@ public function test_handleEmptyOptionsCollection_for_required_field()
]);

$this->artisan('make:publication test-publication')
->doesntExpectOutput('Warning: No tags for this publication type found in tags.json')
->expectsOutput('Error: Unable to create publication: No tags for this publication type found in tags.json')
->doesntExpectOutput('Warning: No tags for this publication type found in tags.yml')
->expectsOutput('Error: Unable to create publication: No tags for this publication type found in tags.yml')
->assertExitCode(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MakePublicationTagCommandTest extends TestCase
{
protected function tearDown(): void
{
unlink(Hyde::path('tags.json'));
unlink(Hyde::path('tags.yml'));

parent::tearDown();
}
Expand All @@ -31,13 +31,19 @@ public function testCanCreateNewPublicationTag()
->expectsOutput('Enter the tag values: (end with an empty line)')
->expectsOutput('Adding the following tags:')
->expectsOutput(' foo: foo, bar, baz')
->expectsOutput('Saving tag data to ['.Hyde::path('tags.json').']')
->expectsOutput('Saving tag data to ['.Hyde::path('tags.yml').']')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('tags.json'));
$this->assertFileExists(Hyde::path('tags.yml'));
$this->assertSame(
json_encode(['foo' => ['foo', 'bar', 'baz']], 128),
file_get_contents(Hyde::path('tags.json'))
<<<'YAML'
foo:
- foo
- bar
- baz

YAML,
file_get_contents(Hyde::path('tags.yml'))
);
}

Expand All @@ -50,13 +56,19 @@ public function testCanCreateNewPublicationTagWithTagNameArgument()
->expectsOutput('Enter the tag values: (end with an empty line)')
->expectsOutput('Adding the following tags:')
->expectsOutput(' foo: foo, bar, baz')
->expectsOutput('Saving tag data to ['.Hyde::path('tags.json').']')
->expectsOutput('Saving tag data to ['.Hyde::path('tags.yml').']')
->assertExitCode(0);

$this->assertFileExists(Hyde::path('tags.json'));
$this->assertFileExists(Hyde::path('tags.yml'));
$this->assertSame(
json_encode(['foo' => ['foo', 'bar', 'baz']], 128),
file_get_contents(Hyde::path('tags.json'))
<<<'YAML'
foo:
- foo
- bar
- baz

YAML,
file_get_contents(Hyde::path('tags.yml'))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function testWithTagFieldInput()
JSON,
'test-publication/schema.json');

unlink(Hyde::path('tags.json'));
unlink(Hyde::path('tags.yml'));
}

public function testWithTagFieldInputButNoTags()
Expand All @@ -249,9 +249,9 @@ public function testWithTagFieldInputButNoTags()
->expectsQuestion('Enter name for field #1', 'MyTag')
->expectsChoice('Enter type for field #1', 'Tag',
self::expectedEnumCases, true)
->expectsOutput('No tag groups have been added to tags.json')
->expectsOutput('No tag groups have been added to tags.yml')
->expectsConfirmation('Would you like to add some tags now?')
->expectsOutput('Error: Can not create a tag field without any tag groups defined in tags.json')
->expectsOutput('Error: Can not create a tag field without any tag groups defined in tags.yml')
->assertExitCode(1);

$this->assertFileDoesNotExist(Hyde::path('test-publication/schema.json'));
Expand All @@ -260,14 +260,14 @@ public function testWithTagFieldInputButNoTags()
public function testWithTagFieldInputButNoTagsCanPromptToCreateTags()
{
$this->directory('test-publication');
$this->cleanUpWhenDone('tags.json');
$this->cleanUpWhenDone('tags.yml');
InputStreamHandler::mockInput("foo\nbar\nbaz\n");

$this->artisan('make:publicationType "Test Publication"')
->expectsQuestion('Enter name for field #1', 'MyTag')
->expectsChoice('Enter type for field #1', 'Tag',
self::expectedEnumCases)
->expectsOutput('No tag groups have been added to tags.json')
->expectsOutput('No tag groups have been added to tags.yml')
->expectsConfirmation('Would you like to add some tags now?', 'yes')
->expectsQuestion('Tag name', 'foo')
->expectsOutput("Okay, we're back on track!")
Expand All @@ -280,14 +280,14 @@ public function testWithTagFieldInputButNoTagsCanPromptToCreateTags()
->expectsChoice('Choose the sort direction', 'Ascending', ['Ascending', 'Descending'])
->expectsQuestion(self::selectPageSizeQuestion, 0)

->doesntExpectOutput('Error: Can not create a tag field without any tag groups defined in tags.json')
->doesntExpectOutput('Error: Can not create a tag field without any tag groups defined in tags.yml')
->assertSuccessful();

$this->assertCommandCalled('make:publicationTag');
$this->assertFileExists(Hyde::path('tags.json'));
$this->assertFileExists(Hyde::path('tags.yml'));
$this->assertSame(
json_encode(['foo' => ['foo', 'bar', 'baz']], 128),
file_get_contents(Hyde::path('tags.json'))
"foo:\n - foo\n - bar\n - baz\n",
file_get_contents(Hyde::path('tags.yml'))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testGetAllTags()
'baz',
],
];
$this->file('tags.json', json_encode($tags));
$this->file('tags.yml', json_encode($tags));
$this->assertSame($tags, PublicationService::getAllTags()->toArray());
}

Expand All @@ -209,7 +209,7 @@ public function testGetValuesForTagName()
],
];

$this->file('tags.json', json_encode($tags));
$this->file('tags.yml', json_encode($tags));

$this->assertSame(['bar', 'baz'], PublicationService::getValuesForTagName('foo')->toArray());
}
Expand Down
Loading