Skip to content

generate service

Create a service trio (protocol + impl + mock) and register it in DI.

Creates a service in three files (protocol, real implementation, mock) and registers it in the DI container automatically.

Terminal window
swiftspawn generate service <Name> [--with-tests] [--dry-run] [--force] [--quiet] [--verbose]

A service is one resource’s behavior. It’s always emitted as three files so tests, previews, and production code each have the implementation they need.

The Service suffix is optional; both generate service Movie and generate service MovieService produce the same files. Internally the canonical name is Movie and the protocol is MovieService.

The implementation has // TODO: markers where you write the real URLSession / API calls. The mock returns canned values suitable for previews. The protocol has a single placeholder method (fetch() by default). For a CRUD-shaped service, use recipe crud instead.

Terminal window
swiftspawn generate service Movie
swiftspawn generate service UserService --with-tests
Services/<Name>Service.swift
Services/<Name>ServiceImpl.swift
Services/Mock<Name>Service.swift

With --with-tests:

Tests/<App>Tests/Services/<Name>ServiceTests.swift
  • Registers the service factory in DIContainer.swift between markers:

    // MARK: - Service Factories (auto-generated)
    var movieService: Factory<MovieService> { self { MovieServiceImpl() } }
    // MARK: - End auto-generated

If DIContainer.swift doesn’t have those markers (legacy projects), the generator prints the snippet to paste in instead.

FlagDescription
--with-testsAlso emit a service test file.
--dry-runPreview without writing.
--forceOverwrite existing files.
CodeMeaning
2Invalid name.
5Not in a swiftspawn project.