Переглянути джерело

Agregue comando para manipular composer.json.

Luciano Andrade 7 роки тому
батько
коміт
753d701a26
2 змінених файлів з 66 додано та 0 видалено
  1. 1 0
      tools/cmd.php
  2. 65 0
      tools/src/Composer.php

+ 1 - 0
tools/cmd.php

@@ -12,6 +12,7 @@ $app->add(new FD3\GetSource());
 $app->add(new FD3\MergeHostsFile());
 $app->add(new FD3\DockerInventory());
 $app->add(new FD3\ImportClient());
+$app->add(new FD3\Composer());
 $app->add(new FD3\Command\ImportONUCommand());
 $app->add(new FD3\Command\MakeVersionCommand());
 $app->add(new FD3\Command\MakeSubversionCommand());

+ 65 - 0
tools/src/Composer.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace FD3;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Composer extends Command{
+    protected function configure()
+    {
+        $this 
+        ->setName('composer:use')
+
+        ->setDescription('Tag all composer software using a ini file.')
+
+        ->setHelp('This command allows you to tag and change the required code on the composer.json file...')
+
+	 ->addArgument('ini_file', InputArgument::REQUIRED, 'The ini file from where to get the source code config.')
+	 ->addArgument('packages', InputArgument::REQUIRED, 'tag the required packages')
+	 ->addArgument('version', InputArgument::REQUIRED, 'use the version expression.')
+    ;
+    }
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+	$file = $input->getArgument("ini_file");
+	$packages = $input->getArgument("packages");
+	$new_version= $input->getArgument("version");
+	$realpath = realpath($file);
+	$dirname = dirname($realpath);
+	if(!chdir($dirname)){
+		throw new \Exception("Can't change working directory to ".$dirname);
+	}
+	
+	$content = parse_ini_file($realpath, true);
+	foreach($content as $sec => $conf){
+		$composer_file = $sec . "/composer.json";
+		if(file_exists($composer_file)){
+			$output->writeln($sec);
+			$composer_data = json_decode(file_get_contents($composer_file), true);
+			foreach(array("require", "require-dev") as $k){
+				foreach($composer_data[$k] as $package => $version){
+					if(strpos($packages, "*") !== false){
+						$preg = "|". str_replace("*", "[^\b]*", $packages) . "|";
+						if(preg_match($preg, $package)){
+							$composer_data[$k][$package] = (string)$new_version; 
+							$output->writeln("\t". $package . " = ". $new_version);
+						}
+					}else{
+						if(strcmp($packages, $package) === 0){
+							$composer_data[$k][$package] = (string)$new_version; 
+							$output->writeln("\t". $package . " = ". $new_version);
+						}
+					}
+				}
+			}
+			file_put_contents($composer_file, json_encode($composer_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
+		}else{
+			$output->writeln($sec . " no composer.json found");
+		}
+	}
+    }
+}