Class: Gasciiart::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/gasciiart/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target, options = {}) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
10
11
12
13
14
# File 'lib/gasciiart/builder.rb', line 6

def initialize(source, target, options = {})
  @source, @target, @options = source, target, options
  @source = "#{@source}/" unless @source[-1..-1] == '/'
  @target = "#{@target}/" unless @target[-1..-1] == '/'
  @verbose = options[:verbose]

  @starting_dir = Dir.pwd
  @target_animation = "#{@target}animation.txt"
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/gasciiart/builder.rb', line 4

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/gasciiart/builder.rb', line 4

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



4
5
6
# File 'lib/gasciiart/builder.rb', line 4

def target
  @target
end

Instance Method Details

#add_runnerObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/gasciiart/builder.rb', line 62

def add_runner
  filename = "#{@target}run.sh"
  File.open(filename, 'w') do |f|
    f.write Gasciiart::RUNNER
  end

  File.chmod 0755, filename

  run %{git add run.sh && git commit -m "Added run.sh"}
end

#build!Object

Where all the magic happens Setups up the output and loops through each file from the source (in ‘ls` order) Creating a commit for each file



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gasciiart/builder.rb', line 19

def build!
  test_git!

  file_list = Dir.glob("#{@source}*").sort # Pull the file list before creating the target directory

  setup_target

  add_runner

  file_list.each do |infile_name|
    rewrite_animation_frame infile_name
    create_commit infile_name
  end
end

#create_commit(infile_name) ⇒ Object



58
59
60
# File 'lib/gasciiart/builder.rb', line 58

def create_commit(infile_name)
  run %{git add animation.txt && git commit -m "#{infile_name}"}
end

#rewrite_animation_frame(infile_name) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/gasciiart/builder.rb', line 49

def rewrite_animation_frame(infile_name)
  log "Writing #{infile_name}"
  File.open(infile_name) do |infile|
    File.open(@target_animation, 'w') do |outfile|
      outfile.write infile.read  # This could be streamed, but a single animation frame is small enough.  Let's start simple
    end
  end
end

#setup_targetObject

Creates and git inits the target directory



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gasciiart/builder.rb', line 35

def setup_target
  log "Setting up #{@target}"

  if File.exists? @target
    raise ArgumentError, %{Target directory "#{@target}" already exists}
  end

  FileUtils.mkdir @target

  run "git init ."

  # TODO: Create runner file
end