-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add gulpfile tasks #3
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
Merged
+2,867
−1
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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,214 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for | ||
| * license information. | ||
| */ | ||
|
|
||
| const gulp = require('gulp'); | ||
| const args = require('yargs').argv; | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const glob = require('glob'); | ||
| const execSync = require('child_process').execSync; | ||
|
|
||
| const azureSDKForJSRepoRoot = __dirname; | ||
| const azureRestAPISpecsRoot = args['azure-rest-api-specs-root'] || path.resolve(azureSDKForJSRepoRoot, '..', 'azure-rest-api-specs'); | ||
| const package = args['package']; | ||
| const use = args['use']; | ||
| const whatif = args['whatif']; | ||
|
|
||
| function findReadmeTypeScriptMdFilePaths(azureRestAPISpecsRoot) { | ||
| const specificationFolderPath = path.resolve(azureRestAPISpecsRoot, 'specification'); | ||
| return glob.sync('**/readme.typescript.md', { absolute: true, cwd: specificationFolderPath }); | ||
| } | ||
|
|
||
| function getPackageNameFromReadmeTypeScriptMdFileContents(readmeTypeScriptMdFileContents) { | ||
| return readmeTypeScriptMdFileContents.match(/package-name: (\S*)/)[1]; | ||
| } | ||
|
|
||
| function getOutputFolderFromReadmeTypeScriptMdFileContents(readmeTypeScriptMdFileContents) { | ||
| return readmeTypeScriptMdFileContents.match(/output-folder: (\S*)/)[1]; | ||
| } | ||
|
|
||
| function npmInstall(packageFolderPath) { | ||
| execSync(`npm install`, { cwd: packageFolderPath, stdio: ['ignore', 'ignore', 'pipe'] }); | ||
| } | ||
|
|
||
| function getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents) { | ||
| const outputFolderPath = getOutputFolderFromReadmeTypeScriptMdFileContents(typeScriptReadmeFileContents); | ||
| const outputFolderPathRelativeToAzureSDKForJSRepoRoot = outputFolderPath.substring('$(typescript-sdks-folder)/'.length); | ||
| return path.resolve(azureSDKForJSRepoRoot, outputFolderPathRelativeToAzureSDKForJSRepoRoot); | ||
| } | ||
|
|
||
| gulp.task('default', function () { | ||
| console.log('gulp install --package <package name>'); | ||
| console.log(' --package'); | ||
| console.log(' NPM package to run "npm install" on.'); | ||
| console.log(); | ||
| console.log('gulp codegen [--azure-rest-api-specs-root <azure-rest-api-specs root>] [--use <autorest.typescript root>] [--package <package name>]'); | ||
| console.log(' --azure-rest-api-specs-root'); | ||
| console.log(' Root location of the local clone of the azure-rest-api-specs-root repository.'); | ||
| console.log(' --use'); | ||
| console.log(' Root location of autorest.typescript repository. If this is not specified, then the latest installed generator for TypeScript will be used.'); | ||
| console.log(' --package'); | ||
| console.log(' NPM package to regenerate. If no package is specified, then all packages will be regenerated.'); | ||
| console.log(); | ||
| console.log('gulp publish [--package <package name>] [--whatif]'); | ||
| console.log(' --package'); | ||
| console.log(' The name of the package to publish. If no package is specified, then all packages will be published.'); | ||
| console.log(' --whatif'); | ||
| console.log(' Don\'t actually publish packages, but just indicate which packages would be published.'); | ||
| }); | ||
|
|
||
| gulp.task("install", () => { | ||
| if (!package) { | ||
| console.log(`No --package specified to run "npm install" on.`); | ||
| } else { | ||
| const typeScriptReadmeFilePaths = findReadmeTypeScriptMdFilePaths(azureRestAPISpecsRoot); | ||
|
|
||
| let foundPackage = false; | ||
|
|
||
| for (let i = 0; i < typeScriptReadmeFilePaths.length; ++i) { | ||
| const typeScriptReadmeFilePath = typeScriptReadmeFilePaths[i]; | ||
|
|
||
| const typeScriptReadmeFileContents = fs.readFileSync(typeScriptReadmeFilePath, 'utf8'); | ||
| const packageName = getPackageNameFromReadmeTypeScriptMdFileContents(typeScriptReadmeFileContents); | ||
|
|
||
| if (package === packageName || packageName.endsWith(`-${package}`)) { | ||
| foundPackage = true; | ||
|
|
||
| const packageFolderPath = getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents); | ||
| console.log(`[${packageFolderPath}]> npm install`); | ||
| npmInstall(packageFolderPath); | ||
| } | ||
| } | ||
|
|
||
| if (!foundPackage) { | ||
| console.log(`No package found with the name "${package}".`); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| //This task is used to generate libraries based on the mappings specified above. | ||
| gulp.task('codegen', () => { | ||
| const typeScriptReadmeFilePaths = findReadmeTypeScriptMdFilePaths(azureRestAPISpecsRoot); | ||
|
|
||
| for (let i = 0; i < typeScriptReadmeFilePaths.length; ++i) { | ||
| const typeScriptReadmeFilePath = typeScriptReadmeFilePaths[i]; | ||
|
|
||
| const typeScriptReadmeFileContents = fs.readFileSync(typeScriptReadmeFilePath, 'utf8'); | ||
| const packageName = getPackageNameFromReadmeTypeScriptMdFileContents(typeScriptReadmeFileContents); | ||
|
|
||
| if (!package || package === packageName || packageName.endsWith(`-${package}`)) { | ||
| console.log(`>>>>>>>>>>>>>>>>>>> Start: "${packageName}" >>>>>>>>>>>>>>>>>>>>>>>>>`); | ||
|
|
||
| const readmeFilePath = path.resolve(path.dirname(typeScriptReadmeFilePath), 'readme.md'); | ||
|
|
||
| let cmd = `autorest --typescript --typescript-sdks-folder=${azureSDKForJSRepoRoot} --license-header=MICROSOFT_MIT_NO_VERSION ${readmeFilePath}`; | ||
| if (use) { | ||
| cmd += ` --use=${use}`; | ||
| } | ||
| else { | ||
| const localAutorestTypeScriptFolderPath = path.resolve(azureSDKForJSRepoRoot, '..', 'autorest.typescript'); | ||
| if (fs.existsSync(localAutorestTypeScriptFolderPath) && fs.lstatSync(localAutorestTypeScriptFolderPath).isDirectory()) { | ||
| cmd += ` --use=${localAutorestTypeScriptFolderPath}`; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| console.log('Executing command:'); | ||
| console.log('------------------------------------------------------------'); | ||
| console.log(cmd); | ||
| console.log('------------------------------------------------------------'); | ||
| const result = execSync(cmd, { encoding: 'utf8' }); | ||
| console.log('Output:'); | ||
| console.log(result); | ||
|
|
||
| console.log('Installing dependencies...'); | ||
| const packageFolderPath = getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents); | ||
| npmInstall(packageFolderPath); | ||
| } catch (err) { | ||
| console.log('Error:'); | ||
| console.log(`An error occurred while generating client for package: "${packageName}":\n ${err.stderr}`); | ||
| } | ||
|
|
||
| console.log(`>>>>>>>>>>>>>>>>>>> End: "${packageName}" >>>>>>>>>>>>>>>>>>>>>>>>>`); | ||
| console.log(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| gulp.task('publish', () => { | ||
| const typeScriptReadmeFilePaths = findReadmeTypeScriptMdFilePaths(azureRestAPISpecsRoot); | ||
|
|
||
| let errorPackages = 0; | ||
| let upToDatePackages = 0; | ||
| let publishedPackages = 0; | ||
| let publishedPackagesSkipped = 0; | ||
|
|
||
| for (let i = 0; i < typeScriptReadmeFilePaths.length; ++i) { | ||
| const typeScriptReadmeFilePath = typeScriptReadmeFilePaths[i]; | ||
| // console.log(`INFO: Processing ${typeScriptReadmeFilePath}`); | ||
|
|
||
| const typeScriptReadmeFileContents = fs.readFileSync(typeScriptReadmeFilePath, 'utf8'); | ||
| const packageFolderPath = getAbsolutePackageFolderPathFromReadmeFileContents(typeScriptReadmeFileContents); | ||
| if (!fs.existsSync(packageFolderPath)) { | ||
| console.log(`ERROR: Package folder ${packageFolderPath} has not been generated.`); | ||
| errorPackages++; | ||
| } | ||
| else { | ||
| const packageJsonFilePath = `${packageFolderPath}/package.json`; | ||
| if (!fs.existsSync(packageJsonFilePath)) { | ||
| console.log(`ERROR: Package folder ${packageFolderPath} is missing its package.json file.`); | ||
| errorPackages++; | ||
| } | ||
| else { | ||
| const packageJson = require(packageJsonFilePath); | ||
| const packageName = packageJson.name; | ||
|
|
||
| if (!package || package === packageName || packageName.endsWith(`-${package}`)) { | ||
| const localPackageVersion = packageJson.version; | ||
| if (!localPackageVersion) { | ||
| console.log(`ERROR: "${packageJsonFilePath}" doesn't have a version specified.`); | ||
| errorPackages++; | ||
| } | ||
| else { | ||
| let npmPackageVersion; | ||
| try { | ||
| const npmViewResult = JSON.parse(execSync(`npm view ${packageName} --json`, { stdio: ['pipe', 'pipe', 'ignore'] })); | ||
| npmPackageVersion = npmViewResult['dist-tags']['latest']; | ||
| } | ||
| catch (error) { | ||
| // This happens if the package doesn't exist in NPM. | ||
| } | ||
|
|
||
| if (localPackageVersion === npmPackageVersion) { | ||
| upToDatePackages++; | ||
| } | ||
| else { | ||
| console.log(`Publishing package "${packageName}" with version "${localPackageVersion}"...${whatif ? " (SKIPPED)" : ""}`); | ||
| if (!whatif) { | ||
| try { | ||
| npmInstall(packageFolderPath); | ||
| execSync(`npm publish`, { cwd: packageFolderPath }); | ||
| publishedPackages++; | ||
| } | ||
| catch (error) { | ||
| errorPackages++; | ||
| } | ||
| } else { | ||
| publishedPackagesSkipped++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log(`Error packages: ${errorPackages}`); | ||
| console.log(`Up to date packages: ${upToDatePackages}`); | ||
| console.log(`Published packages: ${publishedPackages}`); | ||
| console.log(`Published packages skipped: ${publishedPackagesSkipped}`); | ||
| }); | ||
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.
I've got a wip using lerna for this. It handles parallelism as well as running whatever commands on whatever subset of packages you want, i.e. build, test, publish, updating the runtime dependency, etc.
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.
Cool. I look forward to seeing that.