Class: Teapot::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/teapot/controller.rb,
lib/teapot/controller/run.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

Constant Summary collapse

MAXIMUM_FETCH_DEPTH =
20

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Controller

Returns a new instance of Controller.



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

def initialize(root, options)
  @root = Pathname(root)
  @options = options
  
  @log_output = @options.fetch(:log, $stdout)
  
  @options[:maximum_fetch_depth] ||= MAXIMUM_FETCH_DEPTH
end

Instance Method Details

#build(dependency_names) ⇒ Object



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

def build(dependency_names)
  chain = context.dependency_chain(dependency_names, context.configuration)

  ordered = chain.ordered

  if @options[:only]
    ordered = context.direct_targets(ordered)
  end

  ordered.each do |(target, dependency)|
    if target.respond_to?(:build!) and !@options[:dry]
      log "Building #{target.name} for dependency #{dependency}...".color(:cyan)
      
      target.build!(context.configuration)
    end
  end
  
  log "Completed build successfully.".color(:green)
  
  return chain, ordered
end

#cleanObject



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

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

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

#contextObject



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

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 = 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.import! #{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 = 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

#listObject



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

def list
  # Should this somehow consider context.root_package?
  context.configuration.packages.each do |package|
    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 = 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.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



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

def log(*args)
  @log_output.puts *args
end

#run(dependency_names = []) ⇒ Object



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

def run(dependency_names = [])
  configuration = context.configuration
  
  dependency_names += configuration[:run] || []
  
  log "Running dependencies: #{dependency_names}"
  
  chain, ordered = build(dependency_names)
  
  ordered.each do |(target, dependency)|
    if target.respond_to?(:run!) and !@options[:dry]
      log "Running #{target.name} for dependency #{dependency}...".color(:cyan)
      
      target.run!(configuration)
    end
  end
end