Class: CfnDsl::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/cfndsl/rake_task.rb

Overview

Rake TaskLib rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) {|_self| ... } ⇒ RakeTask

Creates Cloudformation generation tasks

directory 'tmp'

namespace :samples do

CfnDsl::RakeTask.new do |t|
  t.specification(file: 'tmp/cloudformation_resources.json')

  desc 'Generate CloudFormation Json'
  t.json(name: :json, files: ["sample/*.rb"], pathmap: 'tmp/%f.json', pretty: true, extras: FileList.new('sample/*.yaml') )

  t.yaml(name: :yaml, files: 'sample/t1.rb', pathmap: 'tmp/%f.yaml', extras: '%X.yaml')
end

end

Yields:

  • (_self)

Yield Parameters:


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cfndsl/rake_task.rb', line 38

def initialize(name = nil)
  @tasks = []
  @spec_task = nil

  last_desc = ::Rake.application.last_description
  desc nil

  yield self if block_given?

  if cfndsl_opts
    desc last_desc if last_desc
    task(name || :generate) { |_t, _args| cfndsl_opts[:files].each { |opts| generate(opts) } }
  else
    define_base_tasks(name || :generate, last_desc)
  end
end

Instance Attribute Details

#cfndsl_optsObject

Deprecated.

pre 1.x rake task generator


20
21
22
# File 'lib/cfndsl/rake_task.rb', line 20

def cfndsl_opts
  @cfndsl_opts
end

Instance Method Details

#json(name:, files:, pathmap:, pretty: false, extras: [], **json_opts) ⇒ Object

Convert DSL sources to json

Generates file tasks for each of the matching file in the files FileList. Each task is dependant on the source,

the specification file, and the required extras files such that if the timestamp of any of these is earlier
the target file will be regenerated.

Finally task <name> is generated that depends on all the generated targets.

rubocop:disable Metrics/ParameterLists

Parameters:

  • name (Symbol|String)

    the name of a task that is dependant on all the files being converted

  • files (Rake::FileList | Array<String>)

    source file list

  • pathmap (String)

    expression to map source files to target files

  • extras (Rake::Filelist|Array<String>) (defaults to: [])

    a list of files to load as external parameters Note String values containing '%' are treated as a pathmap from the source The resulting generated FileList is used as a dependency for generation so any entries not containing '*' MUST exist.

  • pretty (Boolean) (defaults to: false)

    use JSON.pretty_generate

  • json_opts, (Hash)

    other options to pass to JSON generator


113
114
115
116
117
118
119
# File 'lib/cfndsl/rake_task.rb', line 113

def json(name:, files:, pathmap:, pretty: false, extras: [], **json_opts)
  json_method = pretty ? :pretty_generate : :generate
  generate_model_tasks(name: name, files: files, pathmap: pathmap, extras: extras) do |model, f|
    f.write(JSON.send(json_method, model, **json_opts))
  end
  self
end

#specification(name: nil, file:, version: nil) ⇒ Object

TODO:

Add capability to provide a user spec/patches dir

Use a custom specification file

This specification file will be used for any generation tasks

Creates a file task to download from upstream source, with an optional additional named task that depends on it.

The minimum required version can be specified with the version parameter, which can be overriden by invoking rake with the 'cfn_spec_task' argument

CfnDsl::RakeTask.new() do |t|

t.specification(name: :update_spec, file: 'tmp/cloudformation_resources.json', version: '6.3'0)

end

# rake update_spec # >> Will ensure 'tmp/cloudformation_resources.json' exists and is at least 6.3.0 # rake update_spec # >> Will always try to download the latest available spec

Parameters:

  • file (String)

    the number of the specification file to use

  • name (Symbol) (defaults to: nil)

    A pretty name for the task to update this file from upstream source

  • version (String) (defaults to: nil)

    The minimum version required, and the default version used when downloading if not specified and not overriden by 'cfn_spec_version' rake task argument then any existing file is considered sufficient, and 'latest' is the version used for downloading


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cfndsl/rake_task.rb', line 82

def specification(name: nil, file:, version: nil)
  if name
    desc 'Update Resource Specification' unless ::Rake.application.last_description
    task name, [:cfn_spec_version] => file
  end

  @spec_task = file(file, :cfn_spec_version) do |t, args|
    update_specification(t.name, args.with_defaults(cfn_spec_version: version)[:cfn_spec_version])
  end
  @spec_task.define_singleton_method(:needed?) { true } # We always need to check
  self
end

#yaml(name:, files:, pathmap:, extras: [], **yaml_opts) ⇒ Object

Convert DSL sources to yaml

Generates file tasks for each of the matching file in the files FileList. Each task is dependant on the source,

the specification file, and the required extras files such that if the timestamp of any of these is earlier
the target file will be regenerated.

Finally task <name> is generated that depends on all the generated targets.

Parameters:

  • name (Symbol|String)

    the name of a task that is dependant on all the files being converted

  • files (Rake::FileList | Array<String>)

    source file list

  • pathmap (String)

    expression to map source files to target files

  • extras (Rake::Filelist|Array<String>) (defaults to: [])

    a list of files to load as external parameters Note String values containing '%' are treated as a pathmap from the source The resulting generated FileList is used as a dependency for generation so any entries not containing '*' MUST exist.

  • yaml_opts, (Hash)

    other options to pass to YAML generator


136
137
138
139
140
141
# File 'lib/cfndsl/rake_task.rb', line 136

def yaml(name:, files:, pathmap:, extras: [], **yaml_opts)
  generate_model_tasks(name: name, files: files, pathmap: pathmap, extras: extras) do |model, f|
    YAML.dump(model, f, **yaml_opts)
  end
  self
end