-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: separate func invoke and build
- Loading branch information
1 parent
525deb6
commit 33a1a02
Showing
41 changed files
with
351 additions
and
371 deletions.
There are no files selected for viewing
53 changes: 39 additions & 14 deletions
53
packages/amplify-category-function/src/commands/function/build.ts
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,18 +1,43 @@ | ||
import { category as categoryName } from '../../constants'; | ||
import { $TSContext } from 'amplify-cli-core'; | ||
import { ServiceName } from '../..'; | ||
import { category } from '../../constants'; | ||
import { ResourceMeta } from '../../provider-utils/awscloudformation/types/packaging-types'; | ||
import { buildFunction } from '../../provider-utils/awscloudformation/utils/buildFunction'; | ||
import { packageResource } from '../../provider-utils/awscloudformation/utils/package'; | ||
|
||
const subcommand = 'build'; | ||
export const name = 'build'; | ||
|
||
module.exports = { | ||
name: subcommand, | ||
run: async context => { | ||
const { amplify, parameters } = context; | ||
const resourceName = parameters.first; | ||
/** | ||
* To maintain existing behavior, this function builds and then packages lambda functions | ||
*/ | ||
export const run = async (context: $TSContext) => { | ||
const resourceName = context?.input?.subCommands?.[0]; | ||
const cont = | ||
!!resourceName || | ||
context.input?.options?.yes || | ||
(await context.amplify.confirmPrompt( | ||
'This will build all functions and layers in your project. Are you sure you want to continue?', | ||
false, | ||
)); | ||
if (!cont) { | ||
return; | ||
} | ||
try { | ||
const resourcesToBuild = (await getSelectedResources(context, resourceName)) | ||
.filter(resource => resource.build) | ||
.filter(resource => resource.service === ServiceName.LambdaFunction); | ||
for await (const resource of resourcesToBuild) { | ||
resource.lastBuildTimeStamp = await buildFunction(context, resource); | ||
await packageResource(context, resource); | ||
} | ||
} catch (err) { | ||
context.print.info(err.stack); | ||
context.print.error('There was an error building the function resources'); | ||
context.usageData.emitError(err); | ||
process.exitCode = 1; | ||
} | ||
}; | ||
|
||
return amplify.buildResources(context, categoryName, resourceName).catch(err => { | ||
context.print.info(err.stack); | ||
context.print.error('There was an error building the function resources'); | ||
context.usageData.emitError(err); | ||
process.exitCode = 1; | ||
}); | ||
}, | ||
const getSelectedResources = async (context: $TSContext, resourceName?: string) => { | ||
return (await context.amplify.getResourceStatus(category, resourceName)).allResources as ResourceMeta[]; | ||
}; |
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
12 changes: 12 additions & 0 deletions
12
...s/amplify-category-function/src/provider-utils/awscloudformation/types/packaging-types.ts
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,12 @@ | ||
import { $TSContext, ResourceTuple } from 'amplify-cli-core'; | ||
|
||
export type ResourceMeta = ResourceTuple & { | ||
service: string; | ||
build: boolean; | ||
distZipFilename: string; | ||
lastBuildTimeStamp?: string; | ||
lastPackageTimeStamp?: string; | ||
skipHashing: boolean; | ||
}; | ||
|
||
export type Packager = (context: $TSContext, resource: ResourceMeta) => Promise<{ zipFilename: string; zipFilePath: string }>; |
47 changes: 47 additions & 0 deletions
47
...ges/amplify-category-function/src/provider-utils/awscloudformation/utils/buildFunction.ts
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,47 @@ | ||
import { $TSContext, pathManager } from 'amplify-cli-core'; | ||
import { FunctionRuntimeLifecycleManager, BuildRequest, BuildType } from 'amplify-function-plugin-interface'; | ||
import { ResourceMeta } from '../types/packaging-types'; | ||
import * as path from 'path'; | ||
|
||
export const buildFunction = async (context: $TSContext, resource: ResourceMeta, buildType: BuildType = BuildType.PROD) => { | ||
const resourcePath = path.join(pathManager.getBackendDirPath(), resource.category, resource.resourceName); | ||
const breadcrumbs = context.amplify.readBreadcrumbs(resource.category, resource.resourceName); | ||
|
||
const runtimePlugin: FunctionRuntimeLifecycleManager = (await context.amplify.loadRuntimePlugin( | ||
context, | ||
breadcrumbs.pluginId, | ||
)) as FunctionRuntimeLifecycleManager; | ||
|
||
const depCheck = await runtimePlugin.checkDependencies(breadcrumbs.functionRuntime); | ||
if (!depCheck.hasRequiredDependencies) { | ||
context.print.error(depCheck.errorMessage || `You are missing dependencies required to package ${resource.resourceName}`); | ||
throw new Error(`Missing required dependencies to package ${resource.resourceName}`); | ||
} | ||
|
||
const prevBuildTime = resource.lastBuildTimeStamp ? new Date(resource.lastBuildTimeStamp) : undefined; | ||
|
||
// build the function | ||
let rebuilt = false; | ||
if (breadcrumbs.scripts && breadcrumbs.scripts.build) { | ||
// TODO | ||
throw new Error('Executing custom build scripts is not yet implemented'); | ||
} else { | ||
const buildRequest: BuildRequest = { | ||
buildType, | ||
srcRoot: resourcePath, | ||
runtime: breadcrumbs.functionRuntime, | ||
legacyBuildHookParams: { | ||
projectRoot: pathManager.findProjectRoot(), | ||
resourceName: resource.resourceName, | ||
}, | ||
lastBuildTimeStamp: prevBuildTime, | ||
}; | ||
rebuilt = (await runtimePlugin.build(buildRequest)).rebuilt; | ||
} | ||
if (rebuilt) { | ||
context.amplify.updateamplifyMetaAfterBuild(resource, buildType); | ||
return new Date().toISOString(); | ||
} else { | ||
return resource?.lastBuildTimeStamp; | ||
} | ||
}; |
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
19 changes: 19 additions & 0 deletions
19
packages/amplify-category-function/src/provider-utils/awscloudformation/utils/package.ts
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,19 @@ | ||
import { $TSContext } from 'amplify-cli-core'; | ||
import _ from 'lodash'; | ||
import { Packager, ResourceMeta } from '../types/packaging-types'; | ||
import { ServiceName } from './constants'; | ||
import { packageFunction } from './packageFunction'; | ||
import { packageLayer } from './packageLayer'; | ||
|
||
export const packageResource: Packager = async (context, resource) => getServicePackager(resource.service)(context, resource); | ||
|
||
const servicePackagerMap: Record<ServiceName, Packager> = { | ||
[ServiceName.LambdaFunction]: packageFunction, | ||
[ServiceName.LambdaLayer]: packageLayer, | ||
}; | ||
|
||
const getServicePackager = (service: string) => | ||
(servicePackagerMap[service] ?? | ||
(() => { | ||
throw new Error(`Unknown function service type ${service}`); | ||
})) as Packager; |
36 changes: 36 additions & 0 deletions
36
...s/amplify-category-function/src/provider-utils/awscloudformation/utils/packageFunction.ts
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,36 @@ | ||
// For legacy purposes, the method builds and packages the resource | ||
|
||
import { pathManager } from 'amplify-cli-core'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import { Packager } from '../types/packaging-types'; | ||
import { getRuntimeManager } from './functionPluginLoader'; | ||
|
||
/** | ||
* Packages lambda source code and artifacts into a lambda-compatible .zip file | ||
*/ | ||
export const packageFunction: Packager = async (context, resource) => { | ||
const resourcePath = path.join(pathManager.getBackendDirPath(), resource.category, resource.resourceName); | ||
const runtimeManager = await getRuntimeManager(context, resource.resourceName); | ||
const distDir = path.join(resourcePath, 'dist'); | ||
if (!fs.existsSync(distDir)) { | ||
fs.mkdirSync(distDir); | ||
} | ||
const destination = path.join(distDir, 'latest-build.zip'); | ||
const packageRequest = { | ||
env: context.amplify.getEnvInfo().envName, | ||
srcRoot: resourcePath, | ||
dstFilename: destination, | ||
runtime: runtimeManager.runtime, | ||
lastPackageTimeStamp: resource.lastPackageTimeStamp ? new Date(resource.lastPackageTimeStamp) : undefined, | ||
lastBuildTimeStamp: resource.lastBuildTimeStamp ? new Date(resource.lastBuildTimeStamp) : undefined, | ||
skipHashing: resource.skipHashing, | ||
}; | ||
const packageResult = await runtimeManager.package(packageRequest); | ||
const packageHash = packageResult.packageHash; | ||
const zipFilename = packageHash | ||
? `${resource.resourceName}-${packageHash}-build.zip` | ||
: resource.distZipFilename ?? `${resource.category}-${resource.resourceName}-build.zip`; | ||
context.amplify.updateAmplifyMetaAfterPackage(resource, zipFilename); | ||
return { zipFilename, zipFilePath: destination }; | ||
}; |
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.