Skip to content

Commit

Permalink
refactor: a few changes to facilitate packaging/bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Sep 28, 2018
1 parent 8326827 commit d0e4035
Show file tree
Hide file tree
Showing 24 changed files with 145 additions and 131 deletions.
2 changes: 1 addition & 1 deletion bin/bootstrap-osx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# install/update homebrew dependencies
BREW_DEPS="cmake git kubectl kubernetes-helm stern rsync icu4c pkg-config"
BREW_DEPS="cmake git kubectl kubernetes-helm stern rsync icu4c pkg-config faas-cli"

brew update
brew install ${BREW_DEPS}
Expand Down
2 changes: 1 addition & 1 deletion garden-cli/gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const tmpDir = resolve(__dirname, "..", "tmp")

const binPath = (name: string) => resolve(__dirname, "node_modules", ".bin", name)

const destDir = "build"
const destDir = resolve(__dirname, "build")

const children: ChildProcess[] = []

Expand Down
5 changes: 0 additions & 5 deletions garden-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion garden-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"has-ansi": "^3.0.0",
"ignore": "^5.0.1",
"inquirer": "^6.1.0",
"is-subset": "^0.1.1",
"joi": "^13.6.0",
"js-yaml": "^3.12.0",
"json-stringify-safe": "^5.0.1",
Expand Down
8 changes: 4 additions & 4 deletions garden-cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { resolve } from "path"
import { resolve, join } from "path"

export const MODULE_CONFIG_FILENAME = "garden.yml"
export const LOCAL_CONFIG_FILENAME = "local-config.yml"
export const STATIC_DIR = resolve(__dirname, "..", "static")
export const GARDEN_DIR_NAME = ".garden"
export const LOGS_DIR = `${GARDEN_DIR_NAME}/logs`
export const LOGS_DIR = join(GARDEN_DIR_NAME, "logs")
export const ERROR_LOG_FILENAME = "error.log"
export const PROJECT_SOURCES_DIR_NAME = `${GARDEN_DIR_NAME}/sources/project`
export const MODULE_SOURCES_DIR_NAME = `${GARDEN_DIR_NAME}/sources/module`
export const PROJECT_SOURCES_DIR_NAME = join(GARDEN_DIR_NAME, "sources", "project")
export const MODULE_SOURCES_DIR_NAME = join(GARDEN_DIR_NAME, "sources", "module")
export const GARDEN_BUILD_VERSION_FILENAME = ".garden-build-version"
export const GARDEN_VERSIONFILE_NAME = ".garden-version"
export const DEFAULT_NAMESPACE = "default"
Expand Down
17 changes: 8 additions & 9 deletions garden-cli/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
pluginActionDescriptions,
pluginModuleSchema,
pluginSchema,
RegisterPluginParam,
} from "./types/plugin/plugin"
import { Environment, SourceConfig, defaultProvider } from "./config/project"
import {
Expand Down Expand Up @@ -99,7 +98,7 @@ import { FileWriter } from "./logger/writers/file-writer"
import { LogLevel } from "./logger/log-node"
import { ActionHelper } from "./actions"
import { createPluginContext } from "./plugin-context"
import { ModuleAndServiceActions } from "./types/plugin/plugin"
import { ModuleAndServiceActions, Plugins, RegisterPluginParam } from "./types/plugin/plugin"

export interface ActionHandlerMap<T extends keyof PluginActions> {
[actionName: string]: PluginActions[T]
Expand Down Expand Up @@ -127,7 +126,7 @@ export interface ContextOpts {
config?: GardenConfig,
env?: string,
logger?: Logger,
plugins?: RegisterPluginParam[],
plugins?: Plugins,
}

const scanLock = new AsyncLock()
Expand Down Expand Up @@ -181,7 +180,7 @@ export class Garden {
this.actions = new ActionHelper(this)
}

static async factory(currentDirectory: string, { env, config, logger, plugins = [] }: ContextOpts = {}) {
static async factory(currentDirectory: string, { env, config, logger, plugins = {} }: ContextOpts = {}) {
let parsedConfig: GardenConfig

if (config) {
Expand Down Expand Up @@ -289,8 +288,8 @@ export class Garden {
)

// Register plugins
for (const plugin of builtinPlugins.concat(plugins)) {
garden.registerPlugin(plugin)
for (const [name, pluginFactory] of Object.entries({ ...builtinPlugins, ...plugins })) {
garden.registerPlugin(name, pluginFactory)
}

// Load configured plugins
Expand Down Expand Up @@ -318,20 +317,20 @@ export class Garden {
return this.taskGraph.processTasks()
}

private registerPlugin(moduleOrFactory: RegisterPluginParam) {
private registerPlugin(name: string, moduleOrFactory: RegisterPluginParam) {
let factory: PluginFactory
let name: string

if (typeof moduleOrFactory === "function") {
factory = moduleOrFactory
name = factory.pluginName || factory.name!

} else if (isString(moduleOrFactory)) {
let moduleNameOrLocation = moduleOrFactory
const parsedLocation = parse(moduleNameOrLocation)

// allow relative references to project root
if (parse(moduleNameOrLocation).dir !== "") {
console.log(this.projectRoot)
console.log(moduleNameOrLocation)
moduleNameOrLocation = resolve(this.projectRoot, moduleNameOrLocation)
}

Expand Down
11 changes: 11 additions & 0 deletions garden-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const cli = require("./cli/cli")

cli.run()
14 changes: 6 additions & 8 deletions garden-cli/src/plugins/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
} from "../types/plugin/params"
import { BaseServiceSpec } from "../config/service"
import { BaseTestSpec, baseTestSpecSchema } from "../config/test"
import { spawn } from "../util/util"
import { readModuleVersionFile, writeModuleVersionFile, ModuleVersion } from "../vcs/base"
import { GARDEN_BUILD_VERSION_FILENAME } from "../constants"
import { ModuleSpec, ModuleConfig } from "../config/module"
Expand Down Expand Up @@ -107,7 +106,7 @@ export async function buildGenericModule({ module }: BuildModuleParams<GenericMo
const buildPath = module.buildPath

if (config.build.command.length) {
const result = await execa.shell(
const res = await execa.shell(
config.build.command.join(" "),
{
cwd: buildPath,
Expand All @@ -116,7 +115,7 @@ export async function buildGenericModule({ module }: BuildModuleParams<GenericMo
)

output.fresh = true
output.buildLog = result.stdout
output.buildLog = res.stdout + res.stderr
}

// keep track of which version has been built
Expand All @@ -130,9 +129,8 @@ export async function testGenericModule({ module, testConfig }: TestModuleParams
const startedAt = new Date()
const command = testConfig.spec.command

const result = await spawn(
command[0],
command.slice(1),
const result = await execa.shell(
command.join(" "),
{
cwd: module.path,
env: {
Expand All @@ -141,7 +139,7 @@ export async function testGenericModule({ module, testConfig }: TestModuleParams
...mapValues(module.spec.env, v => v + ""),
...mapValues(testConfig.spec.env, v => v + ""),
},
ignoreError: true,
reject: false,
},
)

Expand All @@ -153,7 +151,7 @@ export async function testGenericModule({ module, testConfig }: TestModuleParams
success: result.code === 0,
startedAt,
completedAt: new Date(),
output: result.output,
output: result.stdout + result.stderr,
}
}

Expand Down
2 changes: 1 addition & 1 deletion garden-cli/src/plugins/kubernetes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
} from "@kubernetes/client-node"
import { some, zip, isArray, isPlainObject, pickBy, mapValues } from "lodash"
import { KubernetesProvider } from "./kubernetes"
import * as isSubset from "is-subset"
import { LogEntry } from "../../logger/log-entry"
import { isSubset } from "../../util/is-subset"

export interface RolloutStatus {
state: ServiceState
Expand Down
35 changes: 21 additions & 14 deletions garden-cli/src/plugins/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { mapValues } from "lodash"

import { resolve } from "path"
// TODO: these should be configured, either explicitly or as dependencies of other plugins
import { RegisterPluginParam } from "../types/plugin/plugin"
const generic = require("./generic")
const container = require("./container")
const gcf = require("./google/google-cloud-functions")
const localGcf = require("./local/local-google-cloud-functions")
const kubernetes = require("./kubernetes/kubernetes")
const localKubernetes = require("./kubernetes/local")
const npmPackage = require("./npm-package")
const gae = require("./google/google-app-engine")
const openfaas = require("./openfaas/openfaas")

// These plugins are always registered
export const builtinPlugins: RegisterPluginParam[] = [
"./generic",
"./container",
"./google/google-cloud-functions",
"./local/local-google-cloud-functions",
"./kubernetes/kubernetes",
"./kubernetes/local",
"./npm-package",
"./google/google-app-engine",
"./openfaas/openfaas",
].map(p => resolve(__dirname, p))
export const builtinPlugins = mapValues({
generic,
container,
"google-cloud-functions": gcf,
"local-google-cloud-functions": localGcf,
kubernetes,
"local-kubernetes": localKubernetes,
"npm-package": npmPackage,
"google-app-engine": gae,
openfaas,
}, (m => m.gardenPlugin))

// These plugins are always loaded
export const fixedPlugins = [
Expand Down
4 changes: 3 additions & 1 deletion garden-cli/src/types/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,11 @@ export interface PluginFactoryParams<T extends Provider = any> {

export interface PluginFactory<T extends Provider = any> {
(params: PluginFactoryParams<T>): GardenPlugin | Promise<GardenPlugin>
pluginName?: string
}
export type RegisterPluginParam = string | PluginFactory
export interface Plugins {
[name: string]: RegisterPluginParam
}

export const pluginSchema = Joi.object()
.keys({
Expand Down
45 changes: 45 additions & 0 deletions garden-cli/src/util/is-subset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// NOTE: copied this from the is-subset package to avoid issues with their package manifest
// (https://github.com/studio-b12/is-subset/pull/9)

/**
* Check if an object is contained within another object.
*
* Returns `true` if:
* - all enumerable keys of *subset* are also enumerable in *superset*, and
* - every value assigned to an enumerable key of *subset* strictly equals
* the value assigned to the same key of *superset* – or is a subset of it.
*
* @param {Object} superset
* @param {Object} subset
*
* @returns {Boolean}
*
* @module is-subset
* @function default
* @alias isSubset
*/

export const isSubset = (superset, subset) => {
if (
(typeof superset !== "object" || superset === null) ||
(typeof subset !== "object" || subset === null)
) { return false }

if (
(superset instanceof Date || subset instanceof Date)
) { return superset.valueOf() === subset.valueOf() }

return Object.keys(subset).every((key) => {
if (!superset.propertyIsEnumerable(key)) { return false }

const subsetItem = subset[key]
const supersetItem = superset[key]
if (
(typeof subsetItem === "object" && subsetItem !== null) ?
!isSubset(supersetItem, subsetItem) :
supersetItem !== subsetItem
) { return false }

return true
})
}
3 changes: 3 additions & 0 deletions garden-cli/static/bin/garden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cli = require("../../build/cli/cli")

cli.run()
20 changes: 9 additions & 11 deletions garden-cli/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
PluginActions,
PluginFactory,
ModuleActions,
Plugins,
} from "../src/types/plugin/plugin"
import { Garden } from "../src/garden"
import { ModuleConfig } from "../src/config/module"
Expand Down Expand Up @@ -155,7 +156,6 @@ export const testPlugin: PluginFactory = (): GardenPlugin => {
},
}
}
testPlugin.pluginName = "test-plugin"

export const testPluginB: PluginFactory = async (params) => {
const plugin = await testPlugin(params)
Expand All @@ -164,7 +164,6 @@ export const testPluginB: PluginFactory = async (params) => {
}
return plugin
}
testPluginB.pluginName = "test-plugin-b"

export const testPluginC: PluginFactory = async (params) => {
const plugin = await testPlugin(params)
Expand All @@ -173,7 +172,6 @@ export const testPluginC: PluginFactory = async (params) => {
}
return plugin
}
testPluginC.pluginName = "test-plugin-c"

export const defaultModuleConfig: ModuleConfig = {
type: "test",
Expand All @@ -198,18 +196,18 @@ export const makeTestModule = (params: Partial<ModuleConfig> = {}) => {
return { ...defaultModuleConfig, ...params }
}

export const makeTestGarden = async (projectRoot: string, extraPlugins: PluginFactory[] = []) => {
const testPlugins: PluginFactory[] = [
testPlugin,
testPluginB,
testPluginC,
]
const plugins: PluginFactory[] = testPlugins.concat(extraPlugins)
export const makeTestGarden = async (projectRoot: string, extraPlugins: Plugins = {}) => {
const testPlugins = {
"test-plugin": testPlugin,
"test-plugin-b": testPluginB,
"test-plugin-c": testPluginC,
}
const plugins = { ...testPlugins, ...extraPlugins }

return Garden.factory(projectRoot, { plugins })
}

export const makeTestGardenA = async (extraPlugins: PluginFactory[] = []) => {
export const makeTestGardenA = async (extraPlugins: Plugins = {}) => {
return makeTestGarden(projectRootA, extraPlugins)
}

Expand Down
5 changes: 2 additions & 3 deletions garden-cli/test/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe("ActionHelper", () => {
let service: Service

before(async () => {
garden = await makeTestGardenA([testPlugin, testPluginB])
const plugins = { "test-plugin": testPlugin, "test-plugin-b": testPluginB }
garden = await makeTestGardenA(plugins)
actions = garden.actions
module = await garden.getModule("module-a")
service = await garden.getService("service-a")
Expand Down Expand Up @@ -464,7 +465,5 @@ const testPlugin: PluginFactory = async () => ({
},
},
})
testPlugin.pluginName = "test-plugin"

const testPluginB: PluginFactory = async (params) => omit(await testPlugin(params), ["moduleActions"])
testPluginB.pluginName = "test-plugin-b"
Loading

0 comments on commit d0e4035

Please sign in to comment.