k_manager 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +31 -0
- data/.gitignore +50 -0
- data/.rspec +3 -0
- data/.rubocop.yml +85 -0
- data/Assessment1.md +127 -0
- data/Assessment2.md +88 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +25 -0
- data/Guardfile +30 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +33 -0
- data/STORIES.md +42 -0
- data/ToDo.md +8 -0
- data/USAGE.md +19 -0
- data/bin/console +16 -0
- data/bin/k +36 -0
- data/bin/kgitsync +76 -0
- data/bin/khotfix +244 -0
- data/bin/setup +11 -0
- data/hooks/pre-commit +87 -0
- data/hooks/update-version +33 -0
- data/k_manager.gemspec +47 -0
- data/lib/k_manager.rb +50 -0
- data/lib/k_manager/configuration/project_config.rb +14 -0
- data/lib/k_manager/create_document.rb +31 -0
- data/lib/k_manager/documents/basic_document.rb +21 -0
- data/lib/k_manager/documents/builder_document.rb +18 -0
- data/lib/k_manager/documents/document_taggable.rb +94 -0
- data/lib/k_manager/documents/model_document.rb +19 -0
- data/lib/k_manager/project.rb +50 -0
- data/lib/k_manager/resources/base_resource.rb +182 -0
- data/lib/k_manager/resources/csv_file_resource.rb +27 -0
- data/lib/k_manager/resources/factories/document_factory.rb +52 -0
- data/lib/k_manager/resources/factories/ruby_document_factory.rb +57 -0
- data/lib/k_manager/resources/file_resource.rb +93 -0
- data/lib/k_manager/resources/json_file_resource.rb +22 -0
- data/lib/k_manager/resources/ruby_file_resource.rb +32 -0
- data/lib/k_manager/resources/unknown_file_resource.rb +22 -0
- data/lib/k_manager/resources/x_resource.rb +243 -0
- data/lib/k_manager/resources/yaml_file_resource.rb +21 -0
- data/lib/k_manager/version.rb +5 -0
- data/lib/k_manager/x_project.rb +698 -0
- data/lib/k_manager/x_project_manager.rb +133 -0
- data/lib/k_manager/x_register.rb +199 -0
- data/lib/k_manager/x_resource_documents/resource_document.rb +51 -0
- metadata +150 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KManager
|
4
|
+
module Resources
|
5
|
+
# Represents a Unknown file resource.
|
6
|
+
class UnknownFileResource < KManager::Resources::FileResource
|
7
|
+
def initialize(**opts)
|
8
|
+
super(**opts)
|
9
|
+
@type = :unknown
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_document
|
13
|
+
document.data = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# def debug
|
17
|
+
# L.warn 'unknown document'
|
18
|
+
# L.info content
|
19
|
+
# end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,243 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# module KDsl
|
4
|
+
# module Resources
|
5
|
+
# # Resource represents a file in the project
|
6
|
+
# #
|
7
|
+
# # Resources currently represent DSL's but I think I can have support for
|
8
|
+
# # other types of files such as (PORO, Ruby, JSON, CSV) and be able to use
|
9
|
+
# # them easily.
|
10
|
+
# class Resource
|
11
|
+
# # Resources must belong to a factory
|
12
|
+
# attr_reader :project
|
13
|
+
|
14
|
+
# # Status of the resource
|
15
|
+
# # - :initialized
|
16
|
+
# # - :content_loading
|
17
|
+
# # - :content_loaded
|
18
|
+
# # - :registering
|
19
|
+
# # - :registered
|
20
|
+
# # - :loading
|
21
|
+
# # - :loaded
|
22
|
+
# attr_reader :status
|
23
|
+
|
24
|
+
# # Resources create documents via a resource specific factory
|
25
|
+
# attr_accessor :document_factory
|
26
|
+
|
27
|
+
# # Store an exeption that may exist
|
28
|
+
# # REFACT: This should move to ResourceDocument
|
29
|
+
# attr_accessor :error
|
30
|
+
|
31
|
+
# # Currently supports read from file, but will support read from HTTP in the future
|
32
|
+
# SOURCE_FILE = 'file'
|
33
|
+
|
34
|
+
# # Not implement
|
35
|
+
# # SOURCE_URI = 'uri'
|
36
|
+
# # SOURCE_DYNAMIC = 'dynamic'
|
37
|
+
|
38
|
+
# TYPE_UNKNOWN = 'unknown'
|
39
|
+
# TYPE_CSV = 'csv'
|
40
|
+
# TYPE_JSON = 'json'
|
41
|
+
# TYPE_RUBY = 'ruby'
|
42
|
+
# TYPE_RUBY_DSL = 'dsl'
|
43
|
+
# TYPE_YAML = 'yaml'
|
44
|
+
|
45
|
+
# # Source of the content
|
46
|
+
# #
|
47
|
+
# # :file, :uri, :dynamic
|
48
|
+
# attr_reader :source
|
49
|
+
|
50
|
+
# # Type of resource, infered via the document factory type
|
51
|
+
# attr_accessor :resource_type
|
52
|
+
|
53
|
+
# # Full file path
|
54
|
+
# #
|
55
|
+
# # example: /Users/davidcruwys/dev/kgems/k_dsl/spec/factories/dsls/common-auth/admin_user.rb
|
56
|
+
# attr_reader :file
|
57
|
+
|
58
|
+
# # If the file is watched, what was it's base watch path
|
59
|
+
# #
|
60
|
+
# # Currently only used for informational/debugging purpose
|
61
|
+
# #
|
62
|
+
# # example: /Users/davidcruwys/dev/kgems/k_dsl/spec/factories/dsls/common-auth
|
63
|
+
# attr_reader :watch_path
|
64
|
+
|
65
|
+
# # Content of resource, use read content to load this property
|
66
|
+
# attr_reader :content
|
67
|
+
|
68
|
+
# # List of documents derived from this resource
|
69
|
+
# #
|
70
|
+
# # Most resources will create on document, but a DSL can generate multiple
|
71
|
+
# # documents and some future resources may do as well
|
72
|
+
# # Currently there will always be a minimum of 1 document even if the resource
|
73
|
+
# # is not a data resource, e.g. Ruby class
|
74
|
+
# attr_accessor :documents
|
75
|
+
|
76
|
+
# def initialize(project: nil, source: nil, file: nil, watch_path: nil)
|
77
|
+
# @status = :initialized
|
78
|
+
# @project = project
|
79
|
+
# @source = source
|
80
|
+
# @file = file
|
81
|
+
# @watch_path = watch_path
|
82
|
+
# @documents = []
|
83
|
+
# end
|
84
|
+
|
85
|
+
# def self.instance(project:, source: KDsl::Resources::Resource::SOURCE_FILE, file: nil, watch_path: nil)
|
86
|
+
# raise ::KDsl::Error, 'Unknown source' unless [SOURCE_FILE].include? source
|
87
|
+
|
88
|
+
# resource = Resource.new(
|
89
|
+
# project: project,
|
90
|
+
# source: source,
|
91
|
+
# file: file,
|
92
|
+
# watch_path: watch_path
|
93
|
+
# )
|
94
|
+
|
95
|
+
# resource.document_factory = KDsl::Resources::Factories::DocumentFactory.instance(resource, source, file)
|
96
|
+
# resource
|
97
|
+
# end
|
98
|
+
|
99
|
+
# def self.reset_instance(resource)
|
100
|
+
# resource.project.delete_resource_documents_for_resource(resource)
|
101
|
+
# resource.documents = []
|
102
|
+
# end
|
103
|
+
|
104
|
+
# def create_documents
|
105
|
+
# KDsl.target_resource = self
|
106
|
+
|
107
|
+
# load_content
|
108
|
+
# register
|
109
|
+
|
110
|
+
# KDsl.target_resource = nil
|
111
|
+
# end
|
112
|
+
|
113
|
+
# def load_content
|
114
|
+
# @status = :content_loading
|
115
|
+
# @content = nil
|
116
|
+
# if source === SOURCE_FILE
|
117
|
+
# L.kv 'SOURCE_FILE', SOURCE_FILE
|
118
|
+
# if File.exist?(file)
|
119
|
+
# begin
|
120
|
+
# @content = File.read(file)
|
121
|
+
# rescue StandardError => e
|
122
|
+
# L.error e
|
123
|
+
# end
|
124
|
+
# else
|
125
|
+
# @error = KDsl::Error.new("Source file not found: #{file}")
|
126
|
+
# end
|
127
|
+
# end
|
128
|
+
# @status = :content_loaded
|
129
|
+
# end
|
130
|
+
|
131
|
+
# def register
|
132
|
+
# @status = :registering
|
133
|
+
# document_factory.create_documents
|
134
|
+
# @status = :registered
|
135
|
+
# end
|
136
|
+
|
137
|
+
# def load
|
138
|
+
# @status = :loading
|
139
|
+
# document_factory.parse_content
|
140
|
+
# @status = :loaded
|
141
|
+
# end
|
142
|
+
|
143
|
+
# def exist?
|
144
|
+
# source === SOURCE_FILE && File.exist?(file)
|
145
|
+
# end
|
146
|
+
|
147
|
+
# def new_document(klass: KDsl::Model::Document, key: infer_document_key, type: infer_document_type, namespace: infer_document_namespace)
|
148
|
+
# klass.new(key, type, namespace: namespace)
|
149
|
+
# end
|
150
|
+
|
151
|
+
# # TEST REQUIRED
|
152
|
+
# def add_documents(*documents)
|
153
|
+
# documents.each do |document|
|
154
|
+
# add_document(document)
|
155
|
+
# end
|
156
|
+
# end
|
157
|
+
|
158
|
+
# # TEST REQUIRED
|
159
|
+
# def add_document(document)
|
160
|
+
# # project.register_dsl(document)
|
161
|
+
# project.add_resource_document(self, document)
|
162
|
+
# document.resource = self
|
163
|
+
# documents << document
|
164
|
+
# document
|
165
|
+
# end
|
166
|
+
|
167
|
+
# # example: ~/dev/kgems/k_dsl/spec/factories/dsls
|
168
|
+
# def relative_watch_path
|
169
|
+
# @relative_watch_path ||= watch_path&.delete_prefix(base_resource_path_expanded)
|
170
|
+
# end
|
171
|
+
|
172
|
+
# # example: ~/dev/kgems/k_dsl/spec/factories/dsls
|
173
|
+
# def filename
|
174
|
+
# @filename ||= File.basename(file)
|
175
|
+
# end
|
176
|
+
|
177
|
+
# # example: ~/dev/kgems/k_dsl/spec/factories/dsls
|
178
|
+
# def base_resource_path
|
179
|
+
# project.config.base_resource_path
|
180
|
+
# end
|
181
|
+
|
182
|
+
# # example: /Users/david/dev/kgems/k_dsl/spec/factories/dsls
|
183
|
+
# def base_resource_path_expanded
|
184
|
+
# @base_resource_path ||= File.expand_path(project.config.base_resource_path)
|
185
|
+
# end
|
186
|
+
|
187
|
+
# def infer_document_key
|
188
|
+
# if filename
|
189
|
+
# File.basename(filename, File.extname(filename))
|
190
|
+
# else
|
191
|
+
# 'unknown'
|
192
|
+
# end
|
193
|
+
# end
|
194
|
+
|
195
|
+
# def infer_document_namespace
|
196
|
+
# ''
|
197
|
+
# end
|
198
|
+
|
199
|
+
# def infer_document_type
|
200
|
+
# if filename
|
201
|
+
# ext = File.extname(filename)
|
202
|
+
# case ext
|
203
|
+
# when '.json', '.csv', '.yaml'
|
204
|
+
# ext.delete('.')
|
205
|
+
# when '.rb'
|
206
|
+
# 'ruby'
|
207
|
+
# else
|
208
|
+
# 'unknown'
|
209
|
+
# end
|
210
|
+
# else
|
211
|
+
# 'unknown'
|
212
|
+
# end
|
213
|
+
# end
|
214
|
+
|
215
|
+
# def debug(*formats)
|
216
|
+
# formats = %i[resource] if formats.empty?
|
217
|
+
|
218
|
+
# formats.each do |format|
|
219
|
+
# case format
|
220
|
+
# when :resource
|
221
|
+
# L.kv 'project.name', project.name
|
222
|
+
# L.kv 'document_factory.class', document_factory.class.name
|
223
|
+
# L.kv 'status', status
|
224
|
+
# L.kv 'error', error&.message
|
225
|
+
# L.kv 'source', source
|
226
|
+
# L.kv 'resource_type', resource_type
|
227
|
+
# L.kv 'file', file
|
228
|
+
# L.kv 'watch_path', watch_path
|
229
|
+
# # L.kv 'content', content
|
230
|
+
# L.kv 'document.count', documents.length
|
231
|
+
# when :document
|
232
|
+
# documents.each do |document|
|
233
|
+
# document.debug(true)
|
234
|
+
# end
|
235
|
+
# when :document_data
|
236
|
+
# # Data Only
|
237
|
+
# documents.each(&:debug)
|
238
|
+
# end
|
239
|
+
# end
|
240
|
+
# end
|
241
|
+
# end
|
242
|
+
# end
|
243
|
+
# end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KManager
|
4
|
+
module Resources
|
5
|
+
# Represents a YAML file resource.
|
6
|
+
class YamlFileResource < KManager::Resources::FileResource
|
7
|
+
def initialize(**opts)
|
8
|
+
super(**opts)
|
9
|
+
@type = :yaml
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_document
|
13
|
+
data = YAML.safe_load(content)
|
14
|
+
document.data = data
|
15
|
+
end
|
16
|
+
# def debug
|
17
|
+
# L.ostruct(KDsl::Util.data.to_struct(self.document.data))
|
18
|
+
# end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,698 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# module KManager
|
4
|
+
# # A project represents all the DSL and Data resources that are available, it keeps a track on the
|
5
|
+
# # in memory state of the resources, are they loaded into memory or not.
|
6
|
+
# class Project
|
7
|
+
# # Project name
|
8
|
+
# attr_reader :name
|
9
|
+
|
10
|
+
# # Root namespace
|
11
|
+
# attr_reader :namespace
|
12
|
+
|
13
|
+
# # Configuration for this project
|
14
|
+
# attr_reader :config
|
15
|
+
|
16
|
+
# # Reference to manager that manages all projects
|
17
|
+
# attr_accessor :manager
|
18
|
+
|
19
|
+
# # List of DSL's instances
|
20
|
+
# attr_reader :dsls
|
21
|
+
|
22
|
+
# # List of path and wild card patterns to watch for.
|
23
|
+
# attr_reader :watch_path_patterns
|
24
|
+
|
25
|
+
# # List of paths containing DSL's
|
26
|
+
# #
|
27
|
+
# # This list does not keep the wild card pattern.
|
28
|
+
# # See: :watch_path_patterns if you need that
|
29
|
+
# #
|
30
|
+
# # It does expand the ** for situations where there
|
31
|
+
# # is a file in that child path
|
32
|
+
# attr_reader :watch_paths
|
33
|
+
|
34
|
+
# # List of resource files that are visible to this project
|
35
|
+
# attr_reader :resources
|
36
|
+
|
37
|
+
# # Link resource and document togetner
|
38
|
+
# # Resources generally have 1 document, but in the case of
|
39
|
+
# # DSL resources there can be more then one document in the resource.
|
40
|
+
# # This would then be storing a resource document for each structure
|
41
|
+
# # found in the DSL.
|
42
|
+
# attr_reader :resource_documents
|
43
|
+
|
44
|
+
# # Listener that is watching for file changes for this project
|
45
|
+
# attr_reader :listener
|
46
|
+
|
47
|
+
# # There is currently a tight cupling between is boolean and DSL's so that they know whether they are being refrenced for registration or importation
|
48
|
+
# # The difference is that importation will execute their interal code block while registration will not.
|
49
|
+
# # attr_reader :current_state
|
50
|
+
# # attr_reader :current_register_file
|
51
|
+
|
52
|
+
# # what file is currently being processed
|
53
|
+
# # attr_reader :current_processing_file
|
54
|
+
|
55
|
+
# def initialize(name, config = nil, &block)
|
56
|
+
# raise KDsl::Error, 'Provide a project name' unless name.is_a?(String) || name.is_a?(Symbol)
|
57
|
+
|
58
|
+
# @name = name
|
59
|
+
# @config = config || KDsl::Manage::ProjectConfig.new
|
60
|
+
|
61
|
+
# # REFACT: Wrap DSL's up into it's own class
|
62
|
+
# @dsls = {}
|
63
|
+
# @watch_path_patterns = []
|
64
|
+
# @watch_paths = []
|
65
|
+
# @resources = []
|
66
|
+
# @resource_documents = []
|
67
|
+
|
68
|
+
# begin
|
69
|
+
# instance_eval(&block) if block_given?
|
70
|
+
# rescue => exception
|
71
|
+
# L.heading "Invalid code block in project during initialization: #{name}"
|
72
|
+
# L.exception exception
|
73
|
+
# raise
|
74
|
+
# end
|
75
|
+
# end
|
76
|
+
|
77
|
+
# def add_resource_document(resource, document)
|
78
|
+
# resource_document = get_resource_document(document.key, document.type, document.namespace)
|
79
|
+
# if resource_document.nil?
|
80
|
+
# resource_document = KDsl::ResourceDocuments::ResourceDocument.new(resource, document)
|
81
|
+
# resource_documents << resource_document
|
82
|
+
# else
|
83
|
+
# L.warn "Cannot add to resource_documents for an existing unique_key: #{document.unique_key}"
|
84
|
+
# end
|
85
|
+
# resource_document
|
86
|
+
# end
|
87
|
+
|
88
|
+
# def delete_resource_documents_for_resource(resource)
|
89
|
+
# resource_documents.delete_if { |rd| rd.resource == resource }
|
90
|
+
# end
|
91
|
+
|
92
|
+
# def resource_document_exist?(key, type = nil, namespace = nil)
|
93
|
+
# resource_document = get_resource_document(key, type, namespace)
|
94
|
+
|
95
|
+
# !resource_document.nil?
|
96
|
+
# end
|
97
|
+
|
98
|
+
# def get_resource_document(key, type = nil, namespace = nil)
|
99
|
+
# unique_key = KDsl::Util.dsl.build_unique_key(key, type, namespace)
|
100
|
+
# # L.kv 'uk', unique_key
|
101
|
+
|
102
|
+
# resource_documents.find { |rd| rd.unique_key == unique_key }
|
103
|
+
# end
|
104
|
+
|
105
|
+
# def get_resource_documents_by_type(type = nil, namespace = nil)
|
106
|
+
# type ||= KDsl.config.default_document_type
|
107
|
+
# type = type.to_s
|
108
|
+
# namespace = namespace.to_s
|
109
|
+
|
110
|
+
# if namespace.nil? || namespace.empty?
|
111
|
+
# resource_documents.select { |resource_document| resource_document.type.to_s == type.to_s }
|
112
|
+
# else
|
113
|
+
# resource_documents.select { |resource_document| resource_document.namespace == namespace.to_s && resource_document.type.to_s == type.to_s }
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
# # rubocop:enable Metrics/AbcSize
|
117
|
+
|
118
|
+
# # Register any files found in the absolute path or path relative to base_resource_path
|
119
|
+
# #
|
120
|
+
# # Files are generally DSL's but support for other types (PORO, Ruby, JSON, CSV) will come
|
121
|
+
# def watch_path(path, ignore: nil)
|
122
|
+
# @watch_path_patterns << path
|
123
|
+
|
124
|
+
# # puts "watch path-before: #{path} "
|
125
|
+
# path = KDsl::Util.file.expand_path(path, config.base_resource_path)
|
126
|
+
# # puts "watch path-after: #{path} "
|
127
|
+
|
128
|
+
# Dir[path].sort.each do |file|
|
129
|
+
# watch_path = File.dirname(file)
|
130
|
+
# @watch_paths << watch_path unless @watch_paths.include? watch_path
|
131
|
+
|
132
|
+
# register_file_resource(file, watch_path: watch_path, path_expansion: false, ignore: ignore)
|
133
|
+
# end
|
134
|
+
# end
|
135
|
+
|
136
|
+
# # Work through each resource and register with document into memory
|
137
|
+
# # so that we can access the data in the resource
|
138
|
+
# def register_resources
|
139
|
+
# @resources.each do |resource|
|
140
|
+
# resource.create_documents
|
141
|
+
# end
|
142
|
+
# end
|
143
|
+
|
144
|
+
# def load_resources
|
145
|
+
# # L.progress(nil, 'Load Resource')
|
146
|
+
# @resources.each do |resource|
|
147
|
+
# # L.progress(nil, 'Debug Resource')
|
148
|
+
# # resource.debug
|
149
|
+
# # resource.documents.each(&:debug)
|
150
|
+
# # L.progress(nil, 'Loading')
|
151
|
+
# resource.load
|
152
|
+
# # resource.documents.each { |d| d.debug(include_header: true) }
|
153
|
+
# end
|
154
|
+
# end
|
155
|
+
|
156
|
+
# def get_data(key, type = :entity, namespace = nil)
|
157
|
+
# resource_document = get_resource_document(key, type, namespace)
|
158
|
+
|
159
|
+
# raise "Could not get data for missing DSL: #{KDsl::Util.dsl.build_unique_key(key, type, namespace)}" if resource_document.nil?
|
160
|
+
|
161
|
+
# resource_document.document.data
|
162
|
+
# # load_data_from_dsl(dsl)
|
163
|
+
# end
|
164
|
+
|
165
|
+
# # def load_data_from_dsl(dsl)
|
166
|
+
# # # # Need to load this file
|
167
|
+
# # # if dsl[:state] == :registered
|
168
|
+
# # # load_file(dsl[:file])
|
169
|
+
# # # end
|
170
|
+
|
171
|
+
# # dsl[:document].data
|
172
|
+
# # end
|
173
|
+
|
174
|
+
# # Register a resource in @registered_resources from a file
|
175
|
+
# #
|
176
|
+
# # Primary resource that is registered will be a Klue DSL
|
177
|
+
# # Other resources that can be supported include
|
178
|
+
# # data files:
|
179
|
+
# # CSV, JSON, YAML
|
180
|
+
# # ruby code
|
181
|
+
# # Classes etc..
|
182
|
+
# def register_file_resource(file, watch_path: nil, path_expansion: true, ignore: nil)
|
183
|
+
# file = KDsl::Util.file.expand_path(file, config.base_resource_path) if path_expansion
|
184
|
+
|
185
|
+
# return if ignore && file.match(ignore)
|
186
|
+
# return unless File.exist?(file)
|
187
|
+
|
188
|
+
# resource = KDsl::Resources::Resource.instance(
|
189
|
+
# project: self,
|
190
|
+
# file: file,
|
191
|
+
# watch_path: watch_path,
|
192
|
+
# source: KDsl::Resources::Resource::SOURCE_FILE)
|
193
|
+
|
194
|
+
# @resources << resource unless @resources.include? resource
|
195
|
+
|
196
|
+
# resource
|
197
|
+
# end
|
198
|
+
|
199
|
+
# # # REACT: This method may not belong to project, it should be in it's own class
|
200
|
+
# # def process_code(code, source_file = nil)
|
201
|
+
# # guard_source_file(source_file)
|
202
|
+
|
203
|
+
# # # L.kv 'process_code.file', file
|
204
|
+
# # current_processing_file = source_file
|
205
|
+
|
206
|
+
# # # print_main_properties
|
207
|
+
# # # L.block code
|
208
|
+
# # begin
|
209
|
+
# # # Anything can potentially run, but generally one of the Klue.factory_methods
|
210
|
+
# # # should run such as Klue.structure or Klue.artifact
|
211
|
+
# # # When they run they can figure out for themselves what file called them by
|
212
|
+
# # # storing @current_processing_file into a document propert
|
213
|
+
|
214
|
+
# # # This code is not thread safe
|
215
|
+
# # # SET self as the current project so that we can register within in the document
|
216
|
+
|
217
|
+
# # eval(code)
|
218
|
+
|
219
|
+
# # # Clear self as the current project
|
220
|
+
# # # rubocop:enable Security/Eval
|
221
|
+
# # rescue KDsl::Error => e
|
222
|
+
# # puts "__FILE__: #{__FILE__}"
|
223
|
+
# # puts "__LINE__: #{__LINE__}"
|
224
|
+
# # L.error e.message
|
225
|
+
# # raise
|
226
|
+
# # rescue StandardError => e
|
227
|
+
# # L.kv '@current_processing_file', @current_processing_file
|
228
|
+
# # L.kv '@current_state', current_state
|
229
|
+
# # L.kv '@current_register_file', @current_register_file
|
230
|
+
|
231
|
+
# # L.exception(e)
|
232
|
+
# # end
|
233
|
+
|
234
|
+
# # @current_processing_file = nil
|
235
|
+
# # end
|
236
|
+
|
237
|
+
# def managed?
|
238
|
+
# !self.manager.nil?
|
239
|
+
# end
|
240
|
+
|
241
|
+
# def watch
|
242
|
+
# @listener = Listen.to(*watch_paths) do |modified, added, removed|
|
243
|
+
# update_resources(modified) unless modified.empty?
|
244
|
+
# add_resources(added) unless added.empty?
|
245
|
+
# remove_resources(removed) unless removed.empty?
|
246
|
+
# # puts "modified absolute path: #{modified}"
|
247
|
+
# # puts "added absolute path: #{added}"
|
248
|
+
# # puts "removed absolute path: #{removed}"
|
249
|
+
# end
|
250
|
+
# @listener.start # not blocking
|
251
|
+
|
252
|
+
# # L.subheading 'Listening'
|
253
|
+
# watch_paths.each do |wp|
|
254
|
+
# L.kv self.name, wp
|
255
|
+
# end
|
256
|
+
|
257
|
+
# @listener
|
258
|
+
# end
|
259
|
+
|
260
|
+
# def add_resources(files)
|
261
|
+
# files.each do |file|
|
262
|
+
# add_resource(file)
|
263
|
+
# end
|
264
|
+
# end
|
265
|
+
|
266
|
+
# def update_resources(files)
|
267
|
+
# files.each do |file|
|
268
|
+
# update_resource(file)
|
269
|
+
# end
|
270
|
+
# end
|
271
|
+
|
272
|
+
# def remove_resources(files)
|
273
|
+
# files.each do |file|
|
274
|
+
# remove_resource(file)
|
275
|
+
# end
|
276
|
+
# end
|
277
|
+
|
278
|
+
# def add_resource(file)
|
279
|
+
# if file.start_with?(template_path)
|
280
|
+
# # Resources are ignorable when in the application .template path
|
281
|
+
# # this is reserved for templates only
|
282
|
+
# puts "\rSkipping template #{file}\r"
|
283
|
+
# return
|
284
|
+
# end
|
285
|
+
|
286
|
+
# if File.directory?(file)
|
287
|
+
# puts "\rSkipping directory #{file}\r"
|
288
|
+
# return
|
289
|
+
# end
|
290
|
+
|
291
|
+
# puts "\rAdding #{file}\r"
|
292
|
+
|
293
|
+
# puts @watch_paths
|
294
|
+
|
295
|
+
# watch_path = File.dirname(file)
|
296
|
+
|
297
|
+
# resource = register_file_resource(file, watch_path: watch_path, path_expansion: false, ignore: nil)
|
298
|
+
# resource.load_content
|
299
|
+
# resource.register
|
300
|
+
# resource.load
|
301
|
+
|
302
|
+
# # Unlikely that I want to run a file that is added, if I ever do, thenb
|
303
|
+
# # will need to figure out what in the file will trigger this concept
|
304
|
+
# # resource.documents.each { |d| d.execute_block(run_actions: true) }
|
305
|
+
|
306
|
+
# 2.times { puts '' }
|
307
|
+
# debug(formats: [:watch_path_patterns, :resource, :resource_document])
|
308
|
+
# # manager.debug(format: :detail, project_formats: [:watch_path_patterns, :resource, :resource_document])
|
309
|
+
# end
|
310
|
+
|
311
|
+
# def update_resource(file)
|
312
|
+
# puts "\rUpdating #{file}\r"
|
313
|
+
|
314
|
+
# resource = get_resource(file: file)
|
315
|
+
# if resource
|
316
|
+
# KDsl::Resources::Resource.reset_instance(resource)
|
317
|
+
# resource.load_content
|
318
|
+
# resource.register
|
319
|
+
# resource.load
|
320
|
+
# resource.documents.each { |d| d.execute_block(run_actions: true) }
|
321
|
+
|
322
|
+
# # resource.debug
|
323
|
+
|
324
|
+
# 2.times { puts '' }
|
325
|
+
# debug(formats: [:watch_path_patterns, :resource, :resource_document])
|
326
|
+
# else
|
327
|
+
# # Resources are ignorable when in the application .template path
|
328
|
+
# # this is reserved for templates only
|
329
|
+
# puts 'resource not registered' unless file.start_with?(template_path)
|
330
|
+
# end
|
331
|
+
# end
|
332
|
+
|
333
|
+
# def remove_resource(file)
|
334
|
+
# puts "\rRemoving #{file}\r"
|
335
|
+
|
336
|
+
# @resources.select { |r| r.file == file }
|
337
|
+
# .each { |r| KDsl::Resources::Resource.reset_instance(r) }
|
338
|
+
|
339
|
+
# @resources.delete_if { |r| r.file == file }
|
340
|
+
|
341
|
+
# 2.times { puts '' }
|
342
|
+
# debug(formats: [:watch_path_patterns, :resource, :resource_document])
|
343
|
+
# end
|
344
|
+
|
345
|
+
# def get_resource(file: nil)
|
346
|
+
# if file
|
347
|
+
# @resources.find { |rd| rd.file == file }
|
348
|
+
# end
|
349
|
+
# end
|
350
|
+
|
351
|
+
# def debug(format: :resource, formats: [])
|
352
|
+
# if formats.present?
|
353
|
+
# formats.each { |format| self.debug(format: format) }
|
354
|
+
|
355
|
+
# return
|
356
|
+
# end
|
357
|
+
|
358
|
+
# if format == :resource
|
359
|
+
# puts ''
|
360
|
+
# L.subheading 'List of resources'
|
361
|
+
# tp resources.sort_by { |r| [r.source, r.file]},
|
362
|
+
# { object_id: {} },
|
363
|
+
# :status,
|
364
|
+
# { source: { } },
|
365
|
+
# { resource_type: { display_name: 'R-Type' } },
|
366
|
+
# { content: { width: 100, display_name: 'Content' } },
|
367
|
+
# { error: { width: 40, display_method: lambda { |r| r.error && r.error.message ? '** ERROR **' : '' } } },
|
368
|
+
# { base_resource_path: { width: 100, display_name: 'Resource Path' } },
|
369
|
+
# { relative_watch_path: { width: 100, display_name: 'Watch Path' } },
|
370
|
+
# # { :watch_path => { width: 100, display_name: 'Watch Path' } },
|
371
|
+
# # { :file => { width: 100, display_name: 'File' } },
|
372
|
+
# # { :filename => { width: 100, display_name: 'Filename' } },
|
373
|
+
# { filename: { width: 150, display_method: lambda { |r| "\u001b]8;;file://#{r.file}\u0007#{r.filename}\u001b]8;;\u0007" } } }
|
374
|
+
# elsif format == :watch_path_patterns
|
375
|
+
# puts ''
|
376
|
+
# L.subheading 'Watch these paths and patterns'
|
377
|
+
# watch_path_patterns.each { |path| L.info path }
|
378
|
+
|
379
|
+
# elsif format == :watch_path
|
380
|
+
# puts ''
|
381
|
+
# L.subheading 'Watch these paths'
|
382
|
+
# watch_paths.each { |path| L.info path }
|
383
|
+
|
384
|
+
# elsif format == :resource_document
|
385
|
+
# puts ''
|
386
|
+
# L.subheading 'List of documents'
|
387
|
+
# tp resource_documents.sort_by { |r| [r.type.to_s, r.namespace.to_s, r.key.to_s]},
|
388
|
+
# { object_id: {} },
|
389
|
+
# { resource_id: { display_method: lambda { |r| r.resource.object_id } } },
|
390
|
+
# { document_id: { display_method: lambda { |r| r.document.object_id } } },
|
391
|
+
# :status,
|
392
|
+
# { state: { display_method: lambda { |r| r.document.state } } },
|
393
|
+
# { namespace: { width: 20, display_name: 'Namespace' } },
|
394
|
+
# { key: { width: 30, display_name: 'Key' } },
|
395
|
+
# { type: { width: 20, display_name: 'Type' } },
|
396
|
+
# # :state,
|
397
|
+
# { source: { } },
|
398
|
+
# { resource_type: { display_name: 'R-Type' } },
|
399
|
+
# { data: { width: 40, display_name: 'Data' } },
|
400
|
+
# { error: { width: 40, display_method: lambda { |r| r.error && r.error.message ? '** ERROR **' : '' } } },
|
401
|
+
# { base_resource_path: { width: 100, display_name: 'Resource Path' } },
|
402
|
+
# { relative_watch_path: { width: 100, display_name: 'Watch Path' } },
|
403
|
+
# # { :watch_path => { width: 100, display_name: 'Watch Path' } },
|
404
|
+
# # { :file => { width: 100, display_name: 'File' } },
|
405
|
+
# # { :filename => { width: 100, display_name: 'Filename' } },
|
406
|
+
# { filename: { width: 150, display_method: lambda { |r| "\u001b]8;;file://#{r.file}\u0007#{r.filename}\u001b]8;;\u0007" } } }
|
407
|
+
# else
|
408
|
+
# # projects.each do |project|
|
409
|
+
# # L.subheading(project.name)
|
410
|
+
# # L.kv 'Base Path', project.config.base_path
|
411
|
+
# # L.kv 'Resource Path', project.config.base_resource_path
|
412
|
+
# # L.kv 'Data_Path', project.config.base_cache_path
|
413
|
+
# # L.kv 'Definition Path', project.config.base_definition_path
|
414
|
+
# # L.kv 'Template Path', project.config.base_template_path
|
415
|
+
# # L.kv 'AppTemplate Path', project.config.base_app_template_path
|
416
|
+
# # end
|
417
|
+
# end
|
418
|
+
|
419
|
+
# end
|
420
|
+
|
421
|
+
# # TODO: tests
|
422
|
+
# def template_path
|
423
|
+
# @template_path ||= File.expand_path(config.base_app_template_path)
|
424
|
+
# end
|
425
|
+
|
426
|
+
# # def self.create(base_resource_path, base_cache_path: nil, base_definition_path: nil, base_template_path: nil, &block)
|
427
|
+
# # # L.kv 'create1', '@@instance is Present' if @@instance.present?
|
428
|
+
# # # L.kv 'create1', '@@instance is Nil' if @@instance.nil?
|
429
|
+
|
430
|
+
# # if @@instance.nil?
|
431
|
+
# # # L.heading 'in create'
|
432
|
+
# # # L.kv 'dsl', base_resource_path;
|
433
|
+
# # # L.kv 'data', base_cache_path
|
434
|
+
|
435
|
+
# # @@instance = new(base_resource_path, base_cache_path, base_definition_path, base_template_path)
|
436
|
+
# # @@instance.instance_eval(&block) if block_given?
|
437
|
+
|
438
|
+
# # end
|
439
|
+
|
440
|
+
# # @@instance
|
441
|
+
# # end
|
442
|
+
|
443
|
+
# # private_class_method :new
|
444
|
+
|
445
|
+
# # def process_code(caller, code, source_file = nil)
|
446
|
+
# # # L.kv 'process_code.caller', caller
|
447
|
+
# # # L.kv 'process_code.source_file', source_file
|
448
|
+
|
449
|
+
# # @current_processing_file = source_file
|
450
|
+
|
451
|
+
# # if source_file.blank?
|
452
|
+
# # # L.info 'no source files'
|
453
|
+
# # end
|
454
|
+
|
455
|
+
# # if source_file.present? && !source_file.starts_with?(*@watch_paths)
|
456
|
+
# # L.kv 'source_file', source_file
|
457
|
+
# # raise Klue::Dsl::DslError, 'source file skipped, file is not on a registered path'
|
458
|
+
# # end
|
459
|
+
|
460
|
+
# # print_main_properties
|
461
|
+
# # # L.block code
|
462
|
+
|
463
|
+
# # begin
|
464
|
+
# # # Anything can potentially run, but generally one of the Klue.factory_methods
|
465
|
+
# # # should run such as Klue.structure or Klue.artifact
|
466
|
+
# # # When they run they can figure out for themselves what file called them by
|
467
|
+
# # # storing @current_processing_file into a document property
|
468
|
+
# # eval(code)
|
469
|
+
# # rescue Klue::Dsl::DslError => exception
|
470
|
+
# # # puts "__FILE__: #{__FILE__}"
|
471
|
+
# # # puts "__LINE__: #{__LINE__}"
|
472
|
+
# # L.error exception.message
|
473
|
+
# # raise
|
474
|
+
|
475
|
+
# # rescue => exception
|
476
|
+
# # L.kv '@current_processing_file', @current_processing_file
|
477
|
+
# # L.kv '@current_state', current_state
|
478
|
+
# # L.kv '@current_register_file', @current_register_file
|
479
|
+
|
480
|
+
# # L.exception(exception)
|
481
|
+
# # end
|
482
|
+
# # @current_processing_file = nil
|
483
|
+
# # end
|
484
|
+
|
485
|
+
# # def load_file(file, path_expansion: true)
|
486
|
+
# # file = expand_path(file) if path_expansion
|
487
|
+
|
488
|
+
# # # L.kv 'load_file.file', file
|
489
|
+
|
490
|
+
# # @current_state = :load_file
|
491
|
+
# # @current_register_file = file
|
492
|
+
|
493
|
+
# # content = File.read(file)
|
494
|
+
|
495
|
+
# # process_code(:load_file, content)
|
496
|
+
|
497
|
+
# # @current_register_file = nil
|
498
|
+
# # @current_state = :dynamic
|
499
|
+
# # end
|
500
|
+
|
501
|
+
# # def load_dynamic(content)
|
502
|
+
# # @current_state = :dynamic
|
503
|
+
# # @current_register_file = nil
|
504
|
+
|
505
|
+
# # process_code(:dynamic, content)
|
506
|
+
|
507
|
+
# # @current_register_file = nil
|
508
|
+
# # @current_state = :dynamic
|
509
|
+
# # end
|
510
|
+
|
511
|
+
# # def save(dsl)
|
512
|
+
# # unique_key = build_unique_key(dsl.k_key, dsl.namespace, dsl.type)
|
513
|
+
# # # L.kv 'action', 'save(dsl)'
|
514
|
+
# # # L.kv '@current_state', current_state
|
515
|
+
# # # L.kv '@unique_key', unique_key
|
516
|
+
|
517
|
+
# # case @current_state
|
518
|
+
# # when :register_file
|
519
|
+
# # save_register_file(unique_key, dsl.k_key, dsl.namespace, dsl.type)
|
520
|
+
# # when :load_file
|
521
|
+
# # save_load_file(unique_key, dsl.k_key, dsl.namespace, dsl.type, dsl)
|
522
|
+
# # when :dynamic
|
523
|
+
# # save_dynamic(unique_key, dsl.k_key, dsl.namespace, dsl.type, dsl)
|
524
|
+
# # else
|
525
|
+
# # raise 'unknown state'
|
526
|
+
# # end
|
527
|
+
|
528
|
+
# # dsl
|
529
|
+
# # end
|
530
|
+
|
531
|
+
# # def get_relative_folder(fullpath)
|
532
|
+
# # absolute_path = Pathname.new(fullpath)
|
533
|
+
# # project_root = Pathname.new(base_resource_path)
|
534
|
+
# # relative = absolute_path.relative_path_from(project_root)
|
535
|
+
# # rel_dir, file = relative.split
|
536
|
+
|
537
|
+
# # rel_dir.to_s
|
538
|
+
# # end
|
539
|
+
|
540
|
+
# # def debug(include_header = false)
|
541
|
+
# # if include_header
|
542
|
+
# # L.heading 'Register DSL'
|
543
|
+
# # print_main_properties
|
544
|
+
# # L.line
|
545
|
+
# # end
|
546
|
+
|
547
|
+
# # print_dsls
|
548
|
+
# # end
|
549
|
+
|
550
|
+
# # def print_main_properties
|
551
|
+
# # # L.kv 'base_resource_path' , base_resource_path
|
552
|
+
# # # L.kv 'base_cache_path' , base_cache_path
|
553
|
+
# # # L.kv 'base_definition_path' , base_definition_path
|
554
|
+
# # # L.kv 'current_state' , current_state
|
555
|
+
# # # L.kv 'current_register_file' , current_register_file
|
556
|
+
# # # L.kv 'current_processing_file' , current_processing_file
|
557
|
+
# # end
|
558
|
+
|
559
|
+
# # def print_dsls
|
560
|
+
# # # tp dsls.values, :k_key, :k_type, :state, :save_at, :last_at, :data, :last_data, :source, { :file => { :width => 150 } }, { :rel_folder => { :width => 80 } }
|
561
|
+
# # tp dsls.values, :namespace, :k_key, :k_type, :state, :save_at, :data, :source, { :file => { :width => 50 } }, { :rel_folder => { :width => 80 } }
|
562
|
+
# # end
|
563
|
+
|
564
|
+
# # private
|
565
|
+
|
566
|
+
# def guard_source_file(file)
|
567
|
+
# # if file.blank?
|
568
|
+
# # # L.info 'no source files'
|
569
|
+
# # end
|
570
|
+
|
571
|
+
# return unless !file.nil? && !file.starts_with?(*@watch_paths)
|
572
|
+
|
573
|
+
# L.kv 'file', file
|
574
|
+
# raise KDsl::Error, 'Source file skipped, file is not on a registered path'
|
575
|
+
# end
|
576
|
+
|
577
|
+
# # def default_dsl_data(**data)
|
578
|
+
# # {
|
579
|
+
# # namespace: nil,
|
580
|
+
# # k_key: nil,
|
581
|
+
# # k_type: nil,
|
582
|
+
# # state: nil,
|
583
|
+
# # save_at: nil,
|
584
|
+
# # last_at: nil,
|
585
|
+
# # data: nil,
|
586
|
+
# # last_data: nil,
|
587
|
+
# # source: nil,
|
588
|
+
# # file: nil,
|
589
|
+
# # rel_folder: nil
|
590
|
+
# # }.merge(data)
|
591
|
+
# # end
|
592
|
+
|
593
|
+
# # def save_register_file(unique_key, key, type, namespace)
|
594
|
+
# # k = @dsls[unique_key]
|
595
|
+
|
596
|
+
# # if k.present? && k[:file].present? && k[:file] != @current_register_file
|
597
|
+
# # print_dsls
|
598
|
+
|
599
|
+
# # L.line
|
600
|
+
# # L.kv 'Error', 'Duplicate DSL key found'
|
601
|
+
# # L.kv 'Unique Key', unique_key
|
602
|
+
# # L.kv 'Namespace', namespace
|
603
|
+
# # L.kv 'Key', key
|
604
|
+
# # L.kv 'Type', type
|
605
|
+
# # L.kv 'File', @current_register_file
|
606
|
+
# # L.line
|
607
|
+
# # print
|
608
|
+
# # L.line
|
609
|
+
|
610
|
+
# # raise Klue::Dsl::DslError, "Duplicate DSL key found #{unique_key} in different files"
|
611
|
+
# # end
|
612
|
+
|
613
|
+
# # if k.present?
|
614
|
+
# # L.line
|
615
|
+
# # L.kv 'Warning', 'DSL already registered'
|
616
|
+
# # L.kv 'Unique Key', unique_key
|
617
|
+
# # L.kv 'Namespace', namespace
|
618
|
+
# # L.kv 'Key', key
|
619
|
+
# # L.kv 'Type', type
|
620
|
+
# # L.kv 'Previous File Name', k[:file]
|
621
|
+
# # L.kv 'Register File Name', @current_register_file
|
622
|
+
# # L.line
|
623
|
+
# # print
|
624
|
+
# # L.line
|
625
|
+
# # else
|
626
|
+
# # @dsls[unique_key] = default_dsl_data(
|
627
|
+
# # namespace: namespace,
|
628
|
+
# # k_key: key,
|
629
|
+
# # k_type: type,
|
630
|
+
# # state: :registered,
|
631
|
+
# # source: :file,
|
632
|
+
# # file: @current_register_file,
|
633
|
+
# # rel_folder: get_relative_folder(@current_register_file)
|
634
|
+
# # )
|
635
|
+
# # end
|
636
|
+
# # end
|
637
|
+
|
638
|
+
# # def save_load_file(unique_key, key, type, namespace, dsl)
|
639
|
+
# # k = @dsls[unique_key]
|
640
|
+
|
641
|
+
# # if k.nil?
|
642
|
+
# # # New Record
|
643
|
+
# # @dsls[unique_key] = default_dsl_data(
|
644
|
+
# # namespace: namespace,
|
645
|
+
# # k_key: key,
|
646
|
+
# # k_type: type,
|
647
|
+
# # state: :loaded,
|
648
|
+
# # save_at: Time.now.utc,
|
649
|
+
# # data: dsl.get_data(),
|
650
|
+
# # source: :file,
|
651
|
+
# # file: @current_register_file,
|
652
|
+
# # rel_folder: get_relative_folder(@current_register_file)
|
653
|
+
# # )
|
654
|
+
# # else
|
655
|
+
# # # Update Record
|
656
|
+
# # k[:state] = :loaded
|
657
|
+
# # k[:last_at] = k[:save_at]
|
658
|
+
# # k[:save_at] = Time.now.utc
|
659
|
+
# # k[:last_data] = k[:data]
|
660
|
+
# # k[:data] = dsl.get_data()
|
661
|
+
# # end
|
662
|
+
# # end
|
663
|
+
|
664
|
+
# # def save_dynamic(unique_key, key, type, namespace, dsl)
|
665
|
+
# # k = @dsls[unique_key]
|
666
|
+
|
667
|
+
# # if k.nil?
|
668
|
+
# # # New Record
|
669
|
+
# # @dsls[unique_key] = default_dsl_data(
|
670
|
+
# # namespace: namespace,
|
671
|
+
# # k_key: key,
|
672
|
+
# # k_type: type,
|
673
|
+
# # state: :loaded,
|
674
|
+
# # save_at: Time.now.utc,
|
675
|
+
# # data: dsl.get_data(),
|
676
|
+
# # source: :dynamic
|
677
|
+
# # )
|
678
|
+
# # else
|
679
|
+
# # # Update Record
|
680
|
+
# # k[:state] = :loaded
|
681
|
+
# # k[:last_at] = k[:save_at]
|
682
|
+
# # k[:save_at] = Time.now.utc
|
683
|
+
# # k[:last_data] = k[:data]
|
684
|
+
# # k[:data] = dsl.get_data()
|
685
|
+
# # end
|
686
|
+
# # end
|
687
|
+
|
688
|
+
# # This makes more sense at an APP level, instead of a project level
|
689
|
+
# # def self.reset
|
690
|
+
# # @@instance = nil
|
691
|
+
# # end
|
692
|
+
|
693
|
+
# # def self.get_instance
|
694
|
+
# # # Note: if you have already created an instance using custom code then it will re-used
|
695
|
+
# # @@instance
|
696
|
+
# # end
|
697
|
+
# end
|
698
|
+
# end
|