Skip to content

Commit

Permalink
refactor: add processServices alongside processModules helper
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed May 24, 2018
1 parent d5ba05b commit 4871022
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 62 deletions.
8 changes: 6 additions & 2 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ export class BuildCommand extends Command<typeof buildArguments, typeof buildOpt

ctx.log.header({ emoji: "hammer", command: "build" })

const result = await ctx.processModules(modules, opts.watch, async (module) => {
await ctx.addTask(await BuildTask.factory({ ctx, module, force: opts.force }))
const result = await ctx.processModules({
modules,
watch: opts.watch,
process: async (module) => {
return [await BuildTask.factory({ ctx, module, force: opts.force })]
},
})

ctx.log.info("")
Expand Down
13 changes: 6 additions & 7 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ export class DeployCommand extends Command<typeof deployArgs, typeof deployOpts>
const force = opts.force
const forceBuild = opts["force-build"]

const modules = Array.from(new Set(services.map(s => s.module)))

const result = await ctx.processModules(modules, watch, async (module) => {
const servicesToDeploy = (await module.getServices()).filter(s => names.includes(s.name))
for (const service of servicesToDeploy) {
await ctx.addTask(await DeployTask.factory({ ctx, service, force, forceBuild }))
}
const result = await ctx.processServices({
services,
watch,
process: async (service) => {
return [await DeployTask.factory({ ctx, service, force, forceBuild })]
},
})

ctx.log.info("")
Expand Down
23 changes: 13 additions & 10 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { join } from "path"
import { STATIC_DIR } from "../constants"
import { spawnSync } from "child_process"
import chalk from "chalk"
import Bluebird = require("bluebird")
import moment = require("moment")

const imgcatPath = join(STATIC_DIR, "imgcat")
Expand Down Expand Up @@ -47,16 +46,20 @@ export class DevCommand extends Command {
return
}

await ctx.processModules(modules, true, async (module) => {
const testTasks: Task[] = await module.getTestTasks({})
const deployTasks = await module.getDeployTasks({})
const tasks = testTasks.concat(deployTasks)
await ctx.processModules({
modules,
watch: true,
process: async (module) => {
const testTasks: Task[] = await module.getTestTasks({})
const deployTasks = await module.getDeployTasks({})
const tasks = testTasks.concat(deployTasks)

if (tasks.length === 0) {
await ctx.addTask(await BuildTask.factory({ ctx, module, force: false }))
} else {
await Bluebird.map(tasks, ctx.addTask)
}
if (tasks.length === 0) {
return [await BuildTask.factory({ ctx, module, force: false })]
} else {
return tasks
}
},
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { BooleanParameter, Command, ParameterValues, StringParameter } from "./b
import { values } from "lodash"
import chalk from "chalk"
import { TaskResults } from "../task-graph"
import Bluebird = require("bluebird")

export const testArgs = {
module: new StringParameter({
Expand Down Expand Up @@ -55,9 +54,10 @@ export class TestCommand extends Command<typeof testArgs, typeof testOpts> {
const force = opts.force
const forceBuild = opts["force-build"]

const results = await ctx.processModules(modules, opts.watch, async (module) => {
const tasks = await module.getTestTasks({ group, force, forceBuild })
return Bluebird.map(tasks, ctx.addTask)
const results = await ctx.processModules({
modules,
watch: opts.watch,
process: async (module) => module.getTestTasks({ group, force, forceBuild }),
})

const failed = values(results).filter(r => !!r.error).length
Expand Down
91 changes: 52 additions & 39 deletions src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import chalk from "chalk"
import {
Garden,
} from "./garden"
import {
LogEntry,
} from "./logger"
import { EntryStyle } from "./logger/types"
import { TaskResults } from "./task-graph"
import { DeployTask } from "./tasks/deploy"
import {
PrimitiveMap,
} from "./types/common"
Expand Down Expand Up @@ -79,8 +75,11 @@ import {
padEnd,
keyBy,
omit,
flatten,
} from "lodash"
import { Task } from "./types/task"
import {
getNames,
Omit,
registerCleanupFunction,
sleep,
Expand Down Expand Up @@ -123,6 +122,18 @@ export type WrappedFromGarden = Pick<Garden,
"addTask" |
"processTasks">

export interface ProcessModulesParams {
modules: Module[],
watch: boolean,
process: (module: Module) => Promise<Task[]>,
}

export interface ProcessServicesParams {
services: Service[],
watch: boolean,
process: (service: Service) => Promise<Task[]>,
}

export interface PluginContext extends PluginContextGuard, WrappedFromGarden {
getEnvironmentStatus: (params: {}) => Promise<EnvironmentStatusMap>
configureEnvironment: (params: { force?: boolean }) => Promise<EnvironmentStatusMap>
Expand Down Expand Up @@ -165,12 +176,8 @@ export interface PluginContext extends PluginContextGuard, WrappedFromGarden {
getModuleBuildPath: (moduleName: string) => Promise<string>
stageBuild: (moduleName: string) => Promise<void>
getStatus: () => Promise<ContextStatus>
deployServices: (
params: { names?: string[], force?: boolean, forceBuild?: boolean, logEntry?: LogEntry },
) => Promise<any>
processModules: (
modules: Module[], watch: boolean, process: (module: Module) => Promise<any>,
) => Promise<TaskResults>
processModules: (params: ProcessModulesParams) => Promise<TaskResults>
processServices: (params: ProcessServicesParams) => Promise<TaskResults>
}

export function createPluginContext(garden: Garden): PluginContext {
Expand Down Expand Up @@ -323,6 +330,23 @@ export function createPluginContext(garden: Garden): PluginContext {
return handler({ ...commonParams(handler), key })
},

getLoginStatus: async () => {
const handlers = garden.getActionHandlers("getLoginStatus")
return Bluebird.props(mapValues(handlers, h => h({ ...commonParams(h) })))
},

login: async () => {
const handlers = garden.getActionHandlers("login")
await Bluebird.each(values(handlers), h => h({ ...commonParams(h) }))
return ctx.getLoginStatus({})
},

logout: async () => {
const handlers = garden.getActionHandlers("logout")
await Bluebird.each(values(handlers), h => h({ ...commonParams(h) }))
return ctx.getLoginStatus({})
},

//endregion

//===========================================================================
Expand Down Expand Up @@ -430,18 +454,7 @@ export function createPluginContext(garden: Garden): PluginContext {
}
},

deployServices: async ({ names, force = false, forceBuild = false, logEntry }) => {
const services = await ctx.getServices(names)

await Bluebird.map(services, async (service) => {
const task = await DeployTask.factory({ ctx, service, force, forceBuild, logEntry })
await ctx.addTask(task)
})

return ctx.processTasks()
},

processModules: async (modules: Module[], watch: boolean, process: (module: Module) => Promise<any>) => {
processModules: async ({ modules, watch, process }: ProcessModulesParams) => {
// TODO: log errors as they happen, instead of after processing all tasks
const logErrors = (taskResults: TaskResults) => {
for (const result of values(taskResults).filter(r => !!r.error)) {
Expand All @@ -455,7 +468,8 @@ export function createPluginContext(garden: Garden): PluginContext {
}

for (const module of modules) {
await process(module)
const tasks = await process(module)
await Bluebird.map(tasks, ctx.addTask)
}

const results = await ctx.processTasks()
Expand All @@ -468,7 +482,8 @@ export function createPluginContext(garden: Garden): PluginContext {
const autoReloadDependants = await computeAutoReloadDependants(modules)

async function handleChanges(module: Module) {
await process(module)
const tasks = await process(module)
await Bluebird.map(tasks, ctx.addTask)

const dependantsForModule = autoReloadDependants[module.name]
if (!dependantsForModule) {
Expand Down Expand Up @@ -500,21 +515,19 @@ export function createPluginContext(garden: Garden): PluginContext {
}
},

getLoginStatus: async () => {
const handlers = garden.getActionHandlers("getLoginStatus")
return Bluebird.props(mapValues(handlers, h => h({ ...commonParams(h) })))
},

login: async () => {
const handlers = garden.getActionHandlers("login")
await Bluebird.each(values(handlers), h => h({ ...commonParams(h) }))
return ctx.getLoginStatus({})
},

logout: async () => {
const handlers = garden.getActionHandlers("logout")
await Bluebird.each(values(handlers), h => h({ ...commonParams(h) }))
return ctx.getLoginStatus({})
processServices: async ({ services, watch, process }: ProcessServicesParams) => {
const serviceNames = getNames(services)
const modules = Array.from(new Set(services.map(s => s.module)))

return ctx.processModules({
modules,
watch,
process: async (module) => {
const moduleServices = await module.getServices()
const servicesToDeploy = moduleServices.filter(s => serviceNames.includes(s.name))
return flatten(await Bluebird.map(servicesToDeploy, process))
},
})
},

//endregion
Expand Down

0 comments on commit 4871022

Please sign in to comment.