-
Notifications
You must be signed in to change notification settings - Fork 274
Fix and refactor the generate-docs subcommand #788
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
Open
hellodword
wants to merge
1
commit into
devcontainers:main
Choose a base branch
from
hellodword:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,38 @@ | ||
import { Argv } from 'yargs'; | ||
import { CLIHost } from '../../spec-common/cliHost'; | ||
import { Log } from '../../spec-utils/log'; | ||
|
||
const targetPositionalDescription = (collectionType: GenerateDocsCollectionType) => ` | ||
Generate docs of ${collectionType}s at provided [target] (default is cwd), where [target] is either: | ||
1. A path to the src folder of the collection with [1..n] ${collectionType}s. | ||
2. A path to a single ${collectionType} that contains a devcontainer-${collectionType}.json. | ||
`; | ||
|
||
export function GenerateDocsOptions(y: Argv, collectionType: GenerateDocsCollectionType) { | ||
return y | ||
.options({ | ||
'registry': { type: 'string', alias: 'r', default: 'ghcr.io', description: 'Name of the OCI registry.', hidden: collectionType !== 'feature' }, | ||
'namespace': { type: 'string', alias: 'n', require: collectionType === 'feature', description: `Unique indentifier for the collection of features. Example: <owner>/<repo>`, hidden: collectionType !== 'feature' }, | ||
'github-owner': { type: 'string', default: '', description: `GitHub owner for docs.` }, | ||
'github-repo': { type: 'string', default: '', description: `GitHub repo for docs.` }, | ||
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level.' } | ||
}) | ||
.positional('target', { type: 'string', default: '.', description: targetPositionalDescription(collectionType) }) | ||
.check(_argv => { | ||
return true; | ||
}); | ||
} | ||
|
||
export type GenerateDocsCollectionType = 'feature' | 'template'; | ||
|
||
export interface GenerateDocsCommandInput { | ||
cliHost: CLIHost; | ||
targetFolder: string; | ||
registry?: string; | ||
namespace?: string; | ||
gitHubOwner: string; | ||
gitHubRepo: string; | ||
output: Log; | ||
disposables: (() => Promise<unknown> | undefined)[]; | ||
isSingle?: boolean; // Generating docs for a collection of many features/templates. Should autodetect. | ||
} |
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,7 +1,9 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as jsonc from 'jsonc-parser'; | ||
import { Log, LogLevel } from '../../spec-utils/log'; | ||
import { LogLevel } from '../../spec-utils/log'; | ||
import { GenerateDocsCollectionType, GenerateDocsCommandInput } from './generateDocs'; | ||
import { isLocalFile, isLocalFolder, readLocalDir } from '../../spec-utils/pfs'; | ||
|
||
const FEATURES_README_TEMPLATE = ` | ||
# #{Name} | ||
|
@@ -39,49 +41,42 @@ const TEMPLATE_README_TEMPLATE = ` | |
_Note: This file was auto-generated from the [devcontainer-template.json](#{RepoUrl}). Add additional notes to a \`NOTES.md\`._ | ||
`; | ||
|
||
export async function generateFeaturesDocumentation( | ||
basePath: string, | ||
ociRegistry: string, | ||
namespace: string, | ||
gitHubOwner: string, | ||
gitHubRepo: string, | ||
output: Log | ||
) { | ||
await _generateDocumentation(output, basePath, FEATURES_README_TEMPLATE, | ||
'devcontainer-feature.json', ociRegistry, namespace, gitHubOwner, gitHubRepo); | ||
} | ||
const README_TEMPLATES = { | ||
'feature': FEATURES_README_TEMPLATE, | ||
'template': TEMPLATE_README_TEMPLATE, | ||
}; | ||
|
||
export async function generateDocumentation(args: GenerateDocsCommandInput, collectionType: GenerateDocsCollectionType) { | ||
|
||
const { | ||
targetFolder: basePath, | ||
registry, | ||
namespace, | ||
gitHubOwner, | ||
gitHubRepo, | ||
output, | ||
isSingle, | ||
} = args; | ||
|
||
const readmeTemplate = README_TEMPLATES[collectionType]; | ||
|
||
const directories = isSingle ? ['.'] : await readLocalDir(basePath); | ||
|
||
export async function generateTemplatesDocumentation( | ||
basePath: string, | ||
gitHubOwner: string, | ||
gitHubRepo: string, | ||
output: Log | ||
) { | ||
await _generateDocumentation(output, basePath, TEMPLATE_README_TEMPLATE, | ||
'devcontainer-template.json', '', '', gitHubOwner, gitHubRepo); | ||
} | ||
|
||
async function _generateDocumentation( | ||
output: Log, | ||
basePath: string, | ||
readmeTemplate: string, | ||
metadataFile: string, | ||
ociRegistry: string = '', | ||
namespace: string = '', | ||
gitHubOwner: string = '', | ||
gitHubRepo: string = '' | ||
) { | ||
const directories = fs.readdirSync(basePath); | ||
const metadataFile = `devcontainer-${collectionType}.json`; | ||
|
||
await Promise.all( | ||
directories.map(async (f: string) => { | ||
if (!f.startsWith('.')) { | ||
const readmePath = path.join(basePath, f, 'README.md'); | ||
output.write(`Generating ${readmePath}...`, LogLevel.Info); | ||
if (f.startsWith('..')) { | ||
return; | ||
} | ||
|
||
const readmePath = path.join(basePath, f, 'README.md'); | ||
output.write(`Generating ${readmePath}...`, LogLevel.Info); | ||
|
||
const jsonPath = path.join(basePath, f, metadataFile); | ||
const jsonPath = path.join(basePath, f, metadataFile); | ||
|
||
if (!fs.existsSync(jsonPath)) { | ||
if (!(await isLocalFile(jsonPath))) { | ||
output.write(`(!) Warning: ${metadataFile} not found at path '${jsonPath}'. Skipping...`, LogLevel.Warning); | ||
return; | ||
} | ||
|
@@ -176,8 +171,8 @@ async function _generateDocumentation( | |
.replace('#{Notes}', generateNotesMarkdown()) | ||
.replace('#{RepoUrl}', urlToConfig) | ||
// Features Only | ||
.replace('#{Registry}', ociRegistry) | ||
.replace('#{Namespace}', namespace) | ||
.replace('#{Registry}', registry || '') | ||
.replace('#{Namespace}', namespace || '') | ||
.replace('#{Version}', version) | ||
.replace('#{Customizations}', extensions); | ||
|
||
|
@@ -191,8 +186,36 @@ async function _generateDocumentation( | |
} | ||
|
||
// Write new readme | ||
fs.writeFileSync(readmePath, newReadme); | ||
} | ||
fs.writeFileSync(readmePath, newReadme); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use async variant to allow other code to make progress while this is waiting for completion. |
||
}) | ||
); | ||
} | ||
|
||
export async function prepGenerateDocsCommand(args: GenerateDocsCommandInput, collectionType: GenerateDocsCollectionType): Promise<GenerateDocsCommandInput> { | ||
const { cliHost, targetFolder, registry, namespace, gitHubOwner, gitHubRepo, output, disposables } = args; | ||
|
||
const targetFolderResolved = cliHost.path.resolve(targetFolder); | ||
if (!(await isLocalFolder(targetFolderResolved))) { | ||
throw new Error(`Target folder '${targetFolderResolved}' does not exist`); | ||
} | ||
|
||
// Detect if we're dealing with a collection or a single feature/template | ||
const isValidFolder = await isLocalFolder(cliHost.path.join(targetFolderResolved)); | ||
const isSingle = await isLocalFile(cliHost.path.join(targetFolderResolved, `devcontainer-${collectionType}.json`)); | ||
|
||
if (!isValidFolder) { | ||
throw new Error(`Target folder '${targetFolderResolved}' does not exist`); | ||
} | ||
|
||
return { | ||
cliHost, | ||
targetFolder: targetFolderResolved, | ||
registry, | ||
namespace, | ||
gitHubOwner, | ||
gitHubRepo, | ||
output, | ||
disposables, | ||
isSingle | ||
}; | ||
} |
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
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
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.
Should we keep
--project-folder
and-p
as a hidden option with a warning message in the log when used for compatibility? Since this has been available for a few weeks users might have picked it up and removing it could break things locally or in CIs.