From 47d5ad7193a9ea8107e284521fcc81c56e374545 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Mon, 27 Nov 2023 09:01:51 +0800 Subject: [PATCH] Add gen copyright code tool --- bin/copyright-code | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 bin/copyright-code diff --git a/bin/copyright-code b/bin/copyright-code new file mode 100755 index 0000000..c94c239 --- /dev/null +++ b/bin/copyright-code @@ -0,0 +1,47 @@ +#!/usr/bin/env php +in(dirname(__DIR__)) + ->exclude(['bin', 'docs', 'tests', 'examples']) + ->ignoreDotFiles(true) + ->ignoreVCSIgnored(true) + ->notName(['test.php', 'code.txt']) + ->name('*.php') + ->sortByName() + ->files(); + +file_put_contents('code.txt', ''); + +foreach ($files as $file) { + file_put_contents('code.txt', removeCommentsAndEmptyLines($file), FILE_APPEND); +} + +function removeCommentsAndEmptyLines($filePath) +{ + $cleanCode = ''; + $fileLines = file($filePath); + + foreach ($fileLines as $line) { + $trimmedLine = trim($line); + + // Skip comment lines + if (strpos($trimmedLine, '//') === 0 || strpos($trimmedLine, '/*') === 0 || strpos( + $trimmedLine, + '*' + ) === 0 || strpos($trimmedLine, '*/') === 0) { + continue; + } + + // Skip empty lines + if ($trimmedLine !== '') { + $cleanCode .= $line; + } + } + + return $cleanCode; +}