adachsoft / repo-gen
Tools for repository generation
Requires
- php: ^8.3
- adachsoft/git-platform: ^0.3
- adachsoft/gitlib: ^2.0 || ^3.0 || ^4.0
- adachsoft/packagist-api-client: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.1
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
- rector/rector: ^2.3
- symplify/phpstan-rules: ^14.9
- vlucas/phpdotenv: ^5.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Repo Gen is a PHP library for bootstrapping new Git repositories for PHP libraries.
It automates repository setup from templates, supports GitLab and GitHub through
adachsoft/git-platform, and can optionally register packages on Packagist.
The core entry point is RepoGenFacadeInterface, which supports repository initialization,
remote repository creation and member management, and Packagist package creation.
Features
- Initialize a new Git repository from filesystem templates
- Choose a custom project path, a per-repository template directory, or skip template copying
- Create remote repositories on GitLab or GitHub through
adachsoft/git-platform - Configure multiple hosting accounts and select a platform for each operation
- Initialize a local Git repository, commit generated files and push to the remote
- Register the repository as a package on Packagist
- Add repository members and accept GitHub invitations
- Use SSH host aliases from
~/.ssh/configfor Git remotes - Simple factory for wiring all dependencies (
RepoGenFacadeFactory) - CLI helper script
bin_dev/initialize_repositoryfor quick manual usage
Installation
Install via Composer:
composer require adachsoft/repo-gen
Note: The package name above assumes the library is published as
adachsoft/repo-gen. Adjust it if your fork uses a different vendor or package name.
Requirements
- PHP
^8.3, as required bycomposer.json - Git installed and available on the command line
- An access token for every GitLab or GitHub platform account you use
- Optional: Packagist account and API token for automatic package registration
Basic usage (Facade)
The main entry point is AdachSoft\RepoGen\RepoGenFacadeInterface. You can obtain
an instance using the RepoGenFacadeFactory and a configuration DTO:
use AdachSoft\GitPlatform\Infrastructure\GitHub\Enum\GitHubOwnerTypeEnum;
use AdachSoft\GitPlatform\Infrastructure\GitHub\GitHubConfigDto;
use AdachSoft\GitPlatform\Infrastructure\GitLab\GitLabConfigDto;
use AdachSoft\RepoGen\Config\PlatformConfigDto;
use AdachSoft\RepoGen\Config\RepoGenConfigDto;
use AdachSoft\RepoGen\Dto\CreatePackagistPackageDto;
use AdachSoft\RepoGen\Dto\InitRepositoryDto;
use AdachSoft\RepoGen\Factory\RepoGenFacadeFactory;
$platforms = [
'gitlab' => new PlatformConfigDto(
gitPlatformConfig: new GitLabConfigDto(
baseUrl: getenv('GIT_LAB_BASE_URL') ?: 'https://gitlab.com',
accessToken: getenv('GIT_LAB_KEY') ?: '',
),
namespace: getenv('GIT_LAB_NAMESPACE') ?: 'your-gitlab-namespace',
sshHost: getenv('GIT_LAB_SSH_HOST') ?: 'gitlab.com',
),
'github' => new PlatformConfigDto(
gitPlatformConfig: new GitHubConfigDto(
accessToken: getenv('GITHUB_TOKEN') ?: '',
owner: 'your-github-owner',
ownerType: GitHubOwnerTypeEnum::USER,
),
namespace: 'your-github-owner',
sshHost: getenv('GITHUB_SSH_HOST') ?: 'github.com',
),
'github_ai' => new PlatformConfigDto(
gitPlatformConfig: new GitHubConfigDto(
accessToken: getenv('GITHUB_AI_TOKEN') ?: '',
owner: 'your-github-owner',
ownerType: GitHubOwnerTypeEnum::USER,
),
namespace: 'your-github-owner',
sshHost: 'github-ai',
),
];
$config = new RepoGenConfigDto(
basePath: '/var/www',
platforms: $platforms,
defaultPlatform: 'gitlab',
packagistUsername: getenv('PACKAGIST_USERNAME') ?: '',
packagistApiToken: getenv('PACKAGIST_API_TOKEN') ?: '',
templateDirectory: __DIR__ . '/var/templates/php-library-default',
defaultVisibility: 'public',
);
$facade = RepoGenFacadeFactory::createWithDependencies($config)->create();
// Initialize repository (local + remote)
$repositoryDto = $facade->initializeRepository(
new InitRepositoryDto(
name: 'my-library',
description: 'My first library generated with Repo Gen',
visibility: 'public',
),
);
// Optionally register on Packagist
$facade->createPackagistPackage(
new CreatePackagistPackageDto(
repositoryUrl: $repositoryDto->url ?? 'https://gitlab.com/your-namespace/my-library.git',
),
);
Platforms and SSH host aliases
Platform identifiers are arbitrary keys in the platforms map; names such as gitlab, github, and github_ai are examples. A platform's namespace prefixes bare repository names when Repo Gen resolves a remote repository identifier. The configured sshHost is used verbatim in the SSH remote URL, so it can be an alias from ~/.ssh/config.
For example, this SSH configuration maps the github-ai alias to GitHub while selecting a dedicated key:
Host github-ai
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_ai
IdentitiesOnly yes
Use sshHost: 'github-ai' for the corresponding PlatformConfigDto entry to create remotes such as git@github-ai:owner/repository.git.
Choosing a platform for each operation
InitRepositoryDto, CreateRemoteRepositoryDto, and AddRemoteRepositoryMemberDto each accept an optional platform identifier. Omitting it (null) selects RepoGenConfigDto::defaultPlatform. A platform identifier that is not configured results in AdachSoft\RepoGen\Exception\UnknownPlatformException when the platform registry resolves it.
use AdachSoft\RepoGen\Dto\AddRemoteRepositoryMemberDto;
use AdachSoft\RepoGen\Dto\CreateRemoteRepositoryDto;
use AdachSoft\RepoGen\Dto\InitRepositoryDto;
$facade->createRemoteRepository(
new CreateRemoteRepositoryDto(name: 'my-github-library', platform: 'github'),
);
$facade->initializeRepository(
new InitRepositoryDto(name: 'my-gitlab-library', platform: 'gitlab'),
);
$facade->addRemoteRepositoryMember(
new AddRemoteRepositoryMemberDto(
repositoryId: 'my-library',
username: 'contributor',
platform: 'github',
),
);
Custom project paths and templates
InitRepositoryDto accepts optional projectPath, templateDirectory, and skipTemplate arguments:
$repositoryDto = $facade->initializeRepository(
new InitRepositoryDto(
name: 'my-node-library',
projectPath: '/var/www/my-node-library',
templateDirectory: __DIR__ . '/templates/node-library',
),
);
projectPath is the absolute destination directory and can be outside the configured basePath. When omitted, Repo Gen uses <basePath>/<name>, preserving the default behavior. templateDirectory selects a filesystem template directory for this initialization; when omitted, the configured template provider is used.
Set skipTemplate: true to skip template loading and copying entirely, including when templateDirectory is also set:
$facade->initializeRepository(
new InitRepositoryDto(
name: 'generated-by-workflow',
projectPath: '/var/www/generated-by-workflow',
skipTemplate: true,
),
);
Creating only a remote repository
To create a remote repository without creating a local directory or running git init, commit, or push, use createRemoteRepository():
use AdachSoft\RepoGen\Dto\CreateRemoteRepositoryDto;
use AdachSoft\RepoGen\Factory\RepoGenFacadeFactory;
$facade = RepoGenFacadeFactory::createWithDependencies($config)->create();
$remoteRepository = $facade->createRemoteRepository(
new CreateRemoteRepositoryDto(
name: 'my-lib',
visibility: 'public',
),
);
echo $remoteRepository->sshRemoteUrl;
This method creates only the remote repository. It does not create a local directory or a commit. The defaultBranch is null until the first push creates a branch on the hosting service. The returned RemoteRepositoryDto includes the resolved platform identifier.
Adding a member to a remote repository
addRemoteRepositoryMember() returns a RemoteRepositoryMemberResultDto containing the resolved platform, repository ID, username, status (added or invited), and an optional invitation ID:
use AdachSoft\RepoGen\Dto\AddRemoteRepositoryMemberDto;
$result = $facade->addRemoteRepositoryMember(
new AddRemoteRepositoryMemberDto(
repositoryId: 'my-library',
username: 'contributor',
role: 'developer',
platform: 'github',
),
);
echo $result->status;
Supported roles are guest, reporter, developer, maintainer (the default), and owner.
When repositoryId is a repository name without a path or numeric ID, Repo Gen prefixes
the selected platform's namespace. Full repository paths and numeric IDs are used as provided.
GitHub creates an invitation for a user who is not already a collaborator, so the result has
status invited and can include an invitationId.
Accept an invitation using the configured platform entry that contains the invited member's token:
use AdachSoft\RepoGen\Dto\AcceptRemoteRepositoryInvitationDto;
$facade->acceptRemoteRepositoryInvitation(
new AcceptRemoteRepositoryInvitationDto(
repositoryId: 'repository-owner/my-library',
platform: 'github_ai',
),
);
For a member account, configure that platform entry's namespace as the repository owner or pass
a full owner/name repository ID as above. GitLab does not support invitation acceptance; the operation reports RemoteRepositoryMemberException
through its invitationNotSupported factory. Invalid arguments, unsupported roles, and provider failures
are also reported through RemoteRepositoryMemberException.
CLI helper
For quick manual usage there is a CLI helper script. The selected platform defaults to gitlab:
php bin_dev/initialize_repository \
--base_path=/var/www \
--name=my-library \
--description="My awesome library" \
--visibility=public \
--platform=github
The script reads configuration from environment variables (optionally via .env):
GIT_LAB_KEY– GitLab access token.GIT_LAB_BASE_URL– GitLab API base URL; defaults tohttps://gitlab.com.GIT_LAB_NAMESPACE– GitLab namespace; falls back toPACKAGIST_USERNAMEand is required if that is empty.GIT_LAB_SSH_HOST– GitLab SSH host or alias; defaults togitlab.com.GITHUB_TOKEN– when set, enables the GitHub platform and requiresGITHUB_OWNER.GITHUB_OWNER– GitHub user or organization that owns the repositories.GITHUB_OWNER_TYPE–userororganization; defaults touser.GITHUB_SSH_HOST– GitHub SSH host or alias; defaults togithub.com.PACKAGIST_USERNAMEandPACKAGIST_API_TOKEN– Packagist credentials; the username is also the GitLab namespace fallback.--platform=<id>– selects a configured platform for initialization; defaults togitlab.
The helper builds the configured platform map, calls RepoGenFacadeInterface::initializeRepository(),
and prints the resolved platform and SSH remote URL.
Upgrading from 0.5
- Replace the GitLab-only
RepoGenConfigDtoarguments (gitLabBaseUrl,gitLabAccessToken,gitLabNamespace, andgitLabSshHost) with theplatformsmap anddefaultPlatform. - Replace
GitLabSshRemoteUrlResolverwithSshRemoteUrlResolver; customRemoteUrlResolverInterface::resolve()implementations must acceptPlatformConfigDto. - Update code using
addRemoteRepositoryMember(), which now returnsRemoteRepositoryMemberResultDto, and customRemoteRepositoryDtoconstruction, which now requires aplatformvalue. - The package now requires
adachsoft/git-platform^0.3.
Versioning
This project follows Semantic Versioning.
The first tagged release is 0.1.0, which introduces the initial public API
around RepoGenFacade and its supporting services.