Skip to content

Commit

Permalink
Extend no overwrite feature to all standard markdown files, not just …
Browse files Browse the repository at this point in the history
…routes.
  • Loading branch information
shalvah committed May 12, 2020
1 parent 4d022d0 commit d854a59
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ parameters:
- '#(.*)NunoMaduro\\Collision(.*)#'
- '#Instantiated class Whoops\\Exception\\Inspector not found\.#'
- '#.+Dingo.+#'
- '#Right side of && is always false.#'
36 changes: 21 additions & 15 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Writer
/**
* @var array
*/
private $lastTimesWeModifiedTheseFiles;
private $lastTimesWeModifiedTheseFiles = [];

public function __construct(DocumentationConfig $config = null, bool $shouldOverwrite = false)
{
Expand Down Expand Up @@ -112,10 +112,14 @@ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes)
mkdir($this->sourceOutputPath, 0777, true);
}

$this->fetchLastTimeWeModifiedFilesFromTrackingFile();

$this->writeRoutesMarkdownFile($parsedRoutes, $settings);
$this->writeIndexMarkdownFile($settings);
$this->writeAuthMarkdownFile();

$this->writeModificationTimesTrackingFile();

ConsoleOutputUtils::info('Wrote source Markdown files to: ' . $this->sourceOutputPath);
}

Expand Down Expand Up @@ -298,17 +302,6 @@ protected function writeRoutesMarkdownFile(Collection $parsedRoutes, array $sett
mkdir($this->sourceOutputPath . '/groups', 0777, true);
}

if (file_exists($this->fileModificationTimesFile)) {
$this->lastTimesWeModifiedTheseFiles = explode("\n", file_get_contents($this->fileModificationTimesFile));
array_shift($this->lastTimesWeModifiedTheseFiles);
array_shift($this->lastTimesWeModifiedTheseFiles);
$this->lastTimesWeModifiedTheseFiles = collect($this->lastTimesWeModifiedTheseFiles)
->mapWithKeys(function ($line) {
[$filePath, $mtime] = explode("=", $line);
return [$filePath => $mtime];
})->toArray();
}

// Generate Markdown for each route. Not using a Blade component bc of some complex logic
$parsedRoutesWithOutput = $this->generateMarkdownOutputForEachRoute($parsedRoutes, $settings);
$parsedRoutesWithOutput->each(function ($routesInGroup, $groupName) {
Expand All @@ -335,8 +328,6 @@ protected function writeRoutesMarkdownFile(Collection $parsedRoutes, array $sett
$this->writeFile($routeGroupMarkdownFile, $groupMarkdown);
});

$this->writeModificationTimesFile();

}

/**
Expand All @@ -349,7 +340,7 @@ protected function writeFile(string $filePath, $markdown): void

/**
*/
protected function writeModificationTimesFile(): void
protected function writeModificationTimesTrackingFile(): void
{
$content = "# GENERATED. YOU SHOULDN'T MODIFY OR DELETE THIS FILE.\n";
$content .= "# Scribe uses this file to know when you change something manually in your docs.\n";
Expand All @@ -376,4 +367,19 @@ protected function hasFileBeenModified(string $filePath): bool
return false;
}

protected function fetchLastTimeWeModifiedFilesFromTrackingFile()
{
if (file_exists($this->fileModificationTimesFile)) {
$lastTimesWeModifiedTheseFiles = explode("\n", file_get_contents($this->fileModificationTimesFile));
// First two lines are comments
array_shift($lastTimesWeModifiedTheseFiles);
array_shift($lastTimesWeModifiedTheseFiles);
$this->lastTimesWeModifiedTheseFiles = collect($lastTimesWeModifiedTheseFiles)
->mapWithKeys(function ($line) {
[$filePath, $mtime] = explode("=", $line);
return [$filePath => $mtime];
})->toArray();
}
}

}
25 changes: 18 additions & 7 deletions tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public function sorts_group_naturally()
}

/** @test */
public function will_not_overwrite_modified_markdown_file_unless_force_option_is_set()
public function will_not_overwrite_manually_modified_markdown_files_unless_force_flag_is_set()
{
RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
RouteFacade::get('/api/action1b', TestGroupController::class . '@action1b');
Expand All @@ -401,19 +401,30 @@ public function will_not_overwrite_modified_markdown_file_unless_force_option_is

$this->artisan('scribe:generate');

$file1MtimeAfterFirstGeneration = filemtime(__DIR__ . '/../resources/docs/groups/1-group-1.md');
$file2MtimeAfterFirstGeneration = filemtime(__DIR__ . '/../resources/docs/groups/2-group-2.md');
$group1FilePath = __DIR__ . '/../resources/docs/groups/1-group-1.md';
$group2FilePath = __DIR__ . '/../resources/docs/groups/2-group-2.md';
$authFilePath = __DIR__ . '/../resources/docs/authentication.md';

$file1MtimeAfterFirstGeneration = filemtime($group1FilePath);
$file2MtimeAfterFirstGeneration = filemtime($group2FilePath);
$authFileMtimeAfterFirstGeneration = filemtime($authFilePath);

sleep(1);
touch(__DIR__ . '/../resources/docs/groups/1-group-1.md');
$file1MtimeAfterManualModification = filemtime(__DIR__ . '/../resources/docs/groups/1-group-1.md');
touch($group1FilePath);
touch($authFilePath);
$file1MtimeAfterManualModification = filemtime($group1FilePath);
$authFileMtimeAfterManualModification = filemtime($authFilePath);
$this->assertGreaterThan($file1MtimeAfterFirstGeneration, $file1MtimeAfterManualModification);
$this->assertGreaterThan($authFileMtimeAfterFirstGeneration, $authFileMtimeAfterManualModification);

$this->artisan('scribe:generate');

$file1MtimeAfterSecondGeneration = filemtime(__DIR__ . '/../resources/docs/groups/1-group-1.md');
$file2MtimeAfterSecondGeneration = filemtime(__DIR__ . '/../resources/docs/groups/2-group-2.md');
$file1MtimeAfterSecondGeneration = filemtime($group1FilePath);
$file2MtimeAfterSecondGeneration = filemtime($group2FilePath);
$authFileMtimeAfterSecondGeneration = filemtime($authFilePath);

$this->assertEquals($file1MtimeAfterManualModification, $file1MtimeAfterSecondGeneration);
$this->assertNotEquals($file2MtimeAfterFirstGeneration, $file2MtimeAfterSecondGeneration);
$this->assertNotEquals($authFileMtimeAfterFirstGeneration, $authFileMtimeAfterSecondGeneration);
}
}

0 comments on commit d854a59

Please sign in to comment.