Skip to content

Commit f2c8eed

Browse files
committed
Reorganized and separate all and scoped client
1 parent 97d99de commit f2c8eed

10 files changed

+494
-291
lines changed

packages/telescope/src/builder.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import { plugin as createRegistries } from './generators/create-registries';
1414
import { plugin as createLCDClients } from './generators/create-lcd-clients';
1515
import { plugin as createAggregatedLCDClient } from './generators/create-aggregated-lcd-client';
1616
import { plugin as createLCDClientsScoped } from './generators/create-lcd-client-scoped';
17+
import { plugin as createLCDClientsAll } from './generators/create-lcd-client-all';
1718
import { plugin as createRPCQueryClientsScoped } from './generators/create-rpc-query-client-scoped';
19+
import { plugin as createRPCQueryClientsAll } from './generators/create-rpc-query-client-all';
1820
import { plugin as createRPCMsgClientsScoped } from './generators/create-rpc-msg-client-scoped';
21+
import { plugin as createRPCMsgClientsAll } from './generators/create-rpc-msg-client-all';
1922
import { plugin as createRPCQueryClients } from './generators/create-rpc-query-clients';
2023
import { plugin as createRPCMsgClients } from './generators/create-rpc-msg-clients';
2124
import { plugin as createQueryFuncs } from './generators/create-query-funcs';
@@ -190,10 +193,12 @@ export class TelescopeBuilder {
190193

191194
// post run plugins
192195
bundles.forEach((bundler) => {
196+
createLCDClientsAll(this, bundler);
193197
createLCDClientsScoped(this, bundler);
198+
createRPCQueryClientsAll(this, bundler);
194199
createRPCQueryClientsScoped(this, bundler);
200+
createRPCMsgClientsAll(this,bundler);
195201
createRPCMsgClientsScoped(this, bundler);
196-
197202
createBundle(this, bundler);
198203
});
199204

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import * as dotty from 'dotty';
2+
import { getNestedProto, isRefIncluded, createEmptyProtoRef } from '@cosmology/proto-parser';
3+
import { join } from 'path';
4+
import { TelescopeBuilder } from '../builder';
5+
import { createScopedLCDFactory } from '@cosmology/ast';
6+
import { ALLOWED_RPC_SERVICES, ProtoRef } from '@cosmology/types';
7+
import { fixlocalpaths, getRelativePath } from '../utils';
8+
import { Bundler } from '../bundler';
9+
import { TelescopeParseContext } from '../build';
10+
import { aggregateImports, getImportStatements } from '../imports';
11+
12+
export const plugin = (
13+
builder: TelescopeBuilder,
14+
bundler: Bundler
15+
) => {
16+
17+
// if not enabled, exit
18+
if (!builder.options?.lcdClients?.enabled) {
19+
return;
20+
}
21+
22+
// if no scopes, do them all!
23+
if (
24+
!builder.options.lcdClients.scoped ||
25+
!builder.options.lcdClients.scoped.length ||
26+
!builder.options.lcdClients.scopedIsExclusive
27+
) {
28+
return createAllLCDBundles(
29+
builder,
30+
bundler
31+
);
32+
}
33+
};
34+
35+
const getFileName = (dir, filename) => {
36+
const localname = join(dir, filename ?? 'lcd.ts');
37+
if (localname.endsWith('.ts')) return localname;
38+
return localname + '.ts';
39+
};
40+
41+
const createAllLCDBundles = (
42+
builder: TelescopeBuilder,
43+
bundler: Bundler
44+
) => {
45+
46+
if (!builder.options.lcdClients.bundle) return;
47+
48+
const dir = bundler.bundle.base;
49+
const filename = 'lcd.ts'
50+
51+
// refs with services
52+
const refs = builder.store.getProtos().filter((ref: ProtoRef) => {
53+
const proto = getNestedProto(ref.traversed);
54+
//// Anything except Msg Service OK...
55+
const allowedRpcServices = builder.options.rpcClients.enabledServices.filter(a => a !== 'Msg');
56+
const found = allowedRpcServices.some(svc => {
57+
return proto?.[svc] &&
58+
proto[svc]?.type === 'Service'
59+
});
60+
61+
if (!found) {
62+
return;
63+
}
64+
65+
return true;
66+
});
67+
68+
const check = refs.filter((ref: ProtoRef) => {
69+
const [base] = ref.proto.package.split('.');
70+
return base === bundler.bundle.base;
71+
});
72+
73+
if (!check.length) {
74+
// if there are no services
75+
// exit the plugin
76+
return;
77+
}
78+
79+
const packages = refs.reduce((m, ref: ProtoRef) => {
80+
const [base] = ref.proto.package.split('.');
81+
if (base === 'cosmos' || base === bundler.bundle.base)
82+
return [...new Set([...m, ref.proto.package])];
83+
return m;
84+
}, []);
85+
86+
const methodName = 'createLCDClient'
87+
const localname = getFileName(dir, filename);
88+
89+
const obj = {};
90+
builder.lcdClients.forEach(file => {
91+
92+
// ADD all option
93+
// which defaults to including cosmos
94+
// and defaults to base for each
95+
// if (!packages.includes(file.package)) {
96+
if (!isRefIncluded(createEmptyProtoRef(file.package, file.proto), {
97+
packages,
98+
})) {
99+
return;
100+
}
101+
102+
const f = localname;
103+
const f2 = file.localname;
104+
const importPath = getRelativePath(f, f2);
105+
dotty.put(obj, file.package, importPath);
106+
});
107+
108+
109+
const ctx = new TelescopeParseContext(
110+
createEmptyProtoRef(dir, localname),
111+
builder.store,
112+
builder.options
113+
);
114+
115+
const lcdast = createScopedLCDFactory(
116+
ctx.proto,
117+
obj,
118+
methodName,
119+
'LCDQueryClient' // make option later
120+
);
121+
122+
const imports = aggregateImports(ctx, {}, localname);
123+
124+
const importStmts = getImportStatements(
125+
localname,
126+
[...fixlocalpaths(imports)],
127+
builder.options
128+
);
129+
130+
const prog = []
131+
.concat(importStmts)
132+
.concat(lcdast);
133+
134+
const writeFilename = bundler.getFilename(localname);
135+
bundler.writeAst(prog, writeFilename);
136+
137+
bundler.addToBundleToPackage(`${dir}.ClientFactory`, localname)
138+
139+
};

packages/telescope/src/generators/create-lcd-client-scoped.ts

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,6 @@ export const plugin = (
1919
return;
2020
}
2121

22-
// if no scopes, do them all!
23-
if (
24-
!builder.options.lcdClients.scoped ||
25-
!builder.options.lcdClients.scoped.length
26-
) {
27-
// TODO inefficient
28-
// WE SHOULD NOT DO THIS IN A BUNDLER LOOP
29-
// MAKE SEPARATE PLUGIN
30-
return createAllLCDBundles(
31-
builder,
32-
bundler
33-
);
34-
}
35-
36-
if (!builder.options.lcdClients.scopedIsExclusive) {
37-
// TODO inefficient
38-
// WE SHOULD NOT DO THIS IN A BUNDLER LOOP
39-
// MAKE SEPARATE PLUGIN
40-
createAllLCDBundles(
41-
builder,
42-
bundler
43-
);
44-
}
45-
4622
// we have scopes!
4723
builder.options.lcdClients.scoped.forEach(lcd => {
4824
if (lcd.dir !== bundler.bundle.base) return;
@@ -131,80 +107,3 @@ const makeLCD = (
131107
bundler.addToBundleToPackage(`${dir}.ClientFactory`, localname)
132108
}
133109
};
134-
135-
// TODO
136-
/*
137-
move all options for lcd into previous `lcd` prop and
138-
clean up all these many options for one nested object full of options
139-
*/
140-
141-
const createAllLCDBundles = (
142-
builder: TelescopeBuilder,
143-
bundler: Bundler
144-
) => {
145-
146-
if (!builder.options.lcdClients.bundle) return;
147-
148-
149-
// [x] loop through every bundle
150-
// [x] if not cosmos, add all cosmos
151-
// [x] call makeLCD
152-
// [x] add to bundle
153-
154-
const dir = bundler.bundle.base;
155-
const filename = 'lcd.ts'
156-
157-
///
158-
///
159-
///
160-
161-
// refs with services
162-
const refs = builder.store.getProtos().filter((ref: ProtoRef) => {
163-
const proto = getNestedProto(ref.traversed);
164-
//// Anything except Msg Service OK...
165-
const allowedRpcServices = builder.options.rpcClients.enabledServices.filter(a => a !== 'Msg');
166-
const found = allowedRpcServices.some(svc => {
167-
return proto?.[svc] &&
168-
proto[svc]?.type === 'Service'
169-
});
170-
171-
if (!found) {
172-
return;
173-
}
174-
///
175-
176-
177-
return true;
178-
});
179-
180-
const check = refs.filter((ref: ProtoRef) => {
181-
const [base] = ref.proto.package.split('.');
182-
return base === bundler.bundle.base;
183-
});
184-
185-
if (!check.length) {
186-
// if there are no services
187-
// exit the plugin
188-
return;
189-
}
190-
191-
const packages = refs.reduce((m, ref: ProtoRef) => {
192-
const [base] = ref.proto.package.split('.');
193-
if (base === 'cosmos' || base === bundler.bundle.base)
194-
return [...new Set([...m, ref.proto.package])];
195-
return m;
196-
}, []);
197-
198-
makeLCD(
199-
builder,
200-
bundler,
201-
{
202-
dir,
203-
filename,
204-
packages,
205-
addToBundle: true,
206-
methodName: 'createLCDClient'
207-
}
208-
);
209-
210-
};

0 commit comments

Comments
 (0)