Skip to content

Commit 4533daa

Browse files
committed
Allow alias, path and git to declare multiple things at once (clean up the config file by alot)
1 parent bf6b0c9 commit 4533daa

6 files changed

Lines changed: 55 additions & 35 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "default",
3-
"version": "0.16.1",
3+
"version": "0.17.0-beta4",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {

src/resources/git/repository/git-repository.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ export class GitRepositoryResource extends Resource<GitRepositoryConfig> {
116116
return null;
117117
}
118118

119-
console.log('Refresh', {
120-
parentDirectory: parameters.parentDirectory,
121-
repositories,
122-
autoVerifySSH: parameters.autoVerifySSH,
123-
})
124-
125119
return {
126120
parentDirectory: parameters.parentDirectory,
127121
repositories,
@@ -209,7 +203,7 @@ Please delete ${plan.currentConfig.directory ?? (plan.currentConfig.repositories
209203

210204
const baseUrl = groups!.url!
211205
const { data: existingKey } = await $.spawnSafe(`ssh-keygen -F ${baseUrl}`)
212-
console.log(`Is key blank: ${this.isBlank(existingKey)}`)
206+
213207
if (!this.isBlank(existingKey)) {
214208
// An existing key is already in the file. Skipping..
215209
return;

src/resources/shell/aliases/aliases-resource.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import { Utils } from '../../../utils/index.js';
1818

1919
const ALIAS_REGEX = /^'?([^=]+?)'?='?(.*?)'?$/
2020

21+
interface AliasDeclaration {
22+
alias: string;
23+
value: string;
24+
}
25+
2126
export const schema = z.object({
2227
aliases: z
2328
.array(z.object({
@@ -26,10 +31,14 @@ export const schema = z.object({
2631
}))
2732
.describe('Aliases to create')
2833
.optional(),
34+
declarationsOnly: z.boolean().default(false),
2935
})
3036

3137
export type AliasesConfig = z.infer<typeof schema>;
3238
export class AliasesResource extends Resource<AliasesConfig> {
39+
private readonly ALIAS_DECLARATION_REGEX = /^\s*alias\s+([A-Z_a-z][\w-]*)\s*=\s*(["']?)(.+?)\2\s*(?:#.*)?$/gm;
40+
readonly filePaths = Utils.getShellRcFiles()
41+
3342
getSettings(): ResourceSettings<AliasesConfig> {
3443
return {
3544
id: 'aliases',
@@ -43,37 +52,46 @@ export class AliasesResource extends Resource<AliasesConfig> {
4352
filterInStatelessMode: (desired, current) =>
4453
current.filter((c) => desired.some((d) => d.alias === c.alias)),
4554
canModify: true,
46-
}
55+
},
56+
declarationsOnly: { default: false, setting: true },
4757
},
4858
importAndDestroy: {
49-
requiredParameters: ['aliases'],
50-
},
51-
allowMultiple: {
52-
identifyingParameters: ['alias'],
53-
},
59+
refreshMapper(input) {
60+
if ((input.aliases?.length === 0 || !input?.aliases) && input?.aliases === undefined) {
61+
return { aliases: [], declarationsOnly: true };
62+
}
63+
64+
return input;
65+
}
66+
}
5467
}
5568
}
5669

5770
override async refresh(parameters: any, context: RefreshContext<AliasesConfig>): Promise<Partial<AliasesConfig> | null> {
5871
const $ = getPty();
5972

6073
const { data, status } = await $.spawnSafe('alias', { interactive: true });
61-
62-
console.log('Data', data);
63-
6474
if (status === SpawnStatus.ERROR) {
6575
return null;
6676
}
6777

68-
const aliases = data.split(/\n/g)
78+
let aliases = data.split(/\n/g)
6979
.map((l) => l.trim())
7080
.map((l) => l.match(ALIAS_REGEX))
7181
.filter(Boolean)
7282
.map((m) => (m ? { alias: m[1], value: m[2] } : null))
7383
.filter(Boolean) as Array<{ alias: string; value: string }>;
7484

75-
console.log('Command type', context.commandType);
76-
console.log('Aliases', aliases);
85+
if (parameters.declarationsOnly) {
86+
const aliasDeclarations: AliasDeclaration[] = [];
87+
for (const file of this.filePaths) {
88+
if (await FileUtils.fileExists(file)) {
89+
aliasDeclarations.push(...this.findAllDeclarations(await fs.readFile(file, 'utf8')));
90+
}
91+
}
92+
93+
aliases = aliases.filter((a) => aliasDeclarations.some((d) => d.alias === a.alias));
94+
}
7795

7896
// If validation plan and no aliases match, return null
7997
if (context.commandType === 'validationPlan'
@@ -128,15 +146,12 @@ export class AliasesResource extends Resource<AliasesConfig> {
128146
?.filter((a) => !pc.previousValue?.some((c) => c.alias === a.alias)
129147
|| pc.previousValue?.some((c) => c.alias === a.alias && c.value !== a.value));
130148

131-
console.log('Aliases to add', aliasesToAdd);
132-
133149
await this.removeAliases(aliasesToRemove);
134150
await this.addAliases(aliasesToAdd);
135151
}
136152
}
137153

138154
async destroy(plan: DestroyPlan<AliasesConfig>): Promise<void> {
139-
console.log(plan.currentConfig.aliases);
140155
await this.removeAliases(plan.currentConfig.aliases ?? []);
141156
}
142157

@@ -192,6 +207,16 @@ export class AliasesResource extends Resource<AliasesConfig> {
192207
await FileUtils.addToStartupFile(aliasString);
193208
}
194209
}
195-
}
196210

211+
findAllDeclarations(contents: string): AliasDeclaration[] {
212+
const results = [];
213+
const aliasDeclarations = contents.matchAll(this.ALIAS_DECLARATION_REGEX);
214+
215+
for (const declaration of aliasDeclarations) {
216+
const [_, alias, __, value ] = declaration;
217+
results.push({ alias, value });
218+
}
197219

220+
return results;
221+
}
222+
}

src/resources/shell/path/path-resource.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ export class PathResource extends Resource<PathConfig> {
5151
}
5252
},
5353
allowMultiple: {
54-
matcher: (desired, current) => {
54+
matcher(desired, current) {
5555
if (desired.path) {
5656
return desired.path === current.path;
5757
}
5858

5959
const currentPaths = new Set(current.paths)
6060
return desired.paths?.some((p) => currentPaths.has(p)) ?? false;
61+
},
62+
async findAllParameters() {
63+
return [{
64+
paths: []
65+
}]
6166
}
6267
}
6368
}
@@ -249,6 +254,12 @@ export class PathResource extends Resource<PathConfig> {
249254

250255
return results;
251256
}
257+
258+
private async resolvePathWithVariables(pathWithVariables: string): Promise<string> {
259+
const $ = getPty();
260+
const { data } = await $.spawnSafe(`echo ${pathWithVariables}`);
261+
return data.trim();
262+
}
252263
}
253264

254265
interface PathDeclaration {

src/resources/tart/tart-vm.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,7 @@ export class TartVmResource extends Resource<TartVmConfig> {
7272
// Parse the JSON output to get the list of VMs
7373
try {
7474
const vmList = JSON.parse(data);
75-
console.log('VM list:', vmList);
76-
console.log('Local name:', parameters.localName);
77-
console.log('Includes VM:', vmList.some((vm: { Name: string }) => vm.Name === parameters.localName));
78-
7975
if (!vmList.some((vm: { Name: string }) => vm.Name === parameters.localName)) {
80-
console.log('Not found! returning null')
81-
8276
return null;
8377
}
8478
} catch(e) {
@@ -96,8 +90,6 @@ export class TartVmResource extends Resource<TartVmConfig> {
9690
try {
9791
// Get VM configuration using tart get
9892
const { status: getStatus, data: getData } = await $.spawnSafe(`tart get ${parameters.localName} --format json`);
99-
console.log('Get data:', getData);
100-
console.log('Status:', getStatus);
10193

10294
if (getStatus === SpawnStatus.SUCCESS) {
10395
// Parse the output to extract configuration

src/resources/xcode-tools/xcode-tools.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ export class XcodeToolsResource extends Resource<XCodeToolsConfig> {
6767
return compare(coerce(currentVer)!, coerce(prevVer)!) > 0 ? current : prev;
6868
}) : xcodeToolsVersion.at(0)!;
6969

70-
console.log('Latest version', latestVersion);
71-
7270
await $.spawn(`softwareupdate -i "${latestVersion}" --verbose`, { requiresRoot: true, interactive: true });
7371

7472
} finally {

0 commit comments

Comments
 (0)