-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into TheRealAmazonKendra/disableExecuteApiEndpoint
- Loading branch information
Showing
21 changed files
with
199 additions
and
278 deletions.
There are no files selected for viewing
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
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,90 @@ | ||
import * as path from 'path'; | ||
import { shell } from './os'; | ||
|
||
export type SubstitutePlaceholders = (...fileNames: string[]) => Promise<void>; | ||
|
||
/** | ||
* Helpers passed to hook functions | ||
*/ | ||
export interface HookContext { | ||
/** | ||
* Callback function to replace placeholders on arbitrary files | ||
* | ||
* This makes token substitution available to non-`.template` files. | ||
*/ | ||
readonly substitutePlaceholdersIn: SubstitutePlaceholders; | ||
|
||
/** | ||
* Return a single placeholder | ||
*/ | ||
placeholder(name: string): string; | ||
} | ||
|
||
export type InvokeHook = (targetDirectory: string, context: HookContext) => Promise<void>; | ||
|
||
export interface HookTarget { | ||
readonly targetDirectory: string; | ||
readonly templateName: string; | ||
readonly language: string; | ||
} | ||
|
||
/** | ||
* Invoke hooks for the given init template | ||
* | ||
* Sometimes templates need more complex logic than just replacing tokens. A 'hook' can be | ||
* used to do additional processing other than copying files. | ||
* | ||
* Hooks used to be defined externally to the CLI, by running arbitrarily | ||
* substituted shell scripts in the target directory. | ||
* | ||
* In practice, they're all TypeScript files and all the same, and the dynamism | ||
* that the original solution allowed wasn't used at all. Worse, since the CLI | ||
* is now bundled the hooks can't even reuse code from the CLI libraries at all | ||
* anymore, so all shared code would have to be copy/pasted. | ||
* | ||
* Bundle hooks as built-ins into the CLI, so they get bundled and can take advantage | ||
* of all shared code. | ||
*/ | ||
export async function invokeBuiltinHooks(target: HookTarget, context: HookContext) { | ||
switch (target.language) { | ||
case 'csharp': | ||
if (['app', 'sample-app'].includes(target.templateName)) { | ||
return dotnetAddProject(target.targetDirectory, context); | ||
} | ||
break; | ||
|
||
case 'fsharp': | ||
if (['app', 'sample-app'].includes(target.templateName)) { | ||
return dotnetAddProject(target.targetDirectory, context, 'fsproj'); | ||
} | ||
break; | ||
|
||
case 'python': | ||
// We can't call this file 'requirements.template.txt' because Dependabot needs to be able to find it. | ||
// Therefore, keep the in-repo name but still substitute placeholders. | ||
await context.substitutePlaceholdersIn('requirements.txt'); | ||
break; | ||
|
||
case 'java': | ||
// We can't call this file 'pom.template.xml'... for the same reason as Python above. | ||
await context.substitutePlaceholdersIn('pom.xml'); | ||
break; | ||
|
||
case 'javascript': | ||
case 'typescript': | ||
// See above, but for 'package.json'. | ||
await context.substitutePlaceholdersIn('package.json'); | ||
|
||
} | ||
} | ||
|
||
async function dotnetAddProject(targetDirectory: string, context: HookContext, ext = 'csproj') { | ||
const pname = context.placeholder('name.PascalCased'); | ||
const slnPath = path.join(targetDirectory, 'src', `${pname}.sln`); | ||
const csprojPath = path.join(targetDirectory, 'src', pname, `${pname}.${ext}`); | ||
try { | ||
await shell(['dotnet', 'sln', slnPath, 'add', csprojPath]); | ||
} catch (e) { | ||
throw new Error(`Could not add project ${pname}.${ext} to solution ${pname}.sln. ${e.message}`); | ||
} | ||
}; |
33 changes: 0 additions & 33 deletions
33
packages/aws-cdk/lib/init-templates/app/csharp/add-project.hook.ts
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
packages/aws-cdk/lib/init-templates/app/fsharp/add-project.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/app/java/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/app/javascript/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/app/python/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/app/typescript/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/lib/typescript/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
packages/aws-cdk/lib/init-templates/sample-app/csharp/add-project.hook.ts
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
packages/aws-cdk/lib/init-templates/sample-app/fsharp/add-project.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/sample-app/java/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/sample-app/javascript/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/sample-app/python/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/aws-cdk/lib/init-templates/sample-app/typescript/sub-placeholders.hook.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.