From f54fd274638ddafebd05e9edf10da43dceba35b6 Mon Sep 17 00:00:00 2001 From: Ben <89335033+bz888@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:52:37 +1300 Subject: [PATCH] update codegen pathing for protogen (#196) * update codegen pathing, add tests; * update changelog * clean up --- packages/common-cosmos/CHANGELOG.md | 2 ++ .../src/codegen/codegen-controller.spec.ts | 20 ++++++++++++++++++- .../src/codegen/codegen-controller.ts | 4 ++-- packages/common-cosmos/src/codegen/util.ts | 5 +++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/common-cosmos/CHANGELOG.md b/packages/common-cosmos/CHANGELOG.md index af2a0630a..0039b30cd 100644 --- a/packages/common-cosmos/CHANGELOG.md +++ b/packages/common-cosmos/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Update codegen pathing for OS consistency (#196) ## [3.2.0] - 2023-11-01 ### Added diff --git a/packages/common-cosmos/src/codegen/codegen-controller.spec.ts b/packages/common-cosmos/src/codegen/codegen-controller.spec.ts index 14a159e2f..6edc20acd 100644 --- a/packages/common-cosmos/src/codegen/codegen-controller.spec.ts +++ b/packages/common-cosmos/src/codegen/codegen-controller.spec.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-3.0 import fs from 'fs'; +import os from 'os'; import path from 'path'; import {promisify} from 'util'; import {CosmosRuntimeDatasource} from '@subql/types-cosmos'; @@ -16,9 +17,12 @@ import { processProtoFilePath, tempProtoDir, } from './codegen-controller'; -import {loadCosmwasmAbis} from './util'; +import {loadCosmwasmAbis, tmpProtoDir} from './util'; const PROJECT_PATH = path.join(__dirname, '../../test/protoTest1'); +const describeIf = (condition: boolean, ...args: Parameters) => + // eslint-disable-next-line jest/valid-describe-callback, jest/valid-title, jest/no-disabled-tests + condition ? describe(...args) : describe.skip(...args); describe('Codegen cosmos', () => { describe('Protobuf to ts', () => { @@ -191,4 +195,18 @@ describe('Codegen cosmos', () => { await promisify(rimraf)(path.join(PROJECT_PATH, 'test.ts')); }); }); + it('ensure correct protoDir on macos', () => { + const protoPath = '/Users/ben/subql-workspace/node/subql/node_modules/@protobufs/amino'; + const tmpDir = '/var/folders/ks/720tmlnn3fj6m4sg91c7spjm0000gn/T/wS0Gob'; + const macosPath = path.join(tmpDir, `${protoPath.replace(path.dirname(protoPath), '')}`); + expect(tmpProtoDir(tmpDir, protoPath)).toEqual(macosPath); + }); + describeIf(os.platform() === 'win32', 'ensure correct protoDir on windowsOs', () => { + it('correct pathing on windows', () => { + const winProtoPath = 'C:\\Users\\zzz\\subql\\subql\\node_modules@protobufs\\amino'; + const winTmpDir = 'C:\\Users\\zzz\\AppData\\Local\\Temp\\GZTuPZ'; + + expect(tmpProtoDir(winTmpDir, winProtoPath)).toEqual('C:\\Users\\zzz\\AppData\\Local\\Temp\\GZTuPZ\\amino'); + }); + }); }); diff --git a/packages/common-cosmos/src/codegen/codegen-controller.ts b/packages/common-cosmos/src/codegen/codegen-controller.ts index d5f573703..995ad2247 100644 --- a/packages/common-cosmos/src/codegen/codegen-controller.ts +++ b/packages/common-cosmos/src/codegen/codegen-controller.ts @@ -12,7 +12,7 @@ import {copySync} from 'fs-extra'; import {IDLObject} from 'wasm-ast-types'; import {isRuntimeCosmosDs} from '../project'; import {COSMWASM_OPTS, TELESCOPE_OPTS} from './constants'; -import {loadCosmwasmAbis} from './util'; +import {loadCosmwasmAbis, tmpProtoDir} from './util'; const TYPE_ROOT_DIR = 'src/types'; @@ -186,7 +186,7 @@ export async function tempProtoDir(projectPath: string): Promise { commonProtoPaths.forEach((p) => { // ensure output format is a dir - copySync(p, path.join(tmpDir, `${p.replace(path.dirname(p), '')}`)); + copySync(p, tmpProtoDir(tmpDir, p)); }); copySync(userProto, tmpDir, {overwrite: true}); return tmpDir; diff --git a/packages/common-cosmos/src/codegen/util.ts b/packages/common-cosmos/src/codegen/util.ts index d69b43bd8..5a471ea70 100644 --- a/packages/common-cosmos/src/codegen/util.ts +++ b/packages/common-cosmos/src/codegen/util.ts @@ -1,6 +1,7 @@ // Copyright 2020-2023 SubQuery Pte Ltd authors & contributors // SPDX-License-Identifier: GPL-3.0 +import path from 'path'; import {loadFromJsonOrYaml} from '@subql/common'; import {IDLObject} from 'wasm-ast-types'; import {parseCosmosProjectManifest, ProjectManifestImpls} from '../project'; @@ -16,3 +17,7 @@ export function validateCosmosManifest(manifest: unknown): manifest is ProjectMa export function loadCosmwasmAbis(filePath: string): IDLObject { return loadFromJsonOrYaml(filePath) as IDLObject; } + +export function tmpProtoDir(tmpDir: string, protoPath: string): string { + return path.join(tmpDir, path.basename(protoPath)); +}