Class: Teapot::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/teapot/controller.rb,
lib/teapot/controller/list.rb,
lib/teapot/controller/build.rb,
lib/teapot/controller/clean.rb,
lib/teapot/controller/fetch.rb,
lib/teapot/controller/create.rb,
lib/teapot/controller/generate.rb,
lib/teapot/controller/visualize.rb

Defined Under Namespace

Classes: BuildFailedError

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Controller

Returns a new instance of Controller.



34
35
36
37
38
39
40
# File 'lib/teapot/controller.rb', line 34

def initialize(root, options)
  @root = Pathname(root)
  @options = options
  
  @log_output = @options.fetch(:log, $stderr)
  @logging = @options[:logging] 
end

Instance Method Details

#build(dependency_names) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/teapot/controller/build.rb', line 61

def build(dependency_names)
  chain = context.dependency_chain(dependency_names, context.configuration)
  
  ordered = chain.ordered
  
  if @options[:only]
    ordered = context.direct_targets(ordered)
  end
  
  controller = Build::Controller.new(logger: self.logger, limit: @options[:limit]) do |controller|
    ordered.each do |resolution|
      target = resolution.provider
      
      environment = target.environment(context.configuration, chain)
      
      if target.build
        controller.add_target(target, environment.flatten)
      end
    end
  end
  
  walker = nil
  
  # We need to catch interrupt here, and exit with the correct exit code:
  begin
    controller.run do |walker|
      # show_dependencies(walker)
      
      # Only run once is asked:
      unless @options[:continuous]
        if walker.failed?
          raise BuildFailedError.new("Failed to build all nodes successfully!")
        end
      
        break
      end
    end
  rescue Interrupt
    if walker && walker.failed?
      raise BuildFailedError.new("Failed to build all nodes successfully!")
    end
  end
  
  return chain, ordered
end

#cleanObject



25
26
27
28
29
30
31
32
33
# File 'lib/teapot/controller/clean.rb', line 25

def clean
  configuration = context.configuration
  
  log "Removing #{configuration.platforms_path}...".color(:cyan)
  FileUtils.rm_rf configuration.platforms_path

  log "Removing #{configuration.packages_path}...".color(:cyan)
  FileUtils.rm_rf configuration.packages_path
end

#configurationObject



68
69
70
# File 'lib/teapot/controller.rb', line 68

def configuration
  @options[:configuration]
end

#contextObject



72
73
74
# File 'lib/teapot/controller.rb', line 72

def context
  @context ||= Context.new(@root, configuration: configuration)
end

#create(project_name, source, packages) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/teapot/controller/create.rb', line 28

def create(project_name, source, packages)
  name = Build::Name.new(project_name)
  
  log "Creating project named #{project_name} at path #{@root}...".color(:cyan)
  
  File.open(@root + TEAPOT_FILE, "w") do |output|
    output.puts "\# Teapot v#{VERSION} configuration generated at #{Time.now.to_s}", ''
  
    output.puts "required_version #{LOADER_VERSION.dump}", ''
  
    output.puts "\# Build Targets", ''
  
    output.puts "\# Configurations", ''
  
    output.puts "define_configuration #{name.target.dump} do |configuration|"
  
    output.puts "\tconfiguration[:source] = #{source.dump}", ''
  
    packages.each do |name|
      output.puts "\tconfiguration.require #{name.dump}"
    end
  
    output.puts "end", ''
  end

  # Fetch the initial packages:
  self.fetch

  # Generate the default project if it is possible to do so:
  generate_project(project_name)
  
  self.reload!
  
  # Fetch any additional packages:
  self.fetch
end

#fetch(**options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/teapot/controller/fetch.rb', line 26

def fetch(**options)
  resolved = Set.new
  configuration = context.configuration
  unresolved = context.unresolved(configuration.packages)
  
  while true
    configuration.packages.each do |package|
      next if resolved.include? package
    
      fetch_package(context, configuration, package, **options)
    
      # We are done with this package, don't try to process it again:
      resolved << package
    end
  
    # Resolve any/all imports:
    configuration.materialize
  
    previously_unresolved = unresolved
    unresolved = context.unresolved(configuration.packages)
  
    # No additional packages were resolved, we have reached a fixed point:
    if previously_unresolved == unresolved || unresolved.count == 0
      break
    end
  end

  if unresolved.count > 0
    log "Could not fetch all packages!".color(:red)
    unresolved.each do |package|
      log "\t#{package}".color(:red)
    end
  else
    log "Completed fetch successfully.".color(:green)
  end
end

#generate(name, arguments, force = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/teapot/controller/generate.rb', line 25

def generate(name, arguments, force = false)
  context.configuration.load_all

  unless force
    # Check dirty status of local repository:
    if Repository.new(@root).status.size != 0
      abort "You have unstaged changes/unadded files. Please stash/commit them before running the generator.".color(:red)
    end
  end

  generator = context.generators[name]

  unless generator
    abort "Could not find generator with name #{name.inspect}".color(:red)
  end

  log "Generating #{name.inspect} with arguments #{arguments.inspect}".color(:cyan)
  generator.generate!(*arguments)
end

#list(only = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/teapot/controller/list.rb', line 25

def list(only = nil)
  # Should this somehow consider context.root_package?
  context.configuration.packages.each do |package|
    # The root package is the local package for this context:
    next unless only == nil or only.include?(package.name)
    
    log "Package #{package.name} (from #{package.path}):".bright
  
    begin
      definitions = context.load(package)
    
      definitions.each do |definition|
        log "\t#{definition}"
    
        definition.description.each_line do |line|
          log "\t\t#{line.chomp}".color(:cyan)
        end if definition.description
    
        case definition
        when Project
          log "\t\t- Summary: #{definition.summary}" if definition.summary
          log "\t\t- License: #{definition.license}" if definition.license
          log "\t\t- Website: #{definition.website}" if definition.website
          log "\t\t- Version: #{definition.version}" if definition.version
          
          definition.authors.each do |author|
            contact_text = [author.email, author.website].compact.collect{|text|" <#{text}>"}.join
            log "\t\t- Author: #{author.name}" + contact_text
          end
        when Target
          definition.dependencies.each do |dependency|
            log "\t\t- #{dependency}".color(:red)
          end
  
          definition.provisions.each do |name, provision|
            log "\t\t- #{provision}".color(:green)
          end
        when Configuration
          definition.materialize
        
          definition.packages.each do |package|
            log "\t\t- #{package}".color(:green)
          end
        
          definition.imports.select(&:explicit).each do |import|
            log "\t\t- unmaterialised import #{import.name}".color(:red)
          end
        end
      end
    rescue NonexistantTeapotError => error
      log "\t#{error.message}".color(:red)
    rescue IncompatibleTeapotError => error
      log "\t#{error.message}".color(:red)
    end
  end
end

#log(*args) ⇒ Object



64
65
66
# File 'lib/teapot/controller.rb', line 64

def log(*args)
  logger.info(*args)
end

#loggerObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/teapot/controller.rb', line 50

def logger
  @logger ||= Logger.new(@log_output).tap do |logger|
    logger.formatter = Build::CompactFormatter.new(verbose: verbose?)
    
    if verbose?
      logger.level = Logger::DEBUG
    elsif quiet?
      logger.level = Logger::WARN
    else
      logger.level = Logger::INFO
    end
  end
end

#quiet?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/teapot/controller.rb', line 46

def quiet?
  @logging == :quiet
end

#reload!Object

Reload the current context, e.g. if it’s been modified by a generator.



77
78
79
# File 'lib/teapot/controller.rb', line 77

def reload!
  @context = nil
end

#show_dependencies(walker) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/teapot/controller/build.rb', line 29

def show_dependencies(walker)
  outputs = {}
  
  walker.tasks.each do |node, task|
    # puts "Task #{task} (#{node}) outputs:"
    
    task.outputs.each do |path|
      path = path.to_s
      
      # puts "\t#{path}"
      
      outputs[path] = task
    end
  end
  
  walker.tasks.each do |node, task|
    dependencies = {}
    task.inputs.each do |path|
      path = path.to_s
      
      if generating_task = outputs[path]
        dependencies[path] = generating_task
      end
    end
    
    puts "Task #{task.inspect} has #{dependencies.count} dependencies."
    dependencies.each do |path, task|
      puts "\t#{task.inspect}: #{path}"
    end
  end
end

#verbose?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/teapot/controller.rb', line 42

def verbose?
  @logging == :verbose
end

#visualize(dependency_names = [], output_path: nil, dependency_name: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/teapot/controller/visualize.rb', line 27

def visualize(dependency_names = [], output_path: nil, dependency_name: nil)
  configuration = context.configuration
  
  chain = context.dependency_chain(dependency_names, context.configuration)
  
  if dependency_name
    provider = context.dependencies[dependency_name]
    
    # TODO The visualisation generated isn't quite right. It's introspecting too much from the packages and not reflecting #ordered and #provisions.
    chain = chain.partial(provider)
  end
  
  visualization = Build::Dependency::Visualization.new
  
  graph = visualization.generate(chain)
  
  if output
    Graphviz::output(graph, :path => output)
  end
  
  return graph
end