Skip to content

Commit 329e513

Browse files
Dane Pilcherphani-srikarTravis Sheppard
authored
feat: update imports generated for flutter datastore plugin dependency (#382) (#388)
* chore(amplify-codegen): change flutter import path from datastore to core (#380) * feat(appsync-modelgen-plugin): Change flutter datastore models dependency to use amplify_core * feat(appsync-modelgen-plugin): add amplify_flutter version check to determine datastore dependency import * fix(appsync-modelgen-plugin): correct indentation for error message * fix(appsync-modelgen-plugin): run linter * fix(amplify-codegen): update unit tests * fix(amplify-codegen): lower minimum version to 0.4.0-rc.1 * fix(appsync-modelgen-plugin): remove duplicate .dart from imports Co-authored-by: Travis Sheppard <[email protected]> Co-authored-by: Dane Pilcher <[email protected]> Co-authored-by: Phani Srikar Edupuganti <[email protected]> Co-authored-by: Travis Sheppard <[email protected]>
1 parent ec68b8e commit 329e513

File tree

11 files changed

+1046
-358
lines changed

11 files changed

+1046
-358
lines changed

packages/amplify-codegen/src/commands/models.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const gqlCodeGen = require('@graphql-codegen/core');
77
const { getModelgenPackage } = require('../utils/getModelgenPackage');
88
const { validateDartSDK } = require('../utils/validateDartSDK');
99
const { validateAmplifyFlutterCapableZeroThreeFeatures } = require('../utils/validateAmplifyFlutterCapableZeroThreeFeatures');
10+
const { validateAmplifyFlutterCoreLibraryDependency } = require('../utils/validateAmplifyFlutterCoreLibraryDependency');
1011

1112
const platformToLanguageMap = {
1213
android: 'java',
@@ -90,6 +91,7 @@ async function generateModels(context) {
9091
let isTimestampFieldsAdded = readFeatureFlag('codegen.addTimestampFields');
9192
let enableDartNullSafety = readFeatureFlag('codegen.enableDartNullSafety');
9293
let enableDartZeroThreeFeatures = false;
94+
let dartUpdateAmplifyCoreDependency = false;
9395

9496
if (projectConfig.frontend === 'flutter') {
9597
const isMinimumDartVersionSatisfied = validateDartSDK(context, projectRoot);
@@ -107,6 +109,8 @@ async function generateModels(context) {
107109
// override isTimestampFieldsAdded to true when using amplify-flutter > 0.3.0 || > 0.3.0-rc.2
108110
isTimestampFieldsAdded = validateAmplifyFlutterCapableZeroThreeFeatures(projectRoot);
109111
enableDartZeroThreeFeatures = validateAmplifyFlutterCapableZeroThreeFeatures(projectRoot);
112+
// This feature is supported only for users using amplify-flutter > 0.4.0 || > 0.4.0-rc.1
113+
dartUpdateAmplifyCoreDependency = validateAmplifyFlutterCoreLibraryDependency(projectRoot);
110114
}
111115

112116
const handleListNullabilityTransparently = readFeatureFlag('codegen.handleListNullabilityTransparently');
@@ -124,6 +128,7 @@ async function generateModels(context) {
124128
usePipelinedTransformer,
125129
enableDartZeroThreeFeatures,
126130
transformerVersion,
131+
dartUpdateAmplifyCoreDependency
127132
},
128133
});
129134

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,9 @@
1-
const yaml = require('js-yaml');
2-
const path = require('path');
3-
const fs = require('fs-extra');
4-
const semver = require('semver');
1+
const { validateAmplifyFlutterVersion } = require('./validateAmplifyFlutterVersion');
52

6-
const PUBSPEC_LOCK_FILE_NAME = 'pubspec.lock';
7-
const MINIMUM_VERSION_CONSTRAIN = '>= 0.3.0 || >= 0.3.0-rc.2';
3+
const MINIMUM_VERSION_CONSTRAINT = '>= 0.3.0 || >= 0.3.0-rc.2';
84

95
function validateAmplifyFlutterCapableZeroThreeFeatures(projectRoot) {
10-
try {
11-
const lockFile = yaml.load(fs.readFileSync(path.join(projectRoot, PUBSPEC_LOCK_FILE_NAME), 'utf8'));
12-
// check resolved dependency version written pubspec.lock file
13-
const { version } = lockFile.packages.amplify_flutter || {};
14-
// For this util function it check only if the amplify_flutter version is great than the minimum version
15-
// and it's not concerned with prerelease range, hence including prerelease to ensure
16-
// 0.4.0-rc.2 > 0.3.0-rc.2 is true
17-
if (semver.satisfies(version, MINIMUM_VERSION_CONSTRAIN, { includePrerelease: true })) {
18-
return true;
19-
}
20-
return false;
21-
} catch (e) {
22-
if (e.stack) {
23-
console.log(e.stack);
24-
console.log(e.message);
25-
}
26-
27-
console.log('An error occurred while parsing ' + PUBSPEC_LOCK_FILE_NAME + '.');
28-
return false;
29-
}
6+
return validateAmplifyFlutterVersion(projectRoot, MINIMUM_VERSION_CONSTRAINT);
307
}
318

32-
module.exports = { validateAmplifyFlutterCapableZeroThreeFeatures, PUBSPEC_LOCK_FILE_NAME, MINIMUM_VERSION_CONSTRAIN };
9+
module.exports = { validateAmplifyFlutterCapableZeroThreeFeatures };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { validateAmplifyFlutterVersion } = require('./validateAmplifyFlutterVersion');
2+
3+
const MINIMUM_VERSION_CONSTRAINT = '>= 0.4.0 || >= 0.4.0-rc.1';
4+
5+
function validateAmplifyFlutterCoreLibraryDependency(projectRoot) {
6+
return validateAmplifyFlutterVersion(projectRoot, MINIMUM_VERSION_CONSTRAINT);
7+
}
8+
9+
module.exports = { validateAmplifyFlutterCoreLibraryDependency };
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const yaml = require('js-yaml');
2+
const path = require('path');
3+
const fs = require('fs-extra');
4+
const semver = require('semver');
5+
6+
const PUBSPEC_LOCK_FILE_NAME = 'pubspec.lock';
7+
8+
function validateAmplifyFlutterVersion(projectRoot, versionConstraint) {
9+
try {
10+
const lockFile = yaml.load(fs.readFileSync(path.join(projectRoot, PUBSPEC_LOCK_FILE_NAME), 'utf8'));
11+
// check resolved dependency version written pubspec.lock file
12+
const { version } = lockFile.packages.amplify_flutter || {};
13+
// For this util function it check only if the amplify_flutter version satisfies the given version constraint
14+
if (semver.satisfies(version, versionConstraint, { includePrerelease: true })) {
15+
return true;
16+
}
17+
return false;
18+
} catch (e) {
19+
if (e.stack) {
20+
console.log(e.stack);
21+
console.log(e.message);
22+
}
23+
24+
console.log('An error occurred while parsing ' + PUBSPEC_LOCK_FILE_NAME + '.');
25+
return false;
26+
}
27+
}
28+
29+
module.exports = { validateAmplifyFlutterVersion, PUBSPEC_LOCK_FILE_NAME };

packages/amplify-codegen/tests/commands/models.test.js

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const MOCK_PROJECT_ROOT = 'project';
3131
const MOCK_PROJECT_NAME = 'myapp';
3232
const MOCK_BACKEND_DIRECTORY = 'backend';
3333
const MOCK_GENERATED_CODE = 'This code is auto-generated!';
34-
const MOCK_PRE_EXISTING_FILE = 'preexisting.txt';
3534

3635
// Mock the Feature flag to use migrated moldegen
3736
jest.mock('amplify-cli-core', MOCK_PROJECT_ROOT => {
@@ -109,89 +108,6 @@ describe('command-models-generates models in expected output path', () => {
109108
afterEach(mockFs.restore);
110109
});
111110

112-
describe('codegen models respects cleanGeneratedModelsDirectory', () => {
113-
beforeEach(() => {
114-
jest.resetAllMocks();
115-
addMocksToContext();
116-
graphqlCodegen.codegen.mockReturnValue(MOCK_GENERATED_CODE);
117-
});
118-
119-
for (const frontend in OUTPUT_PATHS) {
120-
it(frontend + ': Should clear the output directory before generation if cleanGeneratedModelsDirectory is true', async () => {
121-
MOCK_CONTEXT.amplify.getProjectConfig.mockReturnValue({ frontend: frontend });
122-
require('amplify-cli-core').FeatureFlags.getBoolean.mockImplementation((name, defaultValue) => {
123-
if (name === 'codegen.useappsyncmodelgenplugin' || name === 'codegen.cleanGeneratedModelsDirectory') {
124-
return true;
125-
}
126-
});
127-
128-
const outputDirectory = path.join(MOCK_PROJECT_ROOT, OUTPUT_PATHS[frontend]);
129-
const preExistingFilePath = path.join(outputDirectory, MOCK_PRE_EXISTING_FILE);
130-
createMockFS(outputDirectory);
131-
132-
// assert output directory has a mock file to be cleared
133-
expect(fs.readdirSync(outputDirectory).length).toEqual(1);
134-
expect(fs.existsSync(preExistingFilePath));
135-
136-
await generateModels(MOCK_CONTEXT);
137-
138-
// assert model generation succeeds
139-
expect(graphqlCodegen.codegen).toBeCalled();
140-
141-
// assert that codegen generated in correct place
142-
expect(fs.readdirSync(outputDirectory).length).toBeGreaterThan(0);
143-
144-
// assert that the pre-existing file is deleted
145-
expect(fs.existsSync(preExistingFilePath)).toBeFalsy;
146-
});
147-
148-
it(frontend + ': Should not clear the output directory before generation if cleanGeneratedModelsDirectory is false', async () => {
149-
MOCK_CONTEXT.amplify.getProjectConfig.mockReturnValue({ frontend: frontend });
150-
require('amplify-cli-core').FeatureFlags.getBoolean.mockImplementation((name, defaultValue) => {
151-
if (name === 'codegen.useappsyncmodelgenplugin') {
152-
return true;
153-
}
154-
if (name === 'codegen.cleanGeneratedModelsDirectory') {
155-
return false;
156-
}
157-
});
158-
159-
const outputDirectory = path.join(MOCK_PROJECT_ROOT, OUTPUT_PATHS[frontend]);
160-
const preExistingFilePath = path.join(outputDirectory, MOCK_PRE_EXISTING_FILE);
161-
createMockFS(outputDirectory);
162-
163-
// assert output directory has a mock file to be cleared
164-
expect(fs.readdirSync(outputDirectory).length).toEqual(1);
165-
expect(fs.existsSync(preExistingFilePath));
166-
167-
await generateModels(MOCK_CONTEXT);
168-
169-
// assert model generation succeeds
170-
expect(graphqlCodegen.codegen).toBeCalled();
171-
172-
// assert that codegen generated in correct place
173-
expect(fs.readdirSync(outputDirectory).length).toBeGreaterThan(1);
174-
175-
// assert that the pre-existing file is retained
176-
expect(fs.existsSync(preExistingFilePath)).toBeTruthy;
177-
});
178-
}
179-
180-
function createMockFS(outputDirectory) {
181-
// mock the input and output file structure
182-
const schemaFilePath = path.join(MOCK_BACKEND_DIRECTORY, 'api', MOCK_PROJECT_NAME);
183-
const mockedFiles = {};
184-
mockedFiles[schemaFilePath] = {
185-
'schema.graphql': ' type SimpleModel { id: ID! status: String } ',
186-
};
187-
mockedFiles[outputDirectory] = {};
188-
mockedFiles[outputDirectory][MOCK_PRE_EXISTING_FILE] = 'A pre-existing mock file';
189-
mockFs(mockedFiles);
190-
}
191-
192-
afterEach(mockFs.restore);
193-
});
194-
195111
// Add models generation specific mocks to Amplify Context
196112
function addMocksToContext() {
197113
MOCK_CONTEXT.amplify.getEnvInfo.mockReturnValue({ projectPath: MOCK_PROJECT_ROOT });

packages/amplify-codegen/tests/utils/validateAmplifyFlutterCapableForNonModel.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const {
2-
validateAmplifyFlutterCapableZeroThreeFeatures,
3-
PUBSPEC_LOCK_FILE_NAME,
4-
MINIMUM_VERSION_CONSTRAIN,
2+
validateAmplifyFlutterCapableZeroThreeFeatures
53
} = require('../../src/utils/validateAmplifyFlutterCapableZeroThreeFeatures');
4+
const { PUBSPEC_LOCK_FILE_NAME } = require('../../src/utils/validateAmplifyFlutterVersion');
65
const mockFs = require('mock-fs');
76
const { join } = require('path');
87
const yaml = require('js-yaml');
98

109
const MOCK_PROJECT_ROOT = 'project';
1110
const MOCK_PUBSPEC_FILE_PATH = join(MOCK_PROJECT_ROOT, PUBSPEC_LOCK_FILE_NAME);
11+
const MINIMUM_VERSION_CONSTRAINT = '>= 0.3.0 || >= 0.3.0-rc.2';
1212
global.console = {log: jest.fn()}
1313
const mockErrorPrinter = console.log;
1414

@@ -17,7 +17,7 @@ describe('Validate amplify flutter version tests', () => {
1717
mockFs.restore();
1818
});
1919

20-
describe(`should return true if the resolved version meets the version constrain: ${MINIMUM_VERSION_CONSTRAIN}`, () => {
20+
describe(`should return true if the resolved version meets the version constrain: ${MINIMUM_VERSION_CONSTRAINT}`, () => {
2121
['0.3.0', '0.3.1', '1.0.0', '0.3.0-rc.2', '0.4.0', '0.4.0-rc.2'].forEach(version => {
2222
test(`when the resolved version is ${version}`, () => {
2323
const lockFile = {
@@ -33,7 +33,7 @@ describe('Validate amplify flutter version tests', () => {
3333
});
3434
});
3535

36-
describe(`should return false if the resolved version does NOT meet the version constrain: ${MINIMUM_VERSION_CONSTRAIN}`, () => {
36+
describe(`should return false if the resolved version does NOT meet the version constrain: ${MINIMUM_VERSION_CONSTRAINT}`, () => {
3737
['0.2.0', '0.2.9', '0.3.0-rc.1'].forEach(version => {
3838
test(`when the resolved version is ${version}`, () => {
3939
const lockFile = {

0 commit comments

Comments
 (0)