Skip to content

Commit

Permalink
Merge pull request #140 from jo-chemla/main
Browse files Browse the repository at this point in the history
Add `mergeJson` command
  • Loading branch information
javagl authored Jul 5, 2024
2 parents ccbfbdc + 903bd2c commit 2a47318
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@
"package-prepare": "npm run eslint && npm run prettier-check && npm run build && npm run test && npm run coverage && npm run docs-generate && npm run generate-third-party",
"package": "npm run package-clean && npm run package-prepare && npm pack"
}
}
}
28 changes: 28 additions & 0 deletions specs/tools/tilesetProcessing/TilesetMergerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,32 @@ describe("TilesetMerger", function () {
];
expect(actualContentUris).toEqual(expectedContentUris);
});

it("merges tilesets into a single tileset for mergeJson", async function () {
await TilesetOperations.mergeJson(basicInputs, basicOutput, overwrite);

// Ensure that the output directory contains the expected files:
const actualRelativeFiles =
SpecHelpers.collectRelativeFileNames(basicOutput);
actualRelativeFiles.sort();
const expectedRelativeFiles = ["tileset.json"];
expect(actualRelativeFiles).toEqual(expectedRelativeFiles);

// Ensure that the single 'tileset.json' contains the
// proper content URIs for the external tilesets:
const tilesetJsonBuffer = fs.readFileSync(
Paths.join(basicOutput, "tileset.json")
);
const tileset = JSON.parse(tilesetJsonBuffer.toString());
const actualContentUris = await SpecHelpers.collectExplicitContentUris(
tileset.root
);
actualContentUris.sort();

const expectedContentUris = [
"specs/data/mergeTilesets/basicMerge/TilesetA/tileset.json",
"specs/data/mergeTilesets/basicMerge/sub/TilesetA/tileset.json",
];
expect(actualContentUris).toEqual(expectedContentUris);
});
});
12 changes: 12 additions & 0 deletions src/cli/ToolsMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,18 @@ export class ToolsMain {
logger.debug(`Executing merge DONE`);
}

static async mergeJson(inputs: string[], output: string, force: boolean) {
logger.debug(`Executing mergeJson`);
logger.debug(` inputs: ${inputs}`);
logger.debug(` output: ${output}`);
logger.debug(` force: ${force}`);

ToolsMain.ensureCanWrite(output, force);
await TilesetOperations.mergeJson(inputs, output, force);

logger.debug(`Executing merge DONE`);
}

static async pipeline(input: string, force: boolean) {
logger.debug(`Executing pipeline`);
logger.debug(` input: ${input}`);
Expand Down
15 changes: 14 additions & 1 deletion src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,18 @@ function parseToolArgs(a: string[]) {
.command(
"merge",
"Merge any number of tilesets together into a single tileset.",
{ i: inputArrayDefinition, o: outputStringDefinition }
{
i: inputArrayDefinition,
o: outputStringDefinition,
}
)
.command(
"mergeJson",
"Merge any number of tilesets together into a single tileset without copying resources to output directory.",
{
i: inputArrayDefinition,
o: outputStringDefinition,
}
)
.command(
"upgrade",
Expand Down Expand Up @@ -537,6 +548,8 @@ async function runCommand(command: string, toolArgs: any, optionArgs: any) {
);
} else if (command === "merge") {
await ToolsMain.merge(inputs, output, force);
} else if (command === "mergeJson") {
await ToolsMain.mergeJson(inputs, output, force);
} else if (command === "pipeline") {
await ToolsMain.pipeline(input, force);
} else if (command === "analyze") {
Expand Down
54 changes: 50 additions & 4 deletions src/tools/tilesetProcessing/TilesetMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,50 @@ export class TilesetMerger {
tilesetSourceNames: string[],
tilesetTargetName: string,
overwrite: boolean
): Promise<void> {
return this.mergeOperation(
tilesetSourceNames,
tilesetTargetName,
overwrite,
false
);
}

/**
* Merges the tileset from the specified sources into one tileset
* that refers to the sources as external ones, and writes the
* result into the given target without copying resources to
* output directory.
*
* @param tilesetSourceNames - The tileset source names
* @param tilesetTargetName - The tileset target name
* @param overwrite - Whether target files should be overwritten
* @returns A promise that resolves when the process is finished
* @throws TilesetError When the input could not be processed
* @throws TilesetError When the output already exists
* and `overwrite` was `false`.
*/
async mergeJson(
tilesetSourceNames: string[],
tilesetTargetName: string,
overwrite: boolean
): Promise<void> {
return this.mergeOperation(
tilesetSourceNames,
tilesetTargetName,
overwrite,
true
);
}

/**
* Internal method to differentiate between `merge` and `mergeJson`
*/
async mergeOperation(
tilesetSourceNames: string[],
tilesetTargetName: string,
overwrite: boolean,
jsonOnly: boolean
): Promise<void> {
// Create the sources and target
for (const tilesetSourceName of tilesetSourceNames) {
Expand All @@ -106,7 +150,9 @@ export class TilesetMerger {
const tilesetSource = TilesetSources.createAndOpen(tilesetSourceName);
this.tilesetSources.push(tilesetSource);
this.tilesetSourceJsonFileNames.push(tilesetSourceJsonFileName);
this.tilesetSourceIdentifiers.push(tilesetSourceIdentifier);
this.tilesetSourceIdentifiers.push(
!jsonOnly ? tilesetSourceIdentifier : tilesetSourceName
);
}

this.tilesetTargetJsonFileName =
Expand All @@ -117,7 +163,7 @@ export class TilesetMerger {
);

// Perform the actual merge
this.mergeInternal();
this.mergeInternal(jsonOnly);

// Clean up by closing the sources and the target
for (const tilesetSource of this.tilesetSources) {
Expand All @@ -134,7 +180,7 @@ export class TilesetMerger {
/**
* Internal method for `merge`
*/
private mergeInternal() {
private mergeInternal(jsonOnly: boolean) {
if (
this.tilesetSources.length == 0 ||
!this.tilesetTarget ||
Expand Down Expand Up @@ -192,7 +238,7 @@ export class TilesetMerger {
);

// Copy the resources from the sources to the target
this.copyResources();
if (!jsonOnly) this.copyResources();
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/tools/tilesetProcessing/TilesetOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ export class TilesetOperations {
await tilesetMerger.merge(tilesetSourceNames, tilesetTargetName, overwrite);
}

/**
* Performs the `mergeJson` command line operation.
*
* @param tilesetSourceName - The tileset source name
* @param tilesetTargetName - The tileset target name
* @param overwrite - Whether the target should be overwritten if
* it already exists
* @returns A promise that resolves when the process is finished
* @throws TilesetError When the input could not be processed,
* or when the output already exists and `overwrite` was `false`.
*/
static async mergeJson(
tilesetSourceNames: string[],
tilesetTargetName: string,
overwrite: boolean
): Promise<void> {
const tilesetMerger = new TilesetMerger();
await tilesetMerger.mergeJson(
tilesetSourceNames,
tilesetTargetName,
overwrite
);
}

/**
* Performs the `upgrade` command line operation.
*
Expand Down

0 comments on commit 2a47318

Please sign in to comment.