Skip to content

Commit

Permalink
chore: adding config for collie-hub version
Browse files Browse the repository at this point in the history
  • Loading branch information
florianow committed Mar 13, 2024
1 parent af6f98a commit 02778a8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/api/git/GitCliFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export class GitCliFacade {
await this.processRunner.run(["git", "pull"], { cwd: repoDir });
}

getTag(repoDir: string): Promise<ProcessResultWithOutput> {
return this.processRunner.run(["git", "describe", "--tags", "--abbrev=0"], { cwd: repoDir });
async getLatestTag(repoDir: string): Promise<string> {
const result = await this.processRunner.run(["git", "describe", "--tags", "--abbrev=0"], { cwd: repoDir });
return result.stdout.trim();
}

async checkout(repoDir: string, tagValue: string) {
Expand Down
7 changes: 4 additions & 3 deletions src/commands/compliance/import.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GlobalCommandOptions } from "../GlobalCommandOptions.ts";
import { TopLevelCommand } from "../TopLevelCommand.ts";
import { CliApiFacadeFactory } from "../../api/CliApiFacadeFactory.ts";
import { CollieHub } from "../../model/CollieHub.ts";
import { CollieConfig } from "../../model/CollieConfig.ts";

interface ImportOptions {
clean?: boolean;
Expand All @@ -31,16 +32,16 @@ export function registerImportCmd(program: TopLevelCommand) {

const factory = new CliApiFacadeFactory(logger);
const git = factory.buildGit();

const hub = new CollieHub(git, collie);
const config = new CollieConfig(collie, logger);
const hub = new CollieHub(git, collie, config);

if (opts.clean) {
logger.progress("cleaning local cache of collie hub");
await hub.cleanHubClone();
}

logger.progress("updating local cache of collie hub from " + hub.url);
const hubDir = await hub.updateHubClone();
const hubDir = await hub.cloneLatestHub();

id = id || (await promptForComplianceFrameworkId(hubDir));

Expand Down
4 changes: 2 additions & 2 deletions src/commands/config/get.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GlobalCommandOptions } from "../GlobalCommandOptions.ts";
import { TopLevelCommand } from "../TopLevelCommand.ts";
import { CollieConfig } from "../../model/CollieConfig.ts";
import { CollieConfig, CollieConfigProperties } from "../../model/CollieConfig.ts";
import { Logger } from "../../cli/Logger.ts";
import { CollieRepository } from "../../model/CollieRepository.ts";
import { CLI } from "../../info.ts";
Expand All @@ -18,7 +18,7 @@ export function registerGetCmd(program: TopLevelCommand) {
const repo = await CollieRepository.load();
const logger = new Logger(repo, opts);
const config = new CollieConfig(repo, logger);
const value = config.getProperty(property);
const value = config.getProperty(property as keyof CollieConfigProperties);
console.log(value);
},
);
Expand Down
7 changes: 4 additions & 3 deletions src/commands/kit/import.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { KitModuleRepository } from "../../kit/KitModuleRepository.ts";
import { ModelValidator } from "../../model/schemas/ModelValidator.ts";
import { InteractivePrompts } from "../../cli/InteractivePrompts.ts";
import { CollieHub } from "../../model/CollieHub.ts";
import { CollieConfig } from "../../model/CollieConfig.ts";

interface ImportOptions {
clean?: boolean;
Expand All @@ -33,16 +34,16 @@ export function registerImportCmd(program: TopLevelCommand) {

const factory = new CliApiFacadeFactory(logger);
const git = factory.buildGit();

const hub = new CollieHub(git, collie);
const config = new CollieConfig(collie, logger);
const hub = new CollieHub(git, collie, config);

if (opts.clean) {
logger.progress("cleaning local cache of hub modules");
await hub.cleanHubClone();
}

logger.progress("updating local cache of hub modules from " + hub.url);
const hubDir = await hub.updateHubClone();
const hubDir = await hub.cloneLatestHub();

id = id || (await promptForKitModuleId(logger, hubDir));

Expand Down
7 changes: 4 additions & 3 deletions src/model/CollieConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CollieRepository } from "./CollieRepository.ts";

export interface CollieConfigProperties {
foundation?: string;
colliehubVersion?: string;
}

export class CollieConfig {
Expand All @@ -19,8 +20,8 @@ export class CollieConfig {
private configFilePath: string;
private properties: CollieConfigProperties;

getProperty(property: string) {
const value = this.properties["foundation"];
getProperty(property: keyof CollieConfigProperties) {
const value = this.properties[property];
if (value) {
this.logger.verbose(
() => `loaded ${property}="${value}" from ${this.configFilePath}`,
Expand All @@ -30,7 +31,7 @@ export class CollieConfig {
}

async setProperty(
property: "foundation",
property: keyof CollieConfigProperties,
value: string,
) {
this.properties[property] = value;
Expand Down
14 changes: 8 additions & 6 deletions src/model/CollieHub.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as fs from "std/fs";
import { GitCliFacade } from "/api/git/GitCliFacade.ts";
import { CollieRepository } from "/model/CollieRepository.ts";
import { CollieConfig } from "./CollieConfig.ts";

export class CollieHub {
constructor(
private readonly git: GitCliFacade,
private readonly repo: CollieRepository,
private readonly config: CollieConfig
) {}
private readonly hubCacheDirPath = [".collie", "hub"];

Expand Down Expand Up @@ -40,7 +42,7 @@ export class CollieHub {
await fs.copy(srcDir, frameworkDestDir, { overwrite: overwrite });
}

async updateHubClone() {
async cloneLatestHub() {
const hubCacheDir = this.repo.resolvePath(...this.hubCacheDirPath);

// we do keep a git clone of the repo locally because copying on the local FS is much faster than downloading and
Expand All @@ -53,12 +55,12 @@ export class CollieHub {
);
const hasAlreadyCloned = await this.git.isRepo(hubCacheGitDir);

if (hasAlreadyCloned) {
const result = await this.git.getTag(hubCacheDir);
await this.git.checkout(hubCacheDir, result.stdout);
} else {
if (! hasAlreadyCloned) {
await this.git.clone(hubCacheDir, this.url);
}
const latestTag = await this.git.getLatestTag(hubCacheDir);
await this.git.checkout(hubCacheDir, latestTag);
this.config.setProperty("colliehubVersion", latestTag);
}

return hubCacheDir;
}
Expand Down

0 comments on commit 02778a8

Please sign in to comment.