Skip to content

Commit

Permalink
simplify and update schema/gen
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Dec 26, 2021
1 parent 1605107 commit 22b18cf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 130 deletions.
22 changes: 12 additions & 10 deletions schema/gen/file.cue
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package gen

// A file which should be generated by hof
#HofGeneratorFile: {
// The local input data
// The local input data, any struct
In: { ... }

// The full path under the output location
// empty implies don't generate, even though it may endup in the list
// empty implies don't generate, even though it may end up in the out list
Filepath: string | *""

// The template contents
Template: string | *""

// Relative name from TemplatesDir
TemplateName: string | *""
// Only one of these may be set
{
// The template contents
TemplateContent: string
} | {
// Path into the loaded templates
TemplatePath: string
}

// Include Common attributes
// '.' will bre replaced by generator defaults
TemplateConfig?: #TemplateConfig

TemplateConfig?: #TemplateConfigReplacible

// WARNING, intentionally closed to prevent user error when creating GenFiles
// Note, intentionally closed to prevent user error when creating GenFiles
}
57 changes: 28 additions & 29 deletions schema/gen/generator.cue
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,44 @@ package gen
// Base directory for the output
Outdir: string | *"./"

// "Global" input, merged with out replacing onto the files
In: { ... } | *{...}
// Generater wide input value to templates.
// Unified with any template or file level In values
In: { ... }

// The list fo files for hof to generate
// Out: [...#HofGeneratorFile] | *[...]
Out: [...#HofGeneratorFile] | *[]
Out: [...#HofGeneratorFile]

// The following will be automatically added to the template context
// under its name for reference in GenFiles and partials in templates
NamedTemplates: { [Name=string]: string }
NamedPartials: { [Name=string]: string }
// Static files are available for pure cue generators that want to have static files
// These should be named by their filepath, but be the content of the file
StaticFiles: { [Name=string]: string }
// Template (top-level) TemplateConfig (globs+config)
Templates: [...#Templates] | *[#Templates & { Globs: ["./templates/**"] }]

//
// For file based generators
// files here will be automatically added to the template context
// under its filepath for reference in GenFiles and partials in templates

// Used for indexing into the vendor directory...
PackageName: string

// Filepath globs for static files to load
StaticGlobs: [...string] | *[]
// Partial (nested) TemplateConfig (globs+config)
Partials: [...#Templates] | *[#Templates & { Globs: ["./partials/**"] }]

// Base directory of partial templatess to load
PartialsDir: string | *"./partials/"
// Statics are copied directly into the output, bypassing the rendering
Statics: [...#Statics] | *[#Statics & { Globs: ["./static/**"], TrimPrefix: "./static/" }]

// Base directory of entrypoint templates to load
TemplatesDir: string | *"./templates/"

TemplatesDirConfig: [Filename=string]: #TemplateConfigReplacible

TemplateConfig: #DefaultTemplateConfig
// The following mirror their non-embedded versions
// however they have the content as a string in CUE
// For templates and partials, Name is the path to reference
EmbeddedTemplates: [Name=string]: #Template
EmbeddedPartials: [Name=string]: #Template
// For statics, Name is the path to write the content
EmbeddedStatics: [Name=string]: string

// For subgenerators so a generator can leverage and design for other hofmods
Generators: [Gen=string]: #HofGenerator

// This should be set to default to the module name
// (i.e. 'string | *"github.com/<org>/<repo>"')
// Users should not have to set this.
//
// Used for indexing into the cue.mod/pkg directory...
// until embed is supported, at which point this shouldn't be needed at all
// only needed when you have example usage in the same module the generator is in
// set to the empty string ("") as a generator writer who is making an example in the same module
PackageName: string
// TODO, hof, can we introspect the generator / example packages and figure this out?

//
// Open for whatever else you may need as a generator writer
...
Expand Down
127 changes: 36 additions & 91 deletions schema/gen/template.cue
Original file line number Diff line number Diff line change
@@ -1,99 +1,44 @@
package gen

#TemplateConfigReplacible: {
// System params
TemplateSystem: *"." | "golang" | "raymond"

//
// Template delimiters
//
// these are for advanced usage, you shouldn't have to modify them normally

// Alt and Swap Delims,
// becuase the defaulttemplate systems use `{{` and `}}`
// and you may choose to use other delimiters, but the lookup system is still based on the template system
// and if you want to preserve those, we need three sets of delimiters
AltDelims: bool | *false
SwapDelims: bool | *false

// The default delimiters
// You should change these when using alternative style like jinjas {% ... %}
// They also need to be different when using the swap system
LHS_D: LHS2_D
RHS_D: RHS2_D
LHS2_D: string | *"."
RHS2_D: string | *"."
LHS3_D: string | *"."
RHS3_D: string | *"."

// These are the same as the default becuase
// the current template systems require these.
// So these should really never change or be overriden until there is a new template system
// supporting setting the delimiters dynamicalldelimiters dynamicallyy
LHS_S: LHS2_S
RHS_S: RHS2_S
LHS2_S: string | *"."
RHS2_S: string | *"."
LHS3_S: string | *"."
RHS3_S: string | *"."

// The temporary delims to replace swap with while also swapping
// the defaults you set to the swap that is required by the current templet systems
// You need this when you are double templating a file and the top-level system is not the default
LHS_T: LHS2_T
RHS_T: RHS2_T
LHS2_T: string | *"."
RHS2_T: string | *"."
LHS3_T: string | *"."
RHS3_T: string | *"."
// #Statics is used for static files copied over, bypassing the template engine
#Statics: {
Globs: [...string]
TrimPrefix: string | *""
OutPrefix: string | *""
}

#DefaultTemplateConfig: {
// Include Common attributes
// System params
TemplateSystem: *"golang" | "raymond"

//
// Template delimiters
//
// these are for advanced usage, you shouldn't have to modify them normally

// Alt and Swap Delims,
// becuase the defaulttemplate systems use `{{` and `}}`
// and you may choose to use other delimiters, but the lookup system is still based on the template system
// and if you want to preserve those, we need three sets of delimiters
AltDelims: bool | *false
SwapDelims: bool | *false

// The default delimiters
// You should change these when using alternative style like jinjas {% ... %}
// They also need to be different when using the swap system
LHS_D: LHS2_D
RHS_D: RHS2_D
LHS2_D: string | *"{{"
RHS2_D: string | *"}}"
LHS3_D: string | *"{{{"
RHS3_D: string | *"}}}"
// #Template is used for embedded or named templates
#Template: {
Content: string
Config: #TemplateConfig
}

// These are the same as the default becuase
// the current template systems require these.
// So these should really never change or be overriden until there is a new template system
// supporting setting the delimiters dynamicalldelimiters dynamicallyy
LHS_S: LHS2_S
RHS_S: RHS2_S
LHS2_S: string | *"{{"
RHS2_S: string | *"}}"
LHS3_S: string | *"{{{"
RHS3_S: string | *"}}}"
// #Templates is used for templates loaded from a filesystem
#Templates: {
Globs: [...string]
Config: #TemplateConfig
}

// The temporary delims to replace swap with while also swapping
// the defaults you set to the swap that is required by the current templet systems
// You need this when you are double templating a file and the top-level system is not the default
LHS_T: LHS2_T
RHS_T: RHS2_T
LHS2_T: string | *"#_hof_l2_#"
RHS2_T: string | *"#_hof_r2_#"
LHS3_T: string | *"#_hof_l3_#"
RHS3_T: string | *"#_hof_r3_#"
// #TemplateConfig determines the engine and delimiters to use when rendering
// For more details see https://docs.hofstadter.io/code-generation/templates/
#TemplateConfig: {
// The template system to use
TemplateSystem: *"golang" | "raymond"

// AltDelims are used when you have
// different delims you'd like to use
AltDelims?: #TemplateDelims

// TmpDelims should be set to true when you have
// output which contains the standard delims
// which will be mistakenly processed
// i.e.: `{{' '}}` '{{{' '}}}'
TmpDelims: bool | *false
}

#TemplateDelims: {
LHS2: string | *"{{"
RHS2: string | *"}}"
LHS3: string | *"{{{"
RHS3: string | *"}}}"
}

0 comments on commit 22b18cf

Please sign in to comment.