Skip to content
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

Release Codegen Utility #747

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .codebuild/build_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ env:
phases:
build:
commands:
- yarn run production-build

# commands need to be run in stand-alone bash scripts so that bash can be used on windows
- bash ./.codebuild/scripts/build_windows.sh
artifacts:
files:
- 'shared-scripts.sh'
8 changes: 8 additions & 0 deletions .codebuild/e2e_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ batch:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
- identifier: test_windows
buildspec: .codebuild/test_windows.yml
env:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
depend-on:
- build_windows
- identifier: test
buildspec: .codebuild/test.yml
env:
Expand Down
8 changes: 8 additions & 0 deletions .codebuild/e2e_workflow_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ batch:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
- identifier: test_windows
buildspec: .codebuild/test_windows.yml
env:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
depend-on:
- build_windows
- identifier: test
buildspec: .codebuild/test.yml
env:
Expand Down
8 changes: 8 additions & 0 deletions .codebuild/pr_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ batch:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
- identifier: test_windows
buildspec: .codebuild/test_windows.yml
env:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
depend-on:
- build_windows
- identifier: test
buildspec: .codebuild/test.yml
depend-on:
Expand Down
8 changes: 8 additions & 0 deletions .codebuild/release_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ batch:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
- identifier: test_windows
buildspec: .codebuild/test_windows.yml
env:
type: WINDOWS_SERVER_2019_CONTAINER
compute-type: BUILD_GENERAL1_LARGE
image: $WINDOWS_IMAGE_2019
depend-on:
- build_windows
- identifier: test
buildspec: .codebuild/test.yml
depend-on:
Expand Down
6 changes: 6 additions & 0 deletions .codebuild/scripts/build_windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# set exit on error to true
set -e

source ./shared-scripts.sh && _buildWindows
6 changes: 6 additions & 0 deletions .codebuild/scripts/test_windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# set exit on error to true
set -e

source ./shared-scripts.sh && _testWindows
11 changes: 11 additions & 0 deletions .codebuild/test_windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 0.2
env:
shell: powershell.exe
phases:
build:
commands:
# commands need to be run in stand-alone bash scripts so that bash can be used on windows
- bash ./.codebuild/scripts/test_windows.sh
artifacts:
files:
- 'shared-scripts.sh'
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function constructGraphQLConfig(
extensions.amplify.codeGenTarget = 'swift';
extensions.amplify.docsFilePath = 'graphql';
extensions.amplify.generatedFileName = 'API.swift';
excludes.push('API.swift');
break;
default:
schemaPath = `amplify/backend/api/${projectName}/build/schema.graphql`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const graphQLConfig = require('graphql-config');
const { isAbsolute, relative, join } = require('path');
const slash = require('slash');
const { graphQlToAmplifyConfig } = require('./utils');
const fs = require('fs-extra');
const fs = require('fs-extra');
const path = require('path');

class AmplifyCodeGenConfig {
static configFileName = '.graphqlconfig.yml';
Expand All @@ -15,10 +16,9 @@ class AmplifyCodeGenConfig {
if (e instanceof graphQLConfig.ConfigNotFoundError) {
const projectRoot = projectPath || process.cwd();
const configPath = join(projectRoot, '.graphqlconfig.yml');
if(fs.existsSync(configPath)) {
if (fs.existsSync(configPath)) {
this.gqlConfig = graphQLConfig.getGraphQLConfig(projectRoot);
}
else {
} else {
this.gqlConfig = new graphQLConfig.GraphQLConfig(null, configPath);
this.gqlConfig.config = {};
}
Expand All @@ -45,7 +45,11 @@ class AmplifyCodeGenConfig {
if (!this.constructor.isValidAmplifyProject(project)) {
return false;
}
const schemaPath = isAbsolute(project.schema) ? relative(this.gqlConfig.configDir, project.schema) : project.schema;
// Set schemaPath to use posix separators. Node can handle windows and posix separators regradless of platform
// Ensures all paths in .graphlqconfig.yml use posix style
const schemaPath = (isAbsolute(project.schema) ? relative(this.gqlConfig.configDir, project.schema) : project.schema)
.split(path.win32.sep)
.join(path.posix.sep);
const newProject = {
schemaPath,
includes: project.includes,
Expand Down
6 changes: 4 additions & 2 deletions packages/amplify-codegen/src/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ async function add(context, apiId = null, region = 'us-east-1') {
const newProject = {
projectName: withoutInit ? 'Codegen Project' : apiDetails.name,
includes: answer.includePattern,
excludes: answer.excludePattern,
schema,
excludes: [...answer.excludePattern, answer.generatedFileName]?.filter(item => item),
// Set schema path to use posix separators. Node can handle windows and posix separators regradless of platform
// Ensures all paths in .graphlqconfig.yml use posix style
schema: schema.split(path.win32.sep).join(path.posix.sep),
amplifyExtension: {
codeGenTarget: answer.target || '',
generatedFileName: answer.generatedFileName || '',
Expand Down
35 changes: 23 additions & 12 deletions packages/amplify-codegen/src/commands/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,29 @@ async function generateTypes(context, forceDownloadSchema, withoutInit = false,
cwd: projectPath,
absolute: true,
});
const queries = queryFilePaths.map(queryFilePath => {
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
if (
queryFilePath.endsWith('.jsx') ||
queryFilePath.endsWith('.js') ||
queryFilePath.endsWith('.tsx') ||
queryFilePath.endsWith('.ts')
) {
return extractDocumentFromJavascript(fileContents, '');
}
return new Source(fileContents, queryFilePath);
});
const queries = queryFilePaths
.map(queryFilePath => {
const fileContents = fs.readFileSync(queryFilePath, 'utf8');
if (
queryFilePath.endsWith('.jsx') ||
queryFilePath.endsWith('.js') ||
queryFilePath.endsWith('.tsx') ||
queryFilePath.endsWith('.ts')
) {
return [queryFilePath, extractDocumentFromJavascript(fileContents, '')];
}
return [queryFilePath, new Source(fileContents, queryFilePath)];
})
.filter(([queryFilePath, source]) => {
if (!source) {
context.print.warning(
`Unable to extract GraphQL queries from ${queryFilePath}. Skipping source. This source matched the includes target in .grapqhlconfig.yml. Modify the includes or excludes target if this file should not be included.`,
);
return false;
}
return true;
})
.map(([, source]) => source);
if (queries.length === 0) {
throw new Error("No queries found to generate types for, you may need to run 'codegen statements' first");
}
Expand Down
6 changes: 4 additions & 2 deletions packages/amplify-codegen/src/walkthrough/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ async function configureProjectWalkThrough(context, amplifyConfig, withoutInit =
selectedProjectConfig.includes = await askCodeGeneQueryFilePattern(includePattern);

if (!(frontend === 'android' || targetLanguage === 'javascript')) {
amplifyExtension.generatedFileName = await askTargetFileName(amplifyExtension.generatedFileName || 'API', targetLanguage);
const generatedFileName = await askTargetFileName(amplifyExtension.generatedFileName || 'API', targetLanguage);
amplifyExtension.generatedFileName = generatedFileName;
selectedProjectConfig.excludes = Array.from(new Set(selectedProjectConfig.excludes || []).add(generatedFileName));
} else {
amplifyExtension.generatedFileName = '';
}
amplifyExtension.codeGenTarget = targetLanguage;
amplifyExtension.docsFilePath = getGraphQLDocPath(frontend, includePatternDefault.graphQLDirectory, selectedProjectConfig.includes)
amplifyExtension.docsFilePath = getGraphQLDocPath(frontend, includePatternDefault.graphQLDirectory, selectedProjectConfig.includes);
amplifyExtension.maxDepth = await askMaxDepth(amplifyExtension.maxDepth);

return selectedProjectConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Object {
"generatedFileName": "API.TS",
"region": "us-east-1",
},
"excludes": "MOCK_EXCLUDE",
"excludes": Array [
"MOCK_EXCLUDE",
"API.TS",
],
"includes": "MOCK_INCLUDE",
"projectName": "Codegen Project",
"schema": "/user/foo/project/schema.json",
Expand All @@ -29,7 +32,10 @@ Object {
"generatedFileName": "API.TS",
"region": "us-east-1",
},
"excludes": "MOCK_EXCLUDE",
"excludes": Array [
"MOCK_EXCLUDE",
"API.TS",
],
"includes": "MOCK_INCLUDE",
"projectName": "Codegen Project",
"schema": "/user/foo/project/schema.json",
Expand All @@ -47,7 +53,10 @@ Object {
"generatedFileName": "API.TS",
"region": "us-east-1",
},
"excludes": "MOCK_EXCLUDE",
"excludes": Array [
"MOCK_EXCLUDE",
"API.TS",
],
"includes": "MOCK_INCLUDE",
"projectName": "Codegen Project",
"schema": "/user/foo/project/schema.graphql",
Expand All @@ -65,7 +74,10 @@ Object {
"generatedFileName": "API.TS",
"region": "us-west-2",
},
"excludes": "MOCK_EXCLUDE",
"excludes": Array [
"MOCK_EXCLUDE",
"API.TS",
],
"includes": "MOCK_INCLUDE",
"projectName": "Codegen Project",
"schema": "/user/foo/project/schema.json",
Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-codegen/tests/commands/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jest.mock('process', () => ({
}));

const MOCK_INCLUDE_PATTERN = 'MOCK_INCLUDE';
const MOCK_EXCLUDE_PATTERN = 'MOCK_EXCLUDE';
const MOCK_EXCLUDE_PATTERN = ['MOCK_EXCLUDE'];
const MOCK_SCHEMA_LOCATION = 'INTROSPECTION_SCHEMA.JSON';
const MOCK_TARGET = 'TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE';
const MOCK_GENERATED_FILE_NAME = 'API.TS';
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('command - add', () => {
const newProjectConfig = LOAD_CONFIG_METHODS.addProject.mock.calls[0][0];
expect(newProjectConfig.projectName).toEqual(MOCK_API_NAME);
expect(newProjectConfig.includes).toEqual(MOCK_INCLUDE_PATTERN);
expect(newProjectConfig.excludes).toEqual(MOCK_EXCLUDE_PATTERN);
expect(newProjectConfig.excludes).toEqual([...MOCK_EXCLUDE_PATTERN, MOCK_GENERATED_FILE_NAME]);
expect(newProjectConfig.schema).toEqual(MOCK_SCHEMA_FILE_LOCATION);
expect(newProjectConfig.amplifyExtension.codeGenTarget).toEqual(MOCK_TARGET);
expect(newProjectConfig.amplifyExtension.generatedFileName).toEqual(MOCK_GENERATED_FILE_NAME);
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-codegen/tests/commands/mock-fs-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function setupMocks(mockFs, loadConfig, apiId, frontend, target, generatedFileNa
{
schema: schemaFilePath,
includes: [path.join(docsFilePath[frontend], '*')],
excludes: ['./amplify/**'],
excludes: ['./amplify/**', './src/graphql/excluded.ts'],
amplifyExtension: {
codeGenTarget: target,
generatedFileName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ describe('command - statements', () => {
await generateStatements(MOCK_CONTEXT, forceDownload);
expect(getFrontEndHandler).toHaveBeenCalledWith(MOCK_CONTEXT);
expect(loadConfig).toHaveBeenCalledWith(MOCK_CONTEXT, false);
expect(getRelativeTypesPath).toHaveBeenCalledWith('MOCK_PROJECT_ROOT/MOCK_STATEMENTS_PATH', 'API.TS');
// ok for getRelativeTypePath to be called with posix or Windows path separators
expect(getRelativeTypesPath).toHaveBeenCalledWith(path.join(MOCK_PROJECT_ROOT, MOCK_STATEMENTS_PATH), 'API.TS');
expect(generateStatementsHelper).toHaveBeenCalledWith({
relativeTypesPath: relativePath,
schema: MOCK_SCHEMA,
Expand Down
22 changes: 22 additions & 0 deletions packages/amplify-codegen/tests/commands/types-mock-fs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const MOCK_APIS = [
const MOCK_CONTEXT = {
print: {
info: jest.fn(),
warning: jest.fn(),
},
amplify: {
getProjectMeta: jest.fn(() => ({ api: { id: MOCK_API_ID } })),
Expand Down Expand Up @@ -121,4 +122,25 @@ describe('command - types (mock fs)', () => {
await generateTypes(MOCK_CONTEXT, false);
expect(fs.existsSync(generatedFileName)).toBeFalsy();
});

it('should skip invalid sources', async () => {
const { generatedFileName } = setupMocks(mockFs, loadConfig, MOCK_API_ID, 'javascript', 'typescript', 'src/graphql/API.ts', {
'src/graphql/excluded.ts': '',
'src/graphql/foo.ts': '',
});

await generateStatements(MOCK_CONTEXT, false);
await generateTypes(MOCK_CONTEXT, false);
expect(MOCK_CONTEXT.print.warning).toHaveBeenCalledWith(
expect.stringMatching(
'Unable to extract GraphQL queries from .*/src/graphql/foo.ts. Skipping source. This source matched the includes target in .grapqhlconfig.yml. Modify the includes or excludes target if this file should not be included.',
),
);
expect(MOCK_CONTEXT.print.warning).not.toHaveBeenCalledWith(
expect.stringMatching(
'Unable to extract GraphQL queries from .*/src/graphql/excluded.ts. Skipping source. This source matched the includes target in .grapqhlconfig.yml. Modify the includes or excludes target if this file should not be included.',
),
);
expect(fs.existsSync(generatedFileName)).toBeTruthy();
});
});
5 changes: 4 additions & 1 deletion packages/amplify-codegen/tests/commands/types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ describe('command - types', () => {
await generateTypes(MOCK_CONTEXT, forceDownload);
expect(getFrontEndHandler).toHaveBeenCalledWith(MOCK_CONTEXT);
expect(loadConfig).toHaveBeenCalledWith(MOCK_CONTEXT, false);
expect(sync).toHaveBeenCalledWith([MOCK_INCLUDE_PATH, `!${MOCK_EXCLUDE_PATH}`], { cwd: MOCK_PROJECT_ROOT, absolute: true });
expect(sync).toHaveBeenCalledWith([MOCK_INCLUDE_PATH, `!${MOCK_EXCLUDE_PATH}`], {
cwd: MOCK_PROJECT_ROOT,
absolute: true,
});
expect(generateTypesHelper).toHaveBeenCalledWith({
queries: [new Source('query 1', 'q1.gql'), new Source('query 2', 'q2.gql')],
schema: 'schema',
Expand Down
Loading