Exakat\Tasks\Initproject::init_project PHP Метод

init_project() приватный Метод

private init_project ( $project, $repositoryURL )
    private function init_project($project, $repositoryURL)
    {
        if (!file_exists($this->config->projects_root . '/projects/' . $project)) {
            mkdir($this->config->projects_root . '/projects/' . $project, 0755);
        } else {
            display($this->config->projects_root . '/projects/' . $project . ' already exists. Reusing' . "\n");
        }
        if (!file_exists($this->config->projects_root . '/projects/' . $project . '/log/')) {
            mkdir($this->config->projects_root . '/projects/' . $project . '/log/', 0755);
        } else {
            display($this->config->projects_root . '/projects/' . $project . '/log/ already exists. Ignoring' . "\n");
            return null;
        }
        $this->datastore = new Datastore(Config::factory(), Datastore::CREATE);
        if (!file_exists($this->config->projects_root . '/projects/' . $project . '/config.ini')) {
            if ($this->config->symlink === true) {
                $vcs = 'symlink';
            } elseif ($this->config->svn === true) {
                $vcs = 'svn';
            } elseif ($this->config->git === true) {
                $vcs = 'git';
            } elseif ($this->config->copy === true) {
                $vcs = 'copy';
            } elseif ($this->config->bzr === true) {
                $vcs = 'bzr';
            } elseif ($this->config->hg === true) {
                $vcs = 'hg';
            } elseif ($this->config->composer === true) {
                $vcs = 'composer';
            } else {
                $vcs = 'git';
            }
            // default initial config. Found in test project.
            $configIni = <<<INI
phpversion = 7.1

ignore_dirs[] = /test
ignore_dirs[] = /tests
ignore_dirs[] = /Tests
ignore_dirs[] = /Test
ignore_dirs[] = /example
ignore_dirs[] = /examples
ignore_dirs[] = /docs
ignore_dirs[] = /doc
ignore_dirs[] = /tmp
ignore_dirs[] = /version
ignore_dirs[] = /vendor
ignore_dirs[] = /js
ignore_dirs[] = /lang
ignore_dirs[] = /data
ignore_dirs[] = /css
ignore_dirs[] = /cache
ignore_dirs[] = /vendor
ignore_dirs[] = /assets
ignore_dirs[] = /spec
ignore_dirs[] = /sql

file_extensions =

project_name        = "{$project}";
project_url         = "{$repositoryURL}";
project_vcs         = "{$vcs}";
project_description = "";
project_packagist   = "";

INI;
            file_put_contents($this->config->projects_root . '/projects/' . $project . '/config.ini', $configIni);
        } else {
            display($this->config->projects_root . '/projects/' . $project . '/config.ini already exists. Ignoring' . "\n");
        }
        shell_exec('chmod -R g+w ' . $this->config->projects_root . '/projects/' . $project);
        $repositoryDetails = parse_url($repositoryURL);
        $skipFiles = false;
        if (!file_exists($this->config->projects_root . '/projects/' . $project . '/code/')) {
            switch (true) {
                // Symlink
                case $this->config->symlink === true:
                    display('Symlink initialization : ' . realpath($repositoryURL));
                    symlink(realpath($repositoryURL), $this->config->projects_root . '/projects/' . $project . '/code');
                    break 1;
                    // Empty initialization
                // Empty initialization
                case $this->config->copy === true:
                    display('Copy initialization');
                    $total = copyDir(realpath($repositoryURL), $this->config->projects_root . '/projects/' . $project . '/code');
                    display($total . ' files were copied');
                    break 1;
                    // Empty initialization
                // Empty initialization
                case $repositoryURL === '' || $repositoryURL === false:
                    display('Empty initialization');
                    break 1;
                    // composer archive (early in the list, as this won't have 'scheme'
                // composer archive (early in the list, as this won't have 'scheme'
                case $this->config->composer === true:
                    display('Initialization with composer');
                    $res = shell_exec('composer --version');
                    if (strpos($res, 'Composer') === false) {
                        throw new HelperException('Composer');
                    }
                    // composer install
                    $composer = new \stdClass();
                    $composer->require = new \stdClass();
                    $composer->require->{$repositoryURL} = 'dev-master';
                    $json = json_encode($composer);
                    mkdir($this->config->projects_root . '/projects/' . $project . '/code', 0755);
                    file_put_contents($this->config->projects_root . '/projects/' . $project . '/code/composer.json', $json);
                    shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '/code; composer -q install');
                    break 1;
                    // SVN
                // SVN
                case isset($repositoryDetails['scheme']) && $repositoryDetails['scheme'] == 'svn' || $this->config->svn === true:
                    $res = shell_exec('svn --version');
                    if (strpos($res, 'svn') === false) {
                        throw new HelperException('SVN');
                    }
                    display('SVN initialization');
                    shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; svn checkout ' . escapeshellarg($repositoryURL) . ' code');
                    break 1;
                    // Bazaar
                // Bazaar
                case $this->config->bzr === true:
                    $res = shell_exec('bzr --version');
                    if (strpos($res, 'Bazaar') === false) {
                        throw new HelperException('Bazar');
                    }
                    display('Bazaar initialization');
                    shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; bzr branch ' . escapeshellarg($repositoryURL) . ' code');
                    break 1;
                    // HG
                // HG
                case $this->config->hg === true:
                    $res = shell_exec('hg --version');
                    if (strpos($res, 'Mercurial') === false) {
                        throw new HelperException('Mercurial');
                    }
                    display('Mercurial initialization');
                    shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; hg clone ' . escapeshellarg($repositoryURL) . ' code');
                    break 1;
                    // Tbz archive
                // Tbz archive
                case $this->config->tbz === true:
                    display('Download the tar.bz2');
                    $binary = file_get_contents($repositoryURL);
                    display('Saving');
                    $archiveFile = tempnam(sys_get_temp_dir(), 'archiveTgz') . '.tgz';
                    file_put_contents($archiveFile, $binary);
                    display('Unarchive');
                    shell_exec('tar -jxf ' . $archiveFile . ' --directory ' . $this->config->projects_root . '/projects/' . $project . '/code/');
                    display('Cleanup');
                    unlink($archiveFile);
                    break 1;
                    // tgz archive
                // tgz archive
                case $this->config->tgz === true:
                    display('Download the tar.gz');
                    $binary = file_get_contents($repositoryURL);
                    display('Saving');
                    $archiveFile = tempnam(sys_get_temp_dir(), 'archiveTgz') . '.tgz';
                    file_put_contents($archiveFile, $binary);
                    display('Unarchive');
                    mkdir($this->config->projects_root . '/projects/' . $project . '/code/');
                    shell_exec('tar -zxf ' . $archiveFile . ' -C ' . $this->config->projects_root . '/projects/' . $project . '/code/');
                    display('Cleanup');
                    unlink($archiveFile);
                    break 1;
                    // zip archive
                // zip archive
                case $this->config->zip === true:
                    $res = shell_exec('zip --version');
                    if (strpos($res, 'Zip') === false) {
                        throw new HelperException('zip');
                    }
                    display('Download the zip');
                    $binary = file_get_contents($repositoryURL);
                    display('Saving');
                    $archiveFile = tempnam(sys_get_temp_dir(), 'archiveZip') . '.zip';
                    file_put_contents($archiveFile, $binary);
                    display('Unzip');
                    shell_exec('unzip ' . $archiveFile . ' -d ' . $this->config->projects_root . '/projects/' . $project . '/code/');
                    display('Cleanup');
                    unlink($archiveFile);
                    break 1;
                    // Git
                    // Git is last, as it will act as a default
                // Git
                // Git is last, as it will act as a default
                case isset($repositoryDetails['scheme']) && $repositoryDetails['scheme'] == 'git' || $this->config->git === true:
                    $res = shell_exec('git --version');
                    if (strpos($res, 'git') === false) {
                        throw new HelperException('git');
                    }
                    display('Git initialization');
                    $res = shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; git clone -q ' . $repositoryURL . ' code 2>&1 ');
                    if (($offset = strpos($res, 'fatal: ')) !== false) {
                        $this->datastore->addRow('hash', array('init error' => trim(substr($res, $offset + 7))));
                        display("An error prevented code initialization : " . trim(substr($res, $offset + 7)) . "\nNo code was loaded.\n");
                        $skipFiles = true;
                    }
                    break 1;
                default:
                    display('No Initialization');
            }
        } elseif (file_exists($this->config->projects_root . '/projects/' . $project . '/code/')) {
            display("Code folder is already there. Leaving it intact.\n");
        }
        display("Counting files\n");
        $this->datastore->addRow('hash', array('status' => 'Initproject'));
        if (!$skipFiles) {
            display("Running files\n");
            $analyze = new Files($this->gremlin, $this->config);
            $analyze->run();
            unset($analyze);
        }
    }