From 97d622af45e628d3d0e102591a4ff87a19940939 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 16 Aug 2023 13:59:55 +0200 Subject: [PATCH] wip #6195 @1 --- app/Fields/QuizDevelopmentVersion.php | 37 ++++++ app/Models/Traits/FluidbookPlayerBranches.php | 110 +--------------- app/Models/Traits/QuizBranches.php | 17 +++ app/Models/Traits/ToolboxPlayerBranches.php | 120 ++++++++++++++++++ 4 files changed, 177 insertions(+), 107 deletions(-) create mode 100644 app/Fields/QuizDevelopmentVersion.php create mode 100644 app/Models/Traits/QuizBranches.php create mode 100644 app/Models/Traits/ToolboxPlayerBranches.php diff --git a/app/Fields/QuizDevelopmentVersion.php b/app/Fields/QuizDevelopmentVersion.php new file mode 100644 index 000000000..a43963571 --- /dev/null +++ b/app/Fields/QuizDevelopmentVersion.php @@ -0,0 +1,37 @@ +_getOptions(); + } + return self::$__options; + } + + protected function _getOptions() + { + $versions = $this->getActiveBranches(); + $res = ['stable' => 'master : git (stable)', + 'dev' => 'master : local (dev)']; + + foreach ($versions as $version) { + if ($version === 'master') { + continue; + } + $res[$version . '|git'] = $version . ' : git'; + $res[$version . '|local'] = $version . ' : local'; + } + return $res; + } +} diff --git a/app/Models/Traits/FluidbookPlayerBranches.php b/app/Models/Traits/FluidbookPlayerBranches.php index b060513c3..e659af7b9 100644 --- a/app/Models/Traits/FluidbookPlayerBranches.php +++ b/app/Models/Traits/FluidbookPlayerBranches.php @@ -8,117 +8,13 @@ use Cubist\Util\PHP; trait FluidbookPlayerBranches { - protected function executeGitCommands($dir, $commands) - { - if (!is_array($commands)) { - $commands = [$commands]; - } - $res = []; - foreach ($commands as $command) { - $res[] = $this->executeGitCommand($dir, $command); - } - return $res; - } - protected function executeGitCommand($dir, $command) - { - $git = new Git($dir); - $output = $git->executeCmd($command); + use ToolboxPlayerBranches; - if (method_exists($this, 'line')) { - $this->line('' . $git->getCommand() . ''); - $this->line($output); - } - return $output; - } + protected static $reposName = 'fluidbook-html5'; - protected static function getFluidbookPlayerBaseDirectory() + protected static function getPlayerBaseDirectory() { return Files::mkdir(resource_path('fluidbookpublication/player')); } - - /** - * @return array - */ - protected function getActiveBranches() - { - $cacheFile = self::getFluidbookPlayerBaseDirectory() . 'activebranches'; - $res = json_decode(file_get_contents($cacheFile), true); - if (!$res) { - return self::updateAllBranches(); - } - return $res; - } - - protected static function reposDirectory() - { - return self::getFluidbookPlayerBaseDirectory() . 'repos/fluidbook-html5'; - } - - protected function fluidbookCreateBranch($branch) - { - $this->updateAllBranches(); - - $dir = self::reposDirectory(); - $this->executeGitCommands($dir, ['checkout -b ' . $branch, 'push --set-upstream origin ' . $branch]); - - $this->updateAllBranches(); - $this->executeGitCommand($dir, 'checkout master'); - - $this->updateAllBranches(); - return in_array($branch, $this->getActiveBranches()); - } - - protected function fluidbookRemoveBranch($branch) - { - $playerDir = self::getFluidbookPlayerBaseDirectory(); - $branchesDir = $playerDir . 'branches/'; - $localDir = $playerDir . 'local/'; - - $this->updateAllBranches(); - $this->executeGitCommand(self::reposDirectory(), 'push --delete origin ' . $branch); - - `rm -rf $localDir$branch`; - `rm -rf $branchesDir$branch`; - - // TODO modifier la branche des fluidbooks qui utilisaient cette branche (self::resetPlayerVersion($branch);) - - $this->updateAllBranches(); - return !in_array($branch, $this->getActiveBranches()); - } - - /** - * @return [] - */ - protected function updateAllBranches() - { - PHP::neverStop(); - - $playerDir = self::getFluidbookPlayerBaseDirectory(); - $branchesDir = Files::mkdir($playerDir . 'branches/'); - $localDir = Files::mkdir($playerDir . 'local/'); - $repos = self::reposDirectory(); - - $this->executeGitCommands($repos, ['stash save --keep-index', 'stash drop', 'pull --all', 'fetch --all --prune']); - - $git = new Git($repos); - $branches = $git->listBranches(); - file_put_contents($playerDir . '/activebranches', json_encode($branches)); - - foreach ($branches as $b) { - $gitsource = $branchesDir . $b; - $local = $localDir . $b; - if (!file_exists($branchesDir . $b)) { - $this->executeGitCommands($branchesDir, 'clone -b ' . $b . ' --single-branch git@git.cubedesigners.com:fluidbook-html5.git ' . $b); - } - if (!file_exists($local)) { - mkdir($local, 0777, true); - `cp -r $gitsource/* $local`; - `rm -rf $local/.git`; - } - $this->executeGitCommands($branchesDir . $b, ['reset --hard origin/' . $b, 'pull']); - } - - return $branches; - } } diff --git a/app/Models/Traits/QuizBranches.php b/app/Models/Traits/QuizBranches.php new file mode 100644 index 000000000..f1047af06 --- /dev/null +++ b/app/Models/Traits/QuizBranches.php @@ -0,0 +1,17 @@ +updateAllBranches(); + + $dir = self::reposDirectory(); + $this->executeGitCommands($dir, ['checkout -b ' . $branch, 'push --set-upstream origin ' . $branch]); + + $this->updateAllBranches(); + $this->executeGitCommand($dir, 'checkout master'); + + $this->updateAllBranches(); + return in_array($branch, $this->getActiveBranches()); + } + + protected function fluidbookRemoveBranch($branch) + { + $playerDir = self::getPlayerBaseDirectory(); + $branchesDir = $playerDir . 'branches/'; + $localDir = $playerDir . 'local/'; + + $this->updateAllBranches(); + $this->executeGitCommand(self::reposDirectory(), 'push --delete origin ' . $branch); + + `rm -rf $localDir$branch`; + `rm -rf $branchesDir$branch`; + + // TODO modifier la branche des fluidbooks qui utilisaient cette branche (self::resetPlayerVersion($branch);) + + $this->updateAllBranches(); + return !in_array($branch, $this->getActiveBranches()); + } + + /** + * @return [] + */ + protected function updateAllBranches() + { + PHP::neverStop(); + + $playerDir = self::getPlayerBaseDirectory(); + $branchesDir = Files::mkdir($playerDir . 'branches/'); + $localDir = Files::mkdir($playerDir . 'local/'); + $repos = self::reposDirectory(); + + $this->executeGitCommands($repos, ['stash save --keep-index', 'stash drop', 'pull --all', 'fetch --all --prune']); + + $git = new Git($repos); + $branches = $git->listBranches(); + file_put_contents($playerDir . '/activebranches', json_encode($branches)); + + foreach ($branches as $b) { + $gitsource = $branchesDir . $b; + $local = $localDir . $b; + if (!file_exists($branchesDir . $b)) { + $this->executeGitCommands($branchesDir, 'clone -b ' . $b . ' --single-branch git@git.cubedesigners.com:' . self::$reposName . '.git ' . $b); + } + if (!file_exists($local)) { + mkdir($local, 0777, true); + `cp -r $gitsource/* $local`; + `rm -rf $local/.git`; + } + $this->executeGitCommands($branchesDir . $b, ['reset --hard origin/' . $b, 'pull']); + } + + return $branches; + } + + protected function executeGitCommands($dir, $commands) + { + if (!is_array($commands)) { + $commands = [$commands]; + } + $res = []; + foreach ($commands as $command) { + $res[] = $this->executeGitCommand($dir, $command); + } + return $res; + } + + protected function executeGitCommand($dir, $command) + { + $git = new Git($dir); + $output = $git->executeCmd($command); + + if (method_exists($this, 'line')) { + $this->line('' . $git->getCommand() . ''); + $this->line($output); + } + return $output; + } +} -- 2.39.5