-
Notifications
You must be signed in to change notification settings - Fork 73
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
Add versioning to sources.json #1069
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97a4ba5
Add migration to sources-versioned.json
datenreisender 7d247b1
Use sources-versioned.json
datenreisender 365d424
Cleanup: Group all migration code
datenreisender 4c2b8dd
sources.ts: Validate schema of file contents
datenreisender 68bde57
Remove legacy handling of apps.json files
datenreisender 55bfa89
Collect all data migration code in own folder
datenreisender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,68 @@ | ||
/* | ||
* Copyright (c) 2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause | ||
*/ | ||
|
||
import { existsSync } from 'fs'; | ||
|
||
import { readSchemedJsonFile, writeSchemedJsonFile } from '../../fileUtil'; | ||
import { migrateSourcesJson, oldSourcesJsonPath } from './migrateSourcesJson'; | ||
|
||
jest.mock('fs'); | ||
jest.mock('../../fileUtil'); | ||
|
||
describe('migrating sources.json', () => { | ||
const mockedExistsSync = jest.mocked(existsSync); | ||
const mockedReadSchemedJsonFile = jest.mocked(readSchemedJsonFile); | ||
const mockedWriteSchemedJsonFile = jest.mocked(writeSchemedJsonFile); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should migrate sources.json to sources-versioned.json', () => { | ||
const oldSourceJsonContent = { | ||
source1: 'http://source1.com/apps.json', | ||
source2: 'http://source2.com/source.json', | ||
}; | ||
const newSourceVersionedJsonContent = { | ||
v1: [ | ||
{ name: 'source1', url: 'http://source1.com/source.json' }, | ||
{ name: 'source2', url: 'http://source2.com/source.json' }, | ||
], | ||
}; | ||
|
||
// Arrange | ||
mockedExistsSync.mockImplementation( | ||
path => path === oldSourcesJsonPath() | ||
); | ||
mockedReadSchemedJsonFile.mockReturnValue(oldSourceJsonContent); | ||
|
||
// Act | ||
migrateSourcesJson(); | ||
|
||
// Assert | ||
expect(mockedWriteSchemedJsonFile).toBeCalledWith( | ||
expect.anything(), | ||
expect.anything(), | ||
newSourceVersionedJsonContent | ||
); | ||
}); | ||
|
||
it('should do nothing if sources.json does not exist', () => { | ||
mockedExistsSync.mockReturnValue(false); | ||
|
||
migrateSourcesJson(); | ||
|
||
expect(mockedReadSchemedJsonFile).not.toBeCalled(); | ||
}); | ||
|
||
it('should do nothing if sources-versioned.json already exists', () => { | ||
mockedExistsSync.mockReturnValue(true); | ||
|
||
migrateSourcesJson(); | ||
|
||
expect(mockedReadSchemedJsonFile).not.toBeCalled(); | ||
}); | ||
}); |
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,40 @@ | ||
/* | ||
* Copyright (c) 2015 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause | ||
*/ | ||
|
||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { z } from 'zod'; | ||
|
||
import { getAppsExternalDir } from '../../config'; | ||
import { readSchemedJsonFile } from '../../fileUtil'; | ||
import { sourcesVersionedJsonPath, writeSourcesFile } from '../sources'; | ||
|
||
export const oldSourcesJsonPath = () => | ||
path.join(getAppsExternalDir(), 'sources.json'); | ||
|
||
const oldSourcesJsonSchema = z.record(z.string(), z.string().url()); | ||
|
||
export const migrateSourcesJson = () => { | ||
if ( | ||
!fs.existsSync(oldSourcesJsonPath()) || | ||
fs.existsSync(sourcesVersionedJsonPath()) | ||
) { | ||
return; | ||
} | ||
|
||
const oldSourcesJson = readSchemedJsonFile( | ||
oldSourcesJsonPath(), | ||
oldSourcesJsonSchema | ||
); | ||
const migratedSources = Object.entries(oldSourcesJson).map( | ||
([name, url]) => ({ | ||
name, | ||
url: url.replace(/apps\.json$/, 'source.json'), | ||
}) | ||
); | ||
|
||
writeSourcesFile(migratedSources); | ||
}; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure I get the intention here, but why is this removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost two years after we switched from using
apps.json
tosource.json
files, I consider this code to be unneeded and wanted to clean up by removing it.The only case when it would still be used, would be if someone would add a URL like https://developer.nordicsemi.com/.pc-tools/nrfconnect-apps/test/apps.json (instead of …/source.json) in the dialog to add a source.