-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d1aa91
commit 237fa06
Showing
13 changed files
with
241 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Argv } from 'yargs'; | ||
|
||
import { runner } from '../utils/process'; | ||
import { createLogger } from '../services/logger.service'; | ||
|
||
import { initWorkspaceTask } from './tasks/init-workspace.task'; | ||
import { initJobTask } from './tasks/init-job.task'; | ||
import { runJobsTask } from './tasks/run-jobs.task'; | ||
import { retrieveSettingsTask } from '../tasks/retrieve-settings.task'; | ||
|
||
const logger = createLogger('BATCH'); | ||
|
||
export const command = 'batch'; | ||
|
||
export const aliases = ['b']; | ||
|
||
export const describe = | ||
'Check out and analyze multiple project repositories and upload results to Omniboard.dev'; | ||
|
||
export const builder = (yargs: Argv) => | ||
yargs | ||
.option('job-path', { | ||
type: 'string', | ||
default: './omniboard-job.json', | ||
description: 'Location of Omniboard batch job file', | ||
}) | ||
.option('workspace-path', { | ||
type: 'string', | ||
default: './omniboard-workspace', | ||
description: 'Location where the Omniboard batch workspace is stored', | ||
}); | ||
|
||
export const handler = async (argv: any) => | ||
runner( | ||
[retrieveSettingsTask, initJobTask, initWorkspaceTask, runJobsTask], | ||
argv, | ||
logger | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ListrTask } from 'listr2'; | ||
|
||
import { Context } from '../../interface'; | ||
import { | ||
getRepoNameFromUrl, | ||
cloneRepo, | ||
pullLatest, | ||
} from '../../services/git.service'; | ||
import { directoryExists, pathJoin } from '../../services/fs.service'; | ||
|
||
export function initJobRepo(job: string): ListrTask { | ||
return { | ||
title: 'Init job repo', | ||
task: async (ctx: Context, task) => { | ||
const { workspacePath, verbose } = ctx.options; | ||
const repoName = getRepoNameFromUrl(job); | ||
const repoPath = pathJoin(workspacePath, repoName); | ||
if (!directoryExists(repoPath)) { | ||
await cloneRepo(job, workspacePath); | ||
task.title = `${task.title}, cloned`; | ||
} else { | ||
await pullLatest(repoPath); | ||
task.title = `${task.title}, updated`; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ListrTask } from 'listr2'; | ||
|
||
import { BatchJob, Context } from '../../interface'; | ||
import { readJson, writeJson } from '../../services/fs.service'; | ||
|
||
const DEFAULT_JOB: BatchJob = { | ||
running: '', | ||
queue: [], | ||
completed: [], | ||
failed: [], | ||
}; | ||
|
||
export const initJobTask: ListrTask = { | ||
title: 'Init job', | ||
task: async (ctx: Context, task) => { | ||
const { jobPath } = ctx.options; | ||
let job = readJson(jobPath); | ||
if (!job) { | ||
job = DEFAULT_JOB; | ||
writeJson(jobPath, job); | ||
task.title = `${task.title} - created new job file at ${jobPath}`; | ||
} else { | ||
task.title = `${task.title} successful, found ${job.queue?.length} jobs`; | ||
} | ||
ctx.batchJob = job; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ListrTask } from 'listr2'; | ||
|
||
import { Context } from '../../interface'; | ||
import { ensureDirectoryExists } from '../../services/fs.service'; | ||
|
||
export const initWorkspaceTask: ListrTask = { | ||
title: 'Init workspace', | ||
task: async (ctx: Context, task) => { | ||
const { workspacePath } = ctx.options; | ||
ensureDirectoryExists(workspacePath); | ||
task.title = `${task.title} successful, workspace initialized ${workspacePath}`; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ListrTask } from 'listr2'; | ||
|
||
import { Context } from '../../interface'; | ||
|
||
import { initJobRepo } from './init-job-repo.task'; | ||
|
||
export function runJobTaskFactory(job: string): ListrTask { | ||
return { | ||
title: `${job}`, | ||
task: async (ctx: Context, task) => { | ||
return task.newListr([initJobRepo(job)]); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ListrTask } from 'listr2'; | ||
|
||
import { Context } from '../../interface'; | ||
|
||
import { runJobTaskFactory } from './run-job.task'; | ||
|
||
export const runJobsTask: ListrTask = { | ||
title: 'Run jobs', | ||
task: async (ctx: Context, task) => | ||
task.newListr( | ||
ctx.batchJob.queue.map((queuedJob) => runJobTaskFactory(queuedJob)) | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { run } from './shell.service'; | ||
|
||
export function getRepoNameFromUrl(url: string): string { | ||
const parts = url.split('/'); | ||
return parts[parts.length - 1].replace(/\.git$/, ''); | ||
} | ||
|
||
export async function cloneRepo(url: string, targetDir: string) { | ||
return await run(`git clone --depth 1 ${url}`, targetDir); | ||
} | ||
|
||
export async function pullLatest(targetDir: string) { | ||
return await run(`git checkout --force && git pull`, targetDir); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import cp from 'child_process'; | ||
import { promisify } from 'util'; | ||
|
||
const execAsync = promisify(cp.exec); | ||
const MAX_BUFFER_SIZE = 1 * 1024 * 1024; | ||
|
||
export async function run( | ||
command: string, | ||
targetDir: string | ||
): Promise<CommandResult> { | ||
return await execAsync(command, { | ||
cwd: targetDir, | ||
maxBuffer: MAX_BUFFER_SIZE, | ||
}); | ||
} | ||
|
||
export interface CommandResult { | ||
stdout: string; | ||
stderr: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.