Class: Courseware::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/courseware/composer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, repository = nil) ⇒ Composer

Returns a new instance of Composer.



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/courseware/composer.rb', line 2

def initialize(config, repository=nil)
  @config     = config
  @repository = repository || Courseware::Repository.new(config)

  if File.exists?(@config[:presfile])
    @showoff    = JSON.parse(File.read(@config[:presfile]))
    @coursename = @showoff['name']
    @prefix     = @showoff['name'].gsub(' ', '_')
    @sections   = @showoff['sections']
  end
end

Instance Method Details

#build(subject) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/courseware/composer.rb', line 14

def build(subject)
  courselevel!

  if subject.nil?
    display_tags
    raise "Please re-run this task with a list of tags to include."
  end

  subject.each do |tag|
    key = "tag:#{tag}"
    @showoff['sections'].each do |section|
      raise 'All sections must be represented as Hashes to customize.' unless section.is_a? Hash

      if section.include? key
        raise "A section in showoff.json refers to #{section[key]}, which does not exist." unless File.exist? section[key]
        section['include'] = section[key]
      end
    end
  end

  # normalize the output and trim unused tags
  @showoff['sections'].each do |section|
    section.select! { |k,v| k == 'include' }
  end
  @showoff['sections'].reject! { |section| section.empty? }

  name = Courseware.question('What would you like to call this variant?', 'custom').gsub(/\W+/, '_')
  desc = Courseware.question("Enter a customized description if you'd like:")

  @showoff['description'] = desc if desc

  File.write("#{name}.json", JSON.pretty_generate(@showoff))
  puts "Run your presentation with `showoff serve -f #{name}.json` or `rake present`"
end

#package(subject) ⇒ Object



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
107
108
109
# File 'lib/courseware/composer.rb', line 49

def package(subject)
  courselevel!
  on_release!
  pristine!

  subject ||= Courseware.choose_variant
  subject   = subject.to_s
  content   = Courseware.parse_showoff(subject)
  variant   = File.basename(subject, '.json')
  current   = @repository.current(@coursename)

  if variant == 'showoff'
    variant = @prefix
    output  = @prefix
  else
    output  = "#{@prefix}-#{variant}"
  end

  FileUtils.rm_rf "build/#{variant}"
  FileUtils.mkdir_p "build/#{variant}"
  FileUtils.cp subject, "build/#{variant}/showoff.json"

  content['sections'].each do |section|
    if section.is_a? Hash
      path = section['include']
    else
      path = section
    end

    dir   = File.dirname path
    FileUtils.mkdir_p "build/#{variant}/#{dir}"
    FileUtils.cp path, "build/#{variant}/#{path}"

    next unless section.is_a? Hash

    files = JSON.parse(File.read(path))
    files.each do |file|
      FileUtils.cp "#{dir}/#{file}", "build/#{variant}/#{dir}/#{file}"
    end
  end

  # support is special
  FileUtils.cp_r '../_support', "build/#{variant}/_support"
  FileUtils.rm_f "build/#{variant}/_support/*.pem"
  FileUtils.rm_f "build/#{variant}/_support/*.pub"
  FileUtils.rm_f "build/#{variant}/_support/aws_credentials"

  # duplicate from cwd to build/variant everything not already copied
  Dir.glob('*').each do |thing|
    next if thing == 'build'
    next if File.extname(thing) == '.json'
    next if File.exist? "build/#{variant}/#{thing}"
    FileUtils.ln_s "../../#{thing}", "build/#{variant}/#{thing}"
  end

  system("tar -C build --dereference -czf build/#{output}-#{current}.tar.gz  #{variant}")
  if Courseware.confirm("Would you like to clean up the output build directory?")
    FileUtils.rm_rf "build/#{variant}"
  end

end