Skip to content

Commit

Permalink
fix: changed how paths are handled when copying build dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 22, 2018
1 parent 2795653 commit d6506da
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
38 changes: 28 additions & 10 deletions src/build-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import { map as bluebirdMap } from "bluebird"
import {
dirname,
isAbsolute,
join,
parse,
resolve,
sep,
} from "path"
import {
emptyDir,
Expand All @@ -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,
Expand All @@ -44,24 +46,40 @@ export class BuildDir {

async syncFromSrc<T extends Module>(module: T) {
await this.sync(
resolve(this.projectRoot, module.path),
this.buildDirPath)
resolve(this.projectRoot, module.path, "*"),
await this.buildPath(module),
)
}

async syncDependencyProducts<T extends Module>(module: T) {
async syncDependencyProducts<T extends Module>(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)
})
})
Expand All @@ -78,7 +96,8 @@ export class BuildDir {
}

private async sync(sourcePath: string, destinationPath: string): Promise<void> {
await ensureDir(destinationPath)
const destinationDir = parse(destinationPath).dir
await ensureDir(destinationDir)

const syncCmd = new Rsync()
.flags(["r", "p", "t", "g", "o"])
Expand All @@ -87,5 +106,4 @@ export class BuildDir {

await execRsyncCmd(syncCmd)
}

}
4 changes: 2 additions & 2 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<any>{ relativeOnly: true }).required(),
target: Joi.string().uri(<any>{ relativeOnly: true }).default(""),
})

export interface BuildDependencyConfig {
Expand Down
8 changes: 4 additions & 4 deletions test/data/test-project-build-products/module-d/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/data/test-project-build-products/module-e/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ module:
- name: module-d
copy:
- source: build/d.txt
target: d
target: d/build/
2 changes: 1 addition & 1 deletion test/src/build-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]

Expand Down

0 comments on commit d6506da

Please sign in to comment.