Skip to content

Instantly share code, notes, and snippets.

@jalogut
Created July 13, 2017 12:48
Show Gist options
  • Save jalogut/c596e150fe9b364f423f10cf3e21f9c1 to your computer and use it in GitHub Desktop.
Save jalogut/c596e150fe9b364f423f10cf3e21f9c1 to your computer and use it in GitHub Desktop.
Script to automatically init all vcs roots inside a project
#!/usr/bin/env php
<?php
class PhpStormVcsRoots
{
private $rootPath = ".";
private $maxDepth = "4";
private $configPath = ".idea/vcs.xml";
public function updateVcsRootsConfig()
{
file_put_contents($this->configPath, $this->getVcsConfigXml());
}
private function getVcsConfigXml()
{
$vcsConfigXml = $this->getXmlHeader();
$vcsConfigXml .= $this->getXmlVcsRootsBody();
$vcsConfigXml .= $this->getXmlFooter();
return $vcsConfigXml;
}
private function getXmlHeader()
{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<project version=\"4\">
<component name=\"VcsDirectoryMappings\">
";
}
private function getXmlVcsRootsBody()
{
$xmlVcsRootsBody = "";
$iterator = $this->getDirectoryIterator();
foreach ($iterator as $path => $dir) {
if ($dir->isDir() && preg_match('_^\.(/(?:.*))?/\.git$_', $path, $matches)) {
if (!isset($matches[1])) {
$matches[1] = "";
}
$xmlVcsRootsBody .= ' <mapping directory="$PROJECT_DIR$'.$matches[1].'" vcs="Git" />' . PHP_EOL;
}
}
return $xmlVcsRootsBody;
}
private function getDirectoryIterator()
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->rootPath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
$iterator->setMaxDepth($this->maxDepth);
return $iterator;
}
private function getXmlFooter()
{
return " </component>
</project>";
}
}
$phpStormVcsRoots = new PhpStormVcsRoots();
$phpStormVcsRoots->updateVcsRootsConfig();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment