Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { clearRegisteredTutorials, getOpenAtStartupSetting,
clearOutputChannels, registerTutorialWithJSON, getAutoInstallDefaultTutorialsSetting,
addNewTutorialWithNameAndCategoryForDidactUri,
removeTutorialByNameAndCategory,
registerEmbeddedTutorials} from './utils';
registerEmbeddedTutorials,
appendAdditionalTutorialsFromEnv} from './utils';
import { DidactUriCompletionItemProvider } from './didactUriCompletionItemProvider';
import { DidactPanelSerializer } from './didactPanelSerializer';
import { didactManager, VIEW_TYPE } from './didactManager';
Expand Down Expand Up @@ -116,6 +117,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
await registerEmbeddedTutorials(context, 'Writing Your First Didact Tutorial', './demos/markdown/tutorial/tutorial.didact.md');
}

// append any additional tutorials if we have them
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment is only repeating the name of the method. it does not provide more information. It can be removed to simplify teh code and ease maintenance

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though you and I disagree on this point, I feel that comments do not clutter the code in this instance and will leave the comment as is. Thank you.

await appendAdditionalTutorialsFromEnv();

// create the view
createIntegrationsView();

Expand Down
20 changes: 19 additions & 1 deletion src/test/suite/registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import {getRegisteredTutorials, getDidactCategories, getTutorialsForCategory, getUriForDidactNameAndCategory, registerTutorialWithCategory, clearRegisteredTutorials, registerTutorialWithArgs} from '../../utils';
import {getRegisteredTutorials, getDidactCategories, getTutorialsForCategory, getUriForDidactNameAndCategory, registerTutorialWithCategory, clearRegisteredTutorials, registerTutorialWithArgs, DIDACT_APPEND_REGISTERED_SETTING, getAppendRegisteredSettingFromEnv, appendAdditionalTutorialsFromEnv} from '../../utils';
import {before} from 'mocha';
import * as vscode from 'vscode';
import { ADD_TUTORIAL_TO_REGISTRY, getContext, REGISTER_TUTORIAL } from '../../extensionFunctions';
Expand Down Expand Up @@ -115,6 +115,24 @@ suite('Didact registry test suite', () => {
assert.ok(foundTutorial, `Did not find new-tutorial-4 registered via JSON`);
});

test('append registry from environment variable', async() => {
const tutsToAppend : String = '[{"name":"AppendMe2","category":"AppendedCat2","sourceUri":"https%3A%2F%2Fraw.githubusercontent.com%2Fredhat-developer%2Fvscode-didact%2Fmaster%2Fexamples%2Fregistry.example.didact.md"}]';
process.env[DIDACT_APPEND_REGISTERED_SETTING] = tutsToAppend.toString();

const envVarJson = getAppendRegisteredSettingFromEnv();
console.log(`envVar = ` + envVarJson);
assert.ok(envVarJson, `Did not find envVarJson`);

const registry = getRegisteredTutorials();
assert.notStrictEqual(registry, undefined);

const tutName = `AppendMe2`;
await appendAdditionalTutorialsFromEnv();
const foundTutorial = verifyTutorialInRegistry(tutName);
delete process.env.DIDACT_APPEND_REGISTERED_SETTING;
assert.ok(foundTutorial, `Did not find ${tutName} registered after appending to tutorial list from settings`);
});

test('Clear all the tutorials', async() => {
const registry = getRegisteredTutorials();
assert.notStrictEqual(registry, undefined);
Expand Down
26 changes: 26 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const DIDACT_OPEN_AT_STARTUP = 'didact.openDefaultTutorialAtStartup';
export const DIDACT_AUTO_INSTALL_DEFAULT_TUTORIALS = 'didact.autoAddDefaultTutorials';
export const DIDACT_CLI_LINK_LF_SETTING = 'didact.edit.cliLinkLF';
export const DIDACT_CLI_LINK_TEXT_SETTING = 'didact.edit.cliLinkText';
export const DIDACT_APPEND_REGISTERED_SETTING = 'DIDACT_APPEND_REGISTRY';

const CACHED_OUTPUT_CHANNELS: OutputChannel[] = new Array<OutputChannel>();

Expand Down Expand Up @@ -500,3 +501,28 @@ export function getFileExtension(pathAsString: string) : string | undefined {
}
return undefined;
}

export function getAppendRegisteredSettingFromEnv() : string | undefined {
const envVar = process.env[DIDACT_APPEND_REGISTERED_SETTING];
if (!envVar || (typeof envVar !== 'string')) {
return;
}
return envVar as string;
}

export async function appendAdditionalTutorialsFromEnv() : Promise<void> {
try {
const appendTutorialsAtStartup: string | undefined = getAppendRegisteredSettingFromEnv();
if (appendTutorialsAtStartup) {
await extensionFunctions.sendTextToOutputChannel(`Didact tutorials appended at startup via ${DIDACT_APPEND_REGISTERED_SETTING}`);
const jsonTutorials = JSON.parse(appendTutorialsAtStartup);
for (const jsonTutorial of jsonTutorials) {
await extensionFunctions.sendTextToOutputChannel(`--Adding tutorial ${jsonTutorial.sourceUri} as ${jsonTutorial.name}/${jsonTutorial.category}`);
await registerTutorialWithCategory(jsonTutorial.name, jsonTutorial.sourceUri, jsonTutorial.category);
}
}
} catch (ex) {
await extensionFunctions.sendTextToOutputChannel(ex);
return Promise.reject(ex);
}
}