Migrate Syrup to use swift-argument-parser
Currently there a lot of warnings of the following nature:
'PathArgument' is deprecated: use swift-argument-parser instead
This PR migrates swift-argument-parser. There are a few benefits we can use with this migration as well:
- Easier to add commands and subcommands
- We can test if the parsing works as expected
- Automatic help message and usage description updates
Easier to add commands and subcommands
The way Swift Argument parser works is that each command and subcommand conforms to ParsableCommand. In the configuration property, we can provide subcommands. And we can provide subcommands to the subcommand. So for example, we could refactor our Generate,GenerateModels and GenerateSupportFiles into one NewAndImprovedGenerate subcommand that handles all of these use cases (and more):
struct NewAndImprovedGenerate: ParsableCommand {
enum GraphQLTypes: String, EnumerableFlag {
case query, response, fragment, mutation, subscription, input, `enum`
}
@Flag var types: [GraphQLTypes]
mutating func run() throws {
// generate the files based on the types given
}
}
Then, we could generate the our queries, fragments and mutations with
$ syrup generate --query --fragment --mutation
Side note
I was attempting to add such functionality because of the noise that Syrup can generate. But Syrup's current model depends on generating all types within the folder. I was able to restrict that to certain.graphql files that changed by passing them in. However, Syrup was continuing to regenerate input types and enum types.
Relevant branch: syrup/hack-days/single-graphql-file-generation/atf
We can test if the parsing works as expected
We can easily add tests for parsing (see the test I added).
Automatic help messages and usage description updates
There are added benefits to this migration in the help text for Syrup that are now out of the box. These are automatically generated when we provide help messages for our commands. For example,
Before
$ syrup generate --help
OVERVIEW: Runs the generator. Expected positional arguments in the ordering of <Operations Location> <Models Destination> <Support Files Destination> <Template>
OPTIONS:
--override-schema Overrides the projects schema location. Can be either a URL or a path on the filesystem.
--project YAML project configuration file
--reports Deprecation report target output file.
--schema YAML schema configuration file
POSITIONAL ARGUMENTS:
queries This provides the path to the folder containing all of your graphql operation definitions. These files are expected to have the .graphql suffix and should be valid graphql operations.
destination This provides the path to the folder where the generated code should be written to.
supportFilesDestination This provides the path to the folder where the necessary support files should be written to.
template This provides the path to the Templates folder that are included in the Syrup repository.
After
$ syrup generate --help
OVERVIEW: Generates all models and support files.
USAGE: syrup generate <queries> <destination> <support-files> <template> --project <project> --schema <schema> --override-schema <override-schema> --reports <reports>
ARGUMENTS:
<queries> The directory path containing all of your GraphQL operation definitions.
These files must have a file extension matching graphql.
They must also be valid graphql operations.
<destination> The directory path syrup puts generated code in.
<support-files> The directory path syrup puts support files in.
<template> The directory path syrup uses to generate your GraphQL files.
OPTIONS:
--project <project> YAML project configuration file
--schema <schema> YAML schema configuration file
--override-schema <override-schema>
The URL or filesystem path which overrides the project's schema location
--reports <reports> The file path syrup uses puts the deprecation report ing.
--version Show the version.
-h, --help Show help information.