k_manager 0.0.13 → 0.0.28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/Gemfile +23 -0
  4. data/Rakefile +2 -0
  5. data/docs/flow.drawio +16 -0
  6. data/exe/k_manager +19 -0
  7. data/hooks/update-version +1 -1
  8. data/k_manager.gemspec +9 -0
  9. data/lib/k_manager/area.rb +49 -0
  10. data/lib/k_manager/cli/base_command.rb +23 -0
  11. data/lib/k_manager/cli/commands.rb +25 -0
  12. data/lib/k_manager/cli/info.rb +81 -0
  13. data/lib/k_manager/cli/new.rb +130 -0
  14. data/lib/k_manager/cli/version.rb +15 -0
  15. data/lib/k_manager/cli/watch.rb +65 -0
  16. data/lib/k_manager/document_factory.rb +81 -0
  17. data/lib/k_manager/manager.rb +158 -0
  18. data/lib/k_manager/overview/dashboard.rb +197 -0
  19. data/lib/k_manager/overview/dump_json.rb +35 -0
  20. data/lib/k_manager/overview/models.rb +76 -0
  21. data/lib/k_manager/overview/queries.rb +53 -0
  22. data/lib/k_manager/resources/base_resource.rb +203 -52
  23. data/lib/k_manager/resources/file_resource.rb +81 -58
  24. data/lib/k_manager/resources/{ruby_file_resource.rb → file_resources/ruby_file_resource.rb} +0 -0
  25. data/lib/k_manager/resources/{unknown_file_resource.rb → file_resources/unknown_file_resource.rb} +0 -0
  26. data/lib/k_manager/resources/mem_resource.rb +17 -0
  27. data/lib/k_manager/resources/resource_document_factory.rb +123 -0
  28. data/lib/k_manager/resources/resource_factory.rb +28 -0
  29. data/lib/k_manager/resources/resource_manager.rb +201 -0
  30. data/lib/k_manager/resources/resource_set.rb +90 -0
  31. data/lib/k_manager/resources/web_resource.rb +113 -0
  32. data/lib/k_manager/version.rb +1 -1
  33. data/lib/k_manager/watcher.rb +113 -0
  34. data/lib/k_manager/{x_project.rb → x_resource_documents/x_project.rb} +0 -0
  35. data/lib/k_manager/{x_project_manager.rb → x_resource_documents/x_project_manager.rb} +0 -0
  36. data/lib/k_manager/{x_register.rb → x_resource_documents/x_register.rb} +0 -0
  37. data/lib/k_manager.rb +111 -24
  38. data/tasks/watch.rake +18 -0
  39. metadata +118 -25
  40. data/Assessment1.md +0 -127
  41. data/Assessment2.md +0 -88
  42. data/lib/k_manager/configuration/project_config.rb +0 -14
  43. data/lib/k_manager/create_document.rb +0 -31
  44. data/lib/k_manager/documents/basic_document.rb +0 -21
  45. data/lib/k_manager/documents/builder_document.rb +0 -18
  46. data/lib/k_manager/documents/document_taggable.rb +0 -94
  47. data/lib/k_manager/documents/model_document.rb +0 -19
  48. data/lib/k_manager/project.rb +0 -50
  49. data/lib/k_manager/resources/csv_file_resource.rb +0 -27
  50. data/lib/k_manager/resources/factories/document_factory.rb +0 -52
  51. data/lib/k_manager/resources/json_file_resource.rb +0 -22
  52. data/lib/k_manager/resources/yaml_file_resource.rb +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e1e2ee281f38810223b80d27b7ed6ba17f6d766c9bc3362925cb781faea6e6f
4
- data.tar.gz: cce3bcc06ce095f5f00c55bc60ac47b0ea493930db4ebed635fe106303cf5957
3
+ metadata.gz: 10d0353e9cdde7cbec88c6b6a7dd47135da7fa026bf90ec9b6e5017c75ac2a0b
4
+ data.tar.gz: a77c72d2aa9888f29f3b3b835919828cbf6480e349cecb723e6862d81478f540
5
5
  SHA512:
6
- metadata.gz: 7c6154b2a689a193cc88415d89959ca564783b04ee256a74c9a845c3d94102c2ae69be11d47e6cf01cc8e75ca52784d2f4a5b4cb8f373be074112464e7a1da5c
7
- data.tar.gz: 70e7121af0c37a4458670b4668e4e8a9f0e4c798e317bca98ccdb27414529b4b7966373f0689a654991eefabdd9af783536aba33691c54b844cc51ade55071c5
6
+ metadata.gz: eda368dd81ee863ad42831c6527f27b56a60d78d2e2417d377d8425fc6bcf823efe3998a61c5c358ff0329ba2251530b16e94e51acf44c2d463e0dc24874d921
7
+ data.tar.gz: 6aa66dabe51fcecf3d76dd65f202e5b21b7023133ca1892d1d743ed7059f0b7d3e2a00a354ea3b6d780ec59a77131d50b784aaf786e0937f20a8f71a419d24ea
data/.rubocop.yml CHANGED
@@ -7,6 +7,7 @@ AllCops:
7
7
  Exclude:
8
8
  - "_/**/*"
9
9
  - "spec/samples/**/*"
10
+ - "spec/k_manager/scenarios/**/*"
10
11
 
11
12
  Metrics/BlockLength:
12
13
  Exclude:
data/Gemfile CHANGED
@@ -23,3 +23,26 @@ group :development, :test do
23
23
  gem 'rubocop-rake', require: false
24
24
  gem 'rubocop-rspec', require: false
25
25
  end
26
+
27
+ # If local dependency
28
+ if ENV['KLUE_LOCAL_GEMS']&.to_s&.downcase == 'true'
29
+ group :development, :test do
30
+ puts 'Using Local GEMs'
31
+ gem 'handlebars-helpers' , path: '../handlebars-helpers'
32
+ gem 'k_builder' , path: '../k_builder'
33
+ gem 'k_builder-dotnet' , path: '../k_builder-dotnet'
34
+ # gem 'k_builder-package_json' , path: '../k_builder-package_json'
35
+ gem 'k_builder-webpack5' , path: '../k_builder-webpack5'
36
+ gem 'k_config' , path: '../k_config'
37
+ gem 'k_decor' , path: '../k_decor'
38
+ gem 'k_director' , path: '../k_director'
39
+ gem 'k_doc' , path: '../k_doc'
40
+ gem 'k_domain' , path: '../k_domain'
41
+ gem 'k_ext-github' , path: '../k_ext-github'
42
+ gem 'k_fileset' , path: '../k_fileset'
43
+ gem 'k_log' , path: '../k_log'
44
+ gem 'k_type' , path: '../k_type'
45
+ gem 'k_util' , path: '../k_util'
46
+ gem 'peeky' , path: '../peeky'
47
+ end
48
+ end
data/Rakefile CHANGED
@@ -8,6 +8,8 @@ require 'bundler/gem_tasks'
8
8
  require 'rspec/core/rake_task'
9
9
  require 'k_manager/version'
10
10
 
11
+ Dir.glob('tasks/*.rake').each { |r| import r }
12
+
11
13
  RSpec::Core::RakeTask.new(:spec)
12
14
 
13
15
  require 'rake/extensiontask'
data/docs/flow.drawio ADDED
@@ -0,0 +1,16 @@
1
+ <mxfile host="65bd71144e">
2
+ <diagram id="AE9tsI4e_FXgj975v7li" name="Page-1">
3
+ <mxGraphModel dx="1087" dy="583" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" background="#ffffff" math="0" shadow="0">
4
+ <root>
5
+ <mxCell id="0"/>
6
+ <mxCell id="1" parent="0"/>
7
+ <mxCell id="2" value="KManager" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
8
+ <mxGeometry x="200" y="70" width="120" height="60" as="geometry"/>
9
+ </mxCell>
10
+ <mxCell id="3" value="" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
11
+ <mxGeometry x="390" y="210" width="120" height="60" as="geometry"/>
12
+ </mxCell>
13
+ </root>
14
+ </mxGraphModel>
15
+ </diagram>
16
+ </mxfile>
data/exe/k_manager ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'pry'
6
+ puts 'dddddddddddddddddddddd'
7
+
8
+ lib_path = File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
9
+ gem_path = File.expand_path(File.join(File.dirname(__FILE__), '../Gemfile'))
10
+
11
+ $LOAD_PATH.unshift(lib_path)
12
+
13
+ ENV['BUNDLE_GEMFILE'] = gem_path
14
+
15
+ require 'bundler/setup'
16
+ require 'k_manager'
17
+ require 'k_manager/cli/commands'
18
+
19
+ Dry::CLI.new(KManager::Cli::Commands).call
data/hooks/update-version CHANGED
@@ -30,4 +30,4 @@ end
30
30
  output.push('')
31
31
 
32
32
  printf "%-25<label>s : %<version>s\n", label: 'GEM VERSION', version: version
33
- File.open('lib/k_manager/version.rb', 'w+') { |f| f.write(output.join("\n")) }
33
+ File.write('lib/k_manager/version.rb', output.join("\n"))
data/k_manager.gemspec CHANGED
@@ -38,10 +38,19 @@ Gem::Specification.new do |spec|
38
38
  spec.require_paths = ['lib']
39
39
  # spec.extensions = ['ext/k_manager/extconf.rb']
40
40
 
41
+ spec.add_dependency 'dry-struct', '~> 1'
42
+ spec.add_dependency 'filewatcher', '~> 2.0.0.beta5'
41
43
  spec.add_dependency 'k_builder', '~> 0.0.0'
44
+ spec.add_dependency 'k_director', '~> 0.1'
42
45
  spec.add_dependency 'k_doc', '~> 0.0.0'
46
+ spec.add_dependency 'k_domain', '~> 0.0.0'
43
47
  spec.add_dependency 'k_ext-github', '~> 0.0.0'
48
+ spec.add_dependency 'k_fileset', '~> 0.0.0'
44
49
  spec.add_dependency 'k_log', '~> 0.0.0'
45
50
  # spec.add_dependency 'k_type' , '~> 0.0.0'
46
51
  # spec.add_dependency 'k_util' , '~> 0.0.0'
52
+ spec.add_dependency 'dry-cli', '~> 0.7.0'
53
+ spec.metadata = {
54
+ 'rubygems_mfa_required' => 'true'
55
+ }
47
56
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ class Area
5
+ include KLog::Logging
6
+ extend Forwardable
7
+
8
+ attr_reader :manager
9
+ # TODO: I have not got a use for area name yet, it may be able to drive default config, but not sure.
10
+ attr_reader :name
11
+ attr_reader :namespace
12
+ attr_reader :config
13
+
14
+ def initialize(**opts)
15
+ @manager = opts[:manager]
16
+ @name = opts[:name]
17
+
18
+ raise 'Area name is required' unless @name
19
+
20
+ @namespace = opts[:namespace] || @name
21
+ @config = KConfig.configuration(@name)
22
+ end
23
+
24
+ def resource_manager
25
+ @resource_manager ||= KManager::Resources::ResourceManager.new(self)
26
+ end
27
+
28
+ def_delegators :resource_manager, :resource_changed, :resources, :find_by_uri
29
+
30
+ def debug(*sections)
31
+ log.kv 'Area' , name
32
+ log.kv 'Namespace' , namespace
33
+
34
+ resource_manager.debug if sections.include?(:resource) || sections.include?(:resources)
35
+ config.debug if sections.include?(:config)
36
+ end
37
+
38
+ # rubocop:disable Metrics/AbcSize
39
+ def attribute_values(prefix = nil)
40
+ result = {}
41
+ result["#{prefix}name".to_sym] = name
42
+ result["#{prefix}namespace".to_sym] = namespace
43
+ result["#{prefix}resource_count".to_sym] = resource_manager.resources.length
44
+ result["#{prefix}document_count".to_sym] = resource_manager.resources.sum { |resource| resource.documents.length }
45
+ result
46
+ end
47
+ # rubocop:enable Metrics/AbcSize
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ module Cli
5
+ module Commands
6
+ class BaseCommand < Dry::CLI::Command
7
+ include KLog::Logging
8
+
9
+ option :log_level,
10
+ default: 'none',
11
+ aliases: ['-l'],
12
+ desc: 'Log level (:none, :debug)'
13
+
14
+ private
15
+
16
+ def absolute_path(path, base_path)
17
+ pathname = Pathname.new(path)
18
+ pathname.absolute? ? path : File.expand_path(path, base_path)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/cli'
4
+ require_relative 'base_command'
5
+ require_relative 'version'
6
+ require_relative 'watch'
7
+ require_relative 'new'
8
+ require_relative 'info'
9
+
10
+ module KManager
11
+ module Cli
12
+ module Commands
13
+ extend Dry::CLI::Registry
14
+
15
+ register 'version' , Version , aliases: %w[v -v --version]
16
+ register 'watch' , Watch , aliases: %w[w]
17
+ register 'new' , New , aliases: %w[n]
18
+ register 'info' , Info , aliases: %w[i]
19
+
20
+ # register 'generate', aliases: ['g'] do |prefix|
21
+ # prefix.register 'config', Generate::Configuration
22
+ # end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ module Cli
5
+ module Commands
6
+ class Info < KManager::Cli::Commands::BaseCommand
7
+ desc 'Print information about builder, project, and watcher folders'
8
+
9
+ argument :project_folder,
10
+ default: '.',
11
+ desc: 'Info project folder (defaults to current directory .)'
12
+
13
+ option :builder_folder,
14
+ default: '.builders',
15
+ desc: 'The builder folder, defaults to (.builders)'
16
+
17
+ def call(project_folder:, builder_folder:, **)
18
+ project_folder = absolute_path(project_folder, Dir.pwd)
19
+ # name must be after project_folder
20
+ name = File.basename(project_folder)
21
+ builder_folder = absolute_path(builder_folder, project_folder)
22
+
23
+ log_params(name, project_folder, builder_folder)
24
+ end
25
+
26
+ private
27
+
28
+ def log_params(name, project_folder, builder_folder)
29
+ log.section_heading('Information')
30
+ log.kv 'Project Name' , name
31
+ log.kv 'Project Folder' , project_folder
32
+ log.kv 'Builder Folder' , builder_folder
33
+ log.kv 'Project File Count' , project_file_count(project_folder, builder_folder)
34
+ log.kv 'Builder File Count' , builder_file_count(builder_folder)
35
+
36
+ # log_files(project_folder, project_file_list(project_folder, builder_folder), 'Project Files List')
37
+ log_files(builder_folder, builder_file_list(builder_folder), 'Builder Files')
38
+ end
39
+
40
+ def log_files(root_folder, files, title)
41
+ log.section_heading(title)
42
+ files.each do |file|
43
+ pathname = Pathname.new(file)
44
+ puts pathname.relative_path_from(root_folder).to_s
45
+ end
46
+ end
47
+
48
+ def project_file_count(project_folder, builder_folder)
49
+ project_file_list(project_folder, builder_folder).length
50
+ end
51
+
52
+ def project_file_list(project_folder, builder_folder)
53
+ return @project_file_list if defined? @project_file_list
54
+
55
+ @project_file_list = begin
56
+ pattern = '{.[^\.]*,*,**}/**/*'
57
+
58
+ result = Dir.chdir(project_folder) do
59
+ Dir.glob(pattern).map { |f| File.realpath(f) }
60
+ end
61
+
62
+ result - builder_file_list(builder_folder)
63
+ end
64
+ end
65
+
66
+ def builder_file_count(builder_folder)
67
+ builder_file_list(builder_folder).length
68
+ end
69
+
70
+ # Dir.glob(".").map(&File.method(:realpath))
71
+ def builder_file_list(builder_folder)
72
+ return @builder_file_list if defined? @builder_file_list
73
+
74
+ @builder_file_list = Dir.chdir(builder_folder) do
75
+ Dir.glob('**/*').map { |f| File.realpath(f) }
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ module Cli
5
+ module Commands
6
+ class New < KManager::Cli::Commands::BaseCommand
7
+ desc 'Create a new KManager project'
8
+
9
+ argument :project_folder,
10
+ default: '.',
11
+ desc: 'New project folder (defaults to current directory .)'
12
+
13
+ option :builder_folder,
14
+ default: '.builders',
15
+ desc: 'The builder folder, defaults to (.builders)'
16
+
17
+ option :force,
18
+ default: false,
19
+ aliases: ['-f'],
20
+ desc: 'Force even if guard fails'
21
+
22
+ option :template,
23
+ default: false,
24
+ aliases: ['-t'],
25
+ desc: 'Starter template'
26
+
27
+ option :log_level,
28
+ default: nil,
29
+ desc: 'Log level, use debug for more info'
30
+
31
+ example [
32
+ ' # Project in current directory - will create a .builders folder and boot file at ./builders/boot.rb',
33
+ '-b ../config/boot.rb # Project in current directory - will create a .builders folder and boot file at ./config/boot.rb',
34
+ '-t ruby_gem # Use starter template `ruby_gem` to setup the files in .builders/*',
35
+ 'my_project # will watch .xmen folder'
36
+ ]
37
+
38
+ # rubocop:disable Metrics/ParameterLists
39
+ def call(project_folder:, builder_folder:, log_level:, force:, template:, **)
40
+ project_folder = absolute_path(project_folder, Dir.pwd)
41
+ name = File.basename(project_folder)
42
+ builder_folder = absolute_path(builder_folder, project_folder)
43
+ template_root_folder = File.expand_path('~/dev/kgems/k_templates/definitions/starter')
44
+
45
+ log_params(name, project_folder, builder_folder, force, log_level, template_root_folder, template) if log_level == 'debug'
46
+
47
+ create_project(name, project_folder, builder_folder, template_root_folder, template) if can_create?(force, builder_folder)
48
+ end
49
+ # rubocop:enable Metrics/ParameterLists
50
+
51
+ private
52
+
53
+ def create_project(name, project_folder, builder_folder, template_root_folder, template)
54
+ FileUtils.mkdir_p(project_folder)
55
+ FileUtils.mkdir_p(builder_folder)
56
+
57
+ # handle_main_start_command
58
+
59
+ setup_builder_from_template(name, builder_folder, template_root_folder, 'default') unless setup_builder_from_template(name, builder_folder, template_root_folder, template)
60
+
61
+ log.info 'Project created'
62
+ end
63
+
64
+ def setup_builder_from_template(name, builder_folder, template_root_folder, template = nil)
65
+ return false unless template
66
+
67
+ # /Users/davidcruwys/dev/kgems/k_templates/definitions/starter/ruby_gem/.starter.json
68
+ template_folder = File.join(template_root_folder, template)
69
+ starter_config = template_starter_config(template_folder)
70
+
71
+ return false unless starter_config
72
+
73
+ builder = get_builder(builder_folder, template_folder)
74
+
75
+ return false if starter_config['files'].nil? || starter_config['files'].empty?
76
+
77
+ starter_config['files']&.each do |relative_file|
78
+ builder.add_file(relative_file, template_file: relative_file, name: name)
79
+ end
80
+ end
81
+
82
+ def template_starter_config(template_folder)
83
+ starter_file = File.join(template_folder, '.starter.json')
84
+
85
+ return nil unless File.exist?(starter_file)
86
+
87
+ JSON.parse(File.read(starter_file))
88
+ end
89
+
90
+ # rubocop:disable Metrics/AbcSize
91
+ def get_builder(builder_folder, template_folder)
92
+ Handlebars::Helpers.configure do |config|
93
+ config.helper_config_file = File.join(Gem.loaded_specs['handlebars-helpers'].full_gem_path, '.handlebars_helpers.json')
94
+ config.string_formatter_config_file = File.join(Gem.loaded_specs['handlebars-helpers'].full_gem_path, '.handlebars_string_formatters.json')
95
+ end
96
+
97
+ KConfig.configure(:starter_template) do |config|
98
+ config.target_folders.add(:builder , builder_folder)
99
+ config.template_folders.add(:template , template_folder)
100
+ end
101
+
102
+ KBuilder::BaseBuilder.init(KConfig.configuration(:starter_template))
103
+ end
104
+ # rubocop:enable Metrics/AbcSize
105
+
106
+ def can_create?(force, builder_folder)
107
+ return true if force
108
+ return true unless File.directory?(builder_folder)
109
+
110
+ log.error "Project builder folder already exists: #{builder_folder}"
111
+
112
+ false
113
+ end
114
+
115
+ # rubocop:disable Metrics/ParameterLists
116
+ def log_params(name, project_folder, builder_folder, force, log_level, template_root_folder, template)
117
+ log.section_heading('Create new project')
118
+ log.kv 'name' , name
119
+ log.kv 'project_folder' , project_folder
120
+ log.kv 'builder_folder' , builder_folder
121
+ log.kv 'force' , force
122
+ log.kv 'log_level' , log_level
123
+ log.kv 'template_root_folder' , template_root_folder
124
+ log.kv 'template' , template
125
+ end
126
+ # rubocop:enable Metrics/ParameterLists
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ module Cli
5
+ module Commands
6
+ class Version < Dry::CLI::Command
7
+ desc 'Print version'
8
+
9
+ def call(*)
10
+ puts KManager::VERSION
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ module Cli
5
+ module Commands
6
+ class Watch < KManager::Cli::Commands::BaseCommand
7
+ desc 'Watch folder'
8
+
9
+ argument :builder_folder,
10
+ default: '.builders',
11
+ desc: 'Folder to watch (aka builder folder), defaults to (.builders)'
12
+
13
+ option :boot_file,
14
+ default: 'boot.rb',
15
+ aliases: ['-b'],
16
+ desc: 'The boot file used for k_manager configuration'
17
+
18
+ example [
19
+ ' # will watch .builders folder and boot from ./builders/boot.rb',
20
+ '-b ../config/boot.rb # will watch .builders folder and boot from ./config/boot.rb',
21
+ '.xmen # will watch .xmen folder'
22
+ ]
23
+
24
+ def call(builder_folder:, boot_file:, log_level: , **)
25
+ builder_folder = absolute_path(builder_folder, Dir.pwd)
26
+ boot_file = absolute_path(boot_file, builder_folder)
27
+
28
+ log_params(builder_folder, boot_file) if log_level == 'debug'
29
+
30
+ # If you do a System Exit (control+c) you can go into a reboot sequence based on options
31
+ # if the option is not set then system will exit gracefully
32
+ while keep_watching(builder_folder, boot_file); end
33
+ end
34
+
35
+ private
36
+
37
+ # rubocop:disable Metrics/AbcSize
38
+ def keep_watching(builder_folder, boot_file)
39
+ Dir.chdir(builder_folder) do
40
+ watcher = KManager::Watcher.new(builder_folder, boot_file)
41
+ watcher.start
42
+ end
43
+ false
44
+ rescue Interrupt, SystemExit
45
+ if KManager.opts.reboot_on_kill == true || KManager.opts.reboot_on_kill == 1
46
+ puts "\nRebooting #{KManager.opts.app_name}..."
47
+ sleep KManager.opts.reboot_sleep unless KManager.opts.reboot_sleep.zero?
48
+
49
+ return true
50
+ end
51
+
52
+ puts "\nExiting..."
53
+ false
54
+ end
55
+ # rubocop:enable Metrics/AbcSize
56
+
57
+ def log_params(builder_folder, boot_file)
58
+ log.section_heading('Watch project')
59
+ log.kv 'builder_folder' , builder_folder
60
+ log.kv 'boot_file' , boot_file
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KManager
4
+ # Factories for creating common DSL documents quickly
5
+ #
6
+ # Example:
7
+ #
8
+ # KManager.csv(file: 'somepath/somefile.csv') do
9
+ # load
10
+ # end
11
+ class DocumentFactory
12
+ include KLog::Logging
13
+
14
+ # Create a KDoc::Model instance
15
+ def action(key = nil, **opts, &block)
16
+ document = new_document(KDoc::Action, key, **opts, &block)
17
+
18
+ attach_to_current_resource(document, change_content_type: :ruby)
19
+ end
20
+
21
+ # Create a KDoc::Model instance
22
+ def model(key = nil, **opts, &block)
23
+ document = new_document(KDoc::Model, key, **opts, &block)
24
+
25
+ attach_to_current_resource(document, change_content_type: :dsl)
26
+ end
27
+
28
+ # Create a KDoc::CsvDoc instance
29
+ def csv(key = nil, **opts, &block)
30
+ document = new_document(KDoc::CsvDoc, key, **opts, &block)
31
+
32
+ attach_to_current_resource(document, change_content_type: :dsl)
33
+ rescue StandardError => e
34
+ log.error(e)
35
+ end
36
+
37
+ # Create a KDoc::JsonDoc instance
38
+ def json(key = nil, **opts, &block)
39
+ document = new_document(KDoc::JsonDoc, key, **opts, &block)
40
+
41
+ attach_to_current_resource(document, change_content_type: :dsl)
42
+ rescue StandardError => e
43
+ log.error(e)
44
+ end
45
+
46
+ # Create a KDoc::YamlDoc instance
47
+ def yaml(key = nil, **opts, &block)
48
+ document = new_document(KDoc::YamlDoc, key, **opts, &block)
49
+
50
+ attach_to_current_resource(document, change_content_type: :dsl)
51
+ rescue StandardError => e
52
+ log.error(e)
53
+ end
54
+
55
+ private
56
+
57
+ # If document gets created dynamically due to class_eval then they
58
+ # can attach themselves to the currently focussed resource.
59
+ #
60
+ # It will throw an error if for_resource has not been called earlier
61
+ # in the thread lifecycle.
62
+ def attach_to_current_resource(document, change_content_type: nil)
63
+ return document unless KManager.current_resource
64
+
65
+ KManager.for_current_resource do |resource|
66
+ resource.attach_document(document, change_content_type: change_content_type)
67
+ end
68
+ end
69
+
70
+ # Create an instance of a document
71
+ #
72
+ # @param [Class<DocumentTaggable>] klass type of document to create
73
+ def new_document(klass, key = nil, **opts, &block)
74
+ # Should be able to infer
75
+ # key = KManager.current_resource.documents.length.zero? ? KManager.current_resource.infer_key : nil
76
+
77
+ # klass.new(key, **{ owner: KManager.current_resource }.merge(opts), &block)
78
+ klass.new(key, **opts, &block)
79
+ end
80
+ end
81
+ end