Skip to content

Commit

Permalink
refactor(rush-plugins): amazon, azure plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Oct 11, 2021
1 parent 1b3b89e commit f171513
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
{
"pluginName": "rush-amazon-s3-build-cache-plugin",
"description": "Rush plugin for Amazon S3 cloud build cache",
"entryPoint": "lib/index.js",
"optionsSchema": "lib/schemas/amazon-s3-config.schema.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import * as path from 'path';
import { Import, JsonSchema } from '@rushstack/node-core-library';
import type { IRushPlugin, RushSession, RushConfiguration, ILogger } from '@microsoft/rush-lib';
import { Import } from '@rushstack/node-core-library';
import type { IRushPlugin, RushSession, RushConfiguration } from '@microsoft/rush-lib';
import type { AmazonS3BuildCacheProvider } from './AmazonS3BuildCacheProvider';

const AmazonS3BuildCacheProviderModule: typeof import('./AmazonS3BuildCacheProvider') = Import.lazy(
Expand Down Expand Up @@ -44,24 +43,11 @@ export interface IAmazonS3ConfigurationJson {
export class RushAmazonS3BuildCachePlugin implements IRushPlugin {
public pluginName: string = PLUGIN_NAME;

private static _getBuildCacheConfigJsonSchema(): JsonSchema {
return JsonSchema.fromFile(path.join(__dirname, 'schemas', 'amazon-s3-config.schema.json'));
}

public apply(rushSession: RushSession, rushConfig: RushConfiguration): void {
rushSession.hooks.initialize.tap(PLUGIN_NAME, () => {
rushSession.cloudCacheProviderFactories.set(
rushSession.registerCloudBuildCacheProviderFactory(
'amazon-s3',
(buildCacheConfig, buildCacheConfigFilePath: string): AmazonS3BuildCacheProvider => {
const logger: ILogger = rushSession.getLogger(PLUGIN_NAME);
try {
RushAmazonS3BuildCachePlugin._getBuildCacheConfigJsonSchema().validateObject(
buildCacheConfig,
buildCacheConfigFilePath
);
} catch (e) {
logger.emitError(e);
}
(buildCacheConfig): AmazonS3BuildCacheProvider => {
type IBuildCache = typeof buildCacheConfig & {
amazonS3Configuration: IAmazonS3ConfigurationJson;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,29 @@

"type": "object",

"additionalProperties": false,

"required": ["s3Region", "s3Bucket"],

"properties": {
"amazonS3Configuration": {
"type": "object",
"s3Region": {
"type": "string",
"description": "(Required) The Amazon S3 region of the bucket to use for build cache (e.g. \"us-east-1\")."
},

"additionalProperties": false,
"s3Bucket": {
"type": "string",
"description": "(Required) The name of the bucket in Amazon S3 to use for build cache."
},

"required": ["s3Region", "s3Bucket"],
"s3Prefix": {
"type": "string",
"description": "An optional prefix (\"folder\") for cache items."
},

"properties": {
"s3Region": {
"type": "string",
"description": "(Required) The Amazon S3 region of the bucket to use for build cache (e.g. \"us-east-1\")."
},
"s3Bucket": {
"type": "string",
"description": "(Required) The name of the bucket in Amazon S3 to use for build cache."
},
"s3Prefix": {
"type": "string",
"description": "An optional prefix (\"folder\") for cache items."
},
"isCacheWriteAllowed": {
"type": "boolean",
"description": "If set to true, allow writing to the cache. Defaults to false."
}
}
"isCacheWriteAllowed": {
"type": "boolean",
"description": "If set to true, allow writing to the cache. Defaults to false."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
{
"pluginName": "rush-azure-storage-build-cache-plugin",
"description": "Rush plugin for Azure storage cloud build cache",
"entryPoint": "lib/index.js",
"optionsSchema": "lib/schemas/azure-blob-storage-config.schema.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import * as path from 'path';
import { Import, JsonSchema } from '@rushstack/node-core-library';
import type { IRushPlugin, RushSession, RushConfiguration, ILogger } from '@microsoft/rush-lib';
import { Import } from '@rushstack/node-core-library';
import type { IRushPlugin, RushSession, RushConfiguration } from '@microsoft/rush-lib';
import type { AzureEnvironmentNames } from './AzureStorageBuildCacheProvider';

const AzureStorageBuildCacheProviderModule: typeof import('./AzureStorageBuildCacheProvider') = Import.lazy(
Expand Down Expand Up @@ -49,37 +48,21 @@ interface IAzureStorageConfigurationJson {
export class RushAzureStorageBuildCachePlugin implements IRushPlugin {
public pluginName: string = PLUGIN_NAME;

private static _getBuildCacheConfigJsonSchema(): JsonSchema {
return JsonSchema.fromFile(path.join(__dirname, 'schemas', 'azure-blob-storage-config.schema.json'));
}

public apply(rushSession: RushSession, rushConfig: RushConfiguration): void {
rushSession.hooks.initialize.tap(PLUGIN_NAME, () => {
rushSession.cloudCacheProviderFactories.set(
'azure-blob-storage',
(buildCacheConfig, buildCacheConfigFilePath) => {
const logger: ILogger = rushSession.getLogger(PLUGIN_NAME);
try {
RushAzureStorageBuildCachePlugin._getBuildCacheConfigJsonSchema().validateObject(
buildCacheConfig,
buildCacheConfigFilePath
);
} catch (e) {
logger.emitError(e);
}
type IBuildCache = typeof buildCacheConfig & {
azureStorageConfiguration: IAzureStorageConfigurationJson;
};
const { azureStorageConfiguration } = buildCacheConfig as IBuildCache;
return new AzureStorageBuildCacheProviderModule.AzureStorageBuildCacheProvider({
storageAccountName: azureStorageConfiguration.storageAccountName,
storageContainerName: azureStorageConfiguration.storageContainerName,
azureEnvironment: azureStorageConfiguration.azureEnvironment,
blobPrefix: azureStorageConfiguration.blobPrefix,
isCacheWriteAllowed: !!azureStorageConfiguration.isCacheWriteAllowed
});
}
);
rushSession.registerCloudBuildCacheProviderFactory('azure-blob-storage', (buildCacheConfig) => {
type IBuildCache = typeof buildCacheConfig & {
azureStorageConfiguration: IAzureStorageConfigurationJson;
};
const { azureStorageConfiguration } = buildCacheConfig as IBuildCache;
return new AzureStorageBuildCacheProviderModule.AzureStorageBuildCacheProvider({
storageAccountName: azureStorageConfiguration.storageAccountName,
storageContainerName: azureStorageConfiguration.storageContainerName,
azureEnvironment: azureStorageConfiguration.azureEnvironment,
blobPrefix: azureStorageConfiguration.blobPrefix,
isCacheWriteAllowed: !!azureStorageConfiguration.isCacheWriteAllowed
});
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,35 @@

"type": "object",

"additionalProperties": false,

"required": ["storageAccountName", "storageContainerName"],

"properties": {
"azureBlobStorageConfiguration": {
"type": "object",

"additionalProperties": false,

"required": ["storageAccountName", "storageContainerName"],

"properties": {
"storageAccountName": {
"type": "string",
"description": "(Required) The name of the the Azure storage account to use for build cache."
},

"storageContainerName": {
"type": "string",
"description": "(Required) The name of the container in the Azure storage account to use for build cache."
},

"azureEnvironment": {
"type": "string",
"description": "The Azure environment the storage account exists in. Defaults to AzurePublicCloud.",
"enum": ["AzurePublicCloud", "AzureChina", "AzureGermany", "AzureGovernment"]
},

"blobPrefix": {
"type": "string",
"description": "An optional prefix for cache item blob names."
},

"isCacheWriteAllowed": {
"type": "boolean",
"description": "If set to true, allow writing to the cache. Defaults to false."
}
}
"storageAccountName": {
"type": "string",
"description": "(Required) The name of the the Azure storage account to use for build cache."
},

"storageContainerName": {
"type": "string",
"description": "(Required) The name of the container in the Azure storage account to use for build cache."
},

"azureEnvironment": {
"type": "string",
"description": "The Azure environment the storage account exists in. Defaults to AzurePublicCloud.",
"enum": ["AzurePublicCloud", "AzureChina", "AzureGermany", "AzureGovernment"]
},

"blobPrefix": {
"type": "string",
"description": "An optional prefix for cache item blob names."
},

"isCacheWriteAllowed": {
"type": "boolean",
"description": "If set to true, allow writing to the cache. Defaults to false."
}
}
}

0 comments on commit f171513

Please sign in to comment.