Skip to content

Commit

Permalink
fix: fixed more issues with cross-repo versioning
Browse files Browse the repository at this point in the history
Turns out I missed a beat on the last fix. Hopefully this is the rest
of it.
  • Loading branch information
edvald committed Jul 2, 2018
1 parent 14ebc2c commit 2b0d93e
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 113 deletions.
64 changes: 21 additions & 43 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import {
spawn as _spawn,
ChildProcess,
} from "child_process"
import {
writeFileSync,
} from "fs"
import {
ensureDir,
pathExists,
Expand All @@ -13,12 +10,14 @@ import {
writeFile,
} from "fs-extra"
import * as handlebars from "handlebars"
import {
join,
relative,
} from "path"
import { join } from "path"
import { generateDocs } from "./src/docs/generate"
import { getUrlChecksum } from "./support/support-util"
import * as Bluebird from "bluebird"
import { GitHandler } from "./src/vcs/git"
import { Garden } from "./src/garden"
import { RootLogNode } from "./src/logger/logger"
import { LogLevel } from "./src/logger/types"
import execa = require("execa")

const gulp = require("gulp")
Expand Down Expand Up @@ -49,12 +48,6 @@ const licenseHeaderPath = "support/license-header.txt"

const destDir = "build"

class TaskError extends Error {
toString() {
return this.message
}
}

const children: ChildProcess[] = []

process.env.FORCE_COLOR = "true"
Expand Down Expand Up @@ -89,37 +82,25 @@ function die() {
process.on("SIGINT", die)
process.on("SIGTERM", die)

gulp.task("add-version-files", (cb) => {
const gardenBinPath = join("static", "bin", "garden")
const proc = _spawn("node", [gardenBinPath, "scan", "--output=json"])
// make sure logger is initialized
try {
RootLogNode.initialize({ level: LogLevel.info })
} catch (_) { }

proc.on("error", err => cb(err))
gulp.task("add-version-files", async () => {
const staticPath = join(__dirname, "static")
const garden = await Garden.factory(staticPath)

let output = ""
let outputWithError = ""
proc.stdout.on("data", d => {
output += d
outputWithError += d
})
proc.stderr.on("data", d => outputWithError += d)
const modules = await garden.getModules()

proc.on("close", () => {
let results
try {
results = JSON.parse(output)
} catch {
const msg = "Got unexpected output from `garden scan`"
console.error(msg + "\n" + outputWithError)
return cb(msg)
}
return Bluebird.map(modules, async (module) => {
const path = module.path
const versionFilePath = join(path, ".garden-version")

for (const module of <any>results.result) {
const relPath = relative(__dirname, module.path)
const versionFilePath = join(__dirname, relPath, ".garden-version")
writeFileSync(versionFilePath, JSON.stringify(module.version))
}
const vcsHandler = new GitHandler(path)
const treeVersion = await vcsHandler.getTreeVersion([path])

cb()
await writeFile(versionFilePath, JSON.stringify(treeVersion, null, 4) + "\n")
})
})

Expand Down Expand Up @@ -266,10 +247,7 @@ gulp.task("watch-code", () => {
})

gulp.task("lint", gulp.parallel("check-licenses", "tslint", "tslint-tests", "tsfmt"))
gulp.task("build", gulp.series(
gulp.parallel("generate-docs", "pegjs", "tsc"),
"add-version-files",
))
gulp.task("build", gulp.parallel("add-version-files", "generate-docs", "pegjs", "tsc"))
gulp.task("test", gulp.parallel("build", "lint", "mocha"))
gulp.task("watch", gulp.series(
"build",
Expand Down
33 changes: 1 addition & 32 deletions src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,15 @@

import Bluebird = require("bluebird")
import chalk from "chalk"
import {
pathExists,
readFile,
} from "fs-extra"
import { join } from "path"
import { CacheContext } from "./cache"
import { GARDEN_VERSIONFILE_NAME } from "./constants"
import { ConfigurationError } from "./exceptions"
import {
Garden,
} from "./garden"
import { EntryStyle } from "./logger/types"
import {
PrimitiveMap,
validate,
} from "./types/common"
import { Module } from "./types/module"
import { moduleVersionSchema } from "./vcs/base"
import {
ModuleActions,
Provider,
Expand Down Expand Up @@ -482,29 +473,7 @@ export function createPluginContext(garden: Garden): PluginContext {
const dependencies = await garden.getModules(moduleDependencies)
const cacheContexts = dependencies.concat([module]).map(m => m.getCacheContext())

// the version file is used internally to specify versions outside of source control
const versionFilePath = join(module.path, GARDEN_VERSIONFILE_NAME)
const versionFileContents = await pathExists(versionFilePath)
&& (await readFile(versionFilePath)).toString().trim()

let version: ModuleVersion

if (!!versionFileContents) {
try {
version = validate(JSON.parse(versionFileContents), moduleVersionSchema)
} catch (err) {
throw new ConfigurationError(
`Unable to parse ${GARDEN_VERSIONFILE_NAME} as valid version file in module directory ${module.path}`,
{
modulePath: module.path,
versionFilePath,
versionFileContents,
},
)
}
} else {
version = await garden.vcs.resolveVersion(module, dependencies)
}
const version = await garden.vcs.resolveVersion(module, dependencies)

garden.cache.set(cacheKey, version, ...cacheContexts)
return version
Expand Down
34 changes: 33 additions & 1 deletion src/vcs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import * as Bluebird from "bluebird"
import { mapValues, keyBy, sortBy, orderBy, omit } from "lodash"
import { createHash } from "crypto"
import * as Joi from "joi"
import { validate } from "../types/common"
import { join } from "path"
import { GARDEN_VERSIONFILE_NAME } from "../constants"
import { pathExists, readFile } from "fs-extra"
import { ConfigurationError } from "../exceptions"

export const NEW_MODULE_VERSION = "0000000000"

Expand Down Expand Up @@ -63,11 +68,38 @@ export const moduleVersionSchema = Joi.object()
export abstract class VcsHandler {
constructor(protected projectRoot: string) { }

abstract name: string
abstract async getTreeVersion(paths: string[]): Promise<TreeVersion>

async resolveTreeVersion(module: Module): Promise<TreeVersion> {
// the version file is used internally to specify versions outside of source control
const versionFilePath = join(module.path, GARDEN_VERSIONFILE_NAME)
const versionFileContents = await pathExists(versionFilePath)
&& (await readFile(versionFilePath)).toString().trim()

if (!!versionFileContents) {
try {
return validate(JSON.parse(versionFileContents), treeVersionSchema)
} catch (err) {
throw new ConfigurationError(
`Unable to parse ${GARDEN_VERSIONFILE_NAME} as valid version file in module directory ${module.path}`,
{
modulePath: module.path,
versionFilePath,
versionFileContents,
},
)
}
} else {
return this.getTreeVersion([module.path])
}
}

async resolveVersion(module: Module, dependencies: Module[]): Promise<ModuleVersion> {
const treeVersion = await this.getTreeVersion([module.path])

validate(treeVersion, treeVersionSchema, { context: `${this.name} tree version for module at ${module.path}` })

if (dependencies.length === 0) {
return {
versionString: treeVersion.latestCommit,
Expand All @@ -78,7 +110,7 @@ export abstract class VcsHandler {

const namedDependencyVersions = await Bluebird.map(
dependencies,
async (m: Module) => ({ name: m.name, ...await this.getTreeVersion([m.path]) }),
async (m: Module) => ({ name: m.name, ...await this.resolveTreeVersion(m) }),
)
const dependencyVersions = mapValues(keyBy(namedDependencyVersions, "name"), v => omit(v, "name"))

Expand Down
14 changes: 14 additions & 0 deletions src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { NEW_MODULE_VERSION, TreeVersion, VcsHandler } from "./base"
import { join } from "path"
import { sortBy } from "lodash"
import { pathExists, stat } from "fs-extra"
import { argv } from "process"
import Bluebird = require("bluebird")

export class GitHandler extends VcsHandler {
name = "git"

private repoRoot: string

async getTreeVersion(directories: string[]) {
Expand Down Expand Up @@ -118,3 +121,14 @@ export class GitHandler extends VcsHandler {
return exec("git " + args, { cwd })
}
}

// used by the build process to resolve and store the tree version for plugin modules
if (require.main === module) {
const path = argv[2]
const handler = new GitHandler(path)

handler.getTreeVersion([path])
.then((treeVersion) => {
console.log(JSON.stringify(treeVersion, null, 4))
})
}
File renamed without changes.
5 changes: 2 additions & 3 deletions test/data/test-project-a/module-a/.garden-version
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"versionString": "1234567890",
"dirtyTimestamp": null,
"dependencyVersions": {}
"latestCommit": "1234567890",
"dirtyTimestamp": null
}
11 changes: 0 additions & 11 deletions test/src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,6 @@ describe("PluginContext", () => {
expect(result).to.eql(version)
})

it("should return version from version file if it exists", async () => {
const module = await ctx.getModule("module-a")
const result = await ctx.resolveVersion("module-a", [])

expect(result).to.eql({
versionString: "1234567890",
dirtyTimestamp: null,
dependencyVersions: {},
})
})

it("should otherwise return version from VCS handler", async () => {
const resolve = td.replace(garden.vcs, "resolveVersion")
const version: ModuleVersion = {
Expand Down
Loading

0 comments on commit 2b0d93e

Please sign in to comment.