Class: Teapot::Command::Create

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/teapot/command/create.rb

Instance Method Summary collapse

Instance Method Details

#generate_project(root, project_name, source, packages) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/teapot/command/create.rb', line 85

def generate_project(root, project_name, source, packages)
  name = ::Build::Name.new(project_name)
  
  # Otherwise the initial commit will try to include teapot/
  File.open(root + ".gitignore", "w") do |output|
    output.puts "teapot/"
  end
  
  # A very basic teapot file to pull in the initial dependencies.
  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 "define_project #{name.target.dump} do |project|"
    output.puts "\tproject.title = #{name.text.dump}"
    output.puts "end", ''
  
    output.puts "\# Build Targets", ''
  
    output.puts "\# Configurations", ''
    
    output.puts "define_configuration 'development' do |configuration|"
    output.puts "\tconfiguration[:source] = #{source.dump}"
    output.puts "\tconfiguration.import #{name.target.dump}"
    packages.each do |name|
      output.puts "\tconfiguration.require #{name.dump}"
    end
    output.puts "end", ''
    
    output.puts "define_configuration #{name.target.dump} do |configuration|"
    output.puts "\tconfiguration.public!"
    output.puts "end"
  end
end

#invoke(parent) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/teapot/command/create.rb', line 36

def invoke(parent)
  raise ArgumentError, "project_name is required" unless @project_name
  raise ArgumentError, "source is required" unless @source
  
  logger = parent.logger
  
  nested = parent['--root', parent.options[:root] || project_name.gsub(/\s+/, '-').downcase]
  root = nested.root
  
  if root.exist?
    raise ArgumentError.new("#{root} already exists!")
  end
  
  # Create and set the project root:
  root.create
  
  repository = Rugged::Repository.init_at(root.to_s)
  
  logger.info "Creating project named #{project_name} at path #{root}...".color(:cyan)
  generate_project(root, @project_name, @source, @packages)
  
  # Fetch the initial packages:
  Fetch[].invoke(nested)
  
  context = nested.context
  selection = context.select
  target_names =  selection.configuration.targets[:create]
  
  if target_names.any?
    # Generate the initial project files:
    Build[*target_names].invoke(nested)
    
    # Fetch any additional packages:
    Fetch[].invoke(nested)
  end
  
  # Stage all files:
  index = repository.index
  index.add_all
  
  # Commit the initial project files:
  Rugged::Commit.create(repository,
    tree: index.write_tree(repository),
    message: "Initial project files.",
    parents: repository.empty? ? [] : [repository.head.target].compact,
    update_ref: 'HEAD'
  )
end