Skip to content

Commit

Permalink
refactor: rename modules project config entry to scan (#4036)
Browse files Browse the repository at this point in the history
Used field renaming on the JOI schema level.
  • Loading branch information
vvagaytsev authored Apr 4, 2023
1 parent 3a39d03 commit 56ba5ec
Show file tree
Hide file tree
Showing 24 changed files with 125 additions and 38 deletions.
4 changes: 2 additions & 2 deletions core/src/commands/get/get-debug-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export async function collectBasicDebugInfo(root: string, gardenDirPath: string,
ignoreFile: projectConfig.dotIgnoreFile || defaultDotIgnoreFile,
cache,
})
const include = projectConfig.modules && projectConfig.modules.include
const exclude = projectConfig.modules && projectConfig.modules.exclude
const include = projectConfig.scan && projectConfig.scan.include
const exclude = projectConfig.scan && projectConfig.scan.exclude
const paths = await findConfigPathsInPath({ vcs, dir: root, include, exclude, log })

// Copy all the service configuration files
Expand Down
31 changes: 27 additions & 4 deletions core/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ export function prepareResource({
}
}

// TODO: remove this function in 0.14
export function prepareProjectResource(log: Log, spec: any): ProjectResource {
const projectSpec = <ProjectResource>spec
// TODO (start): remove these deprecation handlers in 0.14
type DeprecatedConfigHandler = (log: Log, spec: ProjectResource) => ProjectResource

function handleDotIgnoreFiles(log: Log, projectSpec: ProjectResource) {
// If the project config has an explicitly defined `dotIgnoreFile` field,
// it means the config has already been updated to 0.13 format.
if (!!projectSpec.dotIgnoreFile) {
Expand Down Expand Up @@ -290,11 +290,34 @@ export function prepareProjectResource(log: Log, spec: any): ProjectResource {
", "
)}]`,
{
spec,
projectSpec,
}
)
}

function handleProjectModules(log: Log, projectSpec: ProjectResource): ProjectResource {
// Field 'modules' was intentionally removed from the internal interface `ProjectResource`,
// but it still can be presented in the runtime if the old config format is used.
if (projectSpec["modules"]) {
emitNonRepeatableWarning(
log,
"Project configuration field `modules` is deprecated in 0.13 and will be removed in 0.14. Please use the `scan` field instead."
)
}

return projectSpec
}

const bonsaiDeprecatedConfigHandlers: DeprecatedConfigHandler[] = [handleDotIgnoreFiles, handleProjectModules]

export function prepareProjectResource(log: Log, spec: any): ProjectResource {
let projectSpec = <ProjectResource>spec
for (const handler of bonsaiDeprecatedConfigHandlers) {
projectSpec = handler(log, projectSpec)
}
return projectSpec
}

export function prepareModuleResource(spec: any, configPath: string, projectRoot: string): ModuleConfig {
// We allow specifying modules by name only as a shorthand:
// dependencies:
Expand Down
6 changes: 6 additions & 0 deletions core/src/config/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ export interface CreateSchemaParams {
meta?: MetadataKeys
allowUnknown?: boolean
required?: boolean
rename?: [string, string][]
or?: string[]
xor?: string[]
}
Expand Down Expand Up @@ -559,6 +560,11 @@ export function createSchema(spec: CreateSchemaParams): CreateSchemaOutput {
if (spec.required) {
schema = schema.required()
}
if (spec.rename) {
for (const r of spec.rename) {
schema = schema.rename(r[0], r[1])
}
}
if (spec.or) {
schema = schema.or(...spec.or)
}
Expand Down
13 changes: 7 additions & 6 deletions core/src/config/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export interface ProjectConfig {
dotIgnoreFile: string
dotIgnoreFiles?: string[]
environments: EnvironmentConfig[]
modules?: {
scan?: {
include?: string[]
exclude?: string[]
}
Expand All @@ -207,15 +207,15 @@ export const projectNameSchema = () =>

export const projectRootSchema = () => joi.string().description("The path to the project root.")

const projectModulesSchema = createSchema({
name: "project-modules",
const projectScanSchema = createSchema({
name: "project-scan",
keys: () => ({
include: joi
.array()
.items(joi.posixPath().allowGlobs().subPathOnly())
.description(
dedent`
Specify a list of POSIX-style paths or globs that should be scanned for Garden modules.
Specify a list of POSIX-style paths or globs that should be scanned for Garden configuration files.
Note that you can also _exclude_ path using the \`exclude\` field or by placing \`.gardenignore\` files in your source tree, which use the same format as \`.gitignore\` files. See the [Configuration Files guide](${includeGuideLink}) for details.
Expand All @@ -229,7 +229,7 @@ const projectModulesSchema = createSchema({
.items(joi.posixPath().allowGlobs().subPathOnly())
.description(
dedent`
Specify a list of POSIX-style paths or glob patterns that should be excluded when scanning for modules.
Specify a list of POSIX-style paths or glob patterns that should be excluded when scanning for configuration files.
The filters here also affect which files and directories are watched for changes. So if you have a large number of directories in your project that should not be watched, you should specify them here.
Expand Down Expand Up @@ -347,7 +347,7 @@ export const projectSchema = createSchema({
)
.example(["127.0.0.1"]),
}),
modules: projectModulesSchema().description("Control where to scan for modules in the project."),
scan: projectScanSchema().description("Control where to scan for configuration files in the project."),
outputs: joiSparseArray(projectOutputSchema())
.unique("name")
.description(
Expand Down Expand Up @@ -383,6 +383,7 @@ export const projectSchema = createSchema({
"Key/value map of variables to configure for all environments. " + joiVariablesDescription
),
}),
rename: [["modules", "scan"]],
})

export function getDefaultEnvironmentName(defaultName: string, config: ProjectConfig): string {
Expand Down
4 changes: 2 additions & 2 deletions core/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ export const resolveGardenParams = profileAsync(async function _resolveGardenPar
// We always exclude the garden dir
const gardenDirExcludePattern = `${relative(projectRoot, gardenDirPath)}/**/*`
const moduleExcludePatterns = [
...((config.modules || {}).exclude || []),
...((config.scan || {}).exclude || []),
gardenDirExcludePattern,
...fixedProjectExcludes,
]
Expand Down Expand Up @@ -1754,7 +1754,7 @@ export const resolveGardenParams = profileAsync(async function _resolveGardenPar
dotIgnoreFile: config.dotIgnoreFile,
proxy,
log,
moduleIncludePatterns: (config.modules || {}).include,
moduleIncludePatterns: (config.scan || {}).include,
username: _username,
forceRefresh: opts.forceRefresh,
cloudApi,
Expand Down
2 changes: 1 addition & 1 deletion core/test/data/test-project-watch/garden.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind: Project
name: test-project-a
modules:
scan:
exclude: [module-a/project-excluded.txt]
environments:
- name: local
Expand Down
2 changes: 1 addition & 1 deletion core/test/data/test-projects/huge-project/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ name: huge-project
dotIgnoreFile: .gardenignore
environments:
- name: local
modules:
scan:
exclude: [dir0/**/*, dir1/**/*, dir2/**/*, dir3/**/*, dir4/**/*, dir5/**/*, dir6/**/*, dir7/**/*, dir8/**/*]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kind: Project
name: include-exclude-old-syntax
environments:
- name: local
providers:
- name: test-plugin
modules:
include:
- module*/**/*
exclude:
- module-c/**/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Module
name: module-a
type: test
include: ["yes.txt"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Go away!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Oh hai!
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Module
name: module-b
type: test
exclude: ["nope.*"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Go away!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Oh hai!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Module
name: module-c
type: test
include: ["*.txt"]
exclude: ["nope.*"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Go away!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Oh hai!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Module
name: nope
type: test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ environments:
- name: local
providers:
- name: test-plugin
modules:
scan:
include:
- module*/**/*
exclude:
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const modulePathAMultiple = resolve(projectPathMultipleModules, "module-a")
const projectPathDuplicateProjects = getDataDir("test-project-duplicate-project-config")
const log = getRootLogger().createLog()

// TODO: remove this describe block in 0.14
// TODO-0.14: remove this describe block in 0.14
describe("prepareProjectResource", () => {
const projectResourceTemplate = {
apiVersion: DEFAULT_API_VERSION,
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/src/docs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("docs config module", () => {
# Example:
#
# \`\`\`yaml
# modules:
# scan:
# exclude:
# - node_modules/**/*
# - vendor/**/*
Expand All @@ -75,7 +75,7 @@ describe("docs config module", () => {
# Example:
#
# \`\`\`javascript
# modules:
# scan:
# exclude:
# - node_modules/**/*
# - vendor/**/*
Expand All @@ -87,7 +87,7 @@ describe("docs config module", () => {
# Example:
#
# \`\`\`
# modules:
# scan:
# exclude:
# - node_modules/**/*
# - vendor/**/*
Expand All @@ -98,7 +98,7 @@ describe("docs config module", () => {
const expected = dedent`
# Example:
#
# modules:
# scan:
# exclude:
# - node_modules/**/*
# - vendor/**/*
Expand Down
26 changes: 24 additions & 2 deletions core/test/unit/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import { convertExecModule } from "../../../src/plugins/exec/convert"
import { getLogMessages } from "../../../src/util/testing"
import { TreeCache } from "../../../src/cache"
import { omitUndefined } from "../../../src/util/objects"
import { prepareProjectResource } from "../../../src/config/base"
import { CoreLog } from "../../../src/logger/log-entry"
import { Logger, LogLevel } from "../../../src/logger/logger"

// TODO-G2: change all module config based tests to be action-based.

Expand Down Expand Up @@ -2544,6 +2547,25 @@ describe("Garden", () => {
expect(getNames(modules).sort()).to.eql(["module-a", "module-b"])
})

// TODO-0.14: remove this and core/test/data/test-projects/project-include-exclude-old-syntax directory
it("should respect the modules.include and modules.exclude fields, if specified", async () => {
const projectRoot = getDataDir("test-projects", "project-include-exclude-old-syntax")
const garden = await makeTestGarden(projectRoot)
const modules = await garden.resolveModules({ log: garden.log })

// Should NOT include "nope" and "module-c"
expect(getNames(modules).sort()).to.eql(["module-a", "module-b"])
})

it("should respect the scan.include and scan.exclude fields, if specified", async () => {
const projectRoot = getDataDir("test-projects", "project-include-exclude")
const garden = await makeTestGarden(projectRoot)
const modules = await garden.resolveModules({ log: garden.log })

// Should NOT include "nope" and "module-c"
expect(getNames(modules).sort()).to.eql(["module-a", "module-b"])
})

it("should respect .gitignore and .gardenignore files", async () => {
const projectRoot = getDataDir("test-projects", "dotignore")
const garden = await makeTestGarden(projectRoot)
Expand All @@ -2555,7 +2577,7 @@ describe("Garden", () => {
it("should respect custom dotignore files", async () => {
// In this project we have custom dotIgnoreFile: .customignore which overrides the default .gardenignore.
// Thus, all exclusions from .gardenignore will be skipped.
// TODO: amend the config core/test/data/test-projects/dotignore-custom/garden.yml in 0.14
// TODO-0.14: amend the config core/test/data/test-projects/dotignore-custom/garden.yml
const projectRoot = getDataDir("test-projects", "dotignore-custom")
const garden = await makeTestGarden(projectRoot)
const modules = await garden.resolveModules({ log: garden.log })
Expand All @@ -2565,7 +2587,7 @@ describe("Garden", () => {
expect(getNames(modules).sort()).to.eql(["module-a", "module-c"])
})

// TODO: Delete this context AND core/test/data/test-projects/dotignore-custom-legacy directory oin 0.14
// TODO-0.14: Delete this context AND core/test/data/test-projects/dotignore-custom-legacy directory in 0.14
context("dotignore files migration to 0.13", async () => {
it("should remap singleton array `dotIgnoreFiles` to scalar `dotIgnoreFile`", async () => {
// In this project we have custom dotIgnoreFile: .customignore which overrides the default .gardenignore.
Expand Down
Loading

0 comments on commit 56ba5ec

Please sign in to comment.