Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement autoloading #289

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ foreman-installer. We could add the following lines to the generated
installer script.

```ruby
require 'kafo/hooking'
require 'kafo'

# first hook that creates new app option --reset-foreman-db
KafoConfigure.hooking.register_boot(:add_reset_option) do
Expand Down
4 changes: 2 additions & 2 deletions bin/kafo-configure
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'kafo'))
require 'kafo_configure'
$LOAD_PATH.unshift File.expand_path(File.join(__dir__, '..', 'lib'))
require 'kafo'
result = Kafo::KafoConfigure.run
exit result.nil? ? 0 : result.exit_code
18 changes: 8 additions & 10 deletions bin/kafo-export-params
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ require 'rubygems'
require 'ostruct'
require 'clamp'
require 'logging'
require 'kafo/configuration'
require 'kafo/exceptions'
require 'kafo/parser_cache_writer'
require 'kafo/string_helper'
require 'kafo'
require 'logger'
require 'yaml'

KafoConfigure = OpenStruct.new
def KafoConfigure.exit(code)
Kernel.exit(1)
end

module Kafo
class KafoExportParams < Clamp::Command
class KafoConfigure
def exit(code)
Kernel.exit(1)
end
end

class KafoExportParams < ::Clamp::Command
TYPES = %w(md html asciidoc parsercache)

option ['-c', '--config'], 'FILE', 'Config file for which should we generate params',
Expand Down
2 changes: 1 addition & 1 deletion bin/kafofy
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib
require 'fileutils'
require 'optparse'
require 'yaml'
require 'kafo/configuration'
require 'kafo'


# Option Parsing
Expand Down
49 changes: 48 additions & 1 deletion lib/kafo.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
# encoding: UTF-8

require 'kafo/version'
require 'kafo/kafo_configure'

# First of all we have to store ENV variable, requiring facter can override them
module Kafo
module ENV
LANG = ::ENV['LANG']
end

autoload :BaseContext, 'kafo/base_context'
autoload :ColorScheme, 'kafo/color_scheme'
autoload :Condition, 'kafo/condition'
autoload :ConditionError, 'kafo/exceptions'
autoload :Configuration, 'kafo/configuration'
autoload :ConfigurationException, 'kafo/exceptions'
autoload :DataType, 'kafo/data_type'
autoload :DataTypeParser, 'kafo/data_type_parser'
autoload :DataTypes, 'kafo/data_type' # This is inconsistent
autoload :ExecutionEnvironment, 'kafo/execution_environment'
autoload :ExitHandler, 'kafo/exit_handler'
autoload :FactWriter, 'kafo/fact_writer'
autoload :HelpBuilders, 'kafo/help_builders'
autoload :HieraConfigurer, 'kafo/hiera_configurer'
autoload :HookContext, 'kafo/hook_context'
autoload :Hooking, 'kafo/hooking'
autoload :KafoConfigure, 'kafo/kafo_configure'
autoload :Logger, 'kafo/logger'
autoload :Logging, 'kafo/logging'
autoload :MigrationContext, 'kafo/migration_context'
autoload :Migrations, 'kafo/migrations'
autoload :Param, 'kafo/param'
autoload :ParamBuilder, 'kafo/param_builder'
autoload :ParamGroup, 'kafo/param_group'
autoload :ParserCacheReader, 'kafo/parser_cache_reader'
autoload :ParserCacheWriter, 'kafo/parser_cache_writer'
autoload :ParserError, 'kafo/exceptions'
autoload :ProgressBar, 'kafo/progress_bar'
autoload :ProgressBars, 'kafo/progress_bars'
autoload :PuppetCommand, 'kafo/puppet_command'
autoload :PuppetConfigurer, 'kafo/puppet_configurer'
autoload :PuppetLogParser, 'kafo/puppet_log_parser'
autoload :PuppetModule, 'kafo/puppet_module'
autoload :ScenarioManager, 'kafo/scenario_manager'
autoload :Store, 'kafo/store'
autoload :StringHelper, 'kafo/string_helper'
autoload :SystemChecker, 'kafo/system_checker'
autoload :TypeError, 'kafo/exceptions'
autoload :Wizard, 'kafo/wizard'
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems overly verbose compared to something more automatic -- https://github.com/theforeman/tool_belt/blob/master/lib/tool_belt.rb#L2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is that tool_belt is loading it all up front where autoload is lazy loading. You can also copy the pattern used by autoloaded to automatically convert file names to class/module names.

4 changes: 0 additions & 4 deletions lib/kafo/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# encoding: UTF-8
require 'yaml'
require 'tmpdir'
require 'kafo/puppet_module'
require 'kafo/color_scheme'
require 'kafo/data_type_parser'
require 'kafo/execution_environment'

module Kafo
class Configuration
Expand Down
2 changes: 0 additions & 2 deletions lib/kafo/data_type_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'kafo/data_type'

module Kafo
class DataTypeParser
TYPE_DEFINITION = /^type\s+([^\s=]+)\s*=\s*(.+?)(\s+#.*)?\s*$/
Expand Down
4 changes: 0 additions & 4 deletions lib/kafo/execution_environment.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
require 'tmpdir'

require 'kafo/fact_writer'
require 'kafo/hiera_configurer'
require 'kafo/puppet_configurer'

module Kafo
class ExecutionEnvironment
def initialize(config, logger = KafoConfigure.logger)
Expand Down
3 changes: 0 additions & 3 deletions lib/kafo/help_builder.rb

This file was deleted.

7 changes: 7 additions & 0 deletions lib/kafo/help_builders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Kafo
module HelpBuilders
autoload :Advanced, 'kafo/help_builders/advanced'
autoload :Base, 'kafo/help_builders/base'
autoload :Basic, 'kafo/help_builders/basic'
end
end
3 changes: 2 additions & 1 deletion lib/kafo/help_builders/base.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# encoding: UTF-8
require 'clamp'

module Kafo
module HelpBuilders
DEFAULT_GROUP_NAME = 'Basic'
DEFAULT_MODULE_NAME = 'Generic'
IGNORE_IN_GROUP_NAME = /\s*parameters:?/

class Base < Clamp::Help::Builder
class Base < ::Clamp::Help::Builder
include StringHelper

def initialize(params)
Expand Down
3 changes: 0 additions & 3 deletions lib/kafo/hook_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require 'kafo/data_type'
require 'kafo/base_context'

module Kafo
class HookContext < BaseContext
attr_reader :kafo, :logger
Expand Down
2 changes: 0 additions & 2 deletions lib/kafo/hooking.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'kafo/hook_context'

module Kafo
class Hooking
# pre_migrations - just after kafo reads its configuration - useful for config file updates. Only in this stage it is posible to request config reload (`Kafo.request_config_reload`) to get in our changes
Expand Down
30 changes: 2 additions & 28 deletions lib/kafo/kafo_configure.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
# encoding: UTF-8

# First of all we have to store ENV variable, requiring facter can override them
module Kafo
module ENV
LANG = ::ENV['LANG']
end
end

require 'pty'
require 'clamp'
require 'kafo/color_scheme'
require 'kafo_parsers/exceptions'
require 'kafo/exceptions'
require 'kafo/migrations'
require 'kafo/store'
require 'kafo/configuration'
require 'kafo/logger'
require 'kafo/string_helper'
require 'kafo/help_builder'
require 'kafo/wizard'
require 'kafo/system_checker'
require 'kafo/puppet_command'
require 'kafo/puppet_log_parser'
require 'kafo/progress_bar'
require 'kafo/hooking'
require 'kafo/exit_handler'
require 'kafo/scenario_manager'
require 'kafo/execution_environment'
require 'kafo/logging'

module Kafo
class KafoConfigure < Clamp::Command
class KafoConfigure < ::Clamp::Command
include StringHelper

class << self
Expand Down Expand Up @@ -446,6 +419,7 @@ def run_installation
log_parser = PuppetLogParser.new
logger = Logger.new('configure')

require 'pty'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike these mid-code requires. They can hide errors, and lead to missing libraries at runtime.

PTY.spawn(*PuppetCommand.format_command(command)) do |stdin, stdout, pid|
begin
stdin.each do |line|
Expand Down
1 change: 0 additions & 1 deletion lib/kafo/logger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# encoding: UTF-8
require 'kafo/logging'

module Kafo
class Logger
Expand Down
2 changes: 0 additions & 2 deletions lib/kafo/migration_context.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'kafo/base_context'

module Kafo
class MigrationContext < BaseContext

Expand Down
1 change: 0 additions & 1 deletion lib/kafo/migrations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'yaml'
require 'kafo/migration_context'

module Kafo
class Migrations
Expand Down
2 changes: 0 additions & 2 deletions lib/kafo/param.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# encoding: UTF-8
require 'kafo/condition'
require 'kafo/data_type'

module Kafo
class Param
Expand Down
1 change: 0 additions & 1 deletion lib/kafo/param_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# encoding: UTF-8
require 'kafo/param_group'

module Kafo
class ParamBuilder
Expand Down
2 changes: 0 additions & 2 deletions lib/kafo/parser_cache_reader.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'kafo/version'

module Kafo
class ParserCacheReader
attr_accessor :force
Expand Down
2 changes: 0 additions & 2 deletions lib/kafo/parser_cache_writer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'kafo/version'

module Kafo
class ParserCacheWriter
def self.write(modules)
Expand Down
3 changes: 0 additions & 3 deletions lib/kafo/progress_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,3 @@ def find_known_resource(resource)

end
end

require 'kafo/progress_bars/colored'
require 'kafo/progress_bars/black_white'
6 changes: 6 additions & 0 deletions lib/kafo/progress_bars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Kafo
module ProgressBars
autoload :Colored, 'kafo/progress_bars/colored'
autoload :BlackWhite, 'kafo/progress_bars/black_white'
end
end
3 changes: 0 additions & 3 deletions lib/kafo/puppet_module.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# encoding: UTF-8
require 'kafo/param'
require 'kafo/param_builder'
require 'kafo/parser_cache_reader'
require 'kafo_parsers/parsers'

module Kafo
Expand Down
1 change: 0 additions & 1 deletion test/kafo/fact_writer_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'test_helper'
require 'kafo/fact_writer'

module Kafo
describe FactWriter do
Expand Down
1 change: 0 additions & 1 deletion test/kafo/hiera_configurer_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'test_helper'
require 'kafo/hiera_configurer'

module Kafo
describe HieraConfigurer do
Expand Down
1 change: 0 additions & 1 deletion test/kafo/parser_cache_reader_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'test_helper'
require 'kafo/parser_cache_writer'

module Kafo
describe ParserCacheReader do
Expand Down
1 change: 0 additions & 1 deletion test/kafo/parser_cache_writer_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'test_helper'
require 'kafo/parser_cache_writer'
require 'tempfile'

module Kafo
Expand Down
1 change: 0 additions & 1 deletion test/kafo/puppet_configurer_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'test_helper'
require 'tempfile'
require 'kafo/hiera_configurer'

module Kafo
describe PuppetConfigurer do
Expand Down
2 changes: 0 additions & 2 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'kafo_parsers/parsers'

class TestParser
attr_reader :manifest_file

Expand Down