Top Level Namespace

Defined Under Namespace

Modules: Sunniesnow

Instance Method Summary collapse

Instance Method Details

#build(compression_level: Zip.default_compression, **opts, &block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/sscharter/cli.rb', line 160

def build compression_level: Zip.default_compression, **opts, &block
  return 1 unless config = Sunniesnow::Charter::CLI.config
  dir = Sunniesnow::Charter::PROJECT_DIR
  project_name = config[:project_name] || File.basename(dir)
  build_dir = File.join dir, config[:build_dir] || 'build'
  files_dir = File.join dir, config[:files_dir] || 'files'
  sources_dir = File.join dir, config[:sources_dir] || 'src'
  include_files = (config[:include] || []).map { File.join dir, _1 }
  Sunniesnow::Charter.charts.clear
  time = Time.now
  Dir.glob File.join sources_dir, '*.rb' do |filename|
    load filename
  rescue Exception => e
    puts "Error loading #{filename}:"
    puts e.full_message
    puts "Build aborted (took #{Time.now - time} seconds)"
    return 1
  end
  puts "Built #{Sunniesnow::Charter.charts.size} chart(s) (took #{Time.now - time} seconds)"
  time = Time.now
  FileUtils.mkdir_p build_dir
  build_filename = File.join build_dir, "#{project_name}.ssc"
  FileUtils.rm build_filename if File.exist? build_filename
  Zip::File.open build_filename, compression_level:, create: true do |zip_file|
    Dir.glob File.join files_dir, '**', '*' do |filename|
      zip_file.add filename["#{files_dir}/".length..], filename
    end
    include_files.each do |pattern|
      Dir.glob pattern do |filename|
        zip_file.add filename["#{dir}/".length..], filename
      end
    end
    Sunniesnow::Charter.charts.each do |name, chart|
      begin
        sunniesnow_chart = chart.to_sunniesnow **opts
      rescue => e
        puts 'An error happened. Report if this is a bug of sscharter.'
        puts e.full_message
        return 2
      end
      block.(name, sunniesnow_chart) if block
      zip_file.get_output_stream "#{name}.json" do |file|
        file.write sunniesnow_chart.to_json
      rescue => e
        puts 'An error happened. Report if this is a bug of sscharter.'
        puts e.full_message
        return 2
      end
    end
    zip_file.each { _1.time = Time.at 0 }
  end
  puts "Created #{project_name}.ssc (took #{Time.now - time} seconds)"
  0
end