From e46aa0d865ae28afae415a5608bcd81b0188455b Mon Sep 17 00:00:00 2001 From: Chris Schlaeger Date: Sat, 28 Mar 2015 22:30:43 +0100 Subject: New: Make HTML directory location configurable --- lib/postrunner/ActivitiesDB.rb | 35 +++++++++++++++++------------------ lib/postrunner/Activity.rb | 5 ++--- lib/postrunner/ActivityListView.rb | 2 +- lib/postrunner/ActivityView.rb | 2 +- lib/postrunner/Main.rb | 17 +++++++++++++++++ lib/postrunner/RuntimeConfig.rb | 3 ++- 6 files changed, 40 insertions(+), 24 deletions(-) diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index 13052dd..f97820b 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -22,13 +22,12 @@ module PostRunner class ActivitiesDB - attr_reader :db_dir, :cfg, :fit_dir, :html_dir, :activities + attr_reader :db_dir, :cfg, :fit_dir, :activities def initialize(db_dir, cfg) @db_dir = db_dir @cfg = cfg @fit_dir = File.join(@db_dir, 'fit') - @html_dir = File.join(@db_dir, 'html') @archive_file = File.join(@db_dir, 'archive.yml') create_directories @@ -62,6 +61,16 @@ module PostRunner sync if sync_needed end + def create_directories + create_directory(@db_dir, 'data') + create_directory(@fit_dir, 'fit') + create_directory(@cfg[:html_dir], 'html') + + %w( icons jquery flot openlayers ).each do |dir| + create_auxdir(dir) + end + end + # Add a new FIT file to the database. # @param fit_file [String] Name of the FIT file. # @return [TrueClass or FalseClass] True if the file could be added. False @@ -247,7 +256,7 @@ module PostRunner # Show the activity list in a web browser. def show_list_in_browser ActivityListView.new(self).update_html_index - show_in_browser(File.join(@html_dir, 'index.html')) + show_in_browser(File.join(@cfg[:html_dir], 'index.html')) end def list @@ -294,17 +303,6 @@ module PostRunner ActivityListView.new(self).update_html_index end - def create_directories - create_directory(@db_dir, 'data') - create_directory(@fit_dir, 'fit') - create_directory(@html_dir, 'html') - - create_symlink('icons') - create_symlink('jquery') - create_symlink('flot') - create_symlink('openlayers') - end - def create_directory(dir, name) return if Dir.exists?(dir) @@ -316,7 +314,7 @@ module PostRunner end end - def create_symlink(dir) + def create_auxdir(dir) # This file should be in lib/postrunner. The 'misc' directory should be # found in '../../misc'. misc_dir = File.realpath(File.join(File.dirname(__FILE__), @@ -328,12 +326,13 @@ module PostRunner unless Dir.exists?(src_dir) Log.fatal "Cannot find '#{src_dir}': #{$!}" end - dst_dir = File.join(@html_dir, dir) + dst_dir = File.join(@cfg[:html_dir], dir) unless File.exists?(dst_dir) begin - FileUtils.ln_s(src_dir, dst_dir) + #FileUtils.ln_s(src_dir, dst_dir) + FileUtils.cp_r(src_dir, dst_dir) rescue IOError - Log.fatal "Cannot create symbolic link to '#{dst_dir}': #{$!}" + Log.fatal "Cannot create auxilliary directory '#{dst_dir}': #{$!}" end end end diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb index 1ca96b1..3e7e51e 100644 --- a/lib/postrunner/Activity.rb +++ b/lib/postrunner/Activity.rb @@ -19,7 +19,7 @@ module PostRunner class Activity - attr_reader :db, :fit_file, :name, :fit_activity, :html_dir, :html_file + attr_reader :db, :fit_file, :name, :fit_activity # This is a list of variables that provide data from the fit file. To # speed up access to it, we cache the data in the activity database. @@ -100,8 +100,7 @@ module PostRunner # after a YAML::load(). def late_init(db) @db = db - @html_dir = File.join(@db.db_dir, 'html') - @html_file = File.join(@html_dir, "#{@fit_file[0..-5]}.html") + @html_file = File.join(@db.cfg[:html_dir], "#{@fit_file[0..-5]}.html") @unset_variables.each do |name_without_at| # The YAML file does not yet have the instance variable cached. diff --git a/lib/postrunner/ActivityListView.rb b/lib/postrunner/ActivityListView.rb index efcfc31..e3cd627 100644 --- a/lib/postrunner/ActivityListView.rb +++ b/lib/postrunner/ActivityListView.rb @@ -165,7 +165,7 @@ EOT end def write_file(doc) - output_file = File.join(@db.html_dir, + output_file = File.join(@db.cfg[:html_dir], "index#{@page_no == 0 ? '' : @page_no}.html") begin File.write(output_file, doc.to_html) diff --git a/lib/postrunner/ActivityView.rb b/lib/postrunner/ActivityView.rb index fcca726..fa05e4c 100644 --- a/lib/postrunner/ActivityView.rb +++ b/lib/postrunner/ActivityView.rb @@ -31,7 +31,7 @@ module PostRunner @unit_system = unit_system @predecessor = predecessor @successor = successor - @output_dir = activity.html_dir + @output_dir = activity.db.cfg[:html_dir] @output_file = nil @doc = HTMLBuilder.new diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 73f9c4a..9029045 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -158,6 +158,9 @@ summary units Change the unit system. +htmldir + Change the output directory for the generated HTML files + An absolute or relative name of a .FIT file. @@ -239,6 +242,8 @@ EOT process_activities(args, :summary) when 'units' change_unit_system(args) + when 'htmldir' + change_html_dir(args) when nil Log.fatal("No command provided. " + "See 'postrunner -h' for more information.") @@ -340,6 +345,18 @@ EOT end end + def change_html_dir(args) + if args.length != 1 + Log.fatal('You must specify a directory') + end + + if @cfg[:html_dir] != args[0] + @cfg.set_option(:html_dir, args[0]) + @activities.create_directories + @activities.generate_all_html_reports + end + end + end end diff --git a/lib/postrunner/RuntimeConfig.rb b/lib/postrunner/RuntimeConfig.rb index 2f39d0b..5b424df 100644 --- a/lib/postrunner/RuntimeConfig.rb +++ b/lib/postrunner/RuntimeConfig.rb @@ -25,7 +25,8 @@ module PostRunner @options = { :version => '0.0.0', :unit_system => :metric, - :import_dir => nil + :import_dir => nil, + :html_dir => File.join(dir, 'html') } @config_file = File.join(dir, 'config.yml') -- cgit v1.2.3