-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* python cdk wip * wip * [samp local] python cdk support --------- Co-authored-by: ljacobsson <[email protected]>
- Loading branch information
1 parent
cc3e726
commit beb9bd1
Showing
14 changed files
with
320 additions
and
84 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 was deleted.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
src/commands/local/runtime-support/nodejs/cdk/cdk-construct-finder.js
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,25 @@ | ||
const { findFilesWithExtension } = require('../../../../../shared/fileFinder'); | ||
const fs = require('fs'); | ||
|
||
function findConstructs() { | ||
const rootDir = '.'; | ||
const fileExtension = '.ts'; | ||
const searchString = /extends.*Stack/g; | ||
const files = findFilesWithExtension(rootDir, fileExtension); | ||
const matchedFiles = []; | ||
|
||
files.forEach((file) => { | ||
const fileContent = fs.readFileSync(file, 'utf8'); | ||
|
||
if (fileContent.match(searchString)) { | ||
matchedFiles.push(file); | ||
} | ||
}); | ||
|
||
return matchedFiles; | ||
} | ||
|
||
|
||
module.exports = { | ||
findConstructs | ||
}; |
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
25 changes: 25 additions & 0 deletions
25
src/commands/local/runtime-support/python/cdk/cdk-construct-finder.js
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,25 @@ | ||
const { findFilesWithExtension } = require('../../../../../shared/fileFinder'); | ||
const fs = require('fs'); | ||
|
||
function findConstructs() { | ||
const rootDir = '.'; | ||
const fileExtension = '.py'; | ||
const searchString = /from aws_cdk import/g; | ||
const files = findFilesWithExtension(rootDir, fileExtension); | ||
const matchedFiles = []; | ||
|
||
files.forEach((file) => { | ||
const fileContent = fs.readFileSync(file, 'utf8'); | ||
|
||
if (fileContent.match(searchString)) { | ||
matchedFiles.push(file); | ||
} | ||
}); | ||
|
||
return matchedFiles; | ||
} | ||
|
||
|
||
module.exports = { | ||
findConstructs | ||
}; |
84 changes: 84 additions & 0 deletions
84
src/commands/local/runtime-support/python/cdk/cdk-wrapper.js
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,84 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
const yamlDump = require('js-yaml').dump; | ||
function calculateHash(filePath) { | ||
const fileContent = fs.readFileSync(filePath); | ||
return crypto.createHash('md5').update(fileContent).digest('hex'); | ||
} | ||
|
||
function findMatchingSource(assetHash, sourceDirectories) { | ||
for (const sourceDir of sourceDirectories) { | ||
const sourceFiles = fs.readdirSync(sourceDir); | ||
for (const sourceFile of sourceFiles) { | ||
if (fs.statSync(path.join(sourceDir, sourceFile)).isDirectory()) { | ||
const matchingSourceDir = findMatchingSource(assetHash, [path.join(sourceDir, sourceFile)]); | ||
if (matchingSourceDir !== null) { | ||
return matchingSourceDir; | ||
} | ||
} else { | ||
const sourceFilePath = path.join(sourceDir, sourceFile); | ||
const sourceHash = calculateHash(sourceFilePath); | ||
if (sourceHash === assetHash) { | ||
return sourceDir; | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function createAssetSourceMap(assetDirectories, sourceDirectories) { | ||
|
||
const assetSourceMap = {}; | ||
|
||
for (const assetDir of assetDirectories) { | ||
const assetFiles = fs.readdirSync(assetDir); | ||
for (const assetFile of assetFiles) { | ||
const assetFilePath = path.join(assetDir, assetFile); | ||
const assetHash = calculateHash(assetFilePath); | ||
const matchingSource = findMatchingSource(assetHash, sourceDirectories); | ||
if (matchingSource) { | ||
const assetHashKey = path.basename(assetDir); | ||
assetSourceMap[assetHashKey] = matchingSource; | ||
} | ||
} | ||
} | ||
|
||
return assetSourceMap; | ||
} | ||
|
||
|
||
|
||
const assetDirectories = fs.readdirSync('cdk.out') | ||
.map(folder => path.join('cdk.out', folder)) | ||
.filter(folderPath => fs.statSync(folderPath).isDirectory()); | ||
|
||
const sourceDirectories = fs.readdirSync('.') | ||
.map(handler => path.join('.', handler)) | ||
.filter(handlerPath => fs.statSync(handlerPath).isDirectory() && !handlerPath.startsWith('.') && !handlerPath.startsWith('__') && !handlerPath.startsWith('cdk.out')); | ||
const assetSourceMap = createAssetSourceMap(assetDirectories, sourceDirectories); | ||
|
||
const template = JSON.parse(fs.readFileSync('cdk.out/' + process.argv[2] + '.template.json', 'utf-8')); | ||
// Get all AWS::Lambda::Function resources | ||
const functions = Object.keys(template.Resources) | ||
.filter(key => template.Resources[key].Type === "AWS::Lambda::Function") | ||
const mockTemplate = { | ||
Resources: {} | ||
}; | ||
|
||
for (const asset of Object.keys(assetSourceMap)) { | ||
for (const fn of functions) { | ||
const resource = template.Resources[fn]; | ||
if (resource.Metadata['aws:asset:path'] === asset) { | ||
const handlerSplit = resource.Properties.Handler.split('/'); | ||
mockTemplate.Resources[fn] = { | ||
Type: "AWS::Serverless::Function", | ||
Properties: { CodeUri: '.', Handler: `${assetSourceMap[asset]}/${handlerSplit[handlerSplit.length - 1]}`, Runtime: 'python' } | ||
}; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
fs.writeFileSync(".samp-out/mock-template.yaml", yamlDump(mockTemplate)); |
Oops, something went wrong.