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

Constant Summary collapse

MAXIMUM_FETCH_DEPTH =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Controller

Returns a new instance of Controller.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/teapot/controller.rb', line 35

def initialize(root, options)
  @root = Pathname(root)
  @options = options
  
  log_output = @options.fetch(:log, $stdout)
  
  @logger = Logger.new(log_output)
  
  @logger.formatter = Build::CompactFormatter.new(verbose: options[:verbose])
  
  if options[:verbose]
    @logger.level = Logger::DEBUG
  else
    @logger.level = Logger::INFO
  end
  
  @options[:maximum_fetch_depth] ||= MAXIMUM_FETCH_DEPTH
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



54
55
56
# File 'lib/teapot/controller.rb', line 54

def logger
  @logger
end

Instance Method Details

#build(dependency_names) ⇒ Object



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
# File 'lib/teapot/controller/build.rb', line 31

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) do |controller|
    ordered.each do |resolution|
      target = resolution.provider
      
      environment = target.environment(context.configuration)
      
      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|
      if $TEAPOT_DEBUG_GRAPH
        controller.nodes.each do |key, node|
          puts "#{node.status} #{node.inspect}" unless node.clean?
        end
      end
      
      # 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

#contextObject



60
61
62
63
64
# File 'lib/teapot/controller.rb', line 60

def context
  @context ||= Context.new(@root,
    :configuration => @options[: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
# 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 configuration generated at #{Time.now.to_s}", ''
  
    output.puts "required_version #{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 all packages:
  fetch

  # Generate the default project if it is possible to do so:
  generate_project(project_name)
end

#fetchObject



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
# File 'lib/teapot/controller/fetch.rb', line 26

def fetch
  resolved = Set.new
  configuration = context.configuration
  unresolved = context.unresolved(configuration.packages)
  tries = 0
  
  while tries < @options[:maximum_fetch_depth]
    configuration.packages.each do |package|
      next if resolved.include? package
    
      fetch_package(context, configuration, package)
    
      # 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
  
    tries += 1
  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
81
82
83
84
85
86
87
88
89
90
# 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 |name|
            log "\t\t- depends on #{name.inspect}".color(:red)
          end
  
          definition.provisions.each do |(name, provision)|
            if Dependency::Alias === provision
              log "\t\t- provides #{name.inspect} => #{provision.dependencies.inspect}".color(:green)
            else
              log "\t\t- provides #{name.inspect}".color(:green)
            end
          end
        when Configuration
          definition.materialize
        
          definition.packages.each do |package|
            if package.local?
              log "\t\t- links #{package.name} from #{package.options[:local]}".color(:green)
            elsif package.external?
              log "\t\t- clones #{package.name} from #{package.external_url(context.root)}".color(:green)
            else
              log "\t\t- references #{package.name} from #{package.path}".color(:green)
            end
          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



56
57
58
# File 'lib/teapot/controller.rb', line 56

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

#visualize(dependency_names = []) ⇒ 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
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
106
# File 'lib/teapot/controller/visualize.rb', line 28

def visualize(dependency_names = [])
  configuration = context.configuration
  
  chain = context.dependency_chain(dependency_names, context.configuration)
  
  g = Graphviz::Graph.new
  g.attributes[:ratio] = :auto
  
  base_attributes = {
    :shape => 'box',
  }
  
  provision_attributes = base_attributes.dup
  
  alias_attributes = {
    :shape => 'box',
    :color => 'grey',
  }
  
  chain.ordered.each do |resolution|
    provider = resolution.provider
    name = resolution.name
    
    # Provider is the target that provides the dependency referred to by name.
    node = g.add_node(name.to_s, base_attributes.dup)
    
    if chain.dependencies.include?(name)
      node.attributes[:color] = 'blue'
      node.attributes[:penwidth] = 2.0
    elsif chain.selection.include?(provider.name)
      node.attributes[:color] = 'brown'
    end
    
    # A provision has dependencies...
    provider.dependencies.each do |dependency|
      dependency_node = g.nodes[dependency.to_s]
      
      node.connect(dependency_node) if dependency_node
    end
    
    # A provision provides other provisions...
    provider.provisions.each do |(provision_name, provision)|
      next if name == provision_name
      
      provides_node = g.nodes[provision_name.to_s] || g.add_node(provision_name.to_s, provision_attributes)
      
      if Dependency::Alias === provision
        provides_node.attributes = alias_attributes
      end
      
      unless provides_node.connected?(node)
        edge = provides_node.connect(node)
      end
    end
  end
  
  # Put all dependencies at the same level so as to not make the graph too confusing.
  done = Set.new
  chain.ordered.each do |resolution|
    provider = resolution.provider
    name = resolution.name
    
    p = g.graphs[provider.name] || g.add_subgraph(provider.name, :rank => :same)
    
    provider.dependencies.each do |dependency|
      next if done.include? dependency
      
      done << dependency
      
      dependency_node = g.nodes[dependency.to_s]
      
      p.add_node(dependency_node.name)
    end
  end
  
  Graphviz::output(g, :path => "graph.pdf")
  
  puts g.to_dot
end