-
Notifications
You must be signed in to change notification settings - Fork 689
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
feat: autogenerate CLI command docs #2535
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs'; | ||
import { execSync } from 'node:child_process'; | ||
import { slug } from 'github-slugger'; | ||
|
||
interface Command { | ||
name: string; | ||
description: string; | ||
} | ||
|
||
function getSubcommands(commandOutput: string): Command[] { | ||
const subcommands = []; | ||
|
||
let readingSubcommands = false; | ||
for (const line of commandOutput.split('\n').map((l) => l.trim())) { | ||
if (readingSubcommands) { | ||
if (line.length === 0) { | ||
readingSubcommands = false; | ||
} else { | ||
const subcommand = line.substring(0, line.indexOf(' ')); | ||
const description = line.substring(subcommand.length).trim(); | ||
if (subcommand !== 'help') { | ||
subcommands.push({ name: subcommand, description }); | ||
} | ||
} | ||
} | ||
if (line === 'Commands:') { | ||
readingSubcommands = true; | ||
} | ||
} | ||
|
||
return subcommands; | ||
} | ||
|
||
function generateCommandDoc(command: string, level: number, subcommandList: Command[]): string { | ||
const output = execSync(`pnpm tauri ${command} --help`) | ||
.toString() | ||
.replace('pnpm run build', 'tauri'); | ||
const subcommands = getSubcommands(output); | ||
|
||
subcommandList.push( | ||
...subcommands.map((subcommand) => ({ | ||
...subcommand, | ||
name: `${command} ${subcommand.name}`, | ||
})) | ||
); | ||
|
||
const subcommandsDoc = | ||
subcommands.length === 0 | ||
? '' | ||
: `\n${subcommands.map(({ name }) => generateCommandDoc(`${command} ${name}`, level + 1, commandList)).join('\n\n')}`; | ||
|
||
const heading = '#'.repeat(level); | ||
return `${heading} \`${command}\` | ||
|
||
<CommandTabs | ||
npm="npm run tauri ${command}" | ||
yarn="yarn tauri ${command}" | ||
pnpm="pnpm tauri ${command}" | ||
cargo="cargo tauri ${command}" | ||
/> | ||
|
||
\`\`\` | ||
${output} | ||
\`\`\` | ||
${subcommandsDoc} | ||
`; | ||
} | ||
|
||
const output = execSync('pnpm tauri --help').toString(); | ||
const subcommands = getSubcommands(output); | ||
|
||
const commandList: Command[] = []; | ||
|
||
let doc = ''; | ||
|
||
for (const command of subcommands) { | ||
commandList.push(command); | ||
const commandDoc = generateCommandDoc(command.name, 3, commandList); | ||
doc += commandDoc; | ||
} | ||
|
||
let summary = `| Command | Description | | ||
| ---------- | ------------------- | | ||
`; | ||
|
||
for (const { name, description } of commandList) { | ||
summary += `| [\`${name}\`](#${slug(name)}) | ${description} |\n`; | ||
} | ||
|
||
const template = readFileSync('../../src/content/docs/reference/_cli.mdx').toString(); | ||
|
||
writeFileSync( | ||
'../../src/content/docs/reference/cli.mdx', | ||
template.replace( | ||
'$LIST_OF_COMMANDS', | ||
`${summary} | ||
|
||
${doc}` | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "cli-generator", | ||
"version": "1.0.0", | ||
"private": "true", | ||
"description": "", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsm ./build.ts" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@tauri-apps/cli": "2.0.0-rc.3", | ||
"@types/node": "^20.11.20", | ||
"github-slugger": "^2.0.0", | ||
"tsm": "^2.3.0", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2021", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"skipLibCheck": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
config.md | ||
acl/*.md | ||
cli.mdx | ||
javascript/* | ||
!javascript/index.mdx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Command Line Interface | ||
sidebar: | ||
order: 1 | ||
tableOfContents: | ||
minHeadingLevel: 2 | ||
maxHeadingLevel: 5 | ||
--- | ||
|
||
import { Tabs, TabItem } from '@astrojs/starlight/components'; | ||
import CommandTabs from '@components/CommandTabs.astro'; | ||
|
||
The Tauri command line interface (CLI) is the way to interact with Tauri throughout the development lifecycle. | ||
|
||
You can add the Tauri CLI to your current project using your package manager of choice: | ||
|
||
<CommandTabs | ||
npm="npm install --save-dev @tauri-apps/cli@next" | ||
yarn="yarn add -D @tauri-apps/cli@next" | ||
pnpm="pnpm add -D @tauri-apps/cli@next" | ||
cargo='cargo install tauri-cli --version "^2.0.0-rc"' | ||
/> | ||
|
||
:::tip[Developing a Plugin] | ||
|
||
For CLI commands related to developing plugins visit the [Develop a Tauri Plugin guide](/develop/plugins/). | ||
|
||
::: | ||
|
||
## List of Commands | ||
|
||
$LIST_OF_COMMANDS |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach is kinda fragile whenever we change the layout of the help output but I don't think that will happen more than once.
Also running the binary just for the help output sounds weird when we have access to the docs/source code itself since we have tauri as a git submodule.
Is tauri auto generating the rustdocs/scheme and we can consume them the same way we do for the permissions and configs? If not is it not feasible to do so?