diff --git a/src/build-dir.ts b/src/build-dir.ts index 79b41eb626..29c337a43a 100644 --- a/src/build-dir.ts +++ b/src/build-dir.ts @@ -8,10 +8,10 @@ import { map as bluebirdMap } from "bluebird" import { - dirname, + isAbsolute, join, + parse, resolve, - sep, } from "path" import { emptyDir, @@ -20,6 +20,8 @@ import { } from "fs-extra" import * as Rsync from "rsync" import { GARDEN_DIR_NAME } from "./constants" +import { ConfigurationError } from "./exceptions" +import { PluginContext } from "./plugin-context" import { execRsyncCmd } from "./util" import { BuildCopySpec, @@ -44,24 +46,40 @@ export class BuildDir { async syncFromSrc(module: T) { await this.sync( - resolve(this.projectRoot, module.path), - this.buildDirPath) + resolve(this.projectRoot, module.path, "*"), + await this.buildPath(module), + ) } - async syncDependencyProducts(module: T) { + async syncDependencyProducts(ctx: PluginContext, module: T) { await this.syncFromSrc(module) const buildPath = await this.buildPath(module) const config = await module.getConfig() - await bluebirdMap(config.build.dependencies || [], (depConfig) => { + await bluebirdMap(config.build.dependencies || [], async (depConfig) => { if (!depConfig.copy) { return [] } + const sourceModule = await ctx.getModule(depConfig.name) + const sourceBuildPath = await this.buildPath(sourceModule) + // Sync to the module's top-level dir by default. return bluebirdMap(depConfig.copy, (copy: BuildCopySpec) => { - const sourcePath = resolve(this.buildDirPath, depConfig.name, copy.source) - const destinationPath = dirname(resolve(buildPath, copy.target, copy.source)) + sep + if (isAbsolute(copy.source)) { + throw new ConfigurationError(`Source path in build dependency copy spec must be a relative path`, { + copySpec: copy, + }) + } + + if (isAbsolute(copy.target)) { + throw new ConfigurationError(`Target path in build dependency copy spec must be a relative path`, { + copySpec: copy, + }) + } + + const sourcePath = join(sourceBuildPath, copy.source) + const destinationPath = join(buildPath, copy.target) return this.sync(sourcePath, destinationPath) }) }) @@ -78,7 +96,8 @@ export class BuildDir { } private async sync(sourcePath: string, destinationPath: string): Promise { - await ensureDir(destinationPath) + const destinationDir = parse(destinationPath).dir + await ensureDir(destinationDir) const syncCmd = new Rsync() .flags(["r", "p", "t", "g", "o"]) @@ -87,5 +106,4 @@ export class BuildDir { await execRsyncCmd(syncCmd) } - } diff --git a/src/types/module.ts b/src/types/module.ts index 29ec1dff85..252e53044a 100644 --- a/src/types/module.ts +++ b/src/types/module.ts @@ -25,8 +25,8 @@ export interface BuildCopySpec { // TODO: allow : delimited string (e.g. some.file:some-dir/) const copySchema = Joi.object().keys({ // TODO: allow array of strings here - source: Joi.string().required(), - target: Joi.string().default(""), + source: Joi.string().uri({ relativeOnly: true }).required(), + target: Joi.string().uri({ relativeOnly: true }).default(""), }) export interface BuildDependencyConfig { diff --git a/test/data/test-project-build-products/module-d/garden.yml b/test/data/test-project-build-products/module-d/garden.yml index 30614eb966..d94df4bfd2 100644 --- a/test/data/test-project-build-products/module-d/garden.yml +++ b/test/data/test-project-build-products/module-d/garden.yml @@ -7,10 +7,10 @@ module: - name: module-a copy: - source: a.txt - target: a + target: a/ - name: module-b copy: - source: build/b1.txt - target: b - - source: build/build_subdir - target: b + target: b/build/ + - source: build/build_subdir/ + target: b/build_subdir diff --git a/test/data/test-project-build-products/module-e/garden.yml b/test/data/test-project-build-products/module-e/garden.yml index e40027804f..17b6c05f0c 100644 --- a/test/data/test-project-build-products/module-e/garden.yml +++ b/test/data/test-project-build-products/module-e/garden.yml @@ -7,4 +7,4 @@ module: - name: module-d copy: - source: build/d.txt - target: d + target: d/build/ diff --git a/test/src/build-dir.ts b/test/src/build-dir.ts index 1879cb33a4..eb629fbdd0 100644 --- a/test/src/build-dir.ts +++ b/test/src/build-dir.ts @@ -86,7 +86,7 @@ describe("BuildDir", () => { const buildProductDestinations = [ join(buildDirD, "a", "a.txt"), join(buildDirD, "b", "build", "b1.txt"), - join(buildDirD, "b", "build", "build_subdir", "b2.txt"), + join(buildDirD, "b", "build_subdir", "b2.txt"), join(buildDirE, "d", "build", "d.txt"), ]