Skip to content

Commit

Permalink
Reorganized and separate all and scoped client
Browse files Browse the repository at this point in the history
  • Loading branch information
NorOldBurden committed Dec 16, 2024
1 parent 97d99de commit 4a4e751
Show file tree
Hide file tree
Showing 10 changed files with 497 additions and 294 deletions.
7 changes: 6 additions & 1 deletion packages/telescope/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import { plugin as createRegistries } from './generators/create-registries';
import { plugin as createLCDClients } from './generators/create-lcd-clients';
import { plugin as createAggregatedLCDClient } from './generators/create-aggregated-lcd-client';
import { plugin as createLCDClientsScoped } from './generators/create-lcd-client-scoped';
import { plugin as createLCDClientsAll } from './generators/create-lcd-client-all';
import { plugin as createRPCQueryClientsScoped } from './generators/create-rpc-query-client-scoped';
import { plugin as createRPCQueryClientsAll } from './generators/create-rpc-query-client-all';
import { plugin as createRPCMsgClientsScoped } from './generators/create-rpc-msg-client-scoped';
import { plugin as createRPCMsgClientsAll } from './generators/create-rpc-msg-client-all';
import { plugin as createRPCQueryClients } from './generators/create-rpc-query-clients';
import { plugin as createRPCMsgClients } from './generators/create-rpc-msg-clients';
import { plugin as createQueryFuncs } from './generators/create-query-funcs';
Expand Down Expand Up @@ -190,10 +193,12 @@ export class TelescopeBuilder {

// post run plugins
bundles.forEach((bundler) => {
createLCDClientsAll(this, bundler);
createLCDClientsScoped(this, bundler);
createRPCQueryClientsAll(this, bundler);
createRPCQueryClientsScoped(this, bundler);
createRPCMsgClientsAll(this,bundler);
createRPCMsgClientsScoped(this, bundler);

createBundle(this, bundler);
});

Expand Down
139 changes: 139 additions & 0 deletions packages/telescope/src/generators/create-lcd-client-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as dotty from 'dotty';
import { getNestedProto, isRefIncluded, createEmptyProtoRef } from '@cosmology/proto-parser';
import { join } from 'path';
import { TelescopeBuilder } from '../builder';
import { createScopedLCDFactory } from '@cosmology/ast';
import { ALLOWED_RPC_SERVICES, ProtoRef } from '@cosmology/types';
import { fixlocalpaths, getRelativePath } from '../utils';
import { Bundler } from '../bundler';
import { TelescopeParseContext } from '../build';
import { aggregateImports, getImportStatements } from '../imports';

export const plugin = (
builder: TelescopeBuilder,
bundler: Bundler
) => {

// if not enabled, exit
if (!builder.options?.lcdClients?.enabled) {
return;
}

// if no scopes, do them all!
if (
!builder.options.lcdClients.scoped ||
!builder.options.lcdClients.scoped.length ||
!builder.options.lcdClients.scopedIsExclusive
) {
return createAllLCDBundles(
builder,
bundler
);
}
};

const getFileName = (dir, filename) => {
const localname = join(dir, filename ?? 'lcd.ts');
if (localname.endsWith('.ts')) return localname;
return localname + '.ts';
};

const createAllLCDBundles = (
builder: TelescopeBuilder,
bundler: Bundler
) => {

if (!builder.options.lcdClients.bundle) return;

const dir = bundler.bundle.base;
const filename = 'lcd.ts'

// refs with services
const refs = builder.store.getProtos().filter((ref: ProtoRef) => {
const proto = getNestedProto(ref.traversed);
//// Anything except Msg Service OK...
const allowedRpcServices = builder.options.rpcClients.enabledServices.filter(a => a !== 'Msg');
const found = allowedRpcServices.some(svc => {
return proto?.[svc] &&
proto[svc]?.type === 'Service'
});

if (!found) {
return;
}

return true;
});

const check = refs.filter((ref: ProtoRef) => {
const [base] = ref.proto.package.split('.');
return base === bundler.bundle.base;
});

if (!check.length) {
// if there are no services
// exit the plugin
return;
}

const packages = refs.reduce((m, ref: ProtoRef) => {
const [base] = ref.proto.package.split('.');
if (base === 'cosmos' || base === bundler.bundle.base)
return [...new Set([...m, ref.proto.package])];
return m;
}, []);

const methodName = 'createLCDClient'
const localname = getFileName(dir, filename);

const obj = {};
builder.lcdClients.forEach(file => {

// ADD all option
// which defaults to including cosmos
// and defaults to base for each
// if (!packages.includes(file.package)) {
if (!isRefIncluded(createEmptyProtoRef(file.package, file.proto), {
packages,
})) {
return;
}

const f = localname;
const f2 = file.localname;
const importPath = getRelativePath(f, f2);
dotty.put(obj, file.package, importPath);
});


const ctx = new TelescopeParseContext(
createEmptyProtoRef(dir, localname),
builder.store,
builder.options
);

const lcdast = createScopedLCDFactory(
ctx.proto,
obj,
methodName,
'LCDQueryClient' // make option later
);

const imports = aggregateImports(ctx, {}, localname);

const importStmts = getImportStatements(
localname,
[...fixlocalpaths(imports)],
builder.options
);

const prog = []
.concat(importStmts)
.concat(lcdast);

const writeFilename = bundler.getFilename(localname);
bundler.writeAst(prog, writeFilename);

bundler.addToBundleToPackage(`${dir}.ClientFactory`, localname)

};
103 changes: 1 addition & 102 deletions packages/telescope/src/generators/create-lcd-client-scoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,8 @@ export const plugin = (
return;
}

// if no scopes, do them all!
if (
!builder.options.lcdClients.scoped ||
!builder.options.lcdClients.scoped.length
) {
// TODO inefficient
// WE SHOULD NOT DO THIS IN A BUNDLER LOOP
// MAKE SEPARATE PLUGIN
return createAllLCDBundles(
builder,
bundler
);
}

if (!builder.options.lcdClients.scopedIsExclusive) {
// TODO inefficient
// WE SHOULD NOT DO THIS IN A BUNDLER LOOP
// MAKE SEPARATE PLUGIN
createAllLCDBundles(
builder,
bundler
);
}

// we have scopes!
builder.options.lcdClients.scoped.forEach(lcd => {
builder.options.lcdClients.scoped?.forEach(lcd => {
if (lcd.dir !== bundler.bundle.base) return;
makeLCD(
builder,
Expand Down Expand Up @@ -131,80 +107,3 @@ const makeLCD = (
bundler.addToBundleToPackage(`${dir}.ClientFactory`, localname)
}
};

// TODO
/*
move all options for lcd into previous `lcd` prop and
clean up all these many options for one nested object full of options
*/

const createAllLCDBundles = (
builder: TelescopeBuilder,
bundler: Bundler
) => {

if (!builder.options.lcdClients.bundle) return;


// [x] loop through every bundle
// [x] if not cosmos, add all cosmos
// [x] call makeLCD
// [x] add to bundle

const dir = bundler.bundle.base;
const filename = 'lcd.ts'

///
///
///

// refs with services
const refs = builder.store.getProtos().filter((ref: ProtoRef) => {
const proto = getNestedProto(ref.traversed);
//// Anything except Msg Service OK...
const allowedRpcServices = builder.options.rpcClients.enabledServices.filter(a => a !== 'Msg');
const found = allowedRpcServices.some(svc => {
return proto?.[svc] &&
proto[svc]?.type === 'Service'
});

if (!found) {
return;
}
///


return true;
});

const check = refs.filter((ref: ProtoRef) => {
const [base] = ref.proto.package.split('.');
return base === bundler.bundle.base;
});

if (!check.length) {
// if there are no services
// exit the plugin
return;
}

const packages = refs.reduce((m, ref: ProtoRef) => {
const [base] = ref.proto.package.split('.');
if (base === 'cosmos' || base === bundler.bundle.base)
return [...new Set([...m, ref.proto.package])];
return m;
}, []);

makeLCD(
builder,
bundler,
{
dir,
filename,
packages,
addToBundle: true,
methodName: 'createLCDClient'
}
);

};
Loading

0 comments on commit 4a4e751

Please sign in to comment.