Skip to content

Commit

Permalink
Refactor structure #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kpajdzik committed Oct 5, 2018
1 parent 68465f4 commit 281c7a7
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 96 deletions.
18 changes: 17 additions & 1 deletion .scripts/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as minimist from "minimist";
import { arrayContains } from "./common";

export interface CommandLineOptions extends minimist.ParsedArgs {
"azure-rest-api-specs-root": string;
b: boolean,
debugger: boolean,
package: string,
Expand All @@ -19,7 +20,7 @@ export interface CommandLineOptions extends minimist.ParsedArgs {
}

export const commandLineConfiguration = {
string: ["package", "type"],
string: ["'azure-rest-api-specs-root", "package", "type"],
boolean: ["debugger", "use", "verbose", "whatif"],
alias: {
package: "packageName",
Expand All @@ -36,6 +37,21 @@ export enum SdkType {
DataPlane
}

let _options: CommandLineOptions;
export function getCommandLineOptions() {
if (!_options) {
_options = createCommandLineParameters();
}

return _options;
}

export function createCommandLineParameters() {
const args = minimist(process.argv.slice(2), commandLineConfiguration) as CommandLineOptions;
args.getSdkType = getSdkType;
return args;
}

export function getSdkType() {
const resourceManagerStrings = ["arm", "rm", "resourcemanager"]
const dataPlaneStrings = ["dp", "data", "dataplane"]
Expand Down
20 changes: 19 additions & 1 deletion .scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import * as fssync from "fs";
import { promises as fs } from "fs";
import { execSync } from "child_process";
import { logger } from "./logger";

export function arrayContains<T>(array: T[], el: T): boolean {
return array.indexOf(el) != -1
Expand Down Expand Up @@ -33,5 +35,21 @@ export function endsWith(value: string, suffix: string): boolean {
}

export function contains(values: string[], searchString: string): boolean {
return values.indexOf(searchString) !== -1;
return arrayContains(values, searchString);
}

export function execute(command: string, packageFolderPath: string): void {
if (!fssync.existsSync(packageFolderPath)) {
logger.logWithPath(packageFolderPath, "Folder not found.");
} else {
execSync(command, { cwd: packageFolderPath, stdio: "inherit" });
}
}

export function npmRunBuild(packageFolderPath: string): void {
execute("npm run build", packageFolderPath);
}

export function npmInstall(packageFolderPath: string): void {
execute("npm install", packageFolderPath);
}
2 changes: 1 addition & 1 deletion .scripts/generateSdks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { promises as fs } from "fs";
import * as path from "path";
import { SdkType } from "./commandLine";
import { pathExists, isDirectory, arrayContains } from "./common";
import { logger } from "../gulpfile";
import { logger } from "./logger";
import { doesReadmeMdFileSpecifiesTypescriptSdk } from "./readme";

const repositoryName = "azure-rest-api-specs";
Expand Down
14 changes: 8 additions & 6 deletions .scripts/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import * as colors from "colors";
import { CommandLineOptions } from "./commandLine";
import { CommandLineOptions, getCommandLineOptions } from "./commandLine";

export enum Color {
Red,
Expand All @@ -15,12 +15,14 @@ export enum Color {
colors.setTheme({
positive: "green",
negative: "red",
debug: "bgCyan",
});

declare global {
interface String {
positive: string;
negative: string;
debug: string;
}
}

Expand Down Expand Up @@ -56,9 +58,9 @@ export class Logger {
}
}

logDebug(text: string, color?: Color): void {
if (this._options.debug) {
this.log(text, color);
}
logWithPath(path: string, message: string): void {
console.log(`[${path}]> ${message}`);
}
}
}

export const logger = new Logger(getCommandLineOptions());
52 changes: 48 additions & 4 deletions .scripts/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* license information.
*/

import { logger } from "./logger";
import { pathExists, startsWith } from "./common";
import { promises as fs } from "fs";
import * as glob from "glob";
import * as path from "path";
import * as yaml from "js-yaml";
import { pathExists } from "./common";
import { logger } from "../gulpfile";

interface ReadmeSettings {
"nodejs": {
Expand Down Expand Up @@ -89,7 +90,7 @@ async function updateYamlSection(sectionText: string): Promise<string> {
const section = yaml.safeLoad(sectionText);
await updatePackageName(section);
await updateMetadataFields(section);
await updateOutputFolder(section);
await updateOutputFolder(section);
section["typescript"] = section.nodejs;
delete section.nodejs;

Expand All @@ -114,4 +115,47 @@ export async function updateTypeScriptReadmeFile(typescriptReadmePath: string):
outputReadme = outputReadme.replace("node", "typescript");

return outputReadme;
}
}

export function getPackageNamesFromReadmeTypeScriptMdFileContents(readmeTypeScriptMdFileContents: string): string[] {
const packageNamePattern: RegExp = /package-name: (\S*)/g;
const matches: string[] = readmeTypeScriptMdFileContents.match(packageNamePattern) || [];
logger.logVerbose(`"package-name" matches: ${JSON.stringify(matches)}`.debug);

for (let i = 0; i < matches.length; ++i) {
matches[i] = matches[i].substring("package-name: ".length);
}

logger.logVerbose(`"package-name" matches trimmed: ${JSON.stringify(matches)}`.debug);
return matches;
}

export function findReadmeTypeScriptMdFilePaths(azureRestAPISpecsRoot: string): string[] {
logger.logVerbose(`Looking for "readme.typescript.md" files in "${azureRestAPISpecsRoot}"...`.debug);

const specificationFolderPath: string = path.resolve(azureRestAPISpecsRoot, 'specification');
const readmeTypeScriptMdFilePaths: string[] = glob.sync('**/readme.typescript.md', { absolute: true, cwd: specificationFolderPath });
if (readmeTypeScriptMdFilePaths) {
for (let i = 0; i < readmeTypeScriptMdFilePaths.length; ++i) {
const readmeTypeScriptMdFilePath: string = readmeTypeScriptMdFilePaths[i];
logger.logVerbose(` Found "${readmeTypeScriptMdFilePath}".`.debug);

if (readmeTypeScriptMdFilePath && !startsWith(readmeTypeScriptMdFilePath, specificationFolderPath)) {
const resolvedReadmeTypeScriptMdFilePath: string = path.resolve(specificationFolderPath, readmeTypeScriptMdFilePath);
logger.logVerbose(` Updating to "${resolvedReadmeTypeScriptMdFilePath}".`.debug);
readmeTypeScriptMdFilePaths[i] = resolvedReadmeTypeScriptMdFilePath;
}
}
}
return readmeTypeScriptMdFilePaths;
}

export function getOutputFolderFromReadmeTypeScriptMdFileContents(readmeTypeScriptMdFileContents: string): string {
return readmeTypeScriptMdFileContents.match(/output-folder: (\S*)/)[1];
}

export function getAbsolutePackageFolderPathFromReadmeFileContents(azureSDKForJSRepoRoot: string, typeScriptReadmeFileContents: string): string {
const outputFolderPath: string = getOutputFolderFromReadmeTypeScriptMdFileContents(typeScriptReadmeFileContents);
const outputFolderPathRelativeToAzureSDKForJSRepoRoot: string = outputFolderPath.substring('$(typescript-sdks-folder)/'.length);
return path.resolve(azureSDKForJSRepoRoot, outputFolderPathRelativeToAzureSDKForJSRepoRoot);
}
100 changes: 17 additions & 83 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,22 @@
* license information.
*/

import { execSync } from "child_process";
import { getCommandLineOptions } from "./.scripts/commandLine";
import { contains, endsWith, npmInstall, npmRunBuild } from "./.scripts/common";
import { findAzureRestApiSpecsRepository, findMissingSdks } from "./.scripts/generateSdks";
import { generateTsReadme } from "./.scripts/gulp";
import { getPackageNamesFromReadmeTypeScriptMdFileContents, findReadmeTypeScriptMdFilePaths, getAbsolutePackageFolderPathFromReadmeFileContents } from "./.scripts/readme";
import { logger } from "./.scripts/logger";
import * as fs from "fs";
import * as glob from "glob";
import * as gulp from "gulp";
import * as path from "path";
import minimist = require("minimist");
import { argv } from "yargs";
import { CommandLineOptions, commandLineConfiguration, getSdkType } from "./.scripts/commandLine";
import { findAzureRestApiSpecsRepository, findMissingSdks } from "./.scripts/generateSdks";
import { Logger } from "./.scripts/logger";
import { generateTsReadme } from "./.scripts/gulp";
import { startsWith, contains, endsWith } from "./.scripts/common";
import { execSync } from "child_process";

const args = getCommandLineOptions();
const azureSDKForJSRepoRoot: string = __dirname;
const azureRestAPISpecsRoot: string = argv['azure-rest-api-specs-root'] || path.resolve(azureSDKForJSRepoRoot, '..', 'azure-rest-api-specs');

const args = minimist(process.argv.slice(2), commandLineConfiguration) as CommandLineOptions;
args.getSdkType = getSdkType;

export const logger = new Logger(args);

function findReadmeTypeScriptMdFilePaths(azureRestAPISpecsRoot: string): string[] {
// console.log(`Looking for "readme.typescript.md" files in "${azureRestAPISpecsRoot}"...`);
const specificationFolderPath: string = path.resolve(azureRestAPISpecsRoot, 'specification');
const readmeTypeScriptMdFilePaths: string[] = glob.sync('**/readme.typescript.md', { absolute: true, cwd: specificationFolderPath });
if (readmeTypeScriptMdFilePaths) {
for (let i = 0; i < readmeTypeScriptMdFilePaths.length; ++i) {
const readmeTypeScriptMdFilePath: string = readmeTypeScriptMdFilePaths[i];
// console.log(` Found "${readmeTypeScriptMdFilePath}".`);
if (readmeTypeScriptMdFilePath && !startsWith(readmeTypeScriptMdFilePath, specificationFolderPath)) {
const resolvedReadmeTypeScriptMdFilePath: string = path.resolve(specificationFolderPath, readmeTypeScriptMdFilePath);
// console.log(` Updating to "${resolvedReadmeTypeScriptMdFilePath}".`);
readmeTypeScriptMdFilePaths[i] = resolvedReadmeTypeScriptMdFilePath;
}
}
}
return readmeTypeScriptMdFilePaths;
}

function getPackageNamesFromReadmeTypeScriptMdFileContents(readmeTypeScriptMdFileContents: string): string[] {
const packageNamePattern: RegExp = /package-name: (\S*)/g;
const matches: string[] = readmeTypeScriptMdFileContents.match(packageNamePattern) || [];
// console.log(`"package-name" matches: ${JSON.stringify(matches)}`);
for (let i = 0; i < matches.length; ++i) {
matches[i] = matches[i].substring("package-name: ".length);
}
// console.log(`"package-name" matches trimmed: ${JSON.stringify(matches)}`);
return matches;
}

function getOutputFolderFromReadmeTypeScriptMdFileContents(readmeTypeScriptMdFileContents: string): string {
return readmeTypeScriptMdFileContents.match(/output-folder: (\S*)/)[1];
}

function execute(command: string, packageFolderPath: string): void {
if (!fs.existsSync(packageFolderPath)) {
log(packageFolderPath, "Folder not found.");
} else {
execSync(command, { cwd: packageFolderPath, stdio: "inherit" });
}
}

function npmRunBuild(packageFolderPath: string): void {
execute("npm run build", packageFolderPath);
}
const azureRestAPISpecsRoot: string = args["azure-rest-api-specs-root"] || path.resolve(azureSDKForJSRepoRoot, '..', 'azure-rest-api-specs');

function npmInstall(packageFolderPath: string): void {
execute("npm install", packageFolderPath);
}

function getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents: string): string {
const outputFolderPath: string = getOutputFolderFromReadmeTypeScriptMdFileContents(typeScriptReadmeFileContents);
const outputFolderPathRelativeToAzureSDKForJSRepoRoot: string = outputFolderPath.substring('$(typescript-sdks-folder)/'.length);
return path.resolve(azureSDKForJSRepoRoot, outputFolderPathRelativeToAzureSDKForJSRepoRoot);
}


function getPackgeFolderPathFromPackageArgument(): string | undefined {
function getPackageFolderPathFromPackageArgument(): string | undefined {
let packageFolderPath: string | undefined;

if (!args.package) {
Expand All @@ -100,7 +38,7 @@ function getPackgeFolderPathFromPackageArgument(): string | undefined {
if (contains(packageNames, args.package)) {
foundPackage = true;

packageFolderPath = getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents);
packageFolderPath = getAbsolutePackageFolderPathFromReadmeFileContents(azureSDKForJSRepoRoot, typeScriptReadmeFileContents);
}
}

Expand All @@ -112,10 +50,6 @@ function getPackgeFolderPathFromPackageArgument(): string | undefined {
return packageFolderPath;
}

function log(path: string, message: string): void {
console.log(`[${path}]> ${message}`);
}

gulp.task('default', () => {
console.log('gulp build --package <package-name>');
console.log(' --package');
Expand All @@ -141,17 +75,17 @@ gulp.task('default', () => {
});

gulp.task("install", () => {
const packageFolderPath: string | undefined = getPackgeFolderPathFromPackageArgument();
const packageFolderPath: string | undefined = getPackageFolderPathFromPackageArgument();
if (packageFolderPath) {
log(packageFolderPath, "npm install");
logger.logWithPath(packageFolderPath, "npm install");
npmInstall(packageFolderPath);
}
});

gulp.task("build", () => {
const packageFolderPath: string | undefined = getPackgeFolderPathFromPackageArgument();
const packageFolderPath: string | undefined = getPackageFolderPathFromPackageArgument();
if (packageFolderPath) {
log(packageFolderPath, "npm run build");
logger.logWithPath(packageFolderPath, "npm run build");
npmRunBuild(packageFolderPath);
}
});
Expand Down Expand Up @@ -197,7 +131,7 @@ gulp.task('codegen', () => {
execSync(cmd, { encoding: "utf8", stdio: "inherit" });

console.log('Installing dependencies...');
const packageFolderPath: string = getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents);
const packageFolderPath: string = getAbsolutePackageFolderPathFromReadmeFileContents(azureSDKForJSRepoRoot, typeScriptReadmeFileContents);
npmInstall(packageFolderPath);
} catch (err) {
console.log('Error:');
Expand All @@ -223,7 +157,7 @@ gulp.task('publish', () => {
// console.log(`INFO: Processing ${typeScriptReadmeFilePath}`);

const typeScriptReadmeFileContents: string = fs.readFileSync(typeScriptReadmeFilePath, 'utf8');
const packageFolderPath: string = getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents);
const packageFolderPath: string = getAbsolutePackageFolderPathFromReadmeFileContents(azureSDKForJSRepoRoot, typeScriptReadmeFileContents);
if (!fs.existsSync(packageFolderPath)) {
console.log(`ERROR: Package folder ${packageFolderPath} has not been generated.`);
errorPackages++;
Expand Down

0 comments on commit 281c7a7

Please sign in to comment.