Skip to content

Commit

Permalink
Populate the dependsOn in generated ImplementationGuide resource (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoesel authored and Dylan Mahalingam committed Jan 17, 2020
1 parent 8b27587 commit b9a3cb9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function app() {
// If ig-data exists, generate an IG, otherwise, generate resources only
const igDataPath = path.resolve(input, 'ig-data');
if (fs.existsSync(igDataPath)) {
const igExporter = new IGExporter(outPackage, igDataPath);
const igExporter = new IGExporter(outPackage, defs, igDataPath);
igExporter.export(program.out);
} else {
for (const sd of [
Expand Down
45 changes: 28 additions & 17 deletions src/fhirdefs/FHIRDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class FHIRDefinitions implements Fishable {
private types: Map<string, any>;
private valueSets: Map<string, any>;
private codeSystems: Map<string, any>;
private implementationGuides: Map<string, any>;
packages: string[];

constructor() {
Expand All @@ -18,6 +19,7 @@ export class FHIRDefinitions implements Fishable {
this.types = new Map();
this.valueSets = new Map();
this.codeSystems = new Map();
this.implementationGuides = new Map();
}

size(): number {
Expand All @@ -27,7 +29,8 @@ export class FHIRDefinitions implements Fishable {
this.extensions.size +
this.types.size +
this.valueSets.size +
this.codeSystems.size
this.codeSystems.size +
this.implementationGuides.size
);
}

Expand Down Expand Up @@ -57,28 +60,36 @@ export class FHIRDefinitions implements Fishable {
return cloneJsonMapValues(this.codeSystems);
}

allImplementationGuides(): any[] {
return cloneJsonMapValues(this.implementationGuides);
}

add(definition: any): void {
if (
definition.type === 'Extension' &&
definition.baseDefinition !== 'http://hl7.org/fhir/StructureDefinition/Element'
) {
addDefinitionToMap(definition, this.extensions);
} else if (
definition.kind === 'primitive-type' ||
definition.kind === 'complex-type' ||
definition.kind === 'datatype'
) {
addDefinitionToMap(definition, this.types);
} else if (definition.kind === 'resource') {
if (definition.derivation === 'constraint') {
addDefinitionToMap(definition, this.profiles);
} else {
addDefinitionToMap(definition, this.resources);
if (definition.resourceType === 'StructureDefinition') {
if (
definition.type === 'Extension' &&
definition.baseDefinition !== 'http://hl7.org/fhir/StructureDefinition/Element'
) {
addDefinitionToMap(definition, this.extensions);
} else if (
definition.kind === 'primitive-type' ||
definition.kind === 'complex-type' ||
definition.kind === 'datatype'
) {
addDefinitionToMap(definition, this.types);
} else if (definition.kind === 'resource') {
if (definition.derivation === 'constraint') {
addDefinitionToMap(definition, this.profiles);
} else {
addDefinitionToMap(definition, this.resources);
}
}
} else if (definition.resourceType === 'ValueSet') {
addDefinitionToMap(definition, this.valueSets);
} else if (definition.resourceType === 'CodeSystem') {
addDefinitionToMap(definition, this.codeSystems);
} else if (definition.resourceType === 'ImplementationGuide') {
addDefinitionToMap(definition, this.implementationGuides);
}
}

Expand Down
33 changes: 31 additions & 2 deletions src/ig/IGExporter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs-extra';
import path from 'path';
import ini from 'ini';
import sortBy from 'lodash/sortBy';
import { ensureDirSync, copySync, outputJSONSync, outputFileSync } from 'fs-extra';
import { Package } from '../export';
import { ContactDetail, ImplementationGuide } from '../fhirtypes';
import { logger } from '../utils/FSHLogger';
import sortBy from 'lodash/sortBy';
import { FHIRDefinitions } from '../fhirdefs';

/**
* The IG Exporter exports the FSH artifacts into a file structure supported by the IG Publisher.
Expand All @@ -16,7 +17,11 @@ import sortBy from 'lodash/sortBy';
*/
export class IGExporter {
private ig: ImplementationGuide;
constructor(public readonly pkg: Package, public readonly igDataPath: string) {}
constructor(
private readonly pkg: Package,
private readonly fhirDefs: FHIRDefinitions,
private readonly igDataPath: string
) {}

/**
* Export the IG structure to the location specified by the outPath argument
Expand Down Expand Up @@ -72,6 +77,7 @@ export class IGExporter {
packageId: config.name,
license: config.license,
fhirVersion: ['4.0.1'],
dependsOn: [],
definition: {
resource: [],
page: {
Expand All @@ -88,6 +94,29 @@ export class IGExporter {
}
}
};

// Add the dependencies
if (this.pkg.config.dependencies) {
const igs = this.fhirDefs.allImplementationGuides();
for (const key of Object.keys(this.pkg.config.dependencies)) {
if (key === 'hl7.fhir.r4.core') {
continue;
}
const depIG = igs.find(
ig => ig.packageId === key && ig.version == this.pkg.config.dependencies[key]
);
if (depIG) {
this.ig.dependsOn.push({
uri: `${depIG.url}|${depIG.version}`,
packageId: depIG.packageId,
version: depIG.version
});
}
}
if (this.ig.dependsOn.length === 0) {
delete this.ig.dependsOn;
}
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/export/StructureDefinitionExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
import { loggerSpy, TestFisher } from '../testhelpers';
import { ElementDefinitionType } from '../../src/fhirtypes';
import path from 'path';
import { logger } from '../../src/utils';
import { withDebugLogging } from '../testhelpers/withDebugLogging';

describe('StructureDefinitionExporter', () => {
Expand Down
20 changes: 17 additions & 3 deletions test/ig/IGExporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StructureDefinition, InstanceDefinition, CodeSystem } from '../../src/f
import { Package } from '../../src/export';
import { Config } from '../../src/fshtypes';
import { loggerSpy } from '../testhelpers/loggerSpy';
import { FHIRDefinitions, loadFromPath } from '../../src/fhirdefs';

describe('IGExporter', () => {
// Track temp files/folders for cleanup
Expand All @@ -18,6 +19,12 @@ describe('IGExporter', () => {
let tempOut: string;

beforeAll(() => {
const defs = new FHIRDefinitions();
loadFromPath(
path.join(__dirname, '..', 'testhelpers', 'testdefs', 'package'),
'testPackage',
defs
);
const fixtures = path.join(__dirname, 'fixtures', 'simple-ig');
const config: Config = fs.readJSONSync(path.join(fixtures, 'package.json'));
pkg = new Package(config);
Expand Down Expand Up @@ -47,7 +54,7 @@ describe('IGExporter', () => {
codeSystemDef.description = 'A code system description';
pkg.codeSystems.push(codeSystemDef);

exporter = new IGExporter(pkg, path.resolve(fixtures, 'ig-data'));
exporter = new IGExporter(pkg, defs, path.resolve(fixtures, 'ig-data'));
tempOut = temp.mkdirSync('sushi-test');
// No need to regenerate the IG on every test -- generate it once and inspect what you
// need to in the tests
Expand Down Expand Up @@ -138,6 +145,13 @@ describe('IGExporter', () => {
packageId: 'sushi-test',
license: 'CC0-1.0',
fhirVersion: ['4.0.1'],
dependsOn: [
{
uri: 'http://hl7.org/fhir/us/core/ImplementationGuide/hl7.fhir.us.core|3.1.0',
packageId: 'hl7.fhir.us.core',
version: '3.1.0'
}
],
definition: {
resource: [
{
Expand Down Expand Up @@ -254,7 +268,7 @@ describe('IGExporter', () => {
const fixtures = path.join(__dirname, 'fixtures', 'customized-ig');
const config: Config = fs.readJSONSync(path.join(fixtures, 'package.json'));
pkg = new Package(config);
exporter = new IGExporter(pkg, path.resolve(fixtures, 'ig-data'));
exporter = new IGExporter(pkg, new FHIRDefinitions(), path.resolve(fixtures, 'ig-data'));
tempOut = temp.mkdirSync('sushi-test');
// No need to regenerate the IG on every test -- generate it once and inspect what you
// need to in the tests
Expand Down Expand Up @@ -340,7 +354,7 @@ describe('IGExporter', () => {
const fixtures = path.join(__dirname, 'fixtures', 'invalid-data-ig');
const config: Config = fs.readJSONSync(path.join(fixtures, 'package.json'));
pkg = new Package(config);
exporter = new IGExporter(pkg, path.resolve(fixtures, 'ig-data'));
exporter = new IGExporter(pkg, new FHIRDefinitions(), path.resolve(fixtures, 'ig-data'));
tempOut = temp.mkdirSync('sushi-test');
// No need to regenerate the IG on every test -- generate it once and inspect what you
// need to in the tests
Expand Down
3 changes: 2 additions & 1 deletion test/ig/fixtures/simple-ig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"title" : "FSH Test IG",
"description": "Provides a simple example of how FSH can be used to create an IG",
"dependencies": {
"hl7.fhir.core": "4.0.1"
"hl7.fhir.core": "4.0.1",
"hl7.fhir.us.core": "3.1.0"
},
"language": "en",
"author": "James Tuna",
Expand Down

Large diffs are not rendered by default.

0 comments on commit b9a3cb9

Please sign in to comment.