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.



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

def initialize(root, options)
	@root = Pathname(root)
	@options = options
	
	@log_output = @options.fetch(:log, $stdout)
	@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)
			
			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



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

def configuration
	@options[:configuration]
end

#contextObject



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

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
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



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

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

#loggerObject



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

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)


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

def quiet?
	@logging == :quiet
end

#reload!Object

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



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

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)


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

def verbose?
	@logging == :verbose
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