-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Add deno generate
subcommand
#19176
Comments
This feels a lot like a task runner, but we already have |
I think is very doable to at least move forward with |
- Add `examples/embedder`. - Move docs example to `examples/docs`. - Fix README.md typos. - Add new FAQ section. - Add sections from denoland/deno#19176: "Use cases", "Conventions", "Examples".
- Add `examples/embedder`. - Move docs example to `examples/docs`. - Fix README.md typos. - Add new FAQ section. - Add sections from denoland/deno#19176: "Use cases", "Conventions", "Examples".
* Transferred initial source from Dengen. Context: <EthanThatOneKid/dengen#2 (review)>. * Print help text. * Add `check.yaml` workflow * Line/column numbers from `bigint` to `number` Relevant: <#1 (comment)>. - Add `examples/embedder`. - Fix README.md typos. - Add new FAQ section. - Add sections from denoland/deno#19176: "Use cases", "Conventions", "Examples". * Update examples and README. - Add `examples/embedder`. - Move docs example to `examples/docs`. - Fix README.md typos. - Add new FAQ section. - Add sections from denoland/deno#19176: "Use cases", "Conventions", "Examples". * Update `check.yaml` script * `examples/docs`: Target `with_generate_docs.ts`. * Replace `examples/docs` with `examples/typescript` * Add contributing guidelines inspired by Deno Inspired by: https://deno.com/manual/references/contributing. * Fix README typos * Update .gitattributes
The userland script is now available under https://deno.land/x/generate! Check it out: deno task generate
|
@EthanThatOneKid I'm agree there is need for generating code, myself I created small tool for this perpose: https://github.com/elycheikhsmail/templatify-code. |
Thank you for sharing your project templatify-code, @elycheikhsmail! templatify-code is a tool which seems to be compatible with Given that Deno draws inspiration from Go, I believe a |
As someone who embeds stuff often, I've definitely considered features like this. In this proposals current form, I think/agree it would be best as a 3rd party module. However, I do think there is something more minimal/generic that is worth proposing as a change to deno. For code generation and file embedding to work seamlessly, I believe we need an at-compile-time hook. JSX is already effectively a compile time hook. But in terms of custom hooks, AFAIK it is currently not possible to have deno run some code (or command) whenever A Deno compile time hook would combine nicely with a 3rd party deno-generate command. My recommended syntax would be similar to the JSX hook:
|
Wanted to pitch in a suggestion. It would be great if a deno native code generator would tie in with ESM module resolution and make it easy for me to import |
Code generation has long been a valuable tool for developers, enabling us to automate repetitive tasks and improve the quality of our code. The introduction of the
deno generate
subcommand to the Deno toolchain has the potential to make code generation even more accessible and powerful. This feature could help developers to save time and effort, and to produce more consistent and reliable code.Design overview
Add a
deno generate
subcommand to automate code generation by running procedures defined in comment annotations. Thedeno generate
subcommand should scan entrypoint files for directives, which are lines starting with the comment://deno:generate <command> [arguments...]
Where
command
is the generator corresponding to an executable file that can be run locally. Arguments are passed to the generator as command line arguments.When the
deno generate
command is executed, module graphs are initialized for each valid entry point. Thedeno generate
subcommand will then scan each module graph for directives. If a directive is found, the generator is run with the arguments provided in the directive. The generator is run from the directory containing the directive.Desired features
deno generate
and apply the command on each valid Deno entry point file in the current directory if no entry points are listed. Allow users to list space-separated files as entry points.//deno:generate -command <command> [arguments...]
directive.//deno:generate
commands in VSCode).//deno:generate
generator runs independently, and the failure of one command doesn't stop or impact the execution of other commands. A failed command generates an error message or exit code for itself without affecting the rest of the commands.deno generate
:--files
to specify which files to ignore and include.--run
to specify a command to run.--skip
to specify a command to skip. In the event that a command matches both the--run
and--skip
regular expressions, the command is skipped.--dry-run
to print the commands that would be run without actually running them.--verbose
to print the module specifier and directive text of each directive when running the corresponding generator.--trace
to print the commands as they are run.Help text
I have drafted the following help text for the
deno generate
subcommand:Conventions
Pre-commit
To enhance your development workflow, we recommend implementing a pre-commit hook in your project's Git repository. Follow these steps to set it up:
Linguist generated
When dealing with code generation, there are situations where generated files should not be visible to developers during a pull request. To address this, a setting can be used to differentiate their changes, ensuring a cleaner and more focused code review. On GitHub, you can achieve this by marking specific files with the "linguist-generated" attribute in a ".gitattributes" file1. This attribute allows you to hide these files by default in diffs and exclude them from contributing to the repository language statistics.
To implement this, follow these steps:
Now, when viewing pull requests or generating diffs on GitHub, the marked files will be hidden by default, providing a more streamlined code review process and excluding them from language statistics calculations.
Use cases
The
deno generate
subcommand is a powerful feature, encouraging the use of code generation with a convenient developer experience. Code generation is an important programming technique because generated files can automate repetitive or complex tasks, improve code consistency and maintainability, and save developers time and effort.As for use cases, your imagination is the limit. Here are a few:
deno generate
subcommand to automatically generate code that follows those patterns.deno generate
subcommand to automatically generate tests for their code.Usage
The
deno generate
subcommand is capable of facilitating the generation of code in the Deno ecosystem. Developers use code generation to generate code for all layers of your application.Run any Deno script
Deno scripts should be able to invoke another Deno in its
//deno:generate
statement:generator.ts
generate.ts
Generate OpenAPI types
OpenAPI is a JSON-based specification that represents comprehensive API details, providing a formal and professional representation of the API specifications. One of the most common use cases of OpenAPI is to generate API clients, simplifying the development process by automatically generating code based on the defined API specifications. OpenAPI schemas offer versatile applications and integrations, enabling a wide range of possibilities for API design and development.
//deno:generate deno run -A npm:openapi-typescript@6.2.4 ./examples/github_api.json -o ./examples/github_api.ts
Generate static website with Lume
Lume is a website framework for the Deno ecosystem. Entire static websites are generated by Lume with a single command. See more.
//deno:generate deno run -A https://deno.land/x/lume@v1.17.3/cli.ts --src ./examples/lume --dest ./examples/lume/_site
Generate
deno_bindgen
bindingsThis tool aims to simplify glue code generation for Deno FFI libraries written in Rust. See more.
//deno:generate deno run -A https://deno.land/x/deno_bindgen@0.8.0/cli.ts
Generate deno-embedder file
deno-embedder is a tool that simplifies the development and distribution of Deno applications, particularly when access to static files (.txt, .png, etc.) is required at runtime. It allows you to create an embedder.ts file that encompasses both configuration and the main function call, providing benefits such as IDE-based type-checking. See more.
mod.ts
Bundlee is a deno-embedder alternative.
Generate
deno doc
JSONmod.ts
generate_docs.ts
Generate art
mod.ts
generate_art.ts
Implications on permissions
The
deno generate
feature allows us to run our Deno programs in a slightly different way. Normally, when we use "deno run" to execute our code, we have to explicitly specify the permissions we need, likedeno run --allow-etc.
ordeno run -A
. However, withdeno generate
, we can run our program without explicitly mentioning the permissions.It's important to note that this doesn't change how our program works or behaves. It's simply a different way of running it. However, because Deno is very careful about permissions, it's worth mentioning this aspect.
Ideally, only developers should run
deno generate
. This ensures that we have a clear understanding and control over the permissions granted during the development process.Closing remarks
It is recommended that educational materials be added to Deno to clearly communicate to developers what types of generators they are encouraged and warned to run via the
deno generate
subcommand. This is important because Deno is a powerful tool that can be used to create a variety of applications, and it is important for developers to be aware of the potential risks and benefits of using different generators. By adding educational materials, the Deno team can help to ensure that developers are using the tool safely and effectively.Overall, the
deno generate
subcommand has the potential to improve the development experience for Deno developers and attract new users to the ecosystem. Looking forward to the future of Deno!Footnotes
https://docs.github.com/en/repositories/working-with-files/managing-files/customizing-how-changed-files-appear-on-github ↩
The text was updated successfully, but these errors were encountered: