Skip to content

Commit daac198

Browse files
committed
Refactoring project to use an external
configuration.
1 parent 376368b commit daac198

35 files changed

+786
-495
lines changed

.gitignore

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
/ci/phpunit/
33
/ci/psalm/
44
/ci/phpunit.xml
5-
/src/**/*.*
6-
!/src/Configuration/**/*.php
7-
!/src/Generator/**/*.php
8-
!/src/Parser/**/*.php
9-
!/src/CGCodeInterface.php
10-
/tests/**/*.*
11-
!/tests/Configuration/**/*.*
12-
!/tests/Generator/**/*.*
13-
!/tests/Parser/**/*.*
14-
!/tests/CGTestCase.php
5+
/src/Easy/**/*.*
6+
/src/Expert/**/*.*
7+
/src/Hard/**/*.*
8+
/src/Medium/**/*.*
9+
/tests/Easy/**/*.*
10+
/tests/Expert/**/*.*
11+
/tests/Hard/**/*.*
12+
/tests/Medium/**/*.*
1513
/tools/
1614
/var/cache/
1715
/vendor/

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Changed
9+
- Configurations, inputs, outputs and default codes are now in a separate project.
10+
- Default codes and tests are now generated in the `Easy`, `Medium`, `Hard` and `Expert` sub-directories
11+
of the `src` and `tests` directories.
12+
713
## [4.2.0] - 2024-05-13
814
### Added
915
- Community training medium configurations.

bin/generate-tests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ echo "\nStarting generation.\n";
99

1010
$filesGenerator = new FilesGenerator(__DIR__ . '/../templates/');
1111
$filesGenerator->generate(
12-
__DIR__ . '/../config/',
12+
__DIR__ . '/../vendor/cyril-verloop/codingame-configuration/config/',
1313
__DIR__ . '/../src/',
1414
__DIR__ . '/../tests/'
1515
);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"php": "8.3.*",
2424
"twig/twig": "^3.7",
2525
"twig/string-extra": "^3.7",
26-
"cyril-verloop/iterator": "^3.1"
26+
"cyril-verloop/iterator": "^3.1",
27+
"cyril-verloop/codingame-configuration": "^1.0"
2728
},
2829
"require-dev": {
2930
"mikey179/vfsstream": "^1.6"

composer.lock

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Configuration/ConfigurationConvertor.php

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
namespace CyrilVerloop\Codingame\Configuration;
66

7-
use CyrilVerloop\Codingame\Generator\GeneratorCodeConfiguration;
8-
use CyrilVerloop\Codingame\Generator\GeneratorTestConfiguration;
7+
use CyrilVerloop\Codingame\Configuration\TestConfigurations;
8+
use CyrilVerloop\Codingame\Generator\TestConfiguration as TCGenerator;
9+
use CyrilVerloop\Codingame\Generator\TestConfigurations as TCsGenerator;
10+
use CyrilVerloop\Codingame\Generator\CodeGeneratorConfiguration;
11+
use CyrilVerloop\Codingame\Generator\TestGeneratorConfiguration;
912
use CyrilVerloop\Codingame\Parser\ParsedConfiguration;
1013

1114
final class ConfigurationConvertor
@@ -16,51 +19,118 @@ final class ConfigurationConvertor
1619
* Returns the code configuration for the generator.
1720
* @param \CyrilVerloop\Codingame\Parser\ParsedConfiguration $parsedConfiguration the parsed configuration.
1821
* @param string $defaultCodeFile the file with the default code.
19-
* @return \CyrilVerloop\Codingame\Generator\GeneratorCodeConfiguration the code configuration for the generator.
22+
* @return \CyrilVerloop\Codingame\Generator\CodeGeneratorConfiguration the code configuration for the generator.
2023
* @throws \RuntimeException if the default code file is not readable.
2124
*/
22-
static function getGeneratorCodeConfiguration(
25+
static function getCodeGeneratorConfiguration(
2326
ParsedConfiguration $parsedConfiguration,
2427
string $defaultCodeFile
25-
): GeneratorCodeConfiguration {
28+
): CodeGeneratorConfiguration {
2629

2730
if (is_readable($defaultCodeFile) === false) {
2831
throw new \RuntimeException('defaultCodeFileNotReadable');
2932
}
3033

3134
$defaultCode = file_get_contents($defaultCodeFile);
35+
36+
return new CodeGeneratorConfiguration(
37+
self::convertPathToNamespace($parsedConfiguration->getPath()),
38+
$parsedConfiguration->getName(),
39+
$parsedConfiguration->getLink(),
40+
self::adaptCodeForCodeGenerator($defaultCode)
41+
);
42+
}
43+
44+
/**
45+
* Converts a path to a namespace.
46+
* @param string $path the path.
47+
* @return string the namespace.
48+
*/
49+
private static function convertPathToNamespace(string $path): string
50+
{
51+
$explodedPath = explode('/', $path);
52+
53+
foreach ($explodedPath as $index => $pathSection) {
54+
$explodedPath[$index] = ucfirst($pathSection);
55+
}
56+
57+
return implode('\\', $explodedPath);
58+
}
59+
60+
/**
61+
* Adapts the code for the code generator.
62+
* @param string $defaultCode the default code.
63+
* @return string the adapted code.
64+
*/
65+
private static function adaptCodeForCodeGenerator(string $defaultCode): string
66+
{
67+
// Removes the PHP open tag :
68+
$openTagRemovedCode = preg_replace(
69+
'/<\?php\n/',
70+
'',
71+
$defaultCode
72+
);
73+
74+
// Changes "STDIN" to "$stdin" :
75+
$changedStdinCode = preg_replace(
76+
'/STDIN/',
77+
'$stdin',
78+
$openTagRemovedCode
79+
);
80+
81+
// Adds indentation :
3282
$spaceAddedCode = preg_replace(
3383
'/\n/',
3484
"\n ",
35-
$defaultCode
85+
$changedStdinCode
3686
);
37-
$indentedDefaultCode = preg_replace(
87+
88+
// Removes indentation for empty lines :
89+
return preg_replace(
3890
'/\n \n/',
3991
"\n\n",
4092
$spaceAddedCode
4193
);
42-
43-
return new GeneratorCodeConfiguration(
44-
$parsedConfiguration->getNamespace(),
45-
$parsedConfiguration->getName(),
46-
$parsedConfiguration->getLink(),
47-
$indentedDefaultCode
48-
);
4994
}
5095

96+
5197
/**
5298
* Returns the test configuration for the generator.
5399
* @param \CyrilVerloop\Codingame\Parser\ParsedConfiguration $parsedConfiguration the parsed configuration.
54-
* @return \CyrilVerloop\Codingame\Generator\GeneratorTestConfiguration the test configuration for the generator.
100+
* @return \CyrilVerloop\Codingame\Generator\TestGeneratorConfiguration the test configuration for the generator.
55101
*/
56-
static function getGeneratorTestConfiguration(
102+
static function getTestGeneratorConfiguration(
57103
ParsedConfiguration $parsedConfiguration,
58-
): GeneratorTestConfiguration {
59-
return new GeneratorTestConfiguration(
60-
$parsedConfiguration->getNamespace(),
104+
): TestGeneratorConfiguration {
105+
return new TestGeneratorConfiguration(
106+
self::convertPathToNamespace($parsedConfiguration->getPath()),
61107
$parsedConfiguration->getName(),
62-
$parsedConfiguration->getGroup(),
63-
$parsedConfiguration->getTestConfigurations()
108+
$parsedConfiguration->getAlphanumName(),
109+
self::convertTestConfigurationsForGenerator($parsedConfiguration->getTestConfigurations())
64110
);
65111
}
112+
113+
/**
114+
* Converts the tests configurations.
115+
* @param TestConfigurations $testConfigurations the tests configurations.
116+
* @return \CyrilVerloop\Codingame\Generator\TestConfigurations the tests configurations.
117+
*/
118+
private static function convertTestConfigurationsForGenerator(
119+
TestConfigurations $testConfigurations
120+
): TCsGenerator {
121+
$tCsGenerator = new TCsGenerator();
122+
123+
foreach ($testConfigurations as $testConfiguration) {
124+
$tCGenerator = new TCGenerator(
125+
$testConfiguration->getName(),
126+
$testConfiguration->getAlphanumName(),
127+
ucfirst($testConfiguration->getAlphanumName()),
128+
$testConfiguration->getFile()
129+
);
130+
131+
$tCsGenerator->add($tCGenerator);
132+
}
133+
134+
return $tCsGenerator;
135+
}
66136
}

src/Configuration/TestConfiguration.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ final class TestConfiguration
1717
private string $name;
1818

1919
/**
20-
* @var string the group.
20+
* @var string the alphanumeric name.
2121
*/
22-
private string $group;
23-
24-
/**
25-
* @var string the method.
26-
*/
27-
private string $method;
22+
private string $alphanumName;
2823

2924
/**
3025
* @var string the file.
@@ -37,19 +32,16 @@ final class TestConfiguration
3732
/**
3833
* The constructor.
3934
* @param string $name the name.
40-
* @param string $group the group.
41-
* @param string $method the method.
35+
* @param string $alphanumName the alphanumeric name.
4236
* @param string $file the file.
4337
*/
4438
public function __construct(
4539
string $name,
46-
string $group,
47-
string $method,
40+
string $alphanumName,
4841
string $file
4942
) {
5043
$this->name = $name;
51-
$this->group = $group;
52-
$this->method = $method;
44+
$this->alphanumName = $alphanumName;
5345
$this->file = $file;
5446
}
5547

@@ -66,21 +58,12 @@ public function getName(): string
6658
}
6759

6860
/**
69-
* Returns the group.
70-
* @return string the group.
71-
*/
72-
public function getGroup(): string
73-
{
74-
return $this->group;
75-
}
76-
77-
/**
78-
* Returns the method.
79-
* @return string the method.
61+
* Returns the alphanumeric name.
62+
* @return string the alphanumeric name.
8063
*/
81-
public function getMethod(): string
64+
public function getAlphanumName(): string
8265
{
83-
return $this->method;
66+
return $this->alphanumName;
8467
}
8568

8669
/**

src/Generator/CGCodeGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ final class CGCodeGenerator extends FileGenerator
1414
/**
1515
* The template file name.
1616
*/
17-
private const TEMPLATE_FILE_NAME = 'CGCode.twig';
17+
private const string TEMPLATE_FILE_NAME = 'CGCode.twig';
1818

1919
/**
2020
* The php file name.
2121
*/
22-
private const PHP_FILE_NAME = 'CGCode.php';
22+
private const string PHP_FILE_NAME = 'CGCode.php';
2323

2424

2525
// Methods :
2626

2727
/**
2828
* Generates the code.
29-
* @param \CyrilVerloop\Codingame\Generator\GeneratorCodeConfiguration $configuration the code configuration.
29+
* @param \CyrilVerloop\Codingame\Generator\CodeGeneratorConfiguration $configuration the code configuration.
3030
* @param string $toPath the path where to put the generated code.
3131
*/
3232
public function generate(
33-
GeneratorCodeConfiguration $configuration,
33+
CodeGeneratorConfiguration $configuration,
3434
string $toPath
3535
): void {
3636
$codeFile = $toPath . self::PHP_FILE_NAME;

src/Generator/CGTestGenerator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,34 @@ final class CGTestGenerator extends FileGenerator
1616
/**
1717
* The template file name.
1818
*/
19-
private const TEMPLATE_FILE_NAME = 'CGTest.twig';
19+
private const string TEMPLATE_FILE_NAME = 'CGTest.twig';
2020

2121
/**
2222
* The php file name.
2323
*/
24-
private const PHP_FILE_NAME = 'CGTest.php';
24+
private const string PHP_FILE_NAME = 'CGTest.php';
2525

2626
/**
2727
* The input path.
2828
*/
29-
private const INPUT_PATH = 'input' . DIRECTORY_SEPARATOR;
29+
private const string INPUT_PATH = 'input' . DIRECTORY_SEPARATOR;
3030

3131
/**
3232
* The output path.
3333
*/
34-
private const OUTPUT_PATH = 'output' . DIRECTORY_SEPARATOR;
34+
private const string OUTPUT_PATH = 'output' . DIRECTORY_SEPARATOR;
3535

3636

3737
// Methods :
3838

3939
/**
4040
* Generates the test.
41-
* @param \CyrilVerloop\Codingame\Generator\GeneratorTestConfiguration $configuration the test configuration.
41+
* @param \CyrilVerloop\Codingame\Generator\TestGeneratorConfiguration $configuration the test configuration.
4242
* @param string $fromPath the path where the config file is.
4343
* @param string $toPath the path where to put the generated test.
4444
*/
4545
public function generate(
46-
GeneratorTestConfiguration $configuration,
46+
TestGeneratorConfiguration $configuration,
4747
string $fromPath,
4848
string $toPath
4949
): void {

0 commit comments

Comments
 (0)